mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-21 08:42:53 +02:00
TYPESAFETY: FactionName (#644)
This commit is contained in:
@@ -51,7 +51,7 @@ import {
|
||||
} from "../Corporation/Actions";
|
||||
import { CorpUnlocks } from "../Corporation/data/CorporationUnlocks";
|
||||
import { CorpUpgrades } from "../Corporation/data/CorporationUpgrades";
|
||||
import { CorpUnlockName, CorpUpgradeName, CorpEmployeeJob, CityName } from "@enums";
|
||||
import { CorpUnlockName, CorpUpgradeName, CorpEmployeeJob, CityName, FactionName } from "@enums";
|
||||
import { IndustriesData, IndustryResearchTrees } from "../Corporation/data/IndustryData";
|
||||
import * as corpConstants from "../Corporation/data/Constants";
|
||||
import { ResearchMap } from "../Corporation/ResearchMap";
|
||||
@@ -174,8 +174,7 @@ export function NetscriptCorporation(): InternalAPI<NSCorporation> {
|
||||
return division.researched.has(researchName);
|
||||
}
|
||||
|
||||
function bribe(factionName: string, amountCash: number): boolean {
|
||||
if (!player.factions.includes(factionName)) throw new Error("Invalid faction name");
|
||||
function bribe(factionName: FactionName, amountCash: number): boolean {
|
||||
if (isNaN(amountCash) || amountCash < 0)
|
||||
throw new Error("Invalid value for amount field! Must be numeric, greater than 0.");
|
||||
|
||||
@@ -825,7 +824,7 @@ export function NetscriptCorporation(): InternalAPI<NSCorporation> {
|
||||
},
|
||||
bribe: (ctx) => (_factionName, _amountCash) => {
|
||||
checkAccess(ctx);
|
||||
const factionName = helpers.string(ctx, "factionName", _factionName);
|
||||
const factionName = getEnumHelper("FactionName").nsGetMember(ctx, _factionName);
|
||||
const amountCash = helpers.number(ctx, "amountCash", _amountCash);
|
||||
return bribe(factionName, amountCash);
|
||||
},
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import type { Gang as IGang, EquipmentStats, GangOtherInfoObject } from "@nsdefs";
|
||||
import type { Gang } from "../Gang/Gang";
|
||||
import type { GangMember } from "../Gang/GangMember";
|
||||
import type { GangMemberTask } from "../Gang/GangMemberTask";
|
||||
import type { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
||||
|
||||
import { Player } from "@player";
|
||||
import { FactionName } from "@enums";
|
||||
import { GangConstants } from "../Gang/data/Constants";
|
||||
import { Player } from "@player";
|
||||
import { Gang } from "../Gang/Gang";
|
||||
import { AllGangs } from "../Gang/AllGangs";
|
||||
import { GangMemberTasks } from "../Gang/GangMemberTasks";
|
||||
import { GangMemberUpgrades } from "../Gang/GangMemberUpgrades";
|
||||
import { GangMember } from "../Gang/GangMember";
|
||||
import { GangMemberTask } from "../Gang/GangMemberTask";
|
||||
import { helpers } from "../Netscript/NetscriptHelpers";
|
||||
|
||||
import { Gang as IGang, EquipmentStats, GangOtherInfoObject } from "@nsdefs";
|
||||
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
||||
import { getEnumHelper } from "../utils/EnumHelper";
|
||||
|
||||
export function NetscriptGang(): InternalAPI<IGang> {
|
||||
/** Functions as an API check and also returns the gang object */
|
||||
@@ -36,9 +37,7 @@ export function NetscriptGang(): InternalAPI<IGang> {
|
||||
|
||||
return {
|
||||
createGang: (ctx) => (_faction) => {
|
||||
const faction = helpers.string(ctx, "faction", _faction);
|
||||
// this list is copied from Faction/ui/Root.tsx
|
||||
|
||||
const faction = getEnumHelper("FactionName").nsGetMember(ctx, _faction);
|
||||
if (!Player.canAccessGang() || !GangConstants.Names.includes(faction)) return false;
|
||||
if (Player.gang) return false;
|
||||
if (!Player.factions.includes(faction)) return false;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { Singularity as ISingularity } from "@nsdefs";
|
||||
import type { Company } from "../Company/Company";
|
||||
import type { Faction } from "../Faction/Faction";
|
||||
|
||||
import { Player } from "@player";
|
||||
import {
|
||||
@@ -33,7 +32,7 @@ import { formatMoney, formatRam, formatReputation } from "../ui/formatNumber";
|
||||
import { currentNodeMults } from "../BitNode/BitNodeMultipliers";
|
||||
import { Companies } from "../Company/Companies";
|
||||
import { companiesMetadata } from "../Company/data/CompaniesMetadata";
|
||||
import { Factions, factionExists } from "../Faction/Factions";
|
||||
import { Factions } from "../Faction/Factions";
|
||||
import { helpers } from "../Netscript/NetscriptHelpers";
|
||||
import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions";
|
||||
import { getServerOnNetwork } from "../Server/ServerHelpers";
|
||||
@@ -59,14 +58,6 @@ import { ScriptFilePath, resolveScriptFilePath } from "../Paths/ScriptFilePath";
|
||||
import { root } from "../Paths/Directory";
|
||||
|
||||
export function NetscriptSingularity(): InternalAPI<ISingularity> {
|
||||
const getFaction = function (ctx: NetscriptContext, name: string): Faction {
|
||||
if (!factionExists(name)) {
|
||||
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid faction name: '${name}`);
|
||||
}
|
||||
|
||||
return Factions[name];
|
||||
};
|
||||
|
||||
const getCompany = function (ctx: NetscriptContext, name: string): Company {
|
||||
const company = Companies[name];
|
||||
if (!company) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid company name: '${name}'`);
|
||||
@@ -112,9 +103,8 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
|
||||
},
|
||||
getAugmentationsFromFaction: (ctx) => (_facName) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = helpers.string(ctx, "facName", _facName);
|
||||
const faction = getFaction(ctx, facName);
|
||||
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
const faction = Factions[facName];
|
||||
return getFactionAugmentationsFiltered(faction);
|
||||
},
|
||||
getAugmentationPrereq: (ctx) => (_augName) => {
|
||||
@@ -149,19 +139,19 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
|
||||
},
|
||||
purchaseAugmentation: (ctx) => (_facName, _augName) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = helpers.string(ctx, "facName", _facName);
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
const augName = getEnumHelper("AugmentationName").nsGetMember(ctx, _augName);
|
||||
const fac = getFaction(ctx, facName);
|
||||
const fac = Factions[facName];
|
||||
const aug = Augmentations[augName];
|
||||
|
||||
const augs = getFactionAugmentationsFiltered(fac);
|
||||
const factionAugs = getFactionAugmentationsFiltered(fac);
|
||||
|
||||
if (!Player.factions.includes(fac.name)) {
|
||||
if (!Player.factions.includes(facName)) {
|
||||
helpers.log(ctx, () => `You can't purchase augmentations from '${facName}' because you aren't a member`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!augs.includes(augName)) {
|
||||
if (!factionAugs.includes(augName)) {
|
||||
helpers.log(ctx, () => `Faction '${facName}' does not have the '${augName}' augmentation.`);
|
||||
return false;
|
||||
}
|
||||
@@ -867,8 +857,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
|
||||
},
|
||||
joinFaction: (ctx) => (_facName) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = helpers.string(ctx, "facName", _facName);
|
||||
getFaction(ctx, facName);
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
|
||||
if (!Player.factionInvitations.includes(facName)) {
|
||||
helpers.log(ctx, () => `You have not been invited by faction '${facName}'`);
|
||||
@@ -892,10 +881,10 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
|
||||
(ctx) =>
|
||||
(_facName, _type, _focus = true) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = helpers.string(ctx, "facName", _facName);
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
const type = helpers.string(ctx, "type", _type);
|
||||
const focus = !!_focus;
|
||||
const faction = getFaction(ctx, facName);
|
||||
const faction = Factions[facName];
|
||||
|
||||
// if the player is in a gang and the target faction is any of the gang faction, fail
|
||||
if (Player.gang && faction.name === Player.getGangFaction().name) {
|
||||
@@ -987,27 +976,27 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
|
||||
},
|
||||
getFactionRep: (ctx) => (_facName) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = helpers.string(ctx, "facName", _facName);
|
||||
const faction = getFaction(ctx, facName);
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
const faction = Factions[facName];
|
||||
return faction.playerReputation;
|
||||
},
|
||||
getFactionFavor: (ctx) => (_facName) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = helpers.string(ctx, "facName", _facName);
|
||||
const faction = getFaction(ctx, facName);
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
const faction = Factions[facName];
|
||||
return faction.favor;
|
||||
},
|
||||
getFactionFavorGain: (ctx) => (_facName) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = helpers.string(ctx, "facName", _facName);
|
||||
const faction = getFaction(ctx, facName);
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
const faction = Factions[facName];
|
||||
return faction.getFavorGain();
|
||||
},
|
||||
donateToFaction: (ctx) => (_facName, _amt) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = helpers.string(ctx, "facName", _facName);
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
const amt = helpers.number(ctx, "amt", _amt);
|
||||
const faction = getFaction(ctx, facName);
|
||||
const faction = Factions[facName];
|
||||
if (!Player.factions.includes(faction.name)) {
|
||||
helpers.log(ctx, () => `You can't donate to '${facName}' because you aren't a member`);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user