add set_current_word and submit_guess hooks to ScriptInterface.GameState
This commit is contained in:
parent
9039499ad2
commit
cba520b3ec
106
src/Game.svelte
106
src/Game.svelte
|
@ -16,7 +16,7 @@
|
|||
|
||||
let targets = getForNWord(5);
|
||||
|
||||
let word = targets[random(0, targets.length)];
|
||||
let word: string = targets[random(0, targets.length)];
|
||||
let guesses = 6;
|
||||
let guessed: string[][] = [];
|
||||
let current_guess = 0;
|
||||
|
@ -28,6 +28,26 @@
|
|||
let yellow_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(() => {
|
||||
setGameState({
|
||||
word,
|
||||
|
@ -55,48 +75,7 @@
|
|||
updateScriptInterface(state);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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') {
|
||||
function submitGuess() {
|
||||
if (word_position != word.length) return;
|
||||
if (!isIn(current_word())) {
|
||||
not_a_word = true;
|
||||
|
@ -143,6 +122,47 @@
|
|||
word_position = 0;
|
||||
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.ctrlKey || e.shiftKey || e.altKey) return;
|
||||
if (e.key.match(/[a-zA-Z]/i) === null) return;
|
||||
|
|
|
@ -6,6 +6,7 @@ let scriptsAllowed = false;
|
|||
export function allowScripts() {
|
||||
scriptsAllowed = true;
|
||||
}
|
||||
allowScripts();
|
||||
|
||||
export class GameState {
|
||||
word?: string;
|
||||
|
@ -18,6 +19,8 @@ export class GameState {
|
|||
green_letters?: string[];
|
||||
yellow_letters?: string[];
|
||||
unfit_letters?: string[];
|
||||
set_current_word?: {(word: string): void};
|
||||
submit_guess?: {(): void};
|
||||
}
|
||||
|
||||
export class WordleLibrary {
|
||||
|
|
Loading…
Reference in New Issue