Compare commits

..

2 Commits

Author SHA1 Message Date
j 20b7a883fa Travel phase and cleanup of numbers around fort purchases 2024-04-18 00:45:25 -04:00
j 0107af0a7b sanitization around numerical inputs 2024-04-18 00:44:47 -04:00
2 changed files with 70 additions and 9 deletions

View File

@ -330,19 +330,20 @@ class GameFlow {
// Update gameState based on itemType // Update gameState based on itemType
switch (itemType) { switch (itemType) {
case PurchaseOption.OXEN: case PurchaseOption.OXEN:
this.gameState.amountSpentOnAnimals += totalCost * modifier; //Round down after multiplication
this.gameState.amountSpentOnAnimals += Math.floor(totalCost * modifier);
break; break;
case PurchaseOption.FOOD: case PurchaseOption.FOOD:
this.gameState.amountSpentOnFood += totalCost * modifier; this.gameState.amountSpentOnFood += Math.floor(totalCost * modifier);
break; break;
case PurchaseOption.AMMO: case PurchaseOption.AMMO:
this.gameState.amountSpentOnAmmunition += totalCost * 50 * modifier; this.gameState.amountSpentOnAmmunition += Math.floor(totalCost * 50 * modifier);
break; break;
case PurchaseOption.CLOTHING: case PurchaseOption.CLOTHING:
this.gameState.amountSpentOnClothing += totalCost * modifier; this.gameState.amountSpentOnClothing += Math.floor(totalCost * modifier);
break; break;
case PurchaseOption.MISC: case PurchaseOption.MISC:
this.gameState.amountSpentOnMiscellaneousSupplies += totalCost * modifier; this.gameState.amountSpentOnMiscellaneousSupplies += Math.floor(totalCost * modifier);
break; break;
// Add cases for other item types // Add cases for other item types
} }
@ -460,10 +461,62 @@ class GameFlow {
return; return;
} }
private async handleTravel(): Promise<void> { private async handleTravel(userInput?: string | number): Promise<void> {
// Logic for handling the travel phase let responseMessage = "";
if (!userInput || userInput?.toString().toLowerCase() === "start") {
responseMessage = "DO YOU WANT TO EAT\n(1) POORLY\n(2) MODERATELY\n(3) WELL: ";
this.statusUpdate(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 this.statusUpdate(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 this.statusUpdate(responseMessage);
return;
}
// Calculate potential food consumption without updating the game state yet
let potentialAmountSpentOnFood = this.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 this.statusUpdate(responseMessage);
return; // Exit the method to prevent further execution
} else {
// If the amount is sufficient, update the game state with the new amount
this.gameState.amountSpentOnFood = potentialAmountSpentOnFood;
}
// Calculate total mileage
this.gameState.totalMileageWholeTrip += 200 + (this.gameState.amountSpentOnAnimals - 220) / 5 + 10 * Math.random();
this.gameState.blizzardFlag = this.gameState.insufficientClothingFlag = false;
this.gameState.phase = PHASE_ENUM.RIDERS_ATTACK;
// Save the game state after making changes
await this.gameState.save();
// Continue with the game flow
this.executeCurrentPhase();
}
private async handleRidersAttack(userInput?: string | number): Promise<void> { private async handleRidersAttack(userInput?: string | number): Promise<void> {
// Logic for handling a riders attack, may involve checking userInput for response to attack // Logic for handling a riders attack, may involve checking userInput for response to attack
} }

View File

@ -4,6 +4,7 @@ export class CommentParser {
/** /**
* Parses commands from the body of a single comment triggered by !!Oregon. * Parses commands from the body of a single comment triggered by !!Oregon.
* If the comment contains only "!!Oregon", it treats it as a "start" command. * If the comment contains only "!!Oregon", it treats it as a "start" command.
* Additionally, if the command or any argument is a number, it is floored.
* @param comment A single Comment object to be processed. * @param comment A single Comment object to be processed.
* @returns An object containing the command and its arguments, if any. * @returns An object containing the command and its arguments, if any.
*/ */
@ -15,8 +16,15 @@ export class CommentParser {
if (match) { if (match) {
// If the command is exactly "!!Oregon" with no additional command word, set command to "start" // If the command is exactly "!!Oregon" with no additional command word, set command to "start"
const command = match[1] ? match[1].toLowerCase() : "start"; let command = match[1] ? match[1].toLowerCase() : "start";
const args = match[2] ? match[2].split(' ').map(arg => arg.trim()) : []; const numCommand = Number(command);
if (!isNaN(numCommand) && isFinite(numCommand)) {
command = Math.floor(numCommand).toString();
}
const args = match[2] ? match[2].split(' ').map(arg => {
const numArg = Number(arg);
return !isNaN(numArg) && isFinite(numArg) ? Math.floor(numArg).toString() : arg;
}) : [];
return { command, args }; return { command, args };
} }