added reset game state, updated documentation around active column

master
j 2024-04-02 22:57:31 -04:00
parent 22f1b560f6
commit 72b3c4537d
1 changed files with 39 additions and 29 deletions

View File

@ -27,56 +27,66 @@ export class DatabaseService {
} }
/** /**
* Loads an existing game state for a given author from the database. * Loads the active 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 active game state exists, it returns null.
* If no game state exists for the given author, it returns null, indicating that a new game state needs to be initialized. *
* * @param {number} authorId - The unique identifier for the author whose game state is to be loaded.
* @returns {Promise<GameState | null>} The loaded game state if it exists and is active, otherwise null.
* @throws {Error} Throws an error if the database operation fails.
* @example * @example
* const gameState = await DatabaseService.loadGameState(authorId); * const gameState = await DatabaseService.loadGameState(1);
* if (gameState) { * if (gameState) {
* console.log('Game state loaded successfully.'); * // Game state exists and is active
* } else { * } 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<GameState | null>} 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<GameState | null> { public static async loadGameState(authorId: number): Promise<GameState | null> {
const db = await DatabaseService.getDatabase(); 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]); const row = await db.get(sql, [authorId]);
if (row) { 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 { } else {
return null; // Or return a new GameState with defaults return null;
} }
} }
/** /**
* Saves the current game state for a given author to the database. * Saves the current game state to the database. If an active game state already exists for the author,
* If an existing game state for the author exists, it updates the stored values. If no game state exists, it inserts a new record. * it updates that state. Otherwise, it creates a new active game state.
* This method ensures the game state is persisted between sessions, allowing players to resume their game at any time. *
* * @param {number} authorId - The unique identifier for the author of the game state.
* @example * @param {GameState} gameState - The current state of the game to be saved.
* await DatabaseService.saveGameState(authorId, gameState); * @throws {Error} Throws an error if the database operation fails.
* console.log('Game state saved successfully.'); * @example
* * await DatabaseService.saveGameState(1, currentGameState);
* @param {number} authorId - The unique identifier for the author of the game. */
* @param {GameState} gameState - The current game state to be saved.
* @returns {Promise<void>} A promise that resolves when the game state is successfully saved.
* @throws {Error} Will throw an error if the save operation fails.
*/
public static async saveGameState(authorId: number, gameState: GameState): Promise<void> { public static async saveGameState(authorId: number, gameState: GameState): Promise<void> {
const db = await DatabaseService.getDatabase(); const db = await DatabaseService.getDatabase();
const data = JSON.stringify(gameState); const data = JSON.stringify(gameState);
const sql = ` const sql = `
INSERT INTO game_state (authorId, data) VALUES (?, ?) INSERT INTO game_state (authorId, data, active) VALUES (?, ?, TRUE)
ON CONFLICT(authorId) DO UPDATE SET ON CONFLICT(authorId) DO UPDATE SET
data = excluded.data; data = excluded.data
WHERE active = TRUE;
`; `;
await db.run(sql, [authorId, data]); 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<void> {
const db = await DatabaseService.getDatabase();
const sql = `UPDATE game_state SET active = FALSE WHERE authorId = ? AND active = TRUE`;
await db.run(sql, [authorId]);
}
} }