MISC: Refactor code of traveling and going to location (#1365)

This commit is contained in:
catloversg
2024-06-09 03:52:10 +07:00
committed by GitHub
parent b8f03cb50b
commit a354867fc4
8 changed files with 38 additions and 34 deletions
@@ -531,20 +531,23 @@ export function gainCodingContractReward(
}
}
export function travel(this: PlayerObject, to: CityName): boolean {
if (Cities[to] == null) {
console.warn(`Player.travel() called with invalid city: ${to}`);
export function travel(this: PlayerObject, cityName: CityName): boolean {
if (Cities[cityName] == null) {
throw new Error(`Player.travel() was called with an invalid city: ${cityName}`);
}
if (!this.canAfford(CONSTANTS.TravelCost)) {
return false;
}
this.city = to;
this.loseMoney(CONSTANTS.TravelCost, "other");
this.city = cityName;
return true;
}
export function gotoLocation(this: PlayerObject, to: LocationName): boolean {
if (Locations[to] == null) {
console.warn(`Player.gotoLocation() called with invalid location: ${to}`);
return false;
throw new Error(`Player.gotoLocation() was called with an invalid location: ${to}`);
}
this.location = to;
+6 -2
View File
@@ -44,6 +44,7 @@ import { SleeveCrimeWork } from "./Work/SleeveCrimeWork";
import * as sleeveMethods from "./SleeveMethods";
import { calculateIntelligenceBonus } from "../formulas/intelligence";
import { getEnumHelper } from "../../utils/EnumHelper";
import { Cities } from "../../Locations/Cities";
export class Sleeve extends Person implements SleevePerson {
currentWork: SleeveWork | null = null;
@@ -255,13 +256,16 @@ export class Sleeve extends Person implements SleevePerson {
}
/** Travel to another City. Costs money from player */
travel(newCity: CityName): boolean {
travel(cityName: CityName): boolean {
if (Cities[cityName] == null) {
throw new Error(`Sleeve.travel() was called with an invalid city: ${cityName}`);
}
if (!Player.canAfford(CONSTANTS.TravelCost)) {
return false;
}
Player.loseMoney(CONSTANTS.TravelCost, "sleeves");
this.city = newCity;
this.city = cityName;
return true;
}
+2 -4
View File
@@ -1,6 +1,5 @@
import React from "react";
import { Button, Typography } from "@mui/material";
import { Player } from "@player";
import { CityName } from "@enums";
import { Sleeve } from "../Sleeve";
import { CONSTANTS } from "../../../Constants";
@@ -19,11 +18,10 @@ interface IProps {
export function TravelModal(props: IProps): React.ReactElement {
function travel(city: string): void {
if (!Player.canAfford(CONSTANTS.TravelCost)) {
if (!props.sleeve.travel(city as CityName)) {
dialogBoxCreate("You cannot afford to have this sleeve travel to another city");
return;
}
props.sleeve.city = city as CityName;
Player.loseMoney(CONSTANTS.TravelCost, "sleeves");
props.sleeve.stopWork();
props.rerender();
props.onClose();