diff options
Diffstat (limited to 'src/run.ts')
-rw-r--r-- | src/run.ts | 52 |
1 files changed, 46 insertions, 6 deletions
@@ -1,10 +1,15 @@ -import { advanceState } from "./game"; +import readline from "node:readline/promises"; +import { advanceStateRemote, Connection, getConnection } from "./connection"; +import { advanceStateLocal } from "./game"; import { Action } from "./game/types"; -import { createSessionState, SessionState } from "./state"; +import { + createLocalSessionState, + createNetworkedSessionState, + SessionState, +} from "./state"; import { renderState, createAndBindHandler, prepareTerminal, Key } from "./ui"; export const run = async () => { - let state: SessionState = createSessionState("xyz"); let actionQueue: Action[] = []; const updateAction = (key: Key) => { @@ -23,13 +28,48 @@ export const run = async () => { } }; - prepareTerminal(); - createAndBindHandler(updateAction, process.exit); + let connection: Connection; + const rl = readline.createInterface(process.stdin, process.stdout); + const solo = ["y", ""].includes( + (await rl.question("Play solo? (Y/n) > ")).trim().toLowerCase() + ); + + let state: SessionState; + + if (!solo) { + state = createNetworkedSessionState("NETWORKED-SESSION"); + const host = ["y", ""].includes( + (await rl.question("Host? (Y/n) > ")).trim().toLowerCase() + ); + if (host) { + console.log(`Starting the server...`); + connection = await getConnection({ localPort: 9932 }); + } else { + const hostname = + (await rl.question("Enter remote hostname [localhost] > ")).trim() || + "localhost"; + connection = await getConnection({ remotePort: 9932, hostname }); + } + + try { + } catch {} + } else { + state = createLocalSessionState("LOCAL-GAME"); + } + + prepareTerminal(); + while (true) { let nextAction = actionQueue.pop(); - state = await advanceState(state, nextAction); + + state = await advanceStateLocal(state, nextAction); + if (state.remotePlayerGameState) { + connection!.sendState(state); + state = await advanceStateRemote(connection!, state); + } + renderState(state); } }; |