invoking comment added to gamestate initalizer

master
j 2024-03-31 23:25:11 -04:00
parent 94cb64f37a
commit 5b97b4b219
1 changed files with 15 additions and 9 deletions

View File

@ -1,4 +1,5 @@
import { DatabaseService } from "../db/services/Database";
import { Comment } from "../rdrama/models/Comment";
/**
* Represents the state of a game session for an Oregon Trail-style game.
@ -6,7 +7,9 @@ import { DatabaseService } from "../db/services/Database";
* to load from and save to a database.
*/
class GameState {
authorId: number = 0;
comment: Comment;
authorId: number;
authorName: string;
amountSpentOnAnimals: number = 0;
amountSpentOnAmmunition: number = 0;
actualResponseTimeForBang: number = 0;
@ -16,7 +19,7 @@ class GameState {
yesNoResponseToQuestions: string = '';
eventCounter: number = 0;
turnNumberForSettingDate: number = 0;
currentDate: string = '';
currentDate: string = 'MONDAY MARCH 29 1847';
shootingExpertiseLevelChoice: number = 0;
eatingChoice: number = 0;
amountSpentOnFood: number = 0;
@ -43,8 +46,10 @@ class GameState {
phase: PHASE_ENUM = PHASE_ENUM.SETUP;
subPhase: number = 0;
private constructor(authorId: number, state?: Partial<GameState>) {
this.authorId = authorId;
private constructor(comment: Comment, state?: Partial<GameState>) {
this.comment = comment
this.authorId = comment.author_id;
this.authorName = comment.author_name;
Object.assign(this, state); // Initialize with loaded state or undefined
}
@ -57,20 +62,21 @@ class GameState {
/**
* Loads an existing game state from the database or creates a new one if it doesn't exist.
* @param {number} authorId - The ID of the author/player.
* @param {Comment} comment - The invoking comment of the author/player.
* @returns {Promise<GameState>} - The loaded or newly created game state.
*/
public static async load(authorId: number): Promise<GameState> {
const loadedState = await DatabaseService.loadGameState(authorId);
public static async load(comment: Comment): Promise<GameState> {
const loadedState = await DatabaseService.loadGameState(comment.author_id);
if (loadedState) {
return new GameState(authorId, loadedState);
return new GameState(comment, loadedState);
} else {
// Create a new GameState with default values
const newState = new GameState(authorId);
const newState = new GameState(comment);
await newState.save(); // Optionally save the new state to the database
return newState;
}
}
}
export default GameState