From 38ef034d2275e474b0eafcaeb155a2711d997b37 Mon Sep 17 00:00:00 2001 From: Jerred Shepherd Date: Thu, 5 Oct 2023 20:06:28 -0700 Subject: [PATCH] checkpoint --- packages/backend/src/game/machine.ts | 2 +- packages/backend/src/index.ts | 81 +---------------------- packages/backend/src/machine.ts | 20 +++++- packages/backend/src/webserver/machine.ts | 29 ++++++++ 4 files changed, 51 insertions(+), 81 deletions(-) create mode 100644 packages/backend/src/webserver/machine.ts diff --git a/packages/backend/src/game/machine.ts b/packages/backend/src/game/machine.ts index 656e353..4300fdc 100644 --- a/packages/backend/src/game/machine.ts +++ b/packages/backend/src/game/machine.ts @@ -8,7 +8,7 @@ import { exportSave, importSave, setupGame } from "./browser/game.js"; export const gameMachine = createMachine( { - /** @xstate-layout N4IgpgJg5mDOIC5QAoC2BDAxgCwJYDswBKAOl1gH1YAXdAJ2oKgGIIB7Qs-ANzYGswJNFjyFS5KrQZMEBXpnSMOAbQAMAXTXrEoAA5tYuJfh0gAHogC0ARgBMAFhL37ANlUBWd6tXWA7PYBOBwAaEABPRAAOR1sXd3tba0iAZl8fAKSAX0zQ4RwCYjJKGnpGfBYwOjo2OhJdABtFADMa1CEMfLEiyVKZOTYFYy0tU31DY1MLBBsHJ1cPLx9-IPtQiOnra0ck3zTk6xdbAMjVSNts3I7RQtxUfWly1g5BfoF2kQLxO5qyqFkeAaKXAqDQjJAgMZGYEmcFTWyqWwkVS7ZIBHy2XwuezuZJrRB2axODLHezJVLpLI5EB5a5fe6-ZiVaq1BrNVrvTo3b4PP79QbQ4YaUYGKEcSaIeGI5G+VHozHY3HhCWRJHJSKRdwBMlpawZTEXalXT4kOhgepsdAQJ6cV6CGnG03my3-eRAkGaIXgyETWEShFIlFouzynF4hD2dVORIuXxJcm685U+1dR0Wq1Mmp1RrUFp0NrJwqp518t34QXaL0in2gKbueEkXy2dxnTEYrF1sMY9wNpLxFwpHVBA0F0glBgUVlhJjWl4At4jkhj6gTxpT8ouwFDUGevRV6HijazZxu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fAg6NzWew7Z9R3ncRQbPBRPy0tDPykd8N6UiAA */ predictableActionArguments: true, strict: true, tsTypes: {} as import("./machine.typegen.d.ts").Typegen0, diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 282886d..a4f80dd 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -1,81 +1,4 @@ -import { createWebServer } from "./webserver/index.js"; -import { registerSlashCommands } from "./discord/bot/slashCommands/rest.js"; -import { logger } from "./logger.js"; -import { match } from "ts-pattern"; -import { LoginResponse, StatusResponse } from "@discord-plays-pokemon/common"; -import { getConfig } from "./config/index.js"; -import { streamMachine } from "./discord/stream/machine.js"; import { interpret } from "xstate"; -import { gameMachine } from "./game/machine.js"; +import { machine } from "./machine.js"; -const stream = interpret(streamMachine); - -if (getConfig().stream.enabled) { - logger.info("starting stream"); - stream.start(); -} - -const game = interpret(gameMachine); - -if (getConfig().game.enabled) { - logger.info("starting game"); - game.start(); -} - -if (getConfig().bot.commands.update) { - await registerSlashCommands({ - areScreenshotsEnabled: getConfig().bot.commands.screenshot.enabled, - botToken: getConfig().bot.discord_token, - }); -} - -if (getConfig().web.enabled) { - const { socket } = createWebServer({ - port: getConfig().web.port, - webAssetsPath: getConfig().web.assets, - isApiEnabled: getConfig().web.api.enabled, - isCorsEnabled: getConfig().web.cors, - }); - - if (socket) { - socket.subscribe((event) => { - match(event) - .with({ request: { kind: "command" } }, (event) => { - logger.info("handling command request", event.request); - game.send({ type: "command" }); - }) - .with({ request: { kind: "login" } }, (event) => { - logger.info("handling login request", event.request); - // TODO: perform auth here - const player = { discordId: "id", discordUsername: "username" }; - const response: LoginResponse = { - kind: "login", - value: player, - }; - event.socket.emit("response", response); - }) - .with({ request: { kind: "screenshot" } }, (event) => { - logger.info("handling screenshot request", event.request); - game.send({ type: "screenshot" }); - }) - .with({ request: { kind: "status" } }, (event) => { - logger.info("handling status request", event.request); - const response: StatusResponse = { - kind: "status", - value: { - playerList: [], - }, - }; - event.socket.emit("response", response); - }) - .exhaustive(); - }); - } -} - -if (getConfig().game.saves.auto_export.enabled) { - logger.info("auto export saves is enabled"); - setInterval(() => { - game.send({ type: "export" }); - }, getConfig().game.saves.auto_export.interval_in_milliseconds); -} +interpret(machine).start(); diff --git a/packages/backend/src/machine.ts b/packages/backend/src/machine.ts index eef1847..72255b6 100644 --- a/packages/backend/src/machine.ts +++ b/packages/backend/src/machine.ts @@ -1,4 +1,7 @@ import { createMachine } from "xstate"; +import { gameMachine } from "./game/machine.js"; +import { streamMachine } from "./discord/stream/machine.js"; +import { webMachine } from "./webserver/machine.js"; export const machine = createMachine( { @@ -7,10 +10,25 @@ export const machine = createMachine( strict: true, tsTypes: {} as import("./machine.typegen.d.ts").Typegen0, schema: {}, + invoke: [ + { + src: "web", + }, + { + src: "game", + }, + { + src: "stream", + }, + ], states: {}, }, { - services: {}, + services: { + game: gameMachine, + stream: streamMachine, + web: webMachine, + }, actions: {}, }, ); diff --git a/packages/backend/src/webserver/machine.ts b/packages/backend/src/webserver/machine.ts new file mode 100644 index 0000000..b6fe8a4 --- /dev/null +++ b/packages/backend/src/webserver/machine.ts @@ -0,0 +1,29 @@ +import { createMachine } from "xstate"; +import { createWebServer } from "./index.js"; +import { getConfig } from "../config/index.js"; + +export const webMachine = createMachine({ + predictableActionArguments: true, + strict: true, + tsTypes: {} as import("./machine.typegen.d.ts").Typegen0, + schema: { + services: {} as { + start: { + data: undefined; + }; + }, + }, + initial: "ready", + states: { + ready: { + entry: () => { + createWebServer({ + port: getConfig().web.port, + isCorsEnabled: getConfig().web.cors, + isApiEnabled: getConfig().web.api.enabled, + webAssetsPath: getConfig().web.assets, + }); + }, + }, + }, +});