From 72b3c4537d207ab1724cd0863b8dd5301fe3b70a Mon Sep 17 00:00:00 2001 From: j Date: Tue, 2 Apr 2024 22:57:31 -0400 Subject: [PATCH] added reset game state, updated documentation around active column --- src/db/services/Database.ts | 68 +++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/src/db/services/Database.ts b/src/db/services/Database.ts index 082c9fe..5e1b263 100644 --- a/src/db/services/Database.ts +++ b/src/db/services/Database.ts @@ -27,56 +27,66 @@ export class DatabaseService { } /** - * Loads an existing game state for a given author from the database. - * If an existing game state is found, it returns a new GameState instance initialized with the stored values. - * If no game state exists for the given author, it returns null, indicating that a new game state needs to be initialized. - * + * Loads the active game state for a given author from the database. + * If no active game state exists, it returns null. + * + * @param {number} authorId - The unique identifier for the author whose game state is to be loaded. + * @returns {Promise} The loaded game state if it exists and is active, otherwise null. + * @throws {Error} Throws an error if the database operation fails. * @example - * const gameState = await DatabaseService.loadGameState(authorId); + * const gameState = await DatabaseService.loadGameState(1); * if (gameState) { - * console.log('Game state loaded successfully.'); + * // Game state exists and is active * } else { - * console.log('No existing game state found. Initializing a new game.'); + * // No active game state found * } - * - * @param {number} authorId - The unique identifier for the author of the game. - * @returns {Promise} A promise that resolves to a GameState instance if found, or null if no existing game state is present. - * @throws {Error} Will throw an error if the database operation fails. */ public static async loadGameState(authorId: number): Promise { const db = await DatabaseService.getDatabase(); - const sql = `SELECT data FROM game_state WHERE authorId = ?`; + const sql = `SELECT data FROM game_state WHERE authorId = ? AND active = TRUE`; const row = await db.get(sql, [authorId]); if (row) { - return JSON.parse(row.data) as GameState; // Assuming GameState constructor can take authorId and a partial state object + return JSON.parse(row.data) as GameState; } else { - return null; // Or return a new GameState with defaults + return null; } } /** - * Saves the current game state for a given author to the database. - * If an existing game state for the author exists, it updates the stored values. If no game state exists, it inserts a new record. - * This method ensures the game state is persisted between sessions, allowing players to resume their game at any time. - * - * @example - * await DatabaseService.saveGameState(authorId, gameState); - * console.log('Game state saved successfully.'); - * - * @param {number} authorId - The unique identifier for the author of the game. - * @param {GameState} gameState - The current game state to be saved. - * @returns {Promise} A promise that resolves when the game state is successfully saved. - * @throws {Error} Will throw an error if the save operation fails. - */ + * Saves the current game state to the database. If an active game state already exists for the author, + * it updates that state. Otherwise, it creates a new active game state. + * + * @param {number} authorId - The unique identifier for the author of the game state. + * @param {GameState} gameState - The current state of the game to be saved. + * @throws {Error} Throws an error if the database operation fails. + * @example + * await DatabaseService.saveGameState(1, currentGameState); + */ public static async saveGameState(authorId: number, gameState: GameState): Promise { const db = await DatabaseService.getDatabase(); const data = JSON.stringify(gameState); const sql = ` - INSERT INTO game_state (authorId, data) VALUES (?, ?) + INSERT INTO game_state (authorId, data, active) VALUES (?, ?, TRUE) ON CONFLICT(authorId) DO UPDATE SET - data = excluded.data; + data = excluded.data + WHERE active = TRUE; `; await db.run(sql, [authorId, data]); } + /** + * Resets the game state for a given author by setting the active state to false. + * This method should be called after a death event or when the player decides to start over. + * + * @param {number} authorId - The unique identifier for the author of the game state to be reset. + * @throws {Error} Throws an error if the database operation fails. + * @example + * await DatabaseService.resetGameState(1); + */ + public static async resetGameState(authorId: number): Promise { + const db = await DatabaseService.getDatabase(); + const sql = `UPDATE game_state SET active = FALSE WHERE authorId = ? AND active = TRUE`; + await db.run(sql, [authorId]); + } + }