You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
4.3 KiB
109 lines
4.3 KiB
import {SetupServer} from "./SetupServer.js";
|
|
import {checkAndClearXSRFCookie, generateXSRFCookie, XSRFRoute} from "./CookieHelpers.js";
|
|
import {renderError} from "./PugRenderer.js";
|
|
import {getBaseUrl} from "./FastifyHelpers.js";
|
|
import pug from "pug";
|
|
import {BaseServer, BaseServerDeps} from "./BaseServer.js";
|
|
import {PullCommand} from "./commands/game/PullCommand.js";
|
|
|
|
export class GameServer extends BaseServer {
|
|
readonly setupFactory: () => SetupServer
|
|
|
|
constructor(deps: BaseServerDeps & { setupFactory: () => SetupServer }) {
|
|
super(deps)
|
|
this.setupFactory = deps.setupFactory
|
|
}
|
|
|
|
async _initInternal(): Promise<void> {
|
|
this.slashcmd.registerCommand(new PullCommand(this.slashcmd, this.gameWebhook))
|
|
this.server.get("/game/started", async (req, res) => {
|
|
const token = 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) => {
|
|
if (!checkAndClearXSRFCookie(req, res)) {
|
|
return renderError({
|
|
baseUrl: getBaseUrl(req),
|
|
res,
|
|
code: 400,
|
|
error: "Token was incorrect or not set.",
|
|
context: "stopping the game",
|
|
buttonText: "Return to Game",
|
|
buttonUrl: "game/started"
|
|
})
|
|
}
|
|
res.code(200)
|
|
res.type("text/html")
|
|
res.send(pug.renderFile("static/pages/game/stop.pug", {
|
|
startedUrl: "setup"
|
|
}))
|
|
setImmediate(async () => {
|
|
this.server.log.info("Shutting down the game server and switching to the setup server.")
|
|
try {
|
|
await this.server.close()
|
|
} catch (e) {
|
|
this.server.log.error(e, "Failed to shut down the game server")
|
|
}
|
|
try {
|
|
await this.setupFactory().initialize()
|
|
} catch (e) {
|
|
this.server.log.error(e, "Failed to start up the setup server")
|
|
}
|
|
this.server.log.info("Successfully switched from the game server to the setup server.")
|
|
})
|
|
|
|
})
|
|
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...")
|
|
})
|
|
})
|
|
}
|
|
} |