From ac050fc50579ea6d8c3333559d2644d15742d815 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Wed, 6 Jan 2021 17:18:43 -0700 Subject: [PATCH] Begin work on implementing game loading from the datastore. --- server/src/main.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/server/src/main.ts b/server/src/main.ts index e0147df..6294c24 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -1,13 +1,27 @@ import * as toml from "toml"; import { Logger } from "tslog"; -import { readFileSync } from "fs"; import { Game } from "./objects/Game"; import startWebsocket from "./websocket"; import { Validate } from "./utils/validate"; import { processExit } from "./utils/cleanup"; +import { readdirSync, readFileSync } from "fs"; export const conf: config = toml.parse(readFileSync(`server.toml`, `utf-8`)); +/* + * These are the objects that we use to keep track of the different games, + * there are two different types of game, hibernated, and active. Hibernated + * games are games which have data associated with them in the datastore, but + * have not had any requests associated with them since they were hibernated. + * Active games are games that have active socket connections associated with + * them or have not been cleaned up by the game creation process yet. Active + * games become hibernated once the server has been closed while they are in + * the middle of a game, if they are in the lobby, the cleanup systems will + * just delete the game outright instead of saving it to disk. These methods + * allow us to keep games that are in the process of being played if/when the + * server crashes/restarts from having to completely start the game over again. + */ +export var hibernatedGames: string[] = []; export var games: {[index: string]: Game} = {}; export const log: Logger = new Logger({ @@ -25,7 +39,9 @@ if (Validate.config(conf)) { // Add event listeners if we want to use the datastore saving game system if (conf.datastores.enabled) { - // Get games from datastore + // Get game IDs from datastore + hibernatedGames = readdirSync(conf.datastores.directory) + .filter(g => g.endsWith(conf.datastores.filetype)); process.on(`uncaughtException`, processExit); process.on(`SIGINT`, processExit);