add scripting interface

This commit is contained in:
blek 2023-09-03 23:33:13 +10:00
parent 0526044d23
commit 410ff840b0
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 78 additions and 1 deletions

View File

@ -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;

View File

@ -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];
}
}

View File

@ -1,4 +1,6 @@
import './app.css'
import './lib/scriptinterface';
import App from './App.svelte'
const app = new App({

9
src/vite-env.d.ts vendored
View File

@ -1,4 +1,11 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />
import type { ScriptInterface } from "./lib/scriptinterface";
const TARGETS: string;
declare module 'qr-creator';
declare module 'qr-creator';
declare global {
var ScriptInterface: ScriptInterface;
}