Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherdjerred committed Oct 6, 2023
1 parent 82d4437 commit 38ef034
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 81 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/game/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { exportSave, importSave, setupGame } from "./browser/game.js";

export const gameMachine = createMachine(
{
/** @xstate-layout N4IgpgJg5mDOIC5QAoC2BDAxgCwJYDswBKAOl1gH1YAXdAJ2oKgGIIB7Qs-ANzYGswJNFjyFS5KrQZMEBXpnSMOAbQAMAXTXrEoAA5tYuJfh0gAHogC0ARgBMAFhL37ANlUBWd6tXWA7PYBOBwAaEABPRAAOR1sXd3tba0iAZl8fAKSAX0zQ4RwCYjJKGnpGfBYwOjo2OhJdABtFADMa1CEMfLEiyVKZOTYFYy0tU31DY1MLBBsHJ1cPLx9-IPtQiOnra0ck3zTk6xdbAMjVSNts3I7RQtxUfWly1g5BfoF2kQLxO5qyqFkeAaKXAqDQjJAgMZGYEmcFTWyqWwkVS7ZIBHy2XwuezuZJrRB2axODLHezJVLpLI5EB5a5fe6-ZiVaq1BrNVrvTo3b4PP79QbQ4YaUYGKEcSaIeGI5G+VHozHY3HhCWRJHJSKRdwBMlpawZTEXalXT4kOhgepsdAQJ6cV6CGnG03my3-eRAkGaIXgyETWEShFIlFouzynF4hD2dVORIuXxJcm685U+1dR0Wq1Mmp1RrUFp0NrJwqp518t34QXaL0in2gKbueEkXy2dxnTEYrF1sMY9wNpLxFwpHVBA0F0glBgUVlhJjWl4At4jkhj6gTxpT8ouwFDUGevRV6HijazZxuTzePyBEJKhDJFwBEh+A6xgfpROXD5dJcr9BripVTOsnN2QXT9Jz6AF+XdMFd3GfdfUPRxjwWM9lkvdY3F8EgY0fONB1fQ130KUDHjAMx6SgiE9zFODLDOBtkncTZfC8c8VjDPwMKfPtn11Sk305UgiJYIsIHI71YJrP0pUD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Wlns4XDqzcuLNOQt+sNRRKZUqNTq9yasd18etrO63wdWm5ztAwzdJA9ii9Kt94v9CHC2JI5hmlil9nlCXVhwezRENAA+imggVmMaWXcY8I8uRB1Vh+Icx87T9CwNAXyEOZvDL7H4NqsIiKAnXvPYlvZW5t7OYnPFHNGtb3p7P0POGInySmqWnJy0B0O2neW18A5dRHSLdcXRGPcm2vHZlXMUFwi3CVnEiRRVVMKVEMRbxQQ8B8exyADLjAIwXhXP4IN5KD8MFYVRQ9Wt5k8JZTDRLwLCFMIw0IjNSBIhg4wNSinUgkt+XBWIhShJwxQlHZkXY7dr12DxVnMPiCQpV8ClgTAdTAfBYGwJhyFE6igQkhA1VsWVW3bVsrGvTFUJ2Ehwg8bdw3Cex1OVHENV-QT6EwJhUAwfAIAstcaOsvDkV3ZtLAsbwIlbJJULlWUvLBVVsQ9PCu01IjSDIl4R2uNhx1NR8cnK4lAJZJcQPzMDVx5KzjEk+jZJhJiJTcUxIlRbcYVMDCEj8KMgrqsryMa0iyWTSlqVpOaSAaplF2A0CuVirrS3ictPXMb1kMQ5jEDcXZy1MRw-M83YLwbNINXwJgIDgQxJ32zqNwAWjcaxvDwm8fBhaULysCUAb8GwNksZt218nxt28LT6T7V4-uLbqEGOmTTHlNZnGldYPAlIUZUrDw3FiLCkWQzHmg-KhcfE-HiccEgkeSdZA3DdTUI9KIYjy1t5TDAjZtKhkKvEDm4vxhI3BRYUztPZzTCppwTtyjK6Z2TTZf4khhIgJXDtCXybBvZCkUsFZENsBSElleVYlBeJkgvFmcmxl83ytjdvSbTYRT2VUMNWLKlh2RxgaGxYwWB-2BLnOgQ6gzZ44c-nbHcFLvAUlwcu3NtUocGbuzNracfAg6NzWew7Z9R3ncRQbPBRPy0tDPykd8N6UiAA */
predictableActionArguments: true,
strict: true,
tsTypes: {} as import("./machine.typegen.d.ts").Typegen0,
Expand Down
81 changes: 2 additions & 79 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();
20 changes: 19 additions & 1 deletion packages/backend/src/machine.ts
Original file line number Diff line number Diff line change
@@ -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(
{
Expand All @@ -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: {},
},
);
29 changes: 29 additions & 0 deletions packages/backend/src/webserver/machine.ts
Original file line number Diff line number Diff line change
@@ -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,
});
},
},
},
});

0 comments on commit 38ef034

Please sign in to comment.