bitburner-src/src/DarkWeb/DarkWeb.tsx
Snarling e0272ad4af
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 10:26:57 -04:00

113 lines
3.3 KiB
TypeScript

import React from "react";
import { DarkWebItems } from "./DarkWebItems";
import { Player } from "@player";
import { Terminal } from "../Terminal";
import { SpecialServers } from "../Server/data/SpecialServers";
import { formatMoney } from "../ui/formatNumber";
import { Money } from "../ui/React/Money";
import { DarkWebItem } from "./DarkWebItem";
import { isCreateProgramWork } from "../Work/CreateProgramWork";
//Posts a "help" message if connected to DarkWeb
export function checkIfConnectedToDarkweb(): void {
const server = Player.getCurrentServer();
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 'buy -a' to purchase all unowned items.",
);
}
}
export function listAllDarkwebItems(): void {
for (const key of Object.keys(DarkWebItems) as (keyof typeof DarkWebItems)[]) {
const item = DarkWebItems[key];
const cost = Player.getHomeComputer().programs.includes(item.program) ? (
<span style={{ color: `green` }}>[OWNED]</span>
) : (
<Money money={item.price} />
);
Terminal.printRaw(
<>
<span>{item.program}</span> - <span>{cost}</span> - <span>{item.description}</span>
</>,
);
}
}
export function buyDarkwebItem(itemName: string): void {
itemName = itemName.toLowerCase();
// find the program that matches, if any
let item: DarkWebItem | null = null;
for (const key of Object.keys(DarkWebItems) as (keyof typeof DarkWebItems)[]) {
const i = DarkWebItems[key];
if (i.program.toLowerCase() == itemName) {
item = i;
}
}
// return if invalid
if (item === null) {
Terminal.error("Unrecognized item: " + itemName);
return;
}
// return if the player already has it.
if (Player.hasProgram(item.program)) {
Terminal.print("You already have the " + item.program + " program");
return;
}
// return if the player doesn't have enough money
if (Player.money < item.price) {
Terminal.error("Not enough money to purchase " + item.program);
return;
}
// buy and push
Player.loseMoney(item.price, "other");
Player.getHomeComputer().pushProgram(item.program);
// Cancel if the program is in progress of writing
if (isCreateProgramWork(Player.currentWork) && Player.currentWork.programName === item.program) {
Player.finishWork(true);
}
Terminal.print(
"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 of Object.keys(DarkWebItems) as (keyof typeof 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, " + formatMoney(cost) + " required");
return;
}
for (const item of itemsToBuy) {
buyDarkwebItem(item.program);
}
}