Skip to content

Commit

Permalink
Merge pull request #44 from light-200/fix/speedFormula
Browse files Browse the repository at this point in the history
fix/speed formula fixed 🐛
  • Loading branch information
light-200 committed Feb 2, 2022
2 parents 8e51b19 + b716394 commit a0f6078
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
3 changes: 3 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const io = new Server(httpServer, {
cors: {
origin: ["https://light-200.github.io"],
},
// cors: {
// origin: ["http://localhost:8080", "http://127.0.0.1:5500"], // comment in production
// },
});

io.on("connection", (socket) => {
Expand Down
2 changes: 1 addition & 1 deletion docs/bundle.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/functions/getText.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import setWords, { spanWrap } from "../ui/uiListeners";
import setWords, { findTotalWords, spanWrap } from "../ui/uiListeners";
import start from "./start";
import { roomId, textContainer } from "../ui/uiElements";
import { getLocalData, getUserData } from "../storage/localstorage";
Expand Down Expand Up @@ -40,6 +40,7 @@ const getText = async () => {
}

text = textContainer.innerText = data;
findTotalWords(text);
setWords();
spanWrap(textContainer);
start(text);
Expand All @@ -51,6 +52,7 @@ export async function getTextSocket() {

export async function setTextSocket(data) {
text = textContainer.innerText = data;
findTotalWords(text);
setWords();
spanWrap(textContainer);
start(text);
Expand Down
7 changes: 2 additions & 5 deletions src/functions/speed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ var speed;

// this function is responsible for calculating the speed
const speedCalc = async (totalWords, seconds) => {
var minutes = seconds.toPrecision(2) / 60;
// console.log( totalWords , " " , minutes.toPrecision(2))
var minutes = seconds / 60;
let tempSpeed = totalWords / minutes;
speed = Math.floor(tempSpeed.toPrecision(3));
speed = Math.floor(tempSpeed);
speedIndicator.innerText = speed;

let user = await getUserData();
Expand Down Expand Up @@ -41,9 +40,7 @@ const speedCalc = async (totalWords, seconds) => {
};

function setSpeed(speed) {
// console.log(speed, speedIndicator)
speed ? (speedIndicator.innerText = speed) : speed;
// console.log("set speed called with ", speed);
}

export default speedCalc;
Expand Down
14 changes: 10 additions & 4 deletions src/functions/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default (text) => {
let tempText = text;
let index = 0;
let typedWords = 0;
let typedChars = 0;
let totalChars = text.length;
let errorCount = 0;
let start, end;
let Words = document.querySelectorAll(".text>span");
Words[index].classList.add("blink");
Expand All @@ -19,6 +22,7 @@ export default (text) => {
start = new Date().getTime();
firstCall = false;
}
typedChars++;

tempText = tempText.substr(1);

Expand All @@ -40,21 +44,23 @@ export default (text) => {
let progress = Math.ceil((typedWords / totalWords) * 100);
let seconds = new Date().getTime();
seconds = (seconds - start) / 1000;
var minutes = seconds.toPrecision(2) / 60;
let tempSpeed = totalWords / minutes;
let speed = Math.floor(tempSpeed.toPrecision(3));
var minutes = seconds / 60;
let tempSpeed = typedChars / 5 / minutes;
let speed = Math.floor(tempSpeed);
multiplayerMode && typing(progress, speed);
}

if (index == text.length - 1) {
end = new Date().getTime();
speedCalc(totalWords, (end - start) / 1000);

speedCalc(totalChars / 5, (end - start) / 1000);
}

index++;
} else {
if (Words[index] != null) {
Words[index].classList.add("wrongWord");
errorCount++;
}
}
});
Expand Down
1 change: 1 addition & 0 deletions src/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getTextSocket, setTextSocket } from "../functions/getText";
import { renderPlayers } from "./roomHandling";

const socket = io(process.env.SERVER_LINK || "http://localhost:3000");
// const socket = io("http://localhost:3000"); // comment in production

socket.on("connect", () => {
console.log("connection established");
Expand Down
5 changes: 4 additions & 1 deletion src/ui/uiListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ var totalWords;
//sets total word value
const words = document.querySelector(".words");

const setWords = (typedWords = 0) => {
export function findTotalWords(text) {
let whitespace = / /g,
result,
indices = [];
while ((result = whitespace.exec(text))) {
indices.push(result.index);
}
totalWords = indices.length + 1;
}

const setWords = (typedWords = 0) => {
words.innerText = `${totalWords}/${typedWords}`;
};

Expand Down

0 comments on commit a0f6078

Please sign in to comment.