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.
100 lines
3.9 KiB
100 lines
3.9 KiB
import {SavedWebhook} from "./SavedWebhook.js";
|
|
import fastify, {FastifyInstance} from "fastify";
|
|
import {DiscordWebhookHandler} from "./DiscordWebhookHandler.js";
|
|
import {getBaseUrl, RouteWithQuerystring} from "./FastifyHelpers.js";
|
|
import pug from "pug";
|
|
import {renderError} from "./PugRenderer.js";
|
|
|
|
export class GameServer {
|
|
readonly server: FastifyInstance
|
|
readonly gameWebhook: SavedWebhook
|
|
readonly adminWebhook: SavedWebhook
|
|
readonly appId: string
|
|
readonly secret: string
|
|
readonly port: number
|
|
constructor({appId, secret, port}: {appId: string, secret: string, port: number}) {
|
|
this.server = fastify({
|
|
logger: true
|
|
})
|
|
this.appId = appId
|
|
this.secret = secret
|
|
this.port = port
|
|
this.gameWebhook = new SavedWebhook("broadcasthook.json", {logger: this.server.log})
|
|
this.adminWebhook = new SavedWebhook("logginghook.json", {logger: this.server.log})
|
|
}
|
|
|
|
async initialize(): Promise<void> {
|
|
const gameHandler = new DiscordWebhookHandler({
|
|
webhook: this.gameWebhook,
|
|
templateFilename: "setupGame.pug",
|
|
appId: this.appId,
|
|
secret: this.secret,
|
|
destinationFunc: () => {
|
|
if (this.adminWebhook.isPresent) {
|
|
return "clear"
|
|
} else {
|
|
return "adminChannel"
|
|
}
|
|
},
|
|
})
|
|
const adminHandler = new DiscordWebhookHandler({
|
|
webhook: this.adminWebhook,
|
|
templateFilename: "setupAdmin.pug",
|
|
appId: this.appId,
|
|
secret: this.secret,
|
|
destinationFunc: () => {
|
|
if (this.gameWebhook.isPresent) {
|
|
return "clear"
|
|
} else {
|
|
return "gameChannel"
|
|
}
|
|
},
|
|
})
|
|
this.server.get<RouteWithQuerystring>("/setup", async (req, res) => {
|
|
if (!this.gameWebhook.isPresent) {
|
|
res.redirect(`setup/gameChannel`)
|
|
} else if (!this.adminWebhook.isPresent) {
|
|
res.redirect(`setup/adminChannel`)
|
|
} else {
|
|
res.redirect(`setup/done`)
|
|
}
|
|
})
|
|
this.server.get<RouteWithQuerystring>("/setup/gameChannel", async (req, res) => {
|
|
return await gameHandler.handleRequest(req, res)
|
|
})
|
|
this.server.get<RouteWithQuerystring>("/setup/adminChannel", async (req, res) => {
|
|
return await adminHandler.handleRequest(req, res)
|
|
})
|
|
this.server.get<RouteWithQuerystring>("/setup/clear", async (req, res) => {
|
|
try {
|
|
await Promise.all([this.gameWebhook, this.adminWebhook]
|
|
.filter((item) => item.isPresent)
|
|
.map((item) => item.clearHook()))
|
|
} catch (e) {
|
|
return renderError({
|
|
baseUrl: getBaseUrl(req),
|
|
res,
|
|
error: e,
|
|
context: "clearing the broadcast webhook"
|
|
})
|
|
}
|
|
res.redirect(".")
|
|
})
|
|
this.server.get<RouteWithQuerystring>("/setup/done", async (req, res) => {
|
|
if (!this.gameWebhook.isPresent) {
|
|
res.redirect("gameChannel")
|
|
} else if (!this.adminWebhook.isPresent) {
|
|
res.redirect("adminChannel")
|
|
} else {
|
|
res.code(200)
|
|
res.type("text/html")
|
|
res.send(pug.renderFile("static/pages/setupDone.pug", {
|
|
baseUrl: getBaseUrl(req),
|
|
clearUrl: "setup/clear",
|
|
gameSetupUrl: "setup/gameChannel",
|
|
adminSetupUrl: "setup/adminChannel"}))
|
|
}
|
|
})
|
|
await Promise.all([this.gameWebhook.load(), this.adminWebhook.load(), this.server.listen(this.port, "127.0.0.1")])
|
|
}
|
|
} |