OregonTrail/src/phases/travel.ts

62 lines
2.7 KiB
TypeScript

import GameState, { PHASE_ENUM } from '../game/gameState';
import GameFlow from '../game/gameFlow';
import { MessageService } from '../utils/MessageService';
export class travelPhase {
static async handleTravel(gameState: GameState, userInput?: string | number): Promise<void> {
let responseMessage = "";
if (!userInput || userInput?.toString().toLowerCase() === "start") {
responseMessage = "DO YOU WANT TO EAT\n(1) POORLY\n(2) MODERATELY\n(3) WELL: ";
await MessageService.statusUpdate(gameState, responseMessage);
return;
}
// Check if userInput is a number or can be converted to a valid number
if (isNaN(Number(userInput))) {
responseMessage = "Invalid input. Please enter a number.\n\n"
responseMessage += "DO YOU WANT TO EAT\n(1) POORLY\n(2) MODERATELY\n(3) WELL: ";
await MessageService.statusUpdate(gameState, responseMessage);
return; // Exit if input is not a valid number
}
let eatingChoice: number = parseInt(userInput.toString(), 10);
// Validate eating choice
if (eatingChoice < 1 || eatingChoice > 3) {
responseMessage = "Invalid choice.\n\n"
responseMessage += "DO YOU WANT TO EAT\n(1) POORLY\n(2) MODERATELY\n(3) WELL: ";
await MessageService.statusUpdate(gameState, responseMessage);
return;
}
// Calculate potential food consumption without updating the game state yet
let potentialAmountSpentOnFood = gameState.amountSpentOnFood - (8 + 5 * eatingChoice);
// Check if the potential amount spent on food is negative
if (potentialAmountSpentOnFood < 0) {
// If it would result in a negative value, inform the user they can't eat that well
responseMessage = "YOU CAN'T EAT THAT WELL\n\n";
responseMessage += "DO YOU WANT TO EAT\n(1) POORLY\n(2) MODERATELY\n(3) WELL: ";
await MessageService.statusUpdate(gameState, responseMessage);
return; // Exit the method to prevent further execution
} else {
// If the amount is sufficient, update the game state with the new amount
gameState.amountSpentOnFood = potentialAmountSpentOnFood;
}
// Calculate total mileage
gameState.totalMileageWholeTrip += 200 + (gameState.amountSpentOnAnimals - 220) / 5 + 10 * Math.random();
gameState.blizzardFlag = gameState.insufficientClothingFlag = false;
gameState.phase = PHASE_ENUM.RIDERS_ATTACK;
// Save the game state after making changes
await gameState.save();
// Continue with the game flow
GameFlow.executeCurrentPhase(gameState);
}
}