Full game
This commit is contained in:
@@ -2,34 +2,40 @@
|
||||
|
||||
let worker: Worker | null = null;
|
||||
|
||||
export async function runScript(
|
||||
export function sendDataToScript(data: String) {
|
||||
if (worker) {
|
||||
worker.postMessage({ type: "data", data });
|
||||
}
|
||||
}
|
||||
|
||||
export async function startScript(
|
||||
script: string,
|
||||
appendOutput: (line: string) => void,
|
||||
appendTerminal?: (line: string) => void
|
||||
appendTerminal: (line: string) => void
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (worker) {
|
||||
worker.terminate();
|
||||
}
|
||||
|
||||
worker = new Worker(new URL('./script-runner.worker.ts', import.meta.url));
|
||||
worker = new Worker(new URL("./worker.ts", import.meta.url));
|
||||
|
||||
worker.onmessage = (event) => {
|
||||
const { type, line, error } = event.data;
|
||||
|
||||
if (type === 'output') {
|
||||
if (type === "output") {
|
||||
appendOutput(line);
|
||||
} else if (type === 'terminal') {
|
||||
appendTerminal?.(line);
|
||||
} else if (type === 'complete') {
|
||||
} else if (type === "terminal") {
|
||||
appendTerminal(line);
|
||||
} else if (type === "complete") {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
resolve();
|
||||
} else if (type === 'error') {
|
||||
} else if (type === "error") {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
reject(new Error(error));
|
||||
} else if (type === 'stopped') {
|
||||
} else if (type === "stopped") {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
resolve();
|
||||
@@ -42,12 +48,12 @@ export async function runScript(
|
||||
reject(error);
|
||||
};
|
||||
|
||||
worker.postMessage({ type: 'run', script });
|
||||
worker.postMessage({ type: "run", script });
|
||||
});
|
||||
}
|
||||
|
||||
export function stopScript(): void {
|
||||
if (worker) {
|
||||
worker.postMessage({ type: 'stop' });
|
||||
worker.postMessage({ type: "stop" });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import init, { GameState } from "./wasm/script_runner";
|
||||
import init, { GameState, GameStateHuman } from "../wasm/runner";
|
||||
|
||||
let wasmReady = false;
|
||||
let wasmInitPromise: Promise<void> | null = null;
|
||||
let currentGame: GameState | null = null;
|
||||
let currentGame: GameStateHuman | null = null;
|
||||
|
||||
async function initWasm(): Promise<void> {
|
||||
if (wasmReady) return;
|
||||
@@ -20,9 +20,21 @@ async function initWasm(): Promise<void> {
|
||||
}
|
||||
|
||||
self.onmessage = async (event) => {
|
||||
const { type, script } = event.data;
|
||||
const { type, ...event_data } = event.data;
|
||||
|
||||
if (type === "init") {
|
||||
if (type === "data") {
|
||||
if (currentGame !== null) {
|
||||
currentGame.take_input(event_data.data);
|
||||
|
||||
if (currentGame.is_error()) {
|
||||
currentGame = null;
|
||||
self.postMessage({ type: "complete" });
|
||||
} else if (currentGame.is_done()) {
|
||||
currentGame = null;
|
||||
self.postMessage({ type: "complete" });
|
||||
}
|
||||
}
|
||||
} else if (type === "init") {
|
||||
try {
|
||||
await initWasm();
|
||||
self.postMessage({ type: "ready" });
|
||||
@@ -37,24 +49,33 @@ self.onmessage = async (event) => {
|
||||
self.postMessage({ type: "output", line });
|
||||
};
|
||||
|
||||
const appendDebug = (line: string) => {
|
||||
self.postMessage({ type: "output", line }); // debug also goes to output
|
||||
};
|
||||
|
||||
const appendTerminal = (line: string) => {
|
||||
self.postMessage({ type: "terminal", line });
|
||||
};
|
||||
|
||||
currentGame = new GameStateHuman(
|
||||
true,
|
||||
event_data.script,
|
||||
"Agent",
|
||||
appendOutput,
|
||||
appendOutput,
|
||||
appendTerminal
|
||||
);
|
||||
|
||||
currentGame.print_start();
|
||||
|
||||
/*
|
||||
currentGame = new GameState(
|
||||
script,
|
||||
event_data.script,
|
||||
"max",
|
||||
appendOutput,
|
||||
appendDebug,
|
||||
appendOutput,
|
||||
|
||||
script,
|
||||
// TODO: pick opponent
|
||||
event_data.script,
|
||||
"min",
|
||||
appendOutput,
|
||||
appendDebug,
|
||||
appendOutput,
|
||||
|
||||
appendTerminal
|
||||
);
|
||||
@@ -70,6 +91,7 @@ self.onmessage = async (event) => {
|
||||
if (currentGame) {
|
||||
self.postMessage({ type: "complete" });
|
||||
}
|
||||
*/
|
||||
} catch (error) {
|
||||
self.postMessage({ type: "error", error: String(error) });
|
||||
}
|
||||
Reference in New Issue
Block a user