-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82d4437
commit 38ef034
Showing
4 changed files
with
51 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}, | ||
}, | ||
}, | ||
}); |