mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 06:18:42 +02:00
CODEBASE: Refactor ns.singularity.purchaseAugmentation (#1879)
This commit is contained in:
@@ -40,7 +40,7 @@ export function PurchaseAugmentationModal({ aug, faction, onClose, open }: IProp
|
||||
<Button
|
||||
autoFocus
|
||||
onClick={() => {
|
||||
purchaseAugmentation(aug, faction);
|
||||
purchaseAugmentation(faction, aug);
|
||||
onClose();
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -20,6 +20,7 @@ import { SFC32RNG } from "../Casino/RNG";
|
||||
import { isFactionWork } from "../Work/FactionWork";
|
||||
import { getAugCost } from "../Augmentation/AugmentationHelpers";
|
||||
import { getRecordKeys } from "../Types/Record";
|
||||
import type { Result } from "../types";
|
||||
|
||||
export function inviteToFaction(faction: Faction): void {
|
||||
if (faction.alreadyInvited || faction.isMember) return;
|
||||
@@ -58,52 +59,76 @@ export function hasAugmentationPrereqs(aug: Augmentation): boolean {
|
||||
return aug.prereqs.every((aug) => Player.hasAugmentation(aug));
|
||||
}
|
||||
|
||||
export function purchaseAugmentation(aug: Augmentation, fac: Faction, sing = false): string {
|
||||
const hasPrereqs = hasAugmentationPrereqs(aug);
|
||||
const augCosts = getAugCost(aug);
|
||||
if (!hasPrereqs) {
|
||||
const txt = `You must first purchase or install ${aug.prereqs
|
||||
.filter((req) => !Player.hasAugmentation(req))
|
||||
.join(",")} before you can purchase this one.`;
|
||||
if (sing) {
|
||||
return txt;
|
||||
} else {
|
||||
dialogBoxCreate(txt);
|
||||
}
|
||||
} else if (augCosts.moneyCost !== 0 && Player.money < augCosts.moneyCost) {
|
||||
const txt = "You don't have enough money to purchase " + aug.name;
|
||||
if (sing) {
|
||||
return txt;
|
||||
}
|
||||
dialogBoxCreate(txt);
|
||||
} else if (fac.playerReputation < augCosts.repCost) {
|
||||
const txt = "You don't have enough faction reputation to purchase " + aug.name;
|
||||
if (sing) {
|
||||
return txt;
|
||||
}
|
||||
dialogBoxCreate(txt);
|
||||
} else if (augCosts.moneyCost === 0 || Player.money >= augCosts.moneyCost) {
|
||||
Player.queueAugmentation(aug.name);
|
||||
function checkIfPlayerCanPurchaseAugmentation(faction: Faction, augmentation: Augmentation): Result {
|
||||
if (!Player.factions.includes(faction.name)) {
|
||||
return {
|
||||
success: false,
|
||||
message: `You can't purchase augmentations from '${faction.name}' because you aren't a member.`,
|
||||
};
|
||||
}
|
||||
|
||||
Player.loseMoney(augCosts.moneyCost, "augmentations");
|
||||
if (!getFactionAugmentationsFiltered(faction).includes(augmentation.name)) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Faction '${faction.name}' does not have the '${augmentation.name}' augmentation.`,
|
||||
};
|
||||
}
|
||||
|
||||
if (sing) {
|
||||
return "You purchased " + aug.name;
|
||||
} else if (!Settings.SuppressBuyAugmentationConfirmation) {
|
||||
dialogBoxCreate(
|
||||
`You purchased ${aug.name}. Its enhancements will not take effect until they are installed. ` +
|
||||
"To install your augmentations, go to the 'Augmentations' tab on the left-hand navigation menu. " +
|
||||
"Purchasing additional augmentations will now be more expensive.",
|
||||
);
|
||||
if (augmentation.name !== AugmentationName.NeuroFluxGovernor) {
|
||||
for (const queuedAugmentation of Player.queuedAugmentations) {
|
||||
if (queuedAugmentation.name === augmentation.name) {
|
||||
return { success: false, message: `You already purchased the '${augmentation.name}' augmentation.` };
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (const installedAugmentation of Player.augmentations) {
|
||||
if (installedAugmentation.name === augmentation.name) {
|
||||
return { success: false, message: `You already installed the '${augmentation.name}' augmentation.` };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasAugmentationPrereqs(augmentation)) {
|
||||
return {
|
||||
success: false,
|
||||
message: `You must first purchase or install ${augmentation.prereqs
|
||||
.filter((req) => !Player.hasAugmentation(req))
|
||||
.join(",")} before you can purchase this one.`,
|
||||
};
|
||||
}
|
||||
|
||||
const augCosts = getAugCost(augmentation);
|
||||
if (augCosts.moneyCost !== 0 && Player.money < augCosts.moneyCost) {
|
||||
return { success: false, message: `You don't have enough money to purchase ${augmentation.name}.` };
|
||||
}
|
||||
|
||||
if (faction.playerReputation < augCosts.repCost) {
|
||||
return { success: false, message: `You don't have enough faction reputation to purchase ${augmentation.name}.` };
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export function purchaseAugmentation(faction: Faction, augmentation: Augmentation, singularity = false): Result {
|
||||
const result = checkIfPlayerCanPurchaseAugmentation(faction, augmentation);
|
||||
if (!result.success) {
|
||||
if (!singularity) {
|
||||
dialogBoxCreate(result.message);
|
||||
}
|
||||
return { success: false, message: result.message };
|
||||
}
|
||||
|
||||
const augCosts = getAugCost(augmentation);
|
||||
Player.queueAugmentation(augmentation.name);
|
||||
Player.loseMoney(augCosts.moneyCost, "augmentations");
|
||||
|
||||
if (!singularity && !Settings.SuppressBuyAugmentationConfirmation) {
|
||||
dialogBoxCreate(
|
||||
"Hmm, something went wrong when trying to purchase an Augmentation. " +
|
||||
"Please report this to the game developer with an explanation of how to " +
|
||||
"reproduce this.",
|
||||
`You purchased ${augmentation.name}. Its enhancements will not take effect until they are installed. ` +
|
||||
"To install your augmentations, go to the 'Augmentations' tab on the left-hand navigation menu. " +
|
||||
"Purchasing additional augmentations will now be more expensive.",
|
||||
);
|
||||
}
|
||||
return "";
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export function processPassiveFactionRepGain(numCycles: number): void {
|
||||
|
||||
@@ -250,7 +250,7 @@ export function AugmentationsPage({ faction }: { faction: Faction }): React.Reac
|
||||
if (!Settings.SuppressBuyAugmentationConfirmation) {
|
||||
showModal(true);
|
||||
} else {
|
||||
purchaseAugmentation(aug, faction);
|
||||
purchaseAugmentation(faction, aug);
|
||||
rerender();
|
||||
}
|
||||
}}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Singularity as ISingularity } from "@nsdefs";
|
||||
|
||||
import { Player } from "@player";
|
||||
import { AugmentationName, CityName, FactionWorkType, GymType, LocationName, UniversityClassType } from "@enums";
|
||||
import { CityName, FactionWorkType, GymType, LocationName, UniversityClassType } from "@enums";
|
||||
import { purchaseAugmentation, joinFaction, getFactionAugmentationsFiltered } from "../Faction/FactionHelpers";
|
||||
import { startWorkerScript } from "../NetscriptWorker";
|
||||
import { Augmentations } from "../Augmentation/Augmentations";
|
||||
@@ -163,50 +163,17 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
|
||||
const augName = getEnumHelper("AugmentationName").nsGetMember(ctx, _augName);
|
||||
const fac = Factions[facName];
|
||||
const aug = Augmentations[augName];
|
||||
const faction = Factions[facName];
|
||||
const augmentation = Augmentations[augName];
|
||||
|
||||
const factionAugs = getFactionAugmentationsFiltered(fac);
|
||||
|
||||
if (!Player.factions.includes(facName)) {
|
||||
helpers.log(ctx, () => `You can't purchase augmentations from '${facName}' because you aren't a member`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!factionAugs.includes(augName)) {
|
||||
helpers.log(ctx, () => `Faction '${facName}' does not have the '${augName}' augmentation.`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const isNeuroflux = aug.name === AugmentationName.NeuroFluxGovernor;
|
||||
if (!isNeuroflux) {
|
||||
for (let j = 0; j < Player.queuedAugmentations.length; ++j) {
|
||||
if (Player.queuedAugmentations[j].name === aug.name) {
|
||||
helpers.log(ctx, () => `You already have the '${augName}' augmentation.`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (let j = 0; j < Player.augmentations.length; ++j) {
|
||||
if (Player.augmentations[j].name === aug.name) {
|
||||
helpers.log(ctx, () => `You already have the '${augName}' augmentation.`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fac.playerReputation < getAugCost(aug).repCost) {
|
||||
helpers.log(ctx, () => `You do not have enough reputation with '${fac.name}'.`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const res = purchaseAugmentation(aug, fac, true);
|
||||
helpers.log(ctx, () => res);
|
||||
if (res.startsWith("You purchased")) {
|
||||
Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain * 10);
|
||||
return true;
|
||||
} else {
|
||||
const result = purchaseAugmentation(faction, augmentation, true);
|
||||
if (!result.success) {
|
||||
helpers.log(ctx, () => result.message);
|
||||
return false;
|
||||
}
|
||||
helpers.log(ctx, () => `You purchased ${augName}.`);
|
||||
Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain * 10);
|
||||
return true;
|
||||
},
|
||||
softReset: (ctx) => (_cbScript) => {
|
||||
helpers.checkSingularityAccess(ctx);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { installAugmentations } from "../../../src/Augmentation/AugmentationHelpers";
|
||||
import { blackOpsArray } from "../../../src/Bladeburner/data/BlackOperations";
|
||||
import { AugmentationName } from "../../../src/Enums";
|
||||
import { Player } from "../../../src/Player";
|
||||
import { AugmentationName, FactionName } from "@enums";
|
||||
import { Player } from "@player";
|
||||
import { prestigeSourceFile } from "../../../src/Prestige";
|
||||
import { GetServerOrThrow } from "../../../src/Server/AllServers";
|
||||
import { SpecialServers } from "../../../src/Server/data/SpecialServers";
|
||||
import { Factions } from "../../../src/Faction/Factions";
|
||||
import { PlayerOwnedAugmentation } from "../../../src/Augmentation/PlayerOwnedAugmentation";
|
||||
import { getNS, initGameEnvironment, setupBasicTestingEnvironment } from "./Utilities";
|
||||
|
||||
function setNumBlackOpsComplete(value: number): void {
|
||||
@@ -179,3 +182,118 @@ describe("destroyW0r1dD43m0n", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("purchaseAugmentation", () => {
|
||||
beforeEach(() => {
|
||||
setupBasicTestingEnvironment();
|
||||
prestigeSourceFile(true);
|
||||
Player.money = 1e100;
|
||||
Player.factions.push(FactionName.CyberSec);
|
||||
Factions[FactionName.CyberSec].playerReputation = 1e10;
|
||||
Player.factions.push(FactionName.Illuminati);
|
||||
});
|
||||
|
||||
describe("Success", () => {
|
||||
const expectQueuedAugmentation = (augmentationName: AugmentationName, level: number) => {
|
||||
expect(
|
||||
Player.queuedAugmentations.find((augmentation) => augmentation.name === augmentationName)?.level,
|
||||
).toStrictEqual(level);
|
||||
};
|
||||
test("NFG", () => {
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.NeuroFluxGovernor),
|
||||
).toStrictEqual(true);
|
||||
expectQueuedAugmentation(AugmentationName.NeuroFluxGovernor, 1);
|
||||
});
|
||||
// Check if the level of NFG is increased properly.
|
||||
test("Upgrade NFG", () => {
|
||||
Player.augmentations.push(new PlayerOwnedAugmentation(AugmentationName.NeuroFluxGovernor));
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.NeuroFluxGovernor),
|
||||
).toStrictEqual(true);
|
||||
expectQueuedAugmentation(AugmentationName.NeuroFluxGovernor, 2);
|
||||
});
|
||||
test("Normal augmentation", () => {
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.CranialSignalProcessorsG1),
|
||||
).toStrictEqual(true);
|
||||
expectQueuedAugmentation(AugmentationName.CranialSignalProcessorsG1, 1);
|
||||
});
|
||||
test("Normal augmentation with prerequisite", () => {
|
||||
Player.augmentations.push(new PlayerOwnedAugmentation(AugmentationName.CranialSignalProcessorsG1));
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.CranialSignalProcessorsG2),
|
||||
).toStrictEqual(true);
|
||||
expectQueuedAugmentation(AugmentationName.CranialSignalProcessorsG2, 1);
|
||||
});
|
||||
test("Buy 0-money-cost augmentation with negative money", () => {
|
||||
Player.money = -1000;
|
||||
Player.factions.push(FactionName.Daedalus);
|
||||
Factions[FactionName.Daedalus].playerReputation = 1e10;
|
||||
const ns = getNS();
|
||||
expect(ns.singularity.purchaseAugmentation(FactionName.Daedalus, AugmentationName.TheRedPill)).toStrictEqual(
|
||||
true,
|
||||
);
|
||||
expectQueuedAugmentation(AugmentationName.TheRedPill, 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Failure", () => {
|
||||
const expectNoQueuedAugmentation = (augmentationName: AugmentationName) => {
|
||||
expect(Player.queuedAugmentations.find((augmentation) => augmentation.name === augmentationName)).toStrictEqual(
|
||||
undefined,
|
||||
);
|
||||
};
|
||||
test("Not a member of specified faction", () => {
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.Daedalus, AugmentationName.NeuroFluxGovernor),
|
||||
).toStrictEqual(false);
|
||||
expectNoQueuedAugmentation(AugmentationName.NeuroFluxGovernor);
|
||||
});
|
||||
test("Faction does not have specified augmentation", () => {
|
||||
const ns = getNS();
|
||||
expect(ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.QLink)).toStrictEqual(false);
|
||||
expectNoQueuedAugmentation(AugmentationName.QLink);
|
||||
});
|
||||
test("Purchase installed augmentation", () => {
|
||||
Player.augmentations.push(new PlayerOwnedAugmentation(AugmentationName.CranialSignalProcessorsG1));
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.CranialSignalProcessorsG1),
|
||||
).toStrictEqual(false);
|
||||
expectNoQueuedAugmentation(AugmentationName.CranialSignalProcessorsG1);
|
||||
});
|
||||
test("Purchase queued augmentation", () => {
|
||||
Player.queuedAugmentations.push(new PlayerOwnedAugmentation(AugmentationName.CranialSignalProcessorsG1));
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.CranialSignalProcessorsG1),
|
||||
).toStrictEqual(false);
|
||||
});
|
||||
test("Not have prerequisite augmentation", () => {
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.CranialSignalProcessorsG2),
|
||||
).toStrictEqual(false);
|
||||
expectNoQueuedAugmentation(AugmentationName.CranialSignalProcessorsG2);
|
||||
});
|
||||
test("Not enough money", () => {
|
||||
Player.money = 1000;
|
||||
const ns = getNS();
|
||||
expect(
|
||||
ns.singularity.purchaseAugmentation(FactionName.CyberSec, AugmentationName.CranialSignalProcessorsG1),
|
||||
).toStrictEqual(false);
|
||||
expectNoQueuedAugmentation(AugmentationName.CranialSignalProcessorsG1);
|
||||
});
|
||||
test("Not enough reputation", () => {
|
||||
const ns = getNS();
|
||||
expect(ns.singularity.purchaseAugmentation(FactionName.Illuminati, AugmentationName.QLink)).toStrictEqual(false);
|
||||
expectNoQueuedAugmentation(AugmentationName.QLink);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user