add set_current_word and submit_guess hooks to ScriptInterface.GameState

This commit is contained in:
blek 2023-09-03 23:43:16 +10:00
parent 9039499ad2
commit cba520b3ec
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 73 additions and 50 deletions

View File

@ -16,7 +16,7 @@
let targets = getForNWord(5); let targets = getForNWord(5);
let word = targets[random(0, targets.length)]; let word: string = targets[random(0, targets.length)];
let guesses = 6; let guesses = 6;
let guessed: string[][] = []; let guessed: string[][] = [];
let current_guess = 0; let current_guess = 0;
@ -28,6 +28,26 @@
let yellow_letters: string[] = []; let yellow_letters: string[] = [];
let unfit_letters: string[] = []; let unfit_letters: string[] = [];
for (let i = 0; i != guesses; i++) {
guessed.push(new Array(word.length).fill(''));
}
globalThis.ScriptInterface.gameState.set_current_word = word2 => {
if (!globalThis.ScriptInterface.scriptsAllowed())
throw new Error('Scripts are not allowed');
if (word2.length != word.length) {
throw new Error('Guess word must be the same length as the target word!');
}
guessed[current_guess] = word2.split('');
}
globalThis.ScriptInterface.gameState.submit_guess = () => {
if (!globalThis.ScriptInterface.scriptsAllowed())
throw new Error('Scripts are not allowed');
submitGuess();
}
setTimeout(() => { setTimeout(() => {
setGameState({ setGameState({
word, word,
@ -55,48 +75,7 @@
updateScriptInterface(state); updateScriptInterface(state);
} }
let loading = true; function submitGuess() {
let not_a_word = false;
let game_creator = false;
(
function() {
const urlprops = new URLSearchParams(window.location.search);
const challenge = urlprops.get('challenge');
if (challenge !== null) {
word = decode(atob(decodeURIComponent(challenge)));
console.log(word)
}
}
)()
onMount(async () => {
await loadDict();
loading = false;
});
function current_word() {
return guessed[current_guess].join('');
}
function is_win() {
return current_word() == word;
}
for (let i = 0; i != guesses; i++) {
guessed.push(new Array(word.length).fill(''));
}
document.onkeydown = e => {
if (endgame) return;
if (not_a_word) return;
if (e.key == 'Backspace') {
if (word_position == 0) return;
guessed[current_guess][word_position - 1] = '';
word_position -= 1;
}
if (e.key == 'Enter') {
if (word_position != word.length) return; if (word_position != word.length) return;
if (!isIn(current_word())) { if (!isIn(current_word())) {
not_a_word = true; not_a_word = true;
@ -143,6 +122,47 @@
word_position = 0; word_position = 0;
current_guess++; current_guess++;
} }
let loading = true;
let not_a_word = false;
let game_creator = false;
(
function() {
const urlprops = new URLSearchParams(window.location.search);
const challenge = urlprops.get('challenge');
if (challenge !== null) {
word = decode(atob(decodeURIComponent(challenge)));
console.log(word)
}
}
)()
onMount(async () => {
await loadDict();
loading = false;
});
function current_word() {
return guessed[current_guess].join('');
}
function is_win() {
return current_word() == word;
}
document.onkeydown = e => {
if (endgame) return;
if (not_a_word) return;
if (e.key == 'Backspace') {
if (word_position == 0) return;
guessed[current_guess][word_position - 1] = '';
word_position -= 1;
}
if (e.key == 'Enter') {
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;
if (e.key.match(/[a-zA-Z]/i) === null) return; if (e.key.match(/[a-zA-Z]/i) === null) return;

View File

@ -6,6 +6,7 @@ let scriptsAllowed = false;
export function allowScripts() { export function allowScripts() {
scriptsAllowed = true; scriptsAllowed = true;
} }
allowScripts();
export class GameState { export class GameState {
word?: string; word?: string;
@ -18,6 +19,8 @@ export class GameState {
green_letters?: string[]; green_letters?: string[];
yellow_letters?: string[]; yellow_letters?: string[];
unfit_letters?: string[]; unfit_letters?: string[];
set_current_word?: {(word: string): void};
submit_guess?: {(): void};
} }
export class WordleLibrary { export class WordleLibrary {