add set_current_word and submit_guess hooks to ScriptInterface.GameState
This commit is contained in:
parent
9039499ad2
commit
cba520b3ec
120
src/Game.svelte
120
src/Game.svelte
|
@ -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,6 +75,54 @@
|
||||||
updateScriptInterface(state);
|
updateScriptInterface(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function submitGuess() {
|
||||||
|
if (word_position != word.length) return;
|
||||||
|
if (!isIn(current_word())) {
|
||||||
|
not_a_word = true;
|
||||||
|
setTimeout(() => {not_a_word = false}, 1000);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current_guess + 1 == guesses) {
|
||||||
|
endgame = true;
|
||||||
|
wins = is_win();
|
||||||
|
green_letters = word.split('');
|
||||||
|
current_guess++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (is_win()) {
|
||||||
|
endgame = true;
|
||||||
|
wins = true;
|
||||||
|
green_letters = word.split('');
|
||||||
|
current_guess++;
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let c_word = current_word();
|
||||||
|
for (const ii in c_word.split('')) {
|
||||||
|
const i = parseInt(ii);
|
||||||
|
const letter = c_word[i];
|
||||||
|
|
||||||
|
if (word.indexOf(letter) !== -1) {
|
||||||
|
if (word[i] == letter) {
|
||||||
|
if (green_letters.indexOf(letter) === -1)
|
||||||
|
green_letters.push(letter);
|
||||||
|
} else {
|
||||||
|
if (yellow_letters.indexOf(letter) === -1)
|
||||||
|
yellow_letters.push(letter);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unfit_letters.push(letter);
|
||||||
|
}
|
||||||
|
green_letters = green_letters;
|
||||||
|
yellow_letters = yellow_letters;
|
||||||
|
unfit_letters = unfit_letters;
|
||||||
|
}
|
||||||
|
|
||||||
|
word_position = 0;
|
||||||
|
current_guess++;
|
||||||
|
}
|
||||||
|
|
||||||
let loading = true;
|
let loading = true;
|
||||||
let not_a_word = false;
|
let not_a_word = false;
|
||||||
let game_creator = false;
|
let game_creator = false;
|
||||||
|
@ -83,10 +151,6 @@
|
||||||
return current_word() == word;
|
return current_word() == word;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i != guesses; i++) {
|
|
||||||
guessed.push(new Array(word.length).fill(''));
|
|
||||||
}
|
|
||||||
|
|
||||||
document.onkeydown = e => {
|
document.onkeydown = e => {
|
||||||
if (endgame) return;
|
if (endgame) return;
|
||||||
if (not_a_word) return;
|
if (not_a_word) return;
|
||||||
|
@ -97,51 +161,7 @@
|
||||||
word_position -= 1;
|
word_position -= 1;
|
||||||
}
|
}
|
||||||
if (e.key == 'Enter') {
|
if (e.key == 'Enter') {
|
||||||
if (word_position != word.length) return;
|
submitGuess();
|
||||||
if (!isIn(current_word())) {
|
|
||||||
not_a_word = true;
|
|
||||||
setTimeout(() => {not_a_word = false}, 1000);
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current_guess + 1 == guesses) {
|
|
||||||
endgame = true;
|
|
||||||
wins = is_win();
|
|
||||||
green_letters = word.split('');
|
|
||||||
current_guess++;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (is_win()) {
|
|
||||||
endgame = true;
|
|
||||||
wins = true;
|
|
||||||
green_letters = word.split('');
|
|
||||||
current_guess++;
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let c_word = current_word();
|
|
||||||
for (const ii in c_word.split('')) {
|
|
||||||
const i = parseInt(ii);
|
|
||||||
const letter = c_word[i];
|
|
||||||
|
|
||||||
if (word.indexOf(letter) !== -1) {
|
|
||||||
if (word[i] == letter) {
|
|
||||||
if (green_letters.indexOf(letter) === -1)
|
|
||||||
green_letters.push(letter);
|
|
||||||
} else {
|
|
||||||
if (yellow_letters.indexOf(letter) === -1)
|
|
||||||
yellow_letters.push(letter);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unfit_letters.push(letter);
|
|
||||||
}
|
|
||||||
green_letters = green_letters;
|
|
||||||
yellow_letters = yellow_letters;
|
|
||||||
unfit_letters = unfit_letters;
|
|
||||||
}
|
|
||||||
|
|
||||||
word_position = 0;
|
|
||||||
current_guess++;
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue