diff --git a/src/Game.svelte b/src/Game.svelte index dfd7ee6..d77fa24 100644 --- a/src/Game.svelte +++ b/src/Game.svelte @@ -12,6 +12,7 @@ import Keyboard from "./Keyboard.svelte"; import GameCreator from "./GameCreator.svelte"; import { decode } from "./lib/cipher"; + import { GameState, updateScriptInterface } from "./lib/scriptinterface"; let targets = getForNWord(5); @@ -22,6 +23,30 @@ let word_position = 0; let wins = false; let endgame = false; + + setTimeout(() => { + setGameState({ + word, + guesses, + guessed, + current_guess, + word_position, + wins, + endgame + }); + }, 500) + + function setGameState(state: GameState) { + word = state.word ?? word; + guesses = state.guesses ?? guesses; + guessed = state.guessed ?? guessed; + current_guess = state.current_guess ?? current_guess; + word_position = state.word_position ?? word_position; + wins = state.wins ?? wins; + endgame = state.endgame ?? endgame; + + updateScriptInterface(state); + } let loading = true; let not_a_word = false; diff --git a/src/lib/scriptinterface.ts b/src/lib/scriptinterface.ts new file mode 100644 index 0000000..625d2f2 --- /dev/null +++ b/src/lib/scriptinterface.ts @@ -0,0 +1,43 @@ +import { random } from "./random"; +import { encode, decode } from "./cipher"; + +let scriptsAllowed = false; + +export function allowScripts() { + scriptsAllowed = true; +} + +export class GameState { + word?: string; + guesses?: number; + guessed?: string[][]; + current_guess?: number; + word_position?: number; + wins?: boolean; + endgame?: boolean; +} + +export class WordleLibrary { + readonly random = random; + readonly encode = encode; + readonly decode = decode; +} + +export class ScriptInterface { + readonly gameState: GameState = new GameState(); + scriptsAllowed: {(): boolean} = () => scriptsAllowed; + readonly lib: WordleLibrary = new WordleLibrary(); +} +globalThis.ScriptInterface = new ScriptInterface(); +const si = globalThis.ScriptInterface; + +export function updateScriptInterface(state: GameState) { + if (!scriptsAllowed) + return + + for (const key of Object.keys(state)) { + // @ts-ignore + si.gameState[key] = state[key]; + } +} + diff --git a/src/main.ts b/src/main.ts index 8a909a1..1bddf2f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,6 @@ import './app.css' +import './lib/scriptinterface'; + import App from './App.svelte' const app = new App({ diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index a3c177c..d6069ba 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1,4 +1,11 @@ /// /// + +import type { ScriptInterface } from "./lib/scriptinterface"; + const TARGETS: string; -declare module 'qr-creator'; \ No newline at end of file +declare module 'qr-creator'; + +declare global { + var ScriptInterface: ScriptInterface; +}