diff --git a/src/game/gameSession.ts b/src/game/gameSession.ts index 7e8bbdb..2aa2ceb 100644 --- a/src/game/gameSession.ts +++ b/src/game/gameSession.ts @@ -1,74 +1,72 @@ import GameState from './gameState'; import GameFlow from './gameFlow'; +import { Comment } from '../rdrama/models/Comment'; +import { CommentParser } from '../rdrama/services/CommentParser'; class GameSession { private gameState: GameState; - private gameManager: GameFlow; - private authorId: string; + private gameFlow: GameFlow; + private authorId: number; - constructor(authorId: string) { + private constructor(authorId: number, gameState: GameState) { this.authorId = authorId; - // Initialize GameState. If there's saved data for this authorId, load it; otherwise, start with default values. - this.gameState = new GameState(authorId); - // Initialize the GameManager with the current game state. - this.gameManager = new GameFlow(this.gameState); + this.gameState = gameState; + this.gameFlow = new GameFlow(gameState); } - async handleUserInput(userId: string, input: string): Promise { - const gameState = await this.loadGameState(userId); + /** + * Loads an existing game session for the given author or creates a new one if none exists. + * @param authorId The ID of the author for whom to load or create the game session. + * @returns A GameSession instance for the given author. + */ + public static async loadSession(authorId: number): Promise { + const gameState = await GameState.load(authorId); + return new GameSession(authorId, gameState); + } - switch (input.split(' ')[0].toLowerCase()) { - case "!!oregon": - await this.processCommand(gameState, input); + /** + * Handles user input by parsing the command and executing the corresponding action. + * @param comment The comment containing the user input. + */ + public async handleUserInput(comment: Comment): Promise { + const { command, args } = CommentParser.parseCommand(comment); + + // Process the command + switch (command) { + case 'startover': + // Example of resetting the game + this.gameState.reset(); + break; + case 'buysupplies': + // Example of handling a purchase command + // Logic to handle buying supplies + break; + // Add additional cases for other commands + default: + console.error('Unrecognized command:', command); break; - // Additional commands as necessary } - await this.saveGameState(gameState); - } - - private async processCommand(gameState: GameState, command: string): Promise { - const args = command.split(' '); - // Process different game setup commands (e.g., "start over", "buy supplies") - if (args[1].toLowerCase() === "startover") { - gameState.reset(); // Resets game state to initial values - } else if (args[1].toLowerCase() === "buysupplies") { - // Transition to handling purchases - // This would involve setting the next expected action in the game state - // And possibly sending a prompt for the first purchase - } - // Handle other commands and shopping logic + // After processing the command, save any changes to the game state + await this.gameState.save(); } /** - * Saves the current game state. - * This method would serialize the GameState object and save it to a database or file system. - */ - private saveGameState(): void { - // Implementation for saving the game state. - console.log('Saving game state...'); - // Serialize and save the GameState object here. - } - - /** - * Loads a saved game state. - * This method would deserialize a saved GameState object and update the current game state with it. - */ - private loadGameState(): void { - // Implementation for loading a saved game state. - console.log('Loading game state...'); - // Deserialize and load the GameState object here. - // Update this.gameState with the loaded state. - } - - /** - * Retrieves the current game status update for the player. - * This could include information about the player's progress, inventory, and available actions. - * @returns A string representing the current game status. + * Generates a status update message based on the current state of the game. + * @returns A string representing the current game status, formatted for display. */ public getStatusUpdate(): string { - // Generate a status update message based on the current game state. - // This message could be formatted with Markdown for better presentation. - return this.gameManager.generateStatusMessage(); + // Use the GameFlow instance to generate a status message + return this.gameFlow.generateStatusMessage(); + } + + /** + * Resets the game to its initial state and saves the updated state. + */ + private async reset(): Promise { + this.gameState.reset(); + await this.gameState.save(); } } + +export { GameSession }; \ No newline at end of file