fix ScriptInterface.gameState.submit_guess() not working

This commit is contained in:
blek 2023-09-03 23:48:07 +10:00
parent cba520b3ec
commit 220a46270e
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 20 additions and 14 deletions

View File

@ -40,6 +40,7 @@
throw new Error('Guess word must be the same length as the target word!'); throw new Error('Guess word must be the same length as the target word!');
} }
guessed[current_guess] = word2.split(''); guessed[current_guess] = word2.split('');
word_position = word.length;
} }
globalThis.ScriptInterface.gameState.submit_guess = () => { globalThis.ScriptInterface.gameState.submit_guess = () => {
@ -48,7 +49,7 @@
submitGuess(); submitGuess();
} }
setTimeout(() => { function updateState() {
setGameState({ setGameState({
word, word,
guesses, guesses,
@ -61,7 +62,8 @@
yellow_letters, yellow_letters,
unfit_letters unfit_letters
}); });
}, 500) }
setTimeout(updateState, 500);
function setGameState(state: GameState) { function setGameState(state: GameState) {
word = state.word ?? word; word = state.word ?? word;
@ -76,11 +78,11 @@
} }
function submitGuess() { function submitGuess() {
if (word_position != word.length) return; if (word_position != word.length) return updateState();
if (!isIn(current_word())) { if (!isIn(current_word())) {
not_a_word = true; not_a_word = true;
setTimeout(() => {not_a_word = false}, 1000); setTimeout(() => {not_a_word = false}, 1000);
return return updateState();
} }
if (current_guess + 1 == guesses) { if (current_guess + 1 == guesses) {
@ -88,14 +90,14 @@
wins = is_win(); wins = is_win();
green_letters = word.split(''); green_letters = word.split('');
current_guess++; current_guess++;
return; return updateState();
} }
if (is_win()) { if (is_win()) {
endgame = true; endgame = true;
wins = true; wins = true;
green_letters = word.split(''); green_letters = word.split('');
current_guess++; current_guess++;
return return updateState();
} }
let c_word = current_word(); let c_word = current_word();
@ -121,6 +123,7 @@
word_position = 0; word_position = 0;
current_guess++; current_guess++;
updateState();
} }
let loading = true; let loading = true;
@ -133,7 +136,7 @@
const challenge = urlprops.get('challenge'); const challenge = urlprops.get('challenge');
if (challenge !== null) { if (challenge !== null) {
word = decode(atob(decodeURIComponent(challenge))); word = decode(atob(decodeURIComponent(challenge)));
console.log(word) updateState();
} }
} }
)() )()
@ -141,6 +144,7 @@
onMount(async () => { onMount(async () => {
await loadDict(); await loadDict();
loading = false; loading = false;
updateState();
}); });
function current_word() { function current_word() {
@ -152,11 +156,11 @@
} }
document.onkeydown = e => { document.onkeydown = e => {
if (endgame) return; if (endgame) return updateState();
if (not_a_word) return; if (not_a_word) return updateState();
if (e.key == 'Backspace') { if (e.key == 'Backspace') {
if (word_position == 0) return; if (word_position == 0) return updateState();
guessed[current_guess][word_position - 1] = ''; guessed[current_guess][word_position - 1] = '';
word_position -= 1; word_position -= 1;
} }
@ -164,12 +168,13 @@
submitGuess(); submitGuess();
} }
if (e.key.length == 1) { if (e.key.length == 1) {
if (e.ctrlKey || e.shiftKey || e.altKey) return; if (e.ctrlKey || e.shiftKey || e.altKey) return updateState();
if (e.key.match(/[a-zA-Z]/i) === null) return; if (e.key.match(/[a-zA-Z]/i) === null) return updateState();
if (word_position == word.length) return; if (word_position == word.length) return updateState();
guessed[current_guess][word_position] = e.key; guessed[current_guess][word_position] = e.key;
word_position += 1; word_position += 1;
} }
updateState();
} }
function reset_game() { function reset_game() {
@ -187,6 +192,7 @@
green_letters = []; green_letters = [];
yellow_letters = []; yellow_letters = [];
unfit_letters = []; unfit_letters = [];
updateState();
} }
</script> </script>