add scripting interface
This commit is contained in:
parent
0526044d23
commit
410ff840b0
|
@ -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;
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
import './app.css'
|
||||
import './lib/scriptinterface';
|
||||
|
||||
import App from './App.svelte'
|
||||
|
||||
const app = new App({
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue