TYPESAFETY: CompanyName (#650)

This commit is contained in:
Snarling
2023-07-11 09:23:17 -04:00
committed by GitHub
parent e4d3a9020e
commit e2655793f4
40 changed files with 1548 additions and 1516 deletions
+12 -7
View File
@@ -17,7 +17,7 @@ import * as serverMethods from "./PlayerObjectServerMethods";
import * as workMethods from "./PlayerObjectWorkMethods";
import { setPlayer } from "../../Player";
import { FactionName, LocationName } from "@enums";
import { CompanyName, FactionName, JobName, LocationName } from "@enums";
import { HashManager } from "../../Hacknet/HashManager";
import { MoneySourceTracker } from "../../utils/MoneySourceTracker";
import { constructorsForReviver, Generic_toJSON, Generic_fromJSON, IReviverValue } from "../../utils/JSONReviver";
@@ -26,7 +26,8 @@ import { cyrb53 } from "../../utils/StringHelperFunctions";
import { getRandomInt } from "../../utils/helpers/getRandomInt";
import { CONSTANTS } from "../../Constants";
import { Person } from "../Person";
import { getEnumHelper } from "../../utils/EnumHelper";
import { isMember } from "../../utils/EnumHelper";
import { PartialRecord } from "../../Types/Record";
export class PlayerObject extends Person implements IPlayer {
// Player-specific properties
@@ -43,7 +44,7 @@ export class PlayerObject extends Person implements IPlayer {
hashManager = new HashManager();
hasTixApiAccess = false;
hasWseAccount = false;
jobs: Record<string, string> = {};
jobs: PartialRecord<CompanyName, JobName> = {};
karma = 0;
numPeopleKilled = 0;
location = LocationName.TravelAgency;
@@ -172,11 +173,9 @@ export class PlayerObject extends Person implements IPlayer {
player.hp = { current: player.hp?.current ?? 10, max: player.hp?.max ?? 10 };
player.money ??= 0;
// Just remove from the save file any augs that have invalid name
player.augmentations = player.augmentations.filter((ownedAug) =>
getEnumHelper("AugmentationName").isMember(ownedAug.name),
);
player.augmentations = player.augmentations.filter((ownedAug) => isMember("AugmentationName", ownedAug.name));
player.queuedAugmentations = player.queuedAugmentations.filter((ownedAug) =>
getEnumHelper("AugmentationName").isMember(ownedAug.name),
isMember("AugmentationName", ownedAug.name),
);
player.updateSkillLevels();
// Converstion code for Player.sourceFiles is here instead of normal save conversion area because it needs
@@ -186,6 +185,12 @@ export class PlayerObject extends Person implements IPlayer {
type OldSourceFiles = { n: number; lvl: number }[];
player.sourceFiles = new JSONMap((player.sourceFiles as OldSourceFiles).map(({ n, lvl }) => [n, lvl]));
}
// Remove any invalid jobs
for (const [loadedCompanyName, loadedJobName] of Object.entries(player.jobs)) {
if (!isMember("CompanyName", loadedCompanyName) || !isMember("JobName", loadedJobName)) {
delete player.jobs[loadedCompanyName as CompanyName];
}
}
return player;
}
}
@@ -1,4 +1,13 @@
import { AugmentationName, CityName, CompletedProgramName, FactionName, LocationName, ToastVariant } from "@enums";
import {
AugmentationName,
CityName,
CompanyName,
CompletedProgramName,
FactionName,
JobName,
LocationName,
ToastVariant,
} from "@enums";
import type { PlayerObject } from "./PlayerObject";
import type { ProgramFilePath } from "../../Paths/ProgramFilePath";
@@ -13,7 +22,6 @@ import { getNextCompanyPositionHelper } from "../../Company/GetNextCompanyPositi
import { getJobRequirementText } from "../../Company/GetJobRequirementText";
import { CompanyPositions } from "../../Company/CompanyPositions";
import { CompanyPosition } from "../../Company/CompanyPosition";
import * as posNames from "../../Company/data/JobTracks";
import { CONSTANTS } from "../../Constants";
import { Exploit } from "../../Exploits/Exploit";
import { Faction } from "../../Faction/Faction";
@@ -44,6 +52,7 @@ import { achievements } from "../../Achievements/Achievements";
import { isCompanyWork } from "../../Work/CompanyWork";
import { serverMetadata } from "../../Server/data/servers";
import { getEnumHelper, isMember } from "../../utils/EnumHelper";
export function init(this: PlayerObject): void {
/* Initialize Player's home computer */
@@ -262,12 +271,9 @@ export function hospitalize(this: PlayerObject): number {
//The 'sing' argument designates whether or not this is being called from
//the applyToCompany() Netscript Singularity function
export function applyForJob(this: PlayerObject, entryPosType: CompanyPosition, sing = false): boolean {
const company = Companies[this.location]; //Company being applied to
if (!company) {
console.error(`Could not find company that matches the location: ${this.location}. Player.applyToCompany() failed`);
return false;
}
const companyName = getEnumHelper("CompanyName").getMember(this.location);
if (!companyName) return false;
const company = Companies[companyName]; //Company being applied to
let pos = entryPosType;
if (!this.isQualified(company, pos)) {
@@ -347,7 +353,7 @@ export function getNextCompanyPosition(
return entryPosType;
}
export function quitJob(this: PlayerObject, company: string): void {
export function quitJob(this: PlayerObject, company: CompanyName): void {
if (isCompanyWork(this.currentWork) && this.currentWork.companyName === company) {
this.finishWork(true);
}
@@ -370,21 +376,23 @@ export function hasJob(this: PlayerObject): boolean {
}
export function applyForSoftwareJob(this: PlayerObject, sing = false): boolean {
return this.applyForJob(CompanyPositions[posNames.SoftwareCompanyPositions[0]], sing);
return this.applyForJob(CompanyPositions[JobName.software0], sing);
}
export function applyForSoftwareConsultantJob(this: PlayerObject, sing = false): boolean {
return this.applyForJob(CompanyPositions[posNames.SoftwareConsultantCompanyPositions[0]], sing);
return this.applyForJob(CompanyPositions[JobName.softwareConsult0], sing);
}
export function applyForItJob(this: PlayerObject, sing = false): boolean {
return this.applyForJob(CompanyPositions[posNames.ITCompanyPositions[0]], sing);
return this.applyForJob(CompanyPositions[JobName.IT0], sing);
}
export function applyForSecurityEngineerJob(this: PlayerObject, sing = false): boolean {
const company = Companies[this.location]; //Company being applied to
if (this.isQualified(company, CompanyPositions[posNames.SecurityEngineerCompanyPositions[0]])) {
return this.applyForJob(CompanyPositions[posNames.SecurityEngineerCompanyPositions[0]], sing);
const companyName = getEnumHelper("CompanyName").getMember(this.location);
if (!companyName) return false;
const company = Companies[companyName];
if (this.isQualified(company, CompanyPositions[JobName.securityEng])) {
return this.applyForJob(CompanyPositions[JobName.securityEng], sing);
} else {
if (!sing) {
dialogBoxCreate("Unfortunately, you do not qualify for this position");
@@ -394,9 +402,11 @@ export function applyForSecurityEngineerJob(this: PlayerObject, sing = false): b
}
export function applyForNetworkEngineerJob(this: PlayerObject, sing = false): boolean {
const company = Companies[this.location]; //Company being applied to
if (this.isQualified(company, CompanyPositions[posNames.NetworkEngineerCompanyPositions[0]])) {
const pos = CompanyPositions[posNames.NetworkEngineerCompanyPositions[0]];
const companyName = getEnumHelper("CompanyName").getMember(this.location);
if (!companyName) return false;
const company = Companies[companyName];
if (this.isQualified(company, CompanyPositions[JobName.networkEng0])) {
const pos = CompanyPositions[JobName.networkEng0];
return this.applyForJob(pos, sing);
} else {
if (!sing) {
@@ -407,23 +417,25 @@ export function applyForNetworkEngineerJob(this: PlayerObject, sing = false): bo
}
export function applyForBusinessJob(this: PlayerObject, sing = false): boolean {
return this.applyForJob(CompanyPositions[posNames.BusinessCompanyPositions[0]], sing);
return this.applyForJob(CompanyPositions[JobName.business0], sing);
}
export function applyForBusinessConsultantJob(this: PlayerObject, sing = false): boolean {
return this.applyForJob(CompanyPositions[posNames.BusinessConsultantCompanyPositions[0]], sing);
return this.applyForJob(CompanyPositions[JobName.businessConsult0], sing);
}
export function applyForSecurityJob(this: PlayerObject, sing = false): boolean {
// TODO Police Jobs
// Indexing starts at 2 because 0 is for police officer
return this.applyForJob(CompanyPositions[posNames.SecurityCompanyPositions[2]], sing);
return this.applyForJob(CompanyPositions[JobName.security0], sing);
}
export function applyForAgentJob(this: PlayerObject, sing = false): boolean {
const company = Companies[this.location]; //Company being applied to
if (this.isQualified(company, CompanyPositions[posNames.AgentCompanyPositions[0]])) {
const pos = CompanyPositions[posNames.AgentCompanyPositions[0]];
const companyName = getEnumHelper("CompanyName").getMember(this.location);
if (!companyName) return false;
const company = Companies[companyName];
if (this.isQualified(company, CompanyPositions[JobName.agent0])) {
const pos = CompanyPositions[JobName.agent0];
return this.applyForJob(pos, sing);
} else {
if (!sing) {
@@ -434,8 +446,10 @@ export function applyForAgentJob(this: PlayerObject, sing = false): boolean {
}
export function applyForEmployeeJob(this: PlayerObject, sing = false): boolean {
const company = Companies[this.location]; //Company being applied to
const position = posNames.MiscCompanyPositions[1];
const companyName = getEnumHelper("CompanyName").getMember(this.location);
if (!companyName) return false;
const company = Companies[companyName];
const position = JobName.employee;
// Check if this company has the position
if (!company.hasPosition(position)) {
return false;
@@ -458,8 +472,10 @@ export function applyForEmployeeJob(this: PlayerObject, sing = false): boolean {
}
export function applyForPartTimeEmployeeJob(this: PlayerObject, sing = false): boolean {
const company = Companies[this.location]; //Company being applied to
const position = posNames.PartTimeCompanyPositions[1];
const companyName = getEnumHelper("CompanyName").getMember(this.location);
if (!companyName) return false;
const company = Companies[companyName];
const position = JobName.employeePT;
// Check if this company has the position
if (!company.hasPosition(position)) {
return false;
@@ -481,8 +497,10 @@ export function applyForPartTimeEmployeeJob(this: PlayerObject, sing = false): b
}
export function applyForWaiterJob(this: PlayerObject, sing = false): boolean {
const company = Companies[this.location]; //Company being applied to
const position = posNames.MiscCompanyPositions[0];
const companyName = getEnumHelper("CompanyName").getMember(this.location);
if (!companyName) return false;
const company = Companies[companyName];
const position = JobName.waiter;
// Check if this company has the position
if (!company.hasPosition(position)) {
return false;
@@ -502,8 +520,10 @@ export function applyForWaiterJob(this: PlayerObject, sing = false): boolean {
}
export function applyForPartTimeWaiterJob(this: PlayerObject, sing = false): boolean {
const company = Companies[this.location]; //Company being applied to
const position = posNames.PartTimeCompanyPositions[0];
const companyName = getEnumHelper("CompanyName").getMember(this.location);
if (!companyName) return false;
const company = Companies[companyName];
const position = JobName.waiterPT;
// Check if this company has the position
if (!company.hasPosition(position)) {
return false;
@@ -594,20 +614,16 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
const allPositions = Object.values(this.jobs);
// Given a company name, safely returns the reputation (returns 0 if invalid company is specified)
function getCompanyRep(companyName: string): number {
function getCompanyRep(companyName: CompanyName): number {
const company = Companies[companyName];
if (company == null) {
return 0;
} else {
return company.playerReputation;
}
return company.playerReputation;
}
// Helper function that returns a boolean indicating whether the Player meets
// the requirements for the specified company. There are two requirements:
// 1. High enough reputation
// 2. Player is employed at the company
function checkMegacorpRequirements(companyName: string): boolean {
function checkMegacorpRequirements(companyName: CompanyName): boolean {
const serverMeta = serverMetadata.find((s) => s.specialName === companyName);
const server = GetServer(serverMeta ? serverMeta.hostname : "");
const bonus = (server as Server).backdoorInstalled ? -100e3 : 0;
@@ -673,7 +689,7 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!ecorpFac.isBanned &&
!ecorpFac.isMember &&
!ecorpFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.AevumECorp)
checkMegacorpRequirements(CompanyName.ECorp)
) {
invitedFactions.push(ecorpFac);
}
@@ -684,7 +700,7 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!megacorpFac.isBanned &&
!megacorpFac.isMember &&
!megacorpFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.Sector12MegaCorp)
checkMegacorpRequirements(CompanyName.MegaCorp)
) {
invitedFactions.push(megacorpFac);
}
@@ -695,7 +711,7 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!bachmanandassociatesFac.isBanned &&
!bachmanandassociatesFac.isMember &&
!bachmanandassociatesFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.AevumBachmanAndAssociates)
checkMegacorpRequirements(CompanyName.BachmanAndAssociates)
) {
invitedFactions.push(bachmanandassociatesFac);
}
@@ -706,19 +722,14 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!bladeindustriesFac.isBanned &&
!bladeindustriesFac.isMember &&
!bladeindustriesFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.Sector12BladeIndustries)
checkMegacorpRequirements(CompanyName.BladeIndustries)
) {
invitedFactions.push(bladeindustriesFac);
}
//NWO
const nwoFac = Factions[FactionName.NWO];
if (
!nwoFac.isBanned &&
!nwoFac.isMember &&
!nwoFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.VolhavenNWO)
) {
if (!nwoFac.isBanned && !nwoFac.isMember && !nwoFac.alreadyInvited && checkMegacorpRequirements(CompanyName.NWO)) {
invitedFactions.push(nwoFac);
}
@@ -728,7 +739,7 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!clarkeincorporatedFac.isBanned &&
!clarkeincorporatedFac.isMember &&
!clarkeincorporatedFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.AevumClarkeIncorporated)
checkMegacorpRequirements(CompanyName.ClarkeIncorporated)
) {
invitedFactions.push(clarkeincorporatedFac);
}
@@ -739,7 +750,7 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!omnitekincorporatedFac.isBanned &&
!omnitekincorporatedFac.isMember &&
!omnitekincorporatedFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.VolhavenOmniTekIncorporated)
checkMegacorpRequirements(CompanyName.OmniTekIncorporated)
) {
invitedFactions.push(omnitekincorporatedFac);
}
@@ -750,7 +761,7 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!foursigmaFac.isBanned &&
!foursigmaFac.isMember &&
!foursigmaFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.Sector12FourSigma)
checkMegacorpRequirements(CompanyName.FourSigma)
) {
invitedFactions.push(foursigmaFac);
}
@@ -761,7 +772,7 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!kuaigonginternationalFac.isBanned &&
!kuaigonginternationalFac.isMember &&
!kuaigonginternationalFac.alreadyInvited &&
checkMegacorpRequirements(LocationName.ChongqingKuaiGongInternational)
checkMegacorpRequirements(CompanyName.KuaiGongInternational)
) {
invitedFactions.push(kuaigonginternationalFac);
}
@@ -778,7 +789,7 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!fulcrumsecrettechonologiesFac.isMember &&
!fulcrumsecrettechonologiesFac.alreadyInvited &&
fulcrumSecretServer.backdoorInstalled &&
checkMegacorpRequirements(LocationName.AevumFulcrumTechnologies)
checkMegacorpRequirements(CompanyName.FulcrumTechnologies)
) {
invitedFactions.push(fulcrumsecrettechonologiesFac);
}
@@ -966,9 +977,9 @@ export function checkForFactionInvitations(this: PlayerObject): Faction[] {
!silhouetteFac.isBanned &&
!silhouetteFac.isMember &&
!silhouetteFac.alreadyInvited &&
(allPositions.includes("Chief Technology Officer") ||
allPositions.includes("Chief Financial Officer") ||
allPositions.includes("Chief Executive Officer")) &&
(allPositions.includes(JobName.software7) || // CTO
allPositions.includes(JobName.business4) || // CFO
allPositions.includes(JobName.business5)) && // CEO
this.money >= 15000000 &&
this.karma <= -22
) {
@@ -1136,8 +1147,7 @@ export function gainCodingContractReward(
return `Gained ${gainPerFaction} reputation for each of the following factions: ${factions.join(", ")}`;
}
case CodingContractRewardType.CompanyReputation: {
if (!Companies[reward.name]) {
//If no/invalid company was designated, just give rewards to all factions
if (!isMember("CompanyName", reward.name)) {
return this.gainCodingContractReward({ type: CodingContractRewardType.FactionReputationAll });
}
const repGain = CONSTANTS.CodingContractBaseCompanyRepGain * difficulty;
+13 -15
View File
@@ -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;
}
}
+3 -1
View File
@@ -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]);
+6 -2
View File
@@ -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`],