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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 38 additions and 34 deletions

@ -57,7 +57,7 @@ export function CompanyLocation(props: IProps): React.ReactElement {
*/ */
const currentPosition = jobTitle ? CompanyPositions[jobTitle] : null; const currentPosition = jobTitle ? CompanyPositions[jobTitle] : null;
Player.location = companyNameAsLocationName(props.companyName); Player.gotoLocation(companyNameAsLocationName(props.companyName));
function startInfiltration(e: React.MouseEvent<HTMLElement>): void { function startInfiltration(e: React.MouseEvent<HTMLElement>): void {
if (!e.isTrusted) { if (!e.isTrusted) {

@ -24,14 +24,12 @@ import Button from "@mui/material/Button";
import { useRerender } from "../../ui/React/hooks"; import { useRerender } from "../../ui/React/hooks";
function travel(to: CityName): void { function travel(to: CityName): void {
const cost = CONSTANTS.TravelCost; if (!Player.travel(to)) {
if (!Player.canAfford(cost)) {
return; return;
} }
if (!Settings.SuppressTravelConfirmation) {
Player.loseMoney(cost, "other"); dialogBoxCreate(`You are now in ${to}!`);
Player.travel(to); }
if (!Settings.SuppressTravelConfirmation) dialogBoxCreate(`You are now in ${to}!`);
Router.toPage(Page.City); Router.toPage(Page.City);
} }
@ -41,8 +39,7 @@ export function TravelAgencyRoot(): React.ReactElement {
useRerender(1000); useRerender(1000);
function startTravel(city: CityName): void { function startTravel(city: CityName): void {
const cost = CONSTANTS.TravelCost; if (!Player.canAfford(CONSTANTS.TravelCost)) {
if (!Player.canAfford(cost)) {
return; return;
} }
if (Settings.SuppressTravelConfirmation) { if (Settings.SuppressTravelConfirmation) {

@ -14,7 +14,6 @@ interface IProps {
} }
export function TravelConfirmationModal(props: IProps): React.ReactElement { export function TravelConfirmationModal(props: IProps): React.ReactElement {
const cost = CONSTANTS.TravelCost;
function travel(): void { function travel(): void {
props.travel(); props.travel();
} }
@ -22,7 +21,8 @@ export function TravelConfirmationModal(props: IProps): React.ReactElement {
return ( return (
<Modal open={props.open} onClose={props.onClose}> <Modal open={props.open} onClose={props.onClose}>
<Typography> <Typography>
Would you like to travel to {props.city}? The trip will cost <Money money={cost} forPurchase={true} />. Would you like to travel to {props.city}? The trip will cost{" "}
<Money money={CONSTANTS.TravelCost} forPurchase={true} />.
</Typography> </Typography>
<br /> <br />
<br /> <br />

@ -269,7 +269,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
); );
return false; return false;
} }
Player.location = LocationName.Sector12RothmanUniversity; Player.gotoLocation(LocationName.Sector12RothmanUniversity);
break; break;
case LocationName.VolhavenZBInstituteOfTechnology.toLowerCase(): case LocationName.VolhavenZBInstituteOfTechnology.toLowerCase():
if (Player.city != CityName.Volhaven) { if (Player.city != CityName.Volhaven) {
@ -279,7 +279,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
); );
return false; return false;
} }
Player.location = LocationName.VolhavenZBInstituteOfTechnology; Player.gotoLocation(LocationName.VolhavenZBInstituteOfTechnology);
break; break;
default: default:
helpers.log(ctx, () => `Invalid university name: '${universityName}'.`); helpers.log(ctx, () => `Invalid university name: '${universityName}'.`);
@ -327,7 +327,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
); );
return false; return false;
} }
Player.location = LocationName.AevumCrushFitnessGym; Player.gotoLocation(LocationName.AevumCrushFitnessGym);
break; break;
case LocationName.AevumSnapFitnessGym.toLowerCase(): case LocationName.AevumSnapFitnessGym.toLowerCase():
if (Player.city != CityName.Aevum) { if (Player.city != CityName.Aevum) {
@ -338,7 +338,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
); );
return false; return false;
} }
Player.location = LocationName.AevumSnapFitnessGym; Player.gotoLocation(LocationName.AevumSnapFitnessGym);
break; break;
case LocationName.Sector12IronGym.toLowerCase(): case LocationName.Sector12IronGym.toLowerCase():
if (Player.city != CityName.Sector12) { if (Player.city != CityName.Sector12) {
@ -349,7 +349,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
); );
return false; return false;
} }
Player.location = LocationName.Sector12IronGym; Player.gotoLocation(LocationName.Sector12IronGym);
break; break;
case LocationName.Sector12PowerhouseGym.toLowerCase(): case LocationName.Sector12PowerhouseGym.toLowerCase():
if (Player.city != CityName.Sector12) { if (Player.city != CityName.Sector12) {
@ -360,7 +360,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
); );
return false; return false;
} }
Player.location = LocationName.Sector12PowerhouseGym; Player.gotoLocation(LocationName.Sector12PowerhouseGym);
break; break;
case LocationName.VolhavenMilleniumFitnessGym.toLowerCase(): case LocationName.VolhavenMilleniumFitnessGym.toLowerCase():
if (Player.city != CityName.Volhaven) { if (Player.city != CityName.Volhaven) {
@ -371,7 +371,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
); );
return false; return false;
} }
Player.location = LocationName.VolhavenMilleniumFitnessGym; Player.gotoLocation(LocationName.VolhavenMilleniumFitnessGym);
break; break;
default: default:
helpers.log(ctx, () => `Invalid gym name: ${gymName}. gymWorkout() failed`); helpers.log(ctx, () => `Invalid gym name: ${gymName}. gymWorkout() failed`);
@ -401,12 +401,10 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
case CityName.NewTokyo: case CityName.NewTokyo:
case CityName.Ishima: case CityName.Ishima:
case CityName.Volhaven: case CityName.Volhaven:
if (Player.money < CONSTANTS.TravelCost) { if (!Player.travel(cityName)) {
helpers.log(ctx, () => "Not enough money to travel."); helpers.log(ctx, () => "Not enough money to travel.");
return false; return false;
} }
Player.loseMoney(CONSTANTS.TravelCost, "other");
Player.city = cityName;
helpers.log(ctx, () => `Traveled to ${cityName}`); helpers.log(ctx, () => `Traveled to ${cityName}`);
Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain / 50000); Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain / 50000);
return true; return true;

@ -79,7 +79,11 @@ export function NetscriptSleeve(): InternalAPI<NetscriptSleeve> {
const cityName = getEnumHelper("CityName").nsGetMember(ctx, _cityName); const cityName = getEnumHelper("CityName").nsGetMember(ctx, _cityName);
checkSleeveAPIAccess(ctx); checkSleeveAPIAccess(ctx);
checkSleeveNumber(ctx, sleeveNumber); checkSleeveNumber(ctx, sleeveNumber);
return Player.sleeves[sleeveNumber].travel(cityName); if (!Player.sleeves[sleeveNumber].travel(cityName)) {
helpers.log(ctx, () => "Not enough money to travel.");
return false;
}
return true;
}, },
setToCompanyWork: (ctx) => (_sleeveNumber, _companyName) => { setToCompanyWork: (ctx) => (_sleeveNumber, _companyName) => {
const sleeveNumber = helpers.number(ctx, "sleeveNumber", _sleeveNumber); const sleeveNumber = helpers.number(ctx, "sleeveNumber", _sleeveNumber);

@ -531,20 +531,23 @@ export function gainCodingContractReward(
} }
} }
export function travel(this: PlayerObject, to: CityName): boolean { export function travel(this: PlayerObject, cityName: CityName): boolean {
if (Cities[to] == null) { if (Cities[cityName] == null) {
console.warn(`Player.travel() called with invalid city: ${to}`); throw new Error(`Player.travel() was called with an invalid city: ${cityName}`);
}
if (!this.canAfford(CONSTANTS.TravelCost)) {
return false; return false;
} }
this.city = to;
this.loseMoney(CONSTANTS.TravelCost, "other");
this.city = cityName;
return true; return true;
} }
export function gotoLocation(this: PlayerObject, to: LocationName): boolean { export function gotoLocation(this: PlayerObject, to: LocationName): boolean {
if (Locations[to] == null) { if (Locations[to] == null) {
console.warn(`Player.gotoLocation() called with invalid location: ${to}`); throw new Error(`Player.gotoLocation() was called with an invalid location: ${to}`);
return false;
} }
this.location = to; this.location = to;

@ -44,6 +44,7 @@ import { SleeveCrimeWork } from "./Work/SleeveCrimeWork";
import * as sleeveMethods from "./SleeveMethods"; import * as sleeveMethods from "./SleeveMethods";
import { calculateIntelligenceBonus } from "../formulas/intelligence"; import { calculateIntelligenceBonus } from "../formulas/intelligence";
import { getEnumHelper } from "../../utils/EnumHelper"; import { getEnumHelper } from "../../utils/EnumHelper";
import { Cities } from "../../Locations/Cities";
export class Sleeve extends Person implements SleevePerson { export class Sleeve extends Person implements SleevePerson {
currentWork: SleeveWork | null = null; currentWork: SleeveWork | null = null;
@ -255,13 +256,16 @@ export class Sleeve extends Person implements SleevePerson {
} }
/** Travel to another City. Costs money from player */ /** 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)) { if (!Player.canAfford(CONSTANTS.TravelCost)) {
return false; return false;
} }
Player.loseMoney(CONSTANTS.TravelCost, "sleeves"); Player.loseMoney(CONSTANTS.TravelCost, "sleeves");
this.city = newCity; this.city = cityName;
return true; return true;
} }

@ -1,6 +1,5 @@
import React from "react"; import React from "react";
import { Button, Typography } from "@mui/material"; import { Button, Typography } from "@mui/material";
import { Player } from "@player";
import { CityName } from "@enums"; import { CityName } from "@enums";
import { Sleeve } from "../Sleeve"; import { Sleeve } from "../Sleeve";
import { CONSTANTS } from "../../../Constants"; import { CONSTANTS } from "../../../Constants";
@ -19,11 +18,10 @@ interface IProps {
export function TravelModal(props: IProps): React.ReactElement { export function TravelModal(props: IProps): React.ReactElement {
function travel(city: string): void { 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"); 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.sleeve.stopWork();
props.rerender(); props.rerender();
props.onClose(); props.onClose();