Skip to content

Commit

Permalink
Merge branch 'dynamic-wpm' into merges
Browse files Browse the repository at this point in the history
From PR briano1905#8
  • Loading branch information
SeerLite committed Dec 18, 2020
2 parents fae2208 + 6a067ea commit 047154f
Showing 1 changed file with 79 additions and 19 deletions.
98 changes: 79 additions & 19 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ let startDate = 0;
let timer;
let timerActive = false;
let punctuation = false;
let realTime = false;
let resultTimeout = null;

// Initialize global constants
const PUNCREGEX = /!:;'",.\/?!@#$%^&*()_}{\[\]\-s\+\=/;
Expand All @@ -39,6 +41,7 @@ getCookie('wordCount') === '' ? setWordCount(50) : setWordCount(getCookie('wordC
getCookie('timeCount') === '' ? setTimeCount(60) : setTimeCount(getCookie('timeCount'));
getCookie('typingMode') === '' ? setTypingMode('wordcount') : setTypingMode(getCookie('typingMode'));
getCookie('punctuation') === '' ? setPunctuation('false') : setPunctuation(getCookie('punctuation'));
setRealTime(getCookie("realTime"));

// Find a list of words and display it to textDisplay
function setText() {
Expand Down Expand Up @@ -157,13 +160,24 @@ inputField.addEventListener('keydown', e => {
}

// If it is the first character entered
if (currentWord === 0 && inputField.value === '') {
if (currentWord === 0 && inputField.value === '' && e.key >= '!' && e.key <= '~' && e.key.length === 1) {
if (resultTimeout !== null) {
clearTimeout(resultTimeout);
}
(function printResult() {
if (realTime) {
showResult();
}
if (typingMode !== "time" || (typingMode === "time" && timerActive)) {
resultTimeout = setTimeout(printResult, 1000);
}
})();
startDate = Date.now();
switch (typingMode) {
case 'custom':
case 'wordcount':
startDate = Date.now();
break;

case 'time':
if (!timerActive) {
startTimer(timeCount);
Expand All @@ -181,12 +195,32 @@ inputField.addEventListener('keydown', e => {
textDisplay.style.display = 'none';
inputField.className = '';
document.querySelector(`#tc-${timeCount}`).innerHTML = timeCount;
showResult();
end();
clearTimeout(timer);
}
}
}
}

if (wordList[currentWord] !== undefined) {
if (e.key >= "!" && e.key <= "~") {
const word = `${inputField.value}${e.key}`;
if (word[word.length - 1] === wordList[currentWord][word.length - 1]) {
correctKeys += 1;
}
} else if (e.key === " ") {
if (inputField.value !== wordList[currentWord]) {
let i = 0;
while (inputField.value[i] == wordList[currentWord][i]) {
correctKeys -= 1;
i += 1;
}
} else {
correctKeys += 1;
}
}
}

// If it is the space key check the word and add correct/wrong class
if (e.key === ' ') {
e.preventDefault();
Expand All @@ -205,14 +239,13 @@ inputField.addEventListener('keydown', e => {
if (currentWord < wordList.length - 1) {
if (inputField.value === wordList[currentWord]) {
textDisplay.childNodes[currentWord].classList.add('correct');
correctKeys += wordList[currentWord].length + 1;
} else {
textDisplay.childNodes[currentWord].classList.add('wrong');
}
textDisplay.childNodes[currentWord + 1].classList.add('highlight');
} else if (currentWord === wordList.length - 1) {
textDisplay.childNodes[currentWord].classList.add('wrong');
showResult();
end();
}

inputField.value = '';
Expand All @@ -223,37 +256,51 @@ inputField.addEventListener('keydown', e => {
} else if (currentWord === wordList.length - 1) {
if (inputField.value + e.key === wordList[currentWord]) {
textDisplay.childNodes[currentWord].classList.add('correct');
correctKeys += wordList[currentWord].length;
currentWord++;
showResult();
end();
}
}
});

function end() {
if (resultTimeout !== null) {
clearTimeout(resultTimeout);
}
resultTimeout = null;
showResult();
}

// Calculate and display result
function showResult() {
let words, minute, acc;
let minute, acc;
let totalKeys = wordList.length === currentWord ? -1 : inputField.value.length;
const wpm = Math.floor(correctKeys / 5 / ((Date.now() - startDate) / 1000 / 60));
switch (typingMode) {
case 'custom':
case 'wordcount':
words = correctKeys / 5;
minute = (Date.now() - startDate) / 1000 / 60;
let totalKeys = -1;
wordList.forEach(e => (totalKeys += e.length + 1));
wordList.some((e, index) => {
if (currentWord === index) {
return true;
}
totalKeys += e.length + 1;
return false;
});
acc = Math.floor((correctKeys / totalKeys) * 100);
break;

case 'time':
words = correctKeys / 5;
minute = timeCount / 60;
let sumKeys = -1;
for (i = 0; i < currentWord; i++) {
sumKeys += wordList[i].length + 1;
}
acc = acc = Math.min(Math.floor((correctKeys / sumKeys) * 100), 100);
wordList.some((e, index) => {
if (currentWord === index) {
return true;
}
totalKeys += e.length + 1;
return false;
});
acc = Math.min(Math.floor((correctKeys / totalKeys) * 100), 100);
}
let wpm = Math.floor(words / minute);
document.querySelector('#right-wing').innerHTML = `WPM: ${wpm} / ACC: ${acc}`;
document.querySelector('#right-wing').innerHTML = `WPM: ${wpm || 0} / ACC: ${acc || 0}`;
}

// Command actions
Expand All @@ -278,6 +325,10 @@ document.addEventListener('keydown', e => {
if (e.key === 'p') {
setPunctuation(inputField.value);
}
// [mod + r] => Real time stats
if (e.key === 'r') {
setRealTime(!realTime + "");
}
} else if (!document.querySelector('#theme-center').classList.contains('hidden')) {
if (e.key === 'Escape') {
hideThemeCenter();
Expand Down Expand Up @@ -345,6 +396,10 @@ function setLanguage(_lang) {
}

function setTypingMode(_mode) {
if (resultTimeout !== null) {
clearTimeout(resultTimeout);
resultTimeout = null;
}
const mode = _mode.toLowerCase();
switch (mode) {
case 'wordcount':
Expand Down Expand Up @@ -551,3 +606,8 @@ document.querySelector('body').addEventListener('transitionend', function () {
});

setFavicon();

function setRealTime(value) {
realTime = value === "true";
setCookie('realTime', realTime, 90);
}

0 comments on commit 047154f

Please sign in to comment.