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.
57 lines
1.9 KiB
57 lines
1.9 KiB
import dotenv from "dotenv";
|
|
import {SetupServer} from "./SetupServer.js";
|
|
import {GameServer} from "./GameServer.js";
|
|
import cryptoRandomString from "crypto-random-string";
|
|
import {SavedWebhook} from "./SavedWebhook.js";
|
|
import pino from "pino"
|
|
|
|
const log = pino()
|
|
|
|
async function main(): Promise<void> {
|
|
const {parsed, error} = dotenv.config()
|
|
if (error || !parsed) {
|
|
throw error ?? Error("No parsed data.")
|
|
}
|
|
const clientSecret = parsed["DISCORD_CLIENT_SECRET"]
|
|
const appId = parsed["DISCORD_APP_ID"]
|
|
const botToken = parsed["DISCORD_BOT_TOKEN"]
|
|
const publicKey = parsed["DISCORD_PUBLIC_KEY"]
|
|
const listenPort = parseInt(parsed["HTTP_PORT"] ?? "5244")
|
|
const listenAddress = parsed["HTTP_ADDRESS"] ?? "127.0.0.1"
|
|
const cookieSecret = parsed["COOKIE_SECRET"] ?? cryptoRandomString({length: 32, type: "base64"})
|
|
const gameWebhook = new SavedWebhook("game.json", {logger: log})
|
|
const adminWebhook = new SavedWebhook("admin.json", {logger: log})
|
|
const factory: { game: () => GameServer, setup: () => SetupServer } = {
|
|
game(): never {
|
|
throw Error("game factory not set up yet")
|
|
},
|
|
setup(): never {
|
|
throw Error("setup factory not set up yet")
|
|
},
|
|
}
|
|
const deps = {
|
|
appId,
|
|
listenAddress,
|
|
listenPort,
|
|
clientSecret,
|
|
cookieSecret,
|
|
gameFactory: () => factory.game(),
|
|
setupFactory: () => factory.setup(),
|
|
gameWebhook,
|
|
adminWebhook,
|
|
botToken,
|
|
publicKey,
|
|
}
|
|
factory.setup = () => new SetupServer(deps)
|
|
factory.game = () => new GameServer(deps)
|
|
await Promise.all([gameWebhook.load(), adminWebhook.load()])
|
|
if (gameWebhook.isPresent && adminWebhook.isPresent) {
|
|
await factory.game().initialize()
|
|
} else {
|
|
await factory.setup().initialize()
|
|
}
|
|
}
|
|
|
|
main().catch((err) => {
|
|
log.fatal(err, "Startup failed!")
|
|
})
|
|
|