MISC: A bunch of enums stuff. (#212)

* Some game enums moved to utils/enums. Others can eventually be moved there as well.
* findEnumMember function for performing fuzzy matching of player input with enum members, without needing separate fuzzy functions for every enum.
* Also used findEnumMember for safely loading save games (allows case changes in enum values)
* Changed capitalization on some enums.
* BREAKING: removed classGains work formulas function
* Split ClassType enum into UniversityClassType and GymType.
* Added universityGains and gymGains work formulas functions
* Provided the new split enums to the player on ns.enums.
This commit is contained in:
Snarling
2022-11-20 08:37:11 -05:00
committed by GitHub
parent 629d610532
commit 88d51e9a7e
34 changed files with 346 additions and 432 deletions
+1 -1
View File
@@ -58,7 +58,7 @@ import { Factions } from "../Faction/Factions";
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { assertEnumMember, helpers } from "../Netscript/NetscriptHelpers";
import { checkEnum } from "../utils/helpers/checkEnum";
import { checkEnum } from "../utils/helpers/enum";
import { CityName } from "../Locations/data/CityNames";
export function NetscriptCorporation(): InternalAPI<NSCorporation> {
+26 -18
View File
@@ -40,19 +40,18 @@ import { repFromDonation } from "../Faction/formulas/donation";
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { helpers } from "../Netscript/NetscriptHelpers";
import { calculateCrimeWorkStats } from "../Work/Formulas";
import { Crimes } from "../Crime/Crimes";
import { calculateCompanyWorkStats } from "../Work/Formulas";
import { Companies } from "../Company/Companies";
import { calculateClassEarnings } from "../Work/Formulas";
import { ClassType } from "../Work/ClassWork";
import { LocationName } from "../Locations/data/LocationNames";
import { calculateFactionExp, calculateFactionRep } from "../Work/Formulas";
import { FactionWorkType } from "../Work/data/FactionWorkType";
import { FactionWorkType, GymType, UniversityClassType } from "../utils/enums";
import { defaultMultipliers } from "../PersonObjects/Multipliers";
import { checkEnum } from "../utils/helpers/checkEnum";
import { CompanyPosNames, CrimeType } from "../utils/WorkType";
import { checkEnum, findEnumMember } from "../utils/helpers/enum";
import { CompanyPosNames } from "../utils/enums";
import { CompanyPositions } from "../Company/CompanyPositions";
import { findCrime } from "../Crime/CrimeHelpers";
export function NetscriptFormulas(): InternalAPI<IFormulas> {
const checkFormulasAccess = function (ctx: NetscriptContext): void {
@@ -356,22 +355,31 @@ export function NetscriptFormulas(): InternalAPI<IFormulas> {
crimeSuccessChance: (ctx) => (_person, _crimeType) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const crimeType = helpers.string(ctx, "crimeType", _crimeType);
if (!checkEnum(CrimeType, crimeType)) throw new Error(`Invalid crime type: ${crimeType}`);
return Crimes[crimeType].successRate(person);
const crime = findCrime(helpers.string(ctx, "crimeType", _crimeType));
if (!crime) throw new Error(`Invalid crime type: ${_crimeType}`);
return crime.successRate(person);
},
crimeGains: (ctx) => (_person, _crimeType) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const crimeType = helpers.string(ctx, "crimeType", _crimeType);
if (!checkEnum(CrimeType, crimeType)) throw new Error(`Invalid crime type: ${crimeType}`);
return calculateCrimeWorkStats(person, Crimes[crimeType]);
const crime = findCrime(helpers.string(ctx, "crimeType", _crimeType));
if (!crime) throw new Error(`Invalid crime type: ${_crimeType}`);
return calculateCrimeWorkStats(person, crime);
},
classGains: (ctx) => (_person, _classType, _locationName) => {
gymGains: (ctx) => (_person, _classType, _locationName) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const classType = helpers.string(ctx, "classType", _classType);
if (!checkEnum(ClassType, classType)) throw new Error(`Invalid class type: ${classType}`);
const classType = findEnumMember(GymType, helpers.string(ctx, "classType", _classType));
if (!classType) throw new Error(`Invalid gym training type: ${_classType}`);
const locationName = helpers.string(ctx, "locationName", _locationName);
if (!checkEnum(LocationName, locationName)) throw new Error(`Invalid location name: ${locationName}`);
return calculateClassEarnings(person, classType, locationName);
},
universityGains: (ctx) => (_person, _classType, _locationName) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const classType = findEnumMember(UniversityClassType, helpers.string(ctx, "classType", _classType));
if (!classType) throw new Error(`Invalid university class type: ${_classType}`);
const locationName = helpers.string(ctx, "locationName", _locationName);
if (!checkEnum(LocationName, locationName)) throw new Error(`Invalid location name: ${locationName}`);
return calculateClassEarnings(person, classType, locationName);
@@ -379,8 +387,8 @@ export function NetscriptFormulas(): InternalAPI<IFormulas> {
factionGains: (ctx) => (_player, _workType, _favor) => {
checkFormulasAccess(ctx);
const player = helpers.person(ctx, _player);
const workType = helpers.string(ctx, "_workType", _workType);
if (!checkEnum(FactionWorkType, workType)) throw new Error(`Invalid faction work type: ${workType}`);
const workType = findEnumMember(FactionWorkType, helpers.string(ctx, "_workType", _workType));
if (!workType) throw new Error(`Invalid faction work type: ${_workType}`);
const favor = helpers.number(ctx, "favor", _favor);
const exp = calculateFactionExp(player, workType);
const rep = calculateFactionRep(player, workType, favor);
@@ -390,8 +398,8 @@ export function NetscriptFormulas(): InternalAPI<IFormulas> {
companyGains: (ctx) => (_person, _companyName, _positionName, _favor) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const positionName = helpers.string(ctx, "_positionName", _positionName);
if (!checkEnum(CompanyPosNames, positionName)) throw new Error(`Invalid company position: ${positionName}`);
const positionName = findEnumMember(CompanyPosNames, helpers.string(ctx, "_positionName", _positionName));
if (!positionName) throw new Error(`Invalid company position: ${_positionName}`);
const position = CompanyPositions[positionName];
const companyName = helpers.string(ctx, "_companyName", _companyName);
const company = Object.values(Companies).find((c) => c.name === companyName);
+1 -1
View File
@@ -10,7 +10,7 @@ import {
import { FactionNames } from "../Faction/data/FactionNames";
import { Factions } from "../Faction/Factions";
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { checkEnum } from "../utils/helpers/checkEnum";
import { checkEnum } from "../utils/helpers/enum";
import { LocationName } from "../Locations/data/LocationNames";
import { helpers } from "../Netscript/NetscriptHelpers";
+21 -64
View File
@@ -41,14 +41,15 @@ import { InternalAPI, NetscriptContext } from "src/Netscript/APIWrapper";
import { BlackOperationNames } from "../Bladeburner/data/BlackOperationNames";
import { enterBitNode } from "../RedPill";
import { FactionNames } from "../Faction/data/FactionNames";
import { ClassWork, ClassType } from "../Work/ClassWork";
import { ClassWork } from "../Work/ClassWork";
import { CreateProgramWork, isCreateProgramWork } from "../Work/CreateProgramWork";
import { FactionWork } from "../Work/FactionWork";
import { FactionWorkType } from "../Work/data/FactionWorkType";
import { FactionWorkType, GymType, UniversityClassType } from "../utils/enums";
import { CompanyWork } from "../Work/CompanyWork";
import { canGetBonus, onExport } from "../ExportBonus";
import { saveObject } from "../SaveObject";
import { calculateCrimeWorkStats } from "../Work/Formulas";
import { findEnumMember } from "../utils/helpers/enum";
export function NetscriptSingularity(): InternalAPI<ISingularity> {
const getAugmentation = function (ctx: NetscriptContext, name: string): Augmentation {
@@ -253,7 +254,11 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
(_universityName, _className, _focus = true) => {
helpers.checkSingularityAccess(ctx);
const universityName = helpers.string(ctx, "universityName", _universityName);
const className = helpers.string(ctx, "className", _className);
const classType = findEnumMember(UniversityClassType, helpers.string(ctx, "className", _className));
if (!classType) {
helpers.log(ctx, () => `Invalid class name: ${_className}.`);
return false;
}
const focus = !!_focus;
const wasFocusing = Player.focus;
@@ -293,33 +298,9 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
return false;
}
let task: ClassType;
switch (className.toLowerCase()) {
case "Study Computer Science".toLowerCase():
task = ClassType.StudyComputerScience;
break;
case "Data Structures".toLowerCase():
task = ClassType.DataStructures;
break;
case "Networks".toLowerCase():
task = ClassType.Networks;
break;
case "Algorithms".toLowerCase():
task = ClassType.Algorithms;
break;
case "Management".toLowerCase():
task = ClassType.Management;
break;
case "Leadership".toLowerCase():
task = ClassType.Leadership;
break;
default:
helpers.log(ctx, () => `Invalid class name: ${className}.`);
return false;
}
Player.startWork(
new ClassWork({
classType: task,
classType,
location: Player.location,
singularity: true,
}),
@@ -331,7 +312,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
Player.stopFocusing();
Router.toTerminal();
}
helpers.log(ctx, () => `Started ${task} at ${universityName}`);
helpers.log(ctx, () => `Started ${classType} at ${universityName}`);
return true;
},
@@ -340,7 +321,11 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
(_gymName, _stat, _focus = true) => {
helpers.checkSingularityAccess(ctx);
const gymName = helpers.string(ctx, "gymName", _gymName);
const stat = helpers.string(ctx, "stat", _stat);
const classType = findEnumMember(GymType, helpers.string(ctx, "stat", _stat));
if (!classType) {
helpers.log(ctx, () => `Invalid stat: ${_stat}.`);
return false;
}
const focus = !!_focus;
const wasFocusing = Player.focus;
@@ -405,35 +390,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
return false;
}
switch (stat.toLowerCase()) {
case "strength".toLowerCase():
case "str".toLowerCase():
Player.startWork(
new ClassWork({ classType: ClassType.GymStrength, location: Player.location, singularity: true }),
);
break;
case "defense".toLowerCase():
case "def".toLowerCase():
Player.startWork(
new ClassWork({ classType: ClassType.GymDefense, location: Player.location, singularity: true }),
);
break;
case "dexterity".toLowerCase():
case "dex".toLowerCase():
Player.startWork(
new ClassWork({ classType: ClassType.GymDexterity, location: Player.location, singularity: true }),
);
break;
case "agility".toLowerCase():
case "agi".toLowerCase():
Player.startWork(
new ClassWork({ classType: ClassType.GymAgility, location: Player.location, singularity: true }),
);
break;
default:
helpers.log(ctx, () => `Invalid stat: ${stat}.`);
return false;
}
Player.startWork(new ClassWork({ classType, location: Player.location, singularity: true }));
if (focus) {
Player.startFocusing();
Router.toWork();
@@ -441,7 +398,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
Player.stopFocusing();
Router.toTerminal();
}
helpers.log(ctx, () => `Started training ${stat} at ${gymName}`);
helpers.log(ctx, () => `Started training ${classType} at ${gymName}`);
return true;
},
@@ -926,7 +883,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
Player.startWork(
new FactionWork({
singularity: true,
factionWorkType: FactionWorkType.HACKING,
factionWorkType: FactionWorkType.hacking,
faction: faction.name,
}),
);
@@ -949,7 +906,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
Player.startWork(
new FactionWork({
singularity: true,
factionWorkType: FactionWorkType.FIELD,
factionWorkType: FactionWorkType.field,
faction: faction.name,
}),
);
@@ -972,7 +929,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
Player.startWork(
new FactionWork({
singularity: true,
factionWorkType: FactionWorkType.SECURITY,
factionWorkType: FactionWorkType.security,
faction: faction.name,
}),
);
@@ -1118,7 +1075,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
const crime = findCrime(crimeType);
if (crime == null) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid crime: '${crimeType}'`);
helpers.log(ctx, () => `Attempting to commit ${crime.name}...`);
helpers.log(ctx, () => `Attempting to commit ${crime.type}...`);
const crimeTime = crime.commit(1, ctx.workerScript);
if (focus) {
Player.startFocusing();
+1 -1
View File
@@ -5,7 +5,7 @@ import { findCrime } from "../Crime/CrimeHelpers";
import { Augmentation } from "../Augmentation/Augmentation";
import { sleeve } from "../ScriptEditor/NetscriptDefinitions";
import { checkEnum } from "../utils/helpers/checkEnum";
import { checkEnum } from "../utils/helpers/enum";
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { isSleeveBladeburnerWork } from "../PersonObjects/Sleeve/Work/SleeveBladeburnerWork";
import { isSleeveFactionWork } from "../PersonObjects/Sleeve/Work/SleeveFactionWork";