2022-10-05 18:41:34 +00:00
|
|
|
var target = document.getElementById("startup-target").innerText;
|
|
|
|
var buffer = "";
|
|
|
|
var successfulAttempts = 0;
|
|
|
|
var failedAttempts = 0;
|
|
|
|
var isGameActive = true;
|
|
|
|
|
|
|
|
document.body.removeChild(document.getElementById("startup-target"));
|
|
|
|
|
|
|
|
const field = document.getElementById("spell-field");
|
|
|
|
const scoreGood = document.getElementById("score-good");
|
|
|
|
const scoreBad = document.getElementById("score-bad");
|
|
|
|
const scorePercent = document.getElementById("score-percent");
|
|
|
|
|
|
|
|
const image = document.getElementById("word-image");
|
|
|
|
|
|
|
|
async function updateTarget() {
|
|
|
|
isGameActive = false;
|
|
|
|
const response = await fetch("/random");
|
|
|
|
const object = await response.json();
|
|
|
|
target = object.word;
|
|
|
|
image.src = object.image_url;
|
|
|
|
isGameActive = true;
|
|
|
|
console.log("ready!");
|
|
|
|
}
|
|
|
|
|
2023-01-11 22:22:24 +00:00
|
|
|
const levenshteinDistance = (str1, str2) => {
|
|
|
|
const track = Array(str2.length + 1).fill(null).map(() =>
|
|
|
|
Array(str1.length + 1).fill(null));
|
|
|
|
for (let i = 0; i <= str1.length; i += 1) {
|
|
|
|
track[0][i] = i;
|
|
|
|
}
|
|
|
|
for (let j = 0; j <= str2.length; j += 1) {
|
|
|
|
track[j][0] = j;
|
|
|
|
}
|
|
|
|
for (let j = 1; j <= str2.length; j += 1) {
|
|
|
|
for (let i = 1; i <= str1.length; i += 1) {
|
|
|
|
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
|
|
|
|
track[j][i] = Math.min(
|
|
|
|
track[j][i - 1] + 1, // deletion
|
|
|
|
track[j - 1][i] + 1, // insertion
|
|
|
|
track[j - 1][i - 1] + indicator, // substitution
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return track[str2.length][str1.length];
|
|
|
|
};
|
|
|
|
|
2022-10-05 18:41:34 +00:00
|
|
|
function isLetter(str) {
|
|
|
|
return str.length === 1 && str.match(/[a-z]/i);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSpellField = (text) => {
|
|
|
|
field.innerText = text;
|
|
|
|
field.style.color = "";
|
2023-01-11 22:22:24 +00:00
|
|
|
|
|
|
|
if(text.length == 0) {
|
|
|
|
field.innerText = "escribe algo...";
|
|
|
|
field.style.color = "gray";
|
|
|
|
}
|
2022-10-05 18:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateScores = () => {
|
|
|
|
scoreGood.innerText = successfulAttempts;
|
|
|
|
scoreBad.innerText = failedAttempts;
|
|
|
|
var total = successfulAttempts + failedAttempts;
|
|
|
|
var percent = (successfulAttempts / total) * 100;
|
|
|
|
scorePercent.innerText = Math.round(percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
submitBuffer = () => {
|
2023-01-11 22:22:24 +00:00
|
|
|
let distance = levenshteinDistance(buffer.toLowerCase(), target.toLowerCase());
|
|
|
|
if(distance > 1)
|
2022-10-05 18:41:34 +00:00
|
|
|
{
|
|
|
|
updateSpellField(target);
|
|
|
|
field.style.color = "red";
|
|
|
|
failedAttempts += 1;
|
2023-01-11 22:22:24 +00:00
|
|
|
} else if(distance > 0) {
|
|
|
|
updateSpellField(target);
|
|
|
|
field.style.color = "yellow";
|
2022-10-05 18:41:34 +00:00
|
|
|
} else {
|
|
|
|
updateSpellField(buffer);
|
|
|
|
field.style.color = "green";
|
|
|
|
successfulAttempts += 1;
|
|
|
|
}
|
|
|
|
updateScores();
|
|
|
|
buffer = "";
|
|
|
|
updateTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
document.onkeyup = (ev) => {
|
|
|
|
const key = ev.key;
|
|
|
|
if(isGameActive)
|
|
|
|
{
|
|
|
|
if(key === "Enter")
|
|
|
|
{
|
|
|
|
if(buffer.length != 0) submitBuffer();
|
|
|
|
}
|
2023-01-11 22:22:24 +00:00
|
|
|
if(key === "Backspace")
|
|
|
|
{
|
|
|
|
buffer = buffer.slice(0, buffer.length - 1);
|
|
|
|
updateSpellField(buffer);
|
|
|
|
}
|
2022-10-05 18:41:34 +00:00
|
|
|
if(isLetter(key))
|
|
|
|
{
|
|
|
|
buffer += key;
|
|
|
|
updateSpellField(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ev.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|