fix(darkweb:buy) remove partial exe on buy

Remove any matching partial .exe item when buying it from the dark web.

Resolves danielyxie/bitburner#2024
This commit is contained in:
Dan 2021-12-20 05:55:35 +00:00
parent 82f6bbee13
commit b95261d905
No known key found for this signature in database
GPG Key ID: CD0F515DA7B576B5

@ -5,6 +5,7 @@ import { Player } from "../Player";
import { Terminal } from "../Terminal"; import { Terminal } from "../Terminal";
import { SpecialServers } from "../Server/data/SpecialServers"; import { SpecialServers } from "../Server/data/SpecialServers";
import { Money } from "../ui/React/Money"; import { Money } from "../ui/React/Money";
import { DarkWebItem } from "./DarkWebItem";
//Posts a "help" message if connected to DarkWeb //Posts a "help" message if connected to DarkWeb
export function checkIfConnectedToDarkweb(): void { export function checkIfConnectedToDarkweb(): void {
@ -33,7 +34,8 @@ export function buyDarkwebItem(itemName: string): void {
itemName = itemName.toLowerCase(); itemName = itemName.toLowerCase();
// find the program that matches, if any // find the program that matches, if any
let item = null; let item: DarkWebItem | null = null;
for (const key in DarkWebItems) { for (const key in DarkWebItems) {
const i = DarkWebItems[key]; const i = DarkWebItems[key];
if (i.program.toLowerCase() == itemName) { if (i.program.toLowerCase() == itemName) {
@ -61,7 +63,19 @@ export function buyDarkwebItem(itemName: string): void {
// buy and push // buy and push
Player.loseMoney(item.price, "other"); Player.loseMoney(item.price, "other");
const programsRef = Player.getHomeComputer().programs;
// Remove partially created program if there is one
const existingPartialExeIndex = programsRef.findIndex(
(program) => item?.program && program.startsWith(item?.program),
);
// findIndex returns -1 if there is no match, we only want to splice on a match
if (existingPartialExeIndex > -1) {
programsRef.splice(existingPartialExeIndex, 1);
}
// Add the newly bought, full .exe
Player.getHomeComputer().programs.push(item.program); Player.getHomeComputer().programs.push(item.program);
Terminal.print( Terminal.print(
"You have purchased the " + item.program + " program. The new program can be found on your home computer.", "You have purchased the " + item.program + " program. The new program can be found on your home computer.",
); );