MISC: Remove fuzzy matching when checking params (#2091)

This commit is contained in:
catloversg
2025-05-19 06:08:19 +07:00
committed by GitHub
parent f4e70720a6
commit 24b31975e7
26 changed files with 281 additions and 395 deletions
+8 -4
View File
@@ -35,13 +35,17 @@ export function NetscriptBladeburner(): InternalAPI<INetscriptBladeburner> {
throw helpers.errorMessage(ctx, "You must be a member of the Bladeburner division to use this API.");
return bladeburner;
};
function getAction(ctx: NetscriptContext, type: unknown, name: unknown): Action {
function getAction(ctx: NetscriptContext, _type: unknown, name: unknown): Action {
const bladeburner = Player.bladeburner;
assertStringWithNSContext(ctx, "type", type);
const type = getEnumHelper("BladeburnerActionType").nsGetMember(ctx, _type);
assertStringWithNSContext(ctx, "name", name);
if (bladeburner === null) throw new Error("Must have joined bladeburner");
if (bladeburner === null) {
throw new Error("Must have joined bladeburner");
}
const action = bladeburner.getActionFromTypeAndName(type, name);
if (!action) throw helpers.errorMessage(ctx, `Invalid action type='${type}', name='${name}'`);
if (!action) {
throw helpers.errorMessage(ctx, `Invalid action type='${_type}', name='${name}'`);
}
return action;
}
+13 -13
View File
@@ -26,7 +26,7 @@ import {
calculateGrowTime,
calculateWeakenTime,
} from "../Hacking";
import { CityName, CompletedProgramName, FactionWorkType, GymType, LocationName, UniversityClassType } from "@enums";
import { CityName, CompletedProgramName, LocationName } from "@enums";
import { Formulas as IFormulas, Player as IPlayer, Person as IPerson } from "@nsdefs";
import {
calculateRespectGain,
@@ -47,12 +47,11 @@ import { calculateClassEarnings } from "../Work/Formulas";
import { calculateFactionExp, calculateFactionRep } from "../Work/Formulas";
import { defaultMultipliers } from "../PersonObjects/Multipliers";
import { findEnumMember } from "../utils/helpers/enum";
import { getEnumHelper } from "../utils/EnumHelper";
import { CompanyPositions } from "../Company/CompanyPositions";
import { findCrime } from "../Crime/CrimeHelpers";
import { Skills } from "../Bladeburner/data/Skills";
import type { PositiveNumber } from "../types";
import { Crimes } from "../Crime/Crimes";
import { calculateEffectiveSharedThreads, calculateShareBonus } from "../NetworkShare/Share";
export function NetscriptFormulas(): InternalAPI<IFormulas> {
@@ -395,38 +394,39 @@ export function NetscriptFormulas(): InternalAPI<IFormulas> {
crimeSuccessChance: (ctx) => (_person, _crimeType) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const crime = findCrime(helpers.string(ctx, "crimeType", _crimeType));
if (!crime) throw new Error(`Invalid crime type: ${_crimeType}`);
const crime = Crimes[getEnumHelper("CrimeType").nsGetMember(ctx, _crimeType)];
if (!crime) {
throw new Error(`Invalid crime type: ${_crimeType}`);
}
return crime.successRate(person);
},
crimeGains: (ctx) => (_person, _crimeType) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const crime = findCrime(helpers.string(ctx, "crimeType", _crimeType));
if (!crime) throw new Error(`Invalid crime type: ${_crimeType}`);
const crime = Crimes[getEnumHelper("CrimeType").nsGetMember(ctx, _crimeType)];
if (!crime) {
throw new Error(`Invalid crime type: ${_crimeType}`);
}
return calculateCrimeWorkStats(person, crime);
},
gymGains: (ctx) => (_person, _classType, _locationName) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const classType = findEnumMember(GymType, helpers.string(ctx, "classType", _classType));
if (!classType) throw new Error(`Invalid gym training type: ${_classType}`);
const classType = getEnumHelper("GymType").nsGetMember(ctx, _classType);
const locationName = getEnumHelper("LocationName").nsGetMember(ctx, _locationName);
return calculateClassEarnings(person, classType, locationName);
},
universityGains: (ctx) => (_person, _classType, _locationName) => {
checkFormulasAccess(ctx);
const person = helpers.person(ctx, _person);
const classType = findEnumMember(UniversityClassType, helpers.string(ctx, "classType", _classType));
if (!classType) throw new Error(`Invalid university class type: ${_classType}`);
const classType = getEnumHelper("UniversityClassType").nsGetMember(ctx, _classType);
const locationName = getEnumHelper("LocationName").nsGetMember(ctx, _locationName);
return calculateClassEarnings(person, classType, locationName);
},
factionGains: (ctx) => (_player, _workType, _favor) => {
checkFormulasAccess(ctx);
const player = helpers.person(ctx, _player);
const workType = findEnumMember(FactionWorkType, helpers.string(ctx, "_workType", _workType));
if (!workType) throw new Error(`Invalid faction work type: ${_workType}`);
const workType = getEnumHelper("FactionWorkType").nsGetMember(ctx, _workType);
const favor = helpers.number(ctx, "favor", _favor);
const exp = calculateFactionExp(player, workType);
const rep = calculateFactionRep(player, workType, favor);
+46 -56
View File
@@ -1,7 +1,7 @@
import type { Singularity as ISingularity } from "@nsdefs";
import { Player } from "@player";
import { CityName, FactionWorkType, GymType, LocationName, UniversityClassType } from "@enums";
import { CityName, FactionWorkType, LocationName } from "@enums";
import { purchaseAugmentation, joinFaction, getFactionAugmentationsFiltered } from "../Faction/FactionHelpers";
import { startWorkerScript } from "../NetscriptWorker";
import { Augmentations } from "../Augmentation/Augmentations";
@@ -9,7 +9,6 @@ import { getAugCost, installAugmentations } from "../Augmentation/AugmentationHe
import { CONSTANTS } from "../Constants";
import { RunningScript } from "../Script/RunningScript";
import { calculateAchievements } from "../Achievements/Achievements";
import { findCrime } from "../Crime/CrimeHelpers";
import { CompanyPositions } from "../Company/CompanyPositions";
import { DarkWebItems } from "../DarkWeb/DarkWebItems";
import { Router } from "../ui/GameRoot";
@@ -40,7 +39,6 @@ import { CompanyWork } from "../Work/CompanyWork";
import { canGetBonus, onExport } from "../ExportBonus";
import { saveObject } from "../SaveObject";
import { calculateCrimeWorkStats } from "../Work/Formulas";
import { findEnumMember } from "../utils/helpers/enum";
import { Engine } from "../engine";
import { getEnumHelper } from "../utils/EnumHelper";
import { ScriptFilePath, resolveScriptFilePath } from "../Paths/ScriptFilePath";
@@ -53,6 +51,7 @@ import { addRepToFavor } from "../Faction/formulas/favor";
import { validBitNodes } from "../BitNode/BitNodeUtils";
import { exceptionAlert } from "../utils/helpers/exceptionAlert";
import { cat } from "../Terminal/commands/cat";
import { Crimes } from "../Crime/Crimes";
export function NetscriptSingularity(): InternalAPI<ISingularity> {
const runAfterReset = function (cbScript: ScriptFilePath) {
@@ -231,17 +230,13 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
(_universityName, _className, _focus = true) => {
helpers.checkSingularityAccess(ctx);
const universityName = helpers.string(ctx, "universityName", _universityName);
const classType = findEnumMember(UniversityClassType, helpers.string(ctx, "className", _className));
if (!classType) {
helpers.log(ctx, () => `Invalid class name: ${_className}.`);
return false;
}
const classType = getEnumHelper("UniversityClassType").nsGetMember(ctx, _className);
const focus = !!_focus;
const wasFocusing = Player.focus;
switch (universityName.toLowerCase()) {
case LocationName.AevumSummitUniversity.toLowerCase():
if (Player.city != CityName.Aevum) {
switch (universityName) {
case LocationName.AevumSummitUniversity:
if (Player.city !== CityName.Aevum) {
helpers.log(
ctx,
() => `You cannot study at 'Summit University' because you are not in '${CityName.Aevum}'.`,
@@ -250,8 +245,8 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
}
Player.gotoLocation(LocationName.AevumSummitUniversity);
break;
case LocationName.Sector12RothmanUniversity.toLowerCase():
if (Player.city != CityName.Sector12) {
case LocationName.Sector12RothmanUniversity:
if (Player.city !== CityName.Sector12) {
helpers.log(
ctx,
() => `You cannot study at 'Rothman University' because you are not in '${CityName.Sector12}'.`,
@@ -260,8 +255,8 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
}
Player.gotoLocation(LocationName.Sector12RothmanUniversity);
break;
case LocationName.VolhavenZBInstituteOfTechnology.toLowerCase():
if (Player.city != CityName.Volhaven) {
case LocationName.VolhavenZBInstituteOfTechnology:
if (Player.city !== CityName.Volhaven) {
helpers.log(
ctx,
() => `You cannot study at 'ZB Institute of Technology' because you are not in '${CityName.Volhaven}'.`,
@@ -298,17 +293,13 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
(_gymName, _stat, _focus = true) => {
helpers.checkSingularityAccess(ctx);
const gymName = helpers.string(ctx, "gymName", _gymName);
const classType = findEnumMember(GymType, helpers.string(ctx, "stat", _stat));
if (!classType) {
helpers.log(ctx, () => `Invalid stat: ${_stat}.`);
return false;
}
const classType = getEnumHelper("GymType").nsGetMember(ctx, _stat);
const focus = !!_focus;
const wasFocusing = Player.focus;
switch (gymName.toLowerCase()) {
case LocationName.AevumCrushFitnessGym.toLowerCase():
if (Player.city != CityName.Aevum) {
switch (gymName) {
case LocationName.AevumCrushFitnessGym:
if (Player.city !== CityName.Aevum) {
helpers.log(
ctx,
() =>
@@ -318,8 +309,8 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
}
Player.gotoLocation(LocationName.AevumCrushFitnessGym);
break;
case LocationName.AevumSnapFitnessGym.toLowerCase():
if (Player.city != CityName.Aevum) {
case LocationName.AevumSnapFitnessGym:
if (Player.city !== CityName.Aevum) {
helpers.log(
ctx,
() =>
@@ -329,8 +320,8 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
}
Player.gotoLocation(LocationName.AevumSnapFitnessGym);
break;
case LocationName.Sector12IronGym.toLowerCase():
if (Player.city != CityName.Sector12) {
case LocationName.Sector12IronGym:
if (Player.city !== CityName.Sector12) {
helpers.log(
ctx,
() =>
@@ -340,8 +331,8 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
}
Player.gotoLocation(LocationName.Sector12IronGym);
break;
case LocationName.Sector12PowerhouseGym.toLowerCase():
if (Player.city != CityName.Sector12) {
case LocationName.Sector12PowerhouseGym:
if (Player.city !== CityName.Sector12) {
helpers.log(
ctx,
() =>
@@ -351,8 +342,8 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
}
Player.gotoLocation(LocationName.Sector12PowerhouseGym);
break;
case LocationName.VolhavenMilleniumFitnessGym.toLowerCase():
if (Player.city != CityName.Volhaven) {
case LocationName.VolhavenMilleniumFitnessGym:
if (Player.city !== CityName.Volhaven) {
helpers.log(
ctx,
() =>
@@ -719,7 +710,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
applyToCompany: (ctx) => (_companyName, _field) => {
helpers.checkSingularityAccess(ctx);
const companyName = getEnumHelper("CompanyName").nsGetMember(ctx, _companyName);
const field = getEnumHelper("JobField").nsGetMember(ctx, _field, "field", { fuzzy: true });
const field = getEnumHelper("JobField").nsGetMember(ctx, _field, "field");
const company = Companies[companyName];
const entryPos = CompanyPositions[JobTracks[field][0]];
@@ -796,7 +787,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
(_facName, _type, _focus = true) => {
helpers.checkSingularityAccess(ctx);
const facName = getEnumHelper("FactionName").nsGetMember(ctx, _facName);
const type = helpers.string(ctx, "type", _type);
const type = getEnumHelper("FactionWorkType").nsGetMember(ctx, _type);
const focus = !!_focus;
const faction = Factions[facName];
@@ -813,10 +804,8 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
const wasFocusing = Player.focus;
switch (type.toLowerCase()) {
case "hacking":
case "hacking contracts":
case "hackingcontracts":
switch (type) {
case FactionWorkType.hacking:
if (!FactionInfos[faction.name].offerHackingWork) {
helpers.log(ctx, () => `Faction '${faction.name}' do not need help with hacking contracts.`);
return false;
@@ -837,9 +826,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
}
helpers.log(ctx, () => `Started carrying out hacking contracts for '${faction.name}'`);
return true;
case "field":
case "fieldwork":
case "field work":
case FactionWorkType.field:
if (!FactionInfos[faction.name].offerFieldWork) {
helpers.log(ctx, () => `Faction '${faction.name}' do not need help with field missions.`);
return false;
@@ -860,9 +847,7 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
}
helpers.log(ctx, () => `Started carrying out field missions for '${faction.name}'`);
return true;
case "security":
case "securitywork":
case "security work":
case FactionWorkType.security:
if (!FactionInfos[faction.name].offerSecurityWork) {
helpers.log(ctx, () => `Faction '${faction.name}' do not need help with security work.`);
return false;
@@ -1019,16 +1004,19 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
},
commitCrime: (ctx) => (_crimeType, _focus) => {
helpers.checkSingularityAccess(ctx);
const crimeType = helpers.string(ctx, "crimeType", _crimeType);
const crimeType = getEnumHelper("CrimeType").nsGetMember(ctx, _crimeType);
const focus = _focus === undefined ? true : !!_focus;
const wasFocusing = Player.focus;
if (Player.currentWork !== null) Player.finishWork(true);
if (Player.currentWork !== null) {
Player.finishWork(true);
}
Player.gotoLocation(LocationName.Slums);
// If input isn't a crimeType, use search using roughname.
const crime = findCrime(crimeType);
if (crime == null) throw helpers.errorMessage(ctx, `Invalid crime: '${crimeType}'`);
const crime = Crimes[crimeType];
if (crime == null) {
throw helpers.errorMessage(ctx, `Invalid crime: '${crimeType}'`);
}
helpers.log(ctx, () => `Attempting to commit ${crime.type}...`);
const crimeTime = crime.commit(1, ctx.workerScript);
@@ -1043,21 +1031,23 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
},
getCrimeChance: (ctx) => (_crimeType) => {
helpers.checkSingularityAccess(ctx);
const crimeType = helpers.string(ctx, "crimeType", _crimeType);
const crimeType = getEnumHelper("CrimeType").nsGetMember(ctx, _crimeType);
// If input isn't a crimeType, use search using roughname.
const crime = findCrime(crimeType);
if (crime == null) throw helpers.errorMessage(ctx, `Invalid crime: '${crimeType}'`);
const crime = Crimes[crimeType];
if (crime == null) {
throw helpers.errorMessage(ctx, `Invalid crime: '${crimeType}'`);
}
return crime.successRate(Player);
},
getCrimeStats: (ctx) => (_crimeType) => {
helpers.checkSingularityAccess(ctx);
const crimeType = helpers.string(ctx, "crimeType", _crimeType);
const crimeType = getEnumHelper("CrimeType").nsGetMember(ctx, _crimeType);
// If input isn't a crimeType, use search using roughname.
const crime = findCrime(crimeType);
if (crime == null) throw helpers.errorMessage(ctx, `Invalid crime: '${crimeType}'`);
const crime = Crimes[crimeType];
if (crime == null) {
throw helpers.errorMessage(ctx, `Invalid crime: '${crimeType}'`);
}
const crimeStatsWithMultipliers = calculateCrimeWorkStats(Player, crime);
+8 -8
View File
@@ -3,9 +3,8 @@ import type { Sleeve as NetscriptSleeve } from "@nsdefs";
import type { ActionIdentifier } from "../Bladeburner/Types";
import { Player } from "@player";
import { BladeburnerActionType, type BladeburnerContractName } from "@enums";
import { BladeburnerActionType, SpecialBladeburnerActionTypeForSleeve, type BladeburnerContractName } from "@enums";
import { Augmentations } from "../Augmentation/Augmentations";
import { findCrime } from "../Crime/CrimeHelpers";
import { getEnumHelper } from "../utils/EnumHelper";
import { InternalAPI, NetscriptContext, setRemovedFunctions } from "../Netscript/APIWrapper";
import { isSleeveFactionWork } from "../PersonObjects/Sleeve/Work/SleeveFactionWork";
@@ -15,6 +14,7 @@ import { getAugCost } from "../Augmentation/AugmentationHelpers";
import { Factions } from "../Faction/Factions";
import { SleeveWorkType } from "../PersonObjects/Sleeve/Work/Work";
import { canAccessBitNodeFeature } from "../BitNode/BitNodeUtils";
import { Crimes } from "../Crime/Crimes";
export const checkSleeveAPIAccess = function (ctx: NetscriptContext) {
/**
@@ -79,17 +79,17 @@ export function NetscriptSleeve(): InternalAPI<NetscriptSleeve> {
},
setToCommitCrime: (ctx) => (_sleeveNumber, _crimeType) => {
const sleeveNumber = helpers.number(ctx, "sleeveNumber", _sleeveNumber);
const crimeType = helpers.string(ctx, "crimeType", _crimeType);
const crimeType = getEnumHelper("CrimeType").nsGetMember(ctx, _crimeType);
checkSleeveAPIAccess(ctx);
checkSleeveNumber(ctx, sleeveNumber);
const crime = findCrime(crimeType);
const crime = Crimes[crimeType];
if (crime == null) return false;
return Player.sleeves[sleeveNumber].commitCrime(crime.type);
},
setToUniversityCourse: (ctx) => (_sleeveNumber, _universityName, _className) => {
const sleeveNumber = helpers.number(ctx, "sleeveNumber", _sleeveNumber);
const universityName = helpers.string(ctx, "universityName", _universityName);
const className = helpers.string(ctx, "className", _className);
const className = getEnumHelper("UniversityClassType").nsGetMember(ctx, _className);
checkSleeveAPIAccess(ctx);
checkSleeveNumber(ctx, sleeveNumber);
return Player.sleeves[sleeveNumber].takeUniversityCourse(universityName, className);
@@ -130,7 +130,7 @@ export function NetscriptSleeve(): InternalAPI<NetscriptSleeve> {
setToFactionWork: (ctx) => (_sleeveNumber, _factionName, _workType) => {
const sleeveNumber = helpers.number(ctx, "sleeveNumber", _sleeveNumber);
const factionName = getEnumHelper("FactionName").nsGetMember(ctx, _factionName);
const workType = helpers.string(ctx, "workType", _workType);
const workType = getEnumHelper("FactionWorkType").nsGetMember(ctx, _workType);
checkSleeveAPIAccess(ctx);
checkSleeveNumber(ctx, sleeveNumber);
@@ -164,7 +164,7 @@ export function NetscriptSleeve(): InternalAPI<NetscriptSleeve> {
setToGymWorkout: (ctx) => (_sleeveNumber, _gymName, _stat) => {
const sleeveNumber = helpers.number(ctx, "sleeveNumber", _sleeveNumber);
const gymName = helpers.string(ctx, "gymName", _gymName);
const stat = helpers.string(ctx, "stat", _stat);
const stat = getEnumHelper("GymType").nsGetMember(ctx, _stat);
checkSleeveAPIAccess(ctx);
checkSleeveNumber(ctx, sleeveNumber);
@@ -267,7 +267,7 @@ export function NetscriptSleeve(): InternalAPI<NetscriptSleeve> {
return false;
}
let contract: BladeburnerContractName | undefined = undefined;
if (action === "Take on contracts") {
if (action === SpecialBladeburnerActionTypeForSleeve.TakeOnContracts) {
contract = getEnumHelper("BladeburnerContractName").nsGetMember(ctx, _contract);
for (let i = 0; i < Player.sleeves.length; ++i) {
if (i === sleeveNumber) {
+13 -85
View File
@@ -9,7 +9,7 @@ import {
StockMarketPromise,
} from "../StockMarket/StockMarket";
import { getBuyTransactionCost, getSellTransactionGain } from "../StockMarket/StockMarketHelpers";
import { PositionType, OrderType, StockSymbol } from "@enums";
import { StockSymbol } from "@enums";
import {
getStockMarket4SDataCost,
getStockMarket4STixApiCost,
@@ -21,6 +21,7 @@ import { StockOrder, TIX } from "@nsdefs";
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { helpers } from "../Netscript/NetscriptHelpers";
import { StockMarketConstants } from "../StockMarket/data/Constants";
import { getEnumHelper } from "../utils/EnumHelper";
export function NetscriptStockMarket(): InternalAPI<TIX> {
/** Checks if the player has TIX API access. Throws an error if the player does not */
@@ -99,22 +100,12 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
getPurchaseCost: (ctx) => (_symbol, _shares, _posType) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
let shares = helpers.number(ctx, "shares", _shares);
const posType = helpers.string(ctx, "posType", _posType);
const posType = getEnumHelper("PositionType").nsGetMember(ctx, _posType);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
shares = Math.round(shares);
let pos;
const sanitizedPosType = posType.toLowerCase();
if (sanitizedPosType.includes("l")) {
pos = PositionType.Long;
} else if (sanitizedPosType.includes("s")) {
pos = PositionType.Short;
} else {
return Infinity;
}
const res = getBuyTransactionCost(stock, shares, pos);
const res = getBuyTransactionCost(stock, shares, posType);
if (res == null) {
return Infinity;
}
@@ -124,22 +115,12 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
getSaleGain: (ctx) => (_symbol, _shares, _posType) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
let shares = helpers.number(ctx, "shares", _shares);
const posType = helpers.string(ctx, "posType", _posType);
const posType = getEnumHelper("PositionType").nsGetMember(ctx, _posType);
checkTixApiAccess(ctx);
const stock = getStockFromSymbol(ctx, symbol);
shares = Math.round(shares);
let pos;
const sanitizedPosType = posType.toLowerCase();
if (sanitizedPosType.includes("l")) {
pos = PositionType.Long;
} else if (sanitizedPosType.includes("s")) {
pos = PositionType.Short;
} else {
return 0;
}
const res = getSellTransactionGain(stock, shares, pos);
const res = getSellTransactionGain(stock, shares, posType);
if (res == null) {
return 0;
}
@@ -191,46 +172,22 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
const symbol = helpers.string(ctx, "symbol", _symbol);
const shares = helpers.number(ctx, "shares", _shares);
const price = helpers.number(ctx, "price", _price);
const type = helpers.string(ctx, "type", _type);
const pos = helpers.string(ctx, "pos", _pos);
const type = getEnumHelper("OrderType").nsGetMember(ctx, _type);
const pos = getEnumHelper("PositionType").nsGetMember(ctx, _pos);
checkTixApiAccess(ctx);
if (Player.bitNodeN !== 8 && Player.activeSourceFileLvl(8) <= 2) {
throw helpers.errorMessage(ctx, "You must either be in BitNode-8 or you must have Source-File 8 Level 3.");
}
const stock = getStockFromSymbol(ctx, symbol);
let orderType;
let orderPos;
const ltype = type.toLowerCase();
if (ltype.includes("limit") && ltype.includes("buy")) {
orderType = OrderType.LimitBuy;
} else if (ltype.includes("limit") && ltype.includes("sell")) {
orderType = OrderType.LimitSell;
} else if (ltype.includes("stop") && ltype.includes("buy")) {
orderType = OrderType.StopBuy;
} else if (ltype.includes("stop") && ltype.includes("sell")) {
orderType = OrderType.StopSell;
} else {
throw helpers.errorMessage(ctx, `Invalid order type: ${type}`);
}
const lpos = pos.toLowerCase();
if (lpos.includes("l")) {
orderPos = PositionType.Long;
} else if (lpos.includes("s")) {
orderPos = PositionType.Short;
} else {
throw helpers.errorMessage(ctx, `Invalid position type: ${pos}`);
}
return placeOrder(stock, shares, price, orderType, orderPos, ctx);
return placeOrder(stock, shares, price, type, pos, ctx);
},
cancelOrder: (ctx) => (_symbol, _shares, _price, _type, _pos) => {
const symbol = helpers.string(ctx, "symbol", _symbol);
const shares = helpers.number(ctx, "shares", _shares);
const price = helpers.number(ctx, "price", _price);
const type = helpers.string(ctx, "type", _type);
const pos = helpers.string(ctx, "pos", _pos);
const type = getEnumHelper("OrderType").nsGetMember(ctx, _type);
const pos = getEnumHelper("PositionType").nsGetMember(ctx, _pos);
checkTixApiAccess(ctx);
if (Player.bitNodeN !== 8 && Player.activeSourceFileLvl(8) <= 2) {
throw helpers.errorMessage(ctx, "You must either be in BitNode-8 or you must have Source-File 8 Level 3.");
@@ -239,37 +196,8 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
if (isNaN(shares) || isNaN(price)) {
throw helpers.errorMessage(ctx, `Invalid shares or price. Must be numeric. shares=${shares}, price=${price}`);
}
let orderType;
let orderPos;
const ltype = type.toLowerCase();
if (ltype.includes("limit") && ltype.includes("buy")) {
orderType = OrderType.LimitBuy;
} else if (ltype.includes("limit") && ltype.includes("sell")) {
orderType = OrderType.LimitSell;
} else if (ltype.includes("stop") && ltype.includes("buy")) {
orderType = OrderType.StopBuy;
} else if (ltype.includes("stop") && ltype.includes("sell")) {
orderType = OrderType.StopSell;
} else {
throw helpers.errorMessage(ctx, `Invalid order type: ${type}`);
}
const lpos = pos.toLowerCase();
if (lpos.includes("l")) {
orderPos = PositionType.Long;
} else if (lpos.includes("s")) {
orderPos = PositionType.Short;
} else {
throw helpers.errorMessage(ctx, `Invalid position type: ${pos}`);
}
const params = {
stock: stock,
shares: shares,
price: price,
type: orderType,
pos: orderPos,
};
return cancelOrder(params, ctx);
return cancelOrder({ stock, shares, price, type, pos }, ctx);
},
getOrders: (ctx) => () => {
checkTixApiAccess(ctx);
@@ -277,7 +205,7 @@ export function NetscriptStockMarket(): InternalAPI<TIX> {
throw helpers.errorMessage(ctx, "You must either be in BitNode-8 or have Source-File 8 Level 3.");
}
const orders: StockOrder = {};
const orders: Record<string, StockOrder[]> = {};
const stockMarketOrders = StockMarket.Orders;
for (const symbol of Object.keys(stockMarketOrders)) {