From e9fd5f46eb8a40c5a0bf8870eaf1f4fe4d4bcb58 Mon Sep 17 00:00:00 2001 From: Tony Roberts <123ananan@gmail.com> Date: Tue, 28 Dec 2021 02:11:41 -0500 Subject: [PATCH] Add buy all option to buy terminal command --- src/DarkWeb/DarkWeb.tsx | 32 ++++++++++++++++++++++++++++++-- src/Terminal/HelpText.ts | 4 +++- src/Terminal/commands/buy.ts | 5 ++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/DarkWeb/DarkWeb.tsx b/src/DarkWeb/DarkWeb.tsx index a165bdda0..2833d3a11 100644 --- a/src/DarkWeb/DarkWeb.tsx +++ b/src/DarkWeb/DarkWeb.tsx @@ -4,6 +4,7 @@ import { DarkWebItems } from "./DarkWebItems"; import { Player } from "../Player"; import { Terminal } from "../Terminal"; import { SpecialServers } from "../Server/data/SpecialServers"; +import { numeralWrapper } from "../ui/numeralFormat"; import { Money } from "../ui/React/Money"; import { DarkWebItem } from "./DarkWebItem"; @@ -13,8 +14,8 @@ export function checkIfConnectedToDarkweb(): void { if (server !== null && SpecialServers.DarkWeb == server.hostname) { Terminal.print( "You are now connected to the dark web. From the dark web you can purchase illegal items. " + - "Use the 'buy -l' command to display a list of all the items you can buy. Use 'buy [item-name] " + - "to purchase an item.", + "Use the 'buy -l' command to display a list of all the items you can buy. Use 'buy [item-name]' " + + "to purchase an item. Use the 'buy -a' command to purchase unowned all items.", ); } } @@ -87,3 +88,30 @@ export function buyDarkwebItem(itemName: string): void { "You have purchased the " + item.program + " program. The new program can be found on your home computer.", ); } + +export function buyAllDarkwebItems(): void { + const itemsToBuy: DarkWebItem[] = []; + let cost = 0; + + for (const key in DarkWebItems) { + const item = DarkWebItems[key]; + if (!Player.hasProgram(item.program)) { + itemsToBuy.push(item); + cost += item.price; + } + } + + if (itemsToBuy.length === 0) { + Terminal.print("All available programs have been purchased already."); + return; + } + + if (cost > Player.money) { + Terminal.error("Not enough money to purchase remaining programs, " + numeralWrapper.formatMoney(cost) + " required"); + return; + } + + for (const item of itemsToBuy) { + buyDarkwebItem(item.program); + } +} diff --git a/src/Terminal/HelpText.ts b/src/Terminal/HelpText.ts index 3f143645a..ebf46a1fd 100644 --- a/src/Terminal/HelpText.ts +++ b/src/Terminal/HelpText.ts @@ -96,13 +96,15 @@ export const HelpTexts: IMap = { " ", ], buy: [ - "buy [-l / program]", + "buy [-l / -a / program]", " ", "Purchase a program through the Dark Web. Requires a TOR router to use.", " ", "If this command is ran with the '-l' flag, it will display a list of all programs that can be bought through the ", "dark web to the Terminal, as well as their costs.", " ", + "If this command is ran with the '-a' flag, it will attempt to purchase all unowned programs.", + " ", "Otherwise, the name of the program must be passed in as a parameter. This name is NOT case-sensitive.", ], cat: [ diff --git a/src/Terminal/commands/buy.ts b/src/Terminal/commands/buy.ts index 8fda02b7e..56d04ddd6 100644 --- a/src/Terminal/commands/buy.ts +++ b/src/Terminal/commands/buy.ts @@ -2,7 +2,7 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; -import { listAllDarkwebItems, buyDarkwebItem } from "../../DarkWeb/DarkWeb"; +import { listAllDarkwebItems, buyAllDarkwebItems, buyDarkwebItem } from "../../DarkWeb/DarkWeb"; import { SpecialServers } from "../../Server/data/SpecialServers"; import { GetServer } from "../../Server/AllServers"; @@ -22,12 +22,15 @@ export function buy( if (args.length != 1) { terminal.print("Incorrect number of arguments. Usage: "); terminal.print("buy -l"); + terminal.print("buy -a"); terminal.print("buy [item name]"); return; } const arg = args[0] + ""; if (arg == "-l" || arg == "-1" || arg == "--list") { listAllDarkwebItems(); + } else if (arg == "-a" || arg == "--all") { + buyAllDarkwebItems(); } else { buyDarkwebItem(arg); }