mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-26 11:10:58 +02:00
NETSCRIPT: Add ns.stock.getConstants (#692)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Player as player } from "../Player";
|
||||
import { Player } from "../Player";
|
||||
import { buyStock, sellStock, shortStock, sellShort } from "../StockMarket/BuyingAndSelling";
|
||||
import { StockMarket, SymbolToStockMap, placeOrder, cancelOrder, initStockMarket } from "../StockMarket/StockMarket";
|
||||
import { getBuyTransactionCost, getSellTransactionGain } from "../StockMarket/StockMarketHelpers";
|
||||
@@ -13,14 +13,16 @@ import { Stock } from "../StockMarket/Stock";
|
||||
import { StockOrder, TIX } from "@nsdefs";
|
||||
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
||||
import { helpers } from "../Netscript/NetscriptHelpers";
|
||||
import { cloneDeep } from "lodash";
|
||||
import { StockMarketConstants } from "../StockMarket/data/Constants";
|
||||
|
||||
export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
/** Checks if the player has TIX API access. Throws an error if the player does not */
|
||||
const checkTixApiAccess = function (ctx: NetscriptContext): void {
|
||||
if (!player.hasWseAccount) {
|
||||
if (!Player.hasWseAccount) {
|
||||
throw helpers.makeRuntimeErrorMsg(ctx, `You don't have WSE Access! Cannot use ${ctx.function}()`);
|
||||
}
|
||||
if (!player.hasTixApiAccess) {
|
||||
if (!Player.hasTixApiAccess) {
|
||||
throw helpers.makeRuntimeErrorMsg(ctx, `You don't have TIX API Access! Cannot use ${ctx.function}()`);
|
||||
}
|
||||
};
|
||||
@@ -35,18 +37,11 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
};
|
||||
|
||||
return {
|
||||
hasWSEAccount: () => () => {
|
||||
return player.hasWseAccount;
|
||||
},
|
||||
hasTIXAPIAccess: () => () => {
|
||||
return player.hasTixApiAccess;
|
||||
},
|
||||
has4SData: () => () => {
|
||||
return player.has4SData;
|
||||
},
|
||||
has4SDataTIXAPI: () => () => {
|
||||
return player.has4SDataTixApi;
|
||||
},
|
||||
getConstants: () => () => cloneDeep(StockMarketConstants),
|
||||
hasWSEAccount: () => () => Player.hasWseAccount,
|
||||
hasTIXAPIAccess: () => () => Player.hasTixApiAccess,
|
||||
has4SData: () => () => Player.has4SData,
|
||||
has4SDataTIXAPI: () => () => Player.has4SDataTixApi,
|
||||
getSymbols: (ctx) => () => {
|
||||
checkTixApiAccess(ctx);
|
||||
return Object.values(StockSymbol);
|
||||
@@ -166,8 +161,8 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
const symbol = helpers.string(ctx, "symbol", _symbol);
|
||||
const shares = helpers.number(ctx, "shares", _shares);
|
||||
checkTixApiAccess(ctx);
|
||||
if (player.bitNodeN !== 8) {
|
||||
if (player.sourceFileLvl(8) <= 1) {
|
||||
if (Player.bitNodeN !== 8) {
|
||||
if (Player.sourceFileLvl(8) <= 1) {
|
||||
throw helpers.makeRuntimeErrorMsg(
|
||||
ctx,
|
||||
"You must either be in BitNode-8 or you must have Source-File 8 Level 2.",
|
||||
@@ -183,8 +178,8 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
const symbol = helpers.string(ctx, "symbol", _symbol);
|
||||
const shares = helpers.number(ctx, "shares", _shares);
|
||||
checkTixApiAccess(ctx);
|
||||
if (player.bitNodeN !== 8) {
|
||||
if (player.sourceFileLvl(8) <= 1) {
|
||||
if (Player.bitNodeN !== 8) {
|
||||
if (Player.sourceFileLvl(8) <= 1) {
|
||||
throw helpers.makeRuntimeErrorMsg(
|
||||
ctx,
|
||||
"You must either be in BitNode-8 or you must have Source-File 8 Level 2.",
|
||||
@@ -203,8 +198,8 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
const type = helpers.string(ctx, "type", _type);
|
||||
const pos = helpers.string(ctx, "pos", _pos);
|
||||
checkTixApiAccess(ctx);
|
||||
if (player.bitNodeN !== 8) {
|
||||
if (player.sourceFileLvl(8) <= 2) {
|
||||
if (Player.bitNodeN !== 8) {
|
||||
if (Player.sourceFileLvl(8) <= 2) {
|
||||
throw helpers.makeRuntimeErrorMsg(
|
||||
ctx,
|
||||
"You must either be in BitNode-8 or you must have Source-File 8 Level 3.",
|
||||
@@ -246,8 +241,8 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
const type = helpers.string(ctx, "type", _type);
|
||||
const pos = helpers.string(ctx, "pos", _pos);
|
||||
checkTixApiAccess(ctx);
|
||||
if (player.bitNodeN !== 8) {
|
||||
if (player.sourceFileLvl(8) <= 2) {
|
||||
if (Player.bitNodeN !== 8) {
|
||||
if (Player.sourceFileLvl(8) <= 2) {
|
||||
throw helpers.makeRuntimeErrorMsg(
|
||||
ctx,
|
||||
"You must either be in BitNode-8 or you must have Source-File 8 Level 3.",
|
||||
@@ -295,8 +290,8 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
},
|
||||
getOrders: (ctx) => () => {
|
||||
checkTixApiAccess(ctx);
|
||||
if (player.bitNodeN !== 8) {
|
||||
if (player.sourceFileLvl(8) <= 2) {
|
||||
if (Player.bitNodeN !== 8) {
|
||||
if (Player.sourceFileLvl(8) <= 2) {
|
||||
throw helpers.makeRuntimeErrorMsg(ctx, "You must either be in BitNode-8 or have Source-File 8 Level 3.");
|
||||
}
|
||||
}
|
||||
@@ -323,7 +318,7 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
},
|
||||
getVolatility: (ctx) => (_symbol) => {
|
||||
const symbol = helpers.string(ctx, "symbol", _symbol);
|
||||
if (!player.has4SDataTixApi) {
|
||||
if (!Player.has4SDataTixApi) {
|
||||
throw helpers.makeRuntimeErrorMsg(ctx, "You don't have 4S Market Data TIX API Access!");
|
||||
}
|
||||
const stock = getStockFromSymbol(ctx, symbol);
|
||||
@@ -332,7 +327,7 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
},
|
||||
getForecast: (ctx) => (_symbol) => {
|
||||
const symbol = helpers.string(ctx, "symbol", _symbol);
|
||||
if (!player.has4SDataTixApi) {
|
||||
if (!Player.has4SDataTixApi) {
|
||||
throw helpers.makeRuntimeErrorMsg(ctx, "You don't have 4S Market Data TIX API Access!");
|
||||
}
|
||||
const stock = getStockFromSymbol(ctx, symbol);
|
||||
@@ -342,69 +337,69 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
|
||||
return forecast / 100; // Convert from percentage to decimal
|
||||
},
|
||||
purchase4SMarketData: (ctx) => () => {
|
||||
if (player.has4SData) {
|
||||
if (Player.has4SData) {
|
||||
helpers.log(ctx, () => "Already purchased 4S Market Data.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (player.money < getStockMarket4SDataCost()) {
|
||||
if (Player.money < getStockMarket4SDataCost()) {
|
||||
helpers.log(ctx, () => "Not enough money to purchase 4S Market Data.");
|
||||
return false;
|
||||
}
|
||||
|
||||
player.has4SData = true;
|
||||
player.loseMoney(getStockMarket4SDataCost(), "stock");
|
||||
Player.has4SData = true;
|
||||
Player.loseMoney(getStockMarket4SDataCost(), "stock");
|
||||
helpers.log(ctx, () => "Purchased 4S Market Data");
|
||||
return true;
|
||||
},
|
||||
purchase4SMarketDataTixApi: (ctx) => () => {
|
||||
checkTixApiAccess(ctx);
|
||||
|
||||
if (player.has4SDataTixApi) {
|
||||
if (Player.has4SDataTixApi) {
|
||||
helpers.log(ctx, () => "Already purchased 4S Market Data TIX API");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (player.money < getStockMarket4STixApiCost()) {
|
||||
if (Player.money < getStockMarket4STixApiCost()) {
|
||||
helpers.log(ctx, () => "Not enough money to purchase 4S Market Data TIX API");
|
||||
return false;
|
||||
}
|
||||
|
||||
player.has4SDataTixApi = true;
|
||||
player.loseMoney(getStockMarket4STixApiCost(), "stock");
|
||||
Player.has4SDataTixApi = true;
|
||||
Player.loseMoney(getStockMarket4STixApiCost(), "stock");
|
||||
helpers.log(ctx, () => "Purchased 4S Market Data TIX API");
|
||||
return true;
|
||||
},
|
||||
purchaseWseAccount: (ctx) => () => {
|
||||
if (player.hasWseAccount) {
|
||||
if (Player.hasWseAccount) {
|
||||
helpers.log(ctx, () => "Already purchased WSE Account");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (player.money < getStockMarketWseCost()) {
|
||||
if (Player.money < getStockMarketWseCost()) {
|
||||
helpers.log(ctx, () => "Not enough money to purchase WSE Account Access");
|
||||
return false;
|
||||
}
|
||||
|
||||
player.hasWseAccount = true;
|
||||
Player.hasWseAccount = true;
|
||||
initStockMarket();
|
||||
player.loseMoney(getStockMarketWseCost(), "stock");
|
||||
Player.loseMoney(getStockMarketWseCost(), "stock");
|
||||
helpers.log(ctx, () => "Purchased WSE Account Access");
|
||||
return true;
|
||||
},
|
||||
purchaseTixApi: (ctx) => () => {
|
||||
if (player.hasTixApiAccess) {
|
||||
if (Player.hasTixApiAccess) {
|
||||
helpers.log(ctx, () => "Already purchased TIX API");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (player.money < getStockMarketTixApiCost()) {
|
||||
if (Player.money < getStockMarketTixApiCost()) {
|
||||
helpers.log(ctx, () => "Not enough money to purchase TIX API Access");
|
||||
return false;
|
||||
}
|
||||
|
||||
player.hasTixApiAccess = true;
|
||||
player.loseMoney(getStockMarketTixApiCost(), "stock");
|
||||
Player.hasTixApiAccess = true;
|
||||
Player.loseMoney(getStockMarketTixApiCost(), "stock");
|
||||
helpers.log(ctx, () => "Purchased TIX API");
|
||||
return true;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user