mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-16 04:20:08 +02:00
TYPESAFETY: CompanyName (#650)
This commit is contained in:
@@ -9,18 +9,23 @@
|
||||
|
||||
import type { SleevePerson } from "@nsdefs";
|
||||
import type { Augmentation } from "../../Augmentation/Augmentation";
|
||||
import type { Company } from "../../Company/Company";
|
||||
import type { CompanyPosition } from "../../Company/CompanyPosition";
|
||||
import type { SleeveWork } from "./Work/Work";
|
||||
|
||||
import { Player } from "@player";
|
||||
import { Person } from "../Person";
|
||||
|
||||
import { Companies } from "../../Company/Companies";
|
||||
import { CompanyPositions } from "../../Company/CompanyPositions";
|
||||
import { Contracts } from "../../Bladeburner/data/Contracts";
|
||||
import { CONSTANTS } from "../../Constants";
|
||||
import { ClassType, CityName, CrimeType, FactionWorkType, GymType, LocationName, UniversityClassType } from "@enums";
|
||||
import {
|
||||
ClassType,
|
||||
CityName,
|
||||
CrimeType,
|
||||
FactionWorkType,
|
||||
GymType,
|
||||
LocationName,
|
||||
UniversityClassType,
|
||||
CompanyName,
|
||||
} from "@enums";
|
||||
|
||||
import { Factions } from "../../Faction/Factions";
|
||||
|
||||
@@ -277,18 +282,11 @@ export class Sleeve extends Person implements SleevePerson {
|
||||
* Start work for one of the player's companies
|
||||
* Returns boolean indicating success
|
||||
*/
|
||||
workForCompany(companyName: string): boolean {
|
||||
if (!Companies[companyName] || Player.jobs[companyName] == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const company: Company | null = Companies[companyName];
|
||||
const companyPosition: CompanyPosition | null = CompanyPositions[Player.jobs[companyName]];
|
||||
if (company == null) return false;
|
||||
if (companyPosition == null) return false;
|
||||
workForCompany(companyName: CompanyName): boolean {
|
||||
const companyPositionName = Player.jobs[companyName];
|
||||
if (!companyPositionName) return false;
|
||||
|
||||
this.startWork(new SleeveCompanyWork(companyName));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Player } from "@player";
|
||||
import { LocationName } from "@enums";
|
||||
import { CompanyName, JobName } from "@enums";
|
||||
import { Generic_fromJSON, Generic_toJSON, IReviverValue, constructorsForReviver } from "../../../utils/JSONReviver";
|
||||
import { Sleeve } from "../Sleeve";
|
||||
import { applySleeveGains, SleeveWorkClass, SleeveWorkType } from "./Work";
|
||||
@@ -9,29 +9,29 @@ import { calculateCompanyWorkStats } from "../../../Work/Formulas";
|
||||
import { scaleWorkStats, WorkStats } from "../../../Work/WorkStats";
|
||||
import { influenceStockThroughCompanyWork } from "../../../StockMarket/PlayerInfluencing";
|
||||
import { CompanyPositions } from "../../../Company/CompanyPositions";
|
||||
import { isMember } from "../../../utils/EnumHelper";
|
||||
import { invalidWork } from "../../../Work/InvalidWork";
|
||||
|
||||
export const isSleeveCompanyWork = (w: SleeveWorkClass | null): w is SleeveCompanyWork =>
|
||||
w !== null && w.type === SleeveWorkType.COMPANY;
|
||||
|
||||
export class SleeveCompanyWork extends SleeveWorkClass {
|
||||
type: SleeveWorkType.COMPANY = SleeveWorkType.COMPANY;
|
||||
companyName: string;
|
||||
companyName: CompanyName;
|
||||
|
||||
constructor(companyName?: string) {
|
||||
constructor(companyName = CompanyName.NoodleBar) {
|
||||
super();
|
||||
this.companyName = companyName ?? LocationName.NewTokyoNoodleBar;
|
||||
this.companyName = companyName;
|
||||
}
|
||||
|
||||
getCompany(): Company {
|
||||
const c = Companies[this.companyName];
|
||||
if (!c) throw new Error(`Company not found: '${this.companyName}'`);
|
||||
return c;
|
||||
return Companies[this.companyName];
|
||||
}
|
||||
|
||||
getGainRates(sleeve: Sleeve): WorkStats {
|
||||
getGainRates(sleeve: Sleeve, job: JobName): WorkStats {
|
||||
const company = this.getCompany();
|
||||
return scaleWorkStats(
|
||||
calculateCompanyWorkStats(sleeve, company, CompanyPositions[Player.jobs[company.name]], company.favor),
|
||||
calculateCompanyWorkStats(sleeve, company, CompanyPositions[job], company.favor),
|
||||
sleeve.shockBonus(),
|
||||
false,
|
||||
);
|
||||
@@ -39,7 +39,9 @@ export class SleeveCompanyWork extends SleeveWorkClass {
|
||||
|
||||
process(sleeve: Sleeve, cycles: number) {
|
||||
const company = this.getCompany();
|
||||
const gains = this.getGainRates(sleeve);
|
||||
const job = Player.jobs[this.companyName];
|
||||
if (!job) return sleeve.stopWork();
|
||||
const gains = this.getGainRates(sleeve, job);
|
||||
applySleeveGains(sleeve, gains, cycles);
|
||||
company.playerReputation += gains.reputation * cycles;
|
||||
influenceStockThroughCompanyWork(company, gains.reputation, cycles);
|
||||
@@ -59,7 +61,9 @@ export class SleeveCompanyWork extends SleeveWorkClass {
|
||||
|
||||
/** Initializes a CompanyWork object from a JSON save state. */
|
||||
static fromJSON(value: IReviverValue): SleeveCompanyWork {
|
||||
return Generic_fromJSON(SleeveCompanyWork, value.data);
|
||||
const work = Generic_fromJSON(SleeveCompanyWork, value.data);
|
||||
if (!isMember("CompanyName", work.companyName)) return invalidWork();
|
||||
return work;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import { TaskSelector } from "./TaskSelector";
|
||||
import { TravelModal } from "./TravelModal";
|
||||
import { findCrime } from "../../../Crime/CrimeHelpers";
|
||||
import { SleeveWorkType } from "../Work/Work";
|
||||
import { getEnumHelper } from "../../../utils/EnumHelper";
|
||||
|
||||
function getWorkDescription(sleeve: Sleeve, progress: number): string {
|
||||
const work = sleeve.currentWork;
|
||||
@@ -75,7 +76,8 @@ export function SleeveElem(props: SleeveElemProps): React.ReactElement {
|
||||
case "------":
|
||||
break;
|
||||
case "Work for Company":
|
||||
props.sleeve.workForCompany(abc[1]);
|
||||
if (getEnumHelper("CompanyName").isMember(abc[1])) props.sleeve.workForCompany(abc[1]);
|
||||
else console.error(`Invalid company name in setSleeveTask: ${abc[1]}`);
|
||||
break;
|
||||
case "Work for Faction":
|
||||
props.sleeve.workForFaction(abc[1], abc[2]);
|
||||
|
||||
@@ -2,6 +2,8 @@ import React from "react";
|
||||
|
||||
import { Typography, Table, TableBody, TableCell, TableRow } from "@mui/material";
|
||||
|
||||
import { Player } from "@player";
|
||||
|
||||
import { CONSTANTS } from "../../../Constants";
|
||||
|
||||
import {
|
||||
@@ -141,8 +143,10 @@ export function EarningsElement(props: IProps): React.ReactElement {
|
||||
];
|
||||
}
|
||||
|
||||
if (isSleeveCompanyWork(props.sleeve.currentWork)) {
|
||||
const rates = props.sleeve.currentWork.getGainRates(props.sleeve);
|
||||
companyWork: if (isSleeveCompanyWork(props.sleeve.currentWork)) {
|
||||
const job = Player.jobs[props.sleeve.currentWork.companyName];
|
||||
if (!job) break companyWork;
|
||||
const rates = props.sleeve.currentWork.getGainRates(props.sleeve, job);
|
||||
data = [
|
||||
[`Money:`, <MoneyRate key="money-rate" money={CYCLES_PER_SEC * rates.money} />],
|
||||
[`Hacking Exp:`, `${formatExp(CYCLES_PER_SEC * rates.hackExp)} / sec`],
|
||||
|
||||
Reference in New Issue
Block a user