Merge pull request #3280 from nickofolas/improvement/filter-helper-func

[Improvement] Implement helper function for filtering Faction Augmentations
This commit is contained in:
hydroflame 2022-03-30 20:28:31 -04:00 committed by GitHub
commit ce944bfccb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 95 deletions

@ -8,6 +8,7 @@ import { CONSTANTS } from "../Constants";
import { Faction } from "./Faction";
import { Factions } from "./Factions";
import { Player } from "../Player";
import { IPlayer } from "../PersonObjects/IPlayer";
import { Settings } from "../Settings/Settings";
import {
getHackingWorkRepGain,
@ -32,9 +33,8 @@ export function joinFaction(faction: Faction): void {
if (faction.isMember) return;
faction.isMember = true;
Player.factions.push(faction.name);
const allFactions = Object.values(FactionNames).map(faction => faction as string)
Player.factions.sort((a, b) =>
allFactions.indexOf(a) - allFactions.indexOf(b));
const allFactions = Object.values(FactionNames).map((faction) => faction as string);
Player.factions.sort((a, b) => allFactions.indexOf(a) - allFactions.indexOf(b));
const factionInfo = faction.getInfo();
//Determine what factions you are banned from now that you have joined this faction
@ -193,3 +193,30 @@ export function processPassiveFactionRepGain(numCycles: number): void {
faction.playerReputation += rate * numCycles * Player.faction_rep_mult * BitNodeMultipliers.FactionPassiveRepGain;
}
}
export const getFactionAugmentationsFiltered = (player: IPlayer, faction: Faction) => {
// If player has a gang with this faction, return (almost) all augmentations
if (player.hasGangWith(faction.name)) {
let augs = Object.values(Augmentations);
// Remove special augs
augs = augs.filter((a) => !a.isSpecial);
const blacklist: string[] = [AugmentationNames.NeuroFluxGovernor];
if (player.bitNodeN !== 2) {
// Remove faction-unique augs that don't belong to this faction
augs = augs.filter((a) => a.factions.length > 1 || faction.augmentations.includes(a.name));
// TRP is not available outside of BN2 for Gangs
blacklist.push(AugmentationNames.TheRedPill);
}
// Remove blacklisted augs
augs = augs.filter((a) => !blacklist.includes(a.name));
return augs.map((a) => a.name);
}
return faction.augmentations.slice();
};

@ -10,7 +10,7 @@ import { AugmentationNames } from "../../Augmentation/data/AugmentationNames";
import { Faction } from "../Faction";
import { PurchaseAugmentationsOrderSetting } from "../../Settings/SettingEnums";
import { Settings } from "../../Settings/Settings";
import { hasAugmentationPrereqs } from "../FactionHelpers";
import { hasAugmentationPrereqs, getFactionAugmentationsFiltered } from "../FactionHelpers";
import { use } from "../../ui/Context";
import { Reputation } from "../../ui/React/Reputation";
@ -42,28 +42,7 @@ export function AugmentationsPage(props: IProps): React.ReactElement {
}
function getAugs(): string[] {
if (isPlayersGang) {
// TODO: this code is duplicated in getAugmentationsFromFaction DRY
let augs = Object.values(Augmentations);
// Remove special augs.
augs = augs.filter((a) => !a.isSpecial);
if (player.bitNodeN !== 2) {
// Remove faction-unique augs outside BN2. (But keep the one for this faction.)
augs = augs.filter((a) => a.factions.length > 1 || props.faction.augmentations.includes(a.name));
// Remove blacklisted augs.
const blacklist = [AugmentationNames.NeuroFluxGovernor, AugmentationNames.TheRedPill].map(
(augmentation) => augmentation as string,
);
augs = augs.filter((a) => !blacklist.includes(a.name));
}
return augs.map((a) => a.name);
} else {
return props.faction.augmentations.slice();
}
return getFactionAugmentationsFiltered(player, props.faction);
}
function getAugsSorted(): string[] {

@ -9,7 +9,7 @@ import { Table, TableCell } from "../../ui/React/Table";
import { IRouter } from "../../ui/Router";
import { Faction } from "../Faction";
import { joinFaction } from "../FactionHelpers";
import { joinFaction, getFactionAugmentationsFiltered } from "../FactionHelpers";
import { Factions } from "../Factions";
import { FactionNames } from "../data/FactionNames";
@ -52,26 +52,7 @@ export function FactionsRoot(props: IProps): React.ReactElement {
}
const getAugsLeft = (faction: Faction, player: IPlayer): number => {
const isPlayersGang = player.inGang() && player.getGangName() === faction.name;
let augs: string[] = [];
if (isPlayersGang) {
for (const augName of Object.keys(Augmentations)) {
const aug = Augmentations[augName];
if (
augName === AugmentationNames.NeuroFluxGovernor ||
(augName === AugmentationNames.TheRedPill && player.bitNodeN !== 2) ||
// Special augs (i.e. Bladeburner augs)
aug.isSpecial ||
// Exclusive augs (i.e. QLink)
(aug.factions.length <= 1 && !faction.augmentations.includes(augName) && player.bitNodeN !== 2)
)
continue;
augs.push(augName);
}
} else {
augs = faction.augmentations.slice();
}
const augs = getFactionAugmentationsFiltered(player, faction);
return augs.filter((augmentation: string) => !player.hasAugmentation(augmentation)).length;
};

@ -1,7 +1,7 @@
import { INetscriptHelper } from "./INetscriptHelper";
import { WorkerScript } from "../Netscript/WorkerScript";
import { IPlayer } from "../PersonObjects/IPlayer";
import { purchaseAugmentation, joinFaction } from "../Faction/FactionHelpers";
import { purchaseAugmentation, joinFaction, getFactionAugmentationsFiltered } from "../Faction/FactionHelpers";
import { startWorkerScript } from "../NetscriptWorker";
import { Augmentation } from "../Augmentation/Augmentation";
import { Augmentations } from "../Augmentation/Augmentations";
@ -118,28 +118,7 @@ export function NetscriptSingularity(
helper.checkSingularityAccess("getAugmentationsFromFaction");
const faction = getFaction("getAugmentationsFromFaction", facName);
// If player has a gang with this faction, return all augmentations.
if (player.hasGangWith(facName)) {
let augs = Object.values(Augmentations);
// Remove special augs.
augs = augs.filter((a) => !a.isSpecial);
if (player.bitNodeN !== 2) {
// Remove faction-unique augs outside BN2. (But keep the one for this faction.)
augs = augs.filter((a) => a.factions.length > 1 || Factions[facName].augmentations.includes(a.name));
// Remove blacklisted augs.
const blacklist = [AugmentationNames.NeuroFluxGovernor, AugmentationNames.TheRedPill].map(
(augmentation) => augmentation as string,
);
augs = augs.filter((a) => !blacklist.includes(a.name));
}
return augs.map((a) => a.name);
}
return faction.augmentations.slice();
return getFactionAugmentationsFiltered(player, faction);
},
getAugmentationCost: function (_augName: unknown): [number, number] {
const augName = helper.string("getAugmentationCost", "augName", _augName);
@ -184,24 +163,7 @@ export function NetscriptSingularity(
const fac = getFaction("purchaseAugmentation", facName);
const aug = getAugmentation("purchaseAugmentation", augName);
let augs = [];
if (player.hasGangWith(facName)) {
for (const augName of Object.keys(Augmentations)) {
const aug = Augmentations[augName];
if (
augName === AugmentationNames.NeuroFluxGovernor ||
(augName === AugmentationNames.TheRedPill && player.bitNodeN !== 2) ||
// Special augs (i.e. Bladeburner augs)
aug.isSpecial ||
// Exclusive augs (i.e. QLink)
(aug.factions.length <= 1 && !fac.augmentations.includes(augName) && player.bitNodeN !== 2)
)
continue;
augs.push(augName);
}
} else {
augs = fac.augmentations;
}
const augs = getFactionAugmentationsFiltered(player, fac);
if (!augs.includes(augName)) {
workerScript.log(