|
|
|
import {checkAndClearXSRFCookie, generateXSRFCookie, XSRFRoute} from "./CookieHelpers.js";
|
|
|
|
import {renderError} from "./PugRenderer.js";
|
|
|
|
import {getBaseUrl} from "./FastifyHelpers.js";
|
|
|
|
import pug from "pug";
|
|
|
|
import {BaseServer} from "./BaseServer.js";
|
|
|
|
import {PullCommand} from "./commands/game/PullCommand.js";
|
|
|
|
import {JoinCommand} from "./commands/game/JoinCommand.js";
|
|
|
|
import {singleColumnQueryResult} from "./queries/QueryHelpers.js";
|
|
|
|
import {UnjoinCommand} from "./commands/debug/UnjoinCommand.js";
|
|
|
|
import {DailyCommand} from "./commands/game/DailyCommand.js";
|
|
|
|
|
|
|
|
export class GameServer extends BaseServer {
|
|
|
|
|
|
|
|
async _initInternal(): Promise<void> {
|
|
|
|
const promisedGameGuildIds = this.pool.query<[string]>({
|
|
|
|
text: `SELECT *
|
|
|
|
FROM GetGuildIdsAbleToUseGameCommands()`,
|
|
|
|
rowMode: "array",
|
|
|
|
})
|
|
|
|
const promisedGenders = this.pool.query<{ id: string, name: string }>({
|
|
|
|
text: `SELECT id, name
|
|
|
|
FROM GetRegisterableGenders()`,
|
|
|
|
})
|
|
|
|
this.slashcmd.registerCommand(new PullCommand(this.slashcmd, {
|
|
|
|
pool: this.pool,
|
|
|
|
gameGuildIds: singleColumnQueryResult(await promisedGameGuildIds),
|
|
|
|
}))
|
|
|
|
this.slashcmd.registerCommand(new JoinCommand(this.slashcmd, {
|
|
|
|
pool: this.pool,
|
|
|
|
gameGuildIds: singleColumnQueryResult(await promisedGameGuildIds),
|
|
|
|
genders: (await promisedGenders).rows,
|
|
|
|
}))
|
|
|
|
this.slashcmd.registerCommand(new UnjoinCommand(this.slashcmd, {
|
|
|
|
pool: this.pool,
|
|
|
|
gameGuildIds: singleColumnQueryResult(await promisedGameGuildIds),
|
|
|
|
}))
|
|
|
|
this.slashcmd.registerCommand(new DailyCommand(this.slashcmd, {
|
|
|
|
pool: this.pool,
|
|
|
|
gameGuildIds: singleColumnQueryResult(await promisedGameGuildIds),
|
|
|
|
}))
|
|
|
|
this.server.get("/game/started", async (req, res) => {
|
|
|
|
const token = await generateXSRFCookie(res)
|
|
|
|
res.code(200)
|
|
|
|
res.type("text/html")
|
|
|
|
res.send(pug.renderFile("static/pages/game/running.pug", {
|
|
|
|
baseUrl: getBaseUrl(req),
|
|
|
|
setupModeUrl: `game/stop?token=${token}`,
|
|
|
|
shutdownUrl: `shutdown?token=${token}`,
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
this.server.get("/game", async (req, res) => {
|
|
|
|
res.redirect("game/started")
|
|
|
|
})
|
|
|
|
this.server.get("/game/start", async (req, res) => {
|
|
|
|
res.redirect("started")
|
|
|
|
})
|
|
|
|
this.server.get("/setup", async (req, res) => {
|
|
|
|
res.redirect("game/started")
|
|
|
|
})
|
|
|
|
this.server.get("/setup/gameChannel", async (req, res) => {
|
|
|
|
res.redirect("../game/started")
|
|
|
|
})
|
|
|
|
this.server.get("/setup/adminChannel", async (req, res) => {
|
|
|
|
res.redirect("../game/started")
|
|
|
|
})
|
|
|
|
this.server.get("/setup/clear", async (req, res) => {
|
|
|
|
res.redirect("../game/started")
|
|
|
|
})
|
|
|
|
this.server.get("/setup/done", async (req, res) => {
|
|
|
|
res.redirect("../game/started")
|
|
|
|
})
|
|
|
|
this.server.get<XSRFRoute>("/game/stop", async (req, res) => {
|
|
|
|
res.redirect("../shutdown")
|
|
|
|
})
|
|
|
|
this.server.get<XSRFRoute>("/shutdown", async (req, res) => {
|
|
|
|
if (!checkAndClearXSRFCookie(req, res)) {
|
|
|
|
return renderError({
|
|
|
|
baseUrl: getBaseUrl(req),
|
|
|
|
res,
|
|
|
|
code: 400,
|
|
|
|
error: "Token was incorrect or not set.",
|
|
|
|
context: "shutting down the game",
|
|
|
|
buttonText: "Return to Game",
|
|
|
|
buttonUrl: "game/started"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
res.code(200)
|
|
|
|
res.type("text/html")
|
|
|
|
res.send(pug.renderFile("static/pages/shutdown.pug"))
|
|
|
|
setImmediate(async () => {
|
|
|
|
this.server.log.info("Shutting down the game server.")
|
|
|
|
try {
|
|
|
|
await this.server.close()
|
|
|
|
} catch (e) {
|
|
|
|
this.server.log.error(e, "Failed to shut down the game server")
|
|
|
|
}
|
|
|
|
this.server.log.info("Shut down. Good night...")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|