infiltration API fixes.

This commit is contained in:
Olivier Gagnon 2022-04-22 17:12:14 -04:00
parent 9fdfb77dd4
commit f6e5f719d1
3 changed files with 24 additions and 57 deletions

@ -528,7 +528,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
const sleeve = NetscriptSleeve(Player, workerScript, helper); const sleeve = NetscriptSleeve(Player, workerScript, helper);
const extra = NetscriptExtra(Player, workerScript, helper); const extra = NetscriptExtra(Player, workerScript, helper);
const hacknet = NetscriptHacknet(Player, workerScript, helper); const hacknet = NetscriptHacknet(Player, workerScript, helper);
const infiltration = NetscriptInfiltration(Player, workerScript, helper); const infiltration = wrapAPI(helper, {}, workerScript, NetscriptInfiltration(Player), "infiltration")
.infiltration as unknown as IInfiltration;
const stanek = wrapAPI(helper, {}, workerScript, NetscriptStanek(Player, workerScript, helper), "stanek") const stanek = wrapAPI(helper, {}, workerScript, NetscriptStanek(Player, workerScript, helper), "stanek")
.stanek as unknown as IStanek; .stanek as unknown as IStanek;
const bladeburner = NetscriptBladeburner(Player, workerScript, helper); const bladeburner = NetscriptBladeburner(Player, workerScript, helper);

@ -18,28 +18,21 @@ import {
} from "../Infiltration/formulas/victory"; } from "../Infiltration/formulas/victory";
import { FactionNames } from "../Faction/data/FactionNames"; import { FactionNames } from "../Faction/data/FactionNames";
import { Factions } from "../Faction/Factions"; import { Factions } from "../Faction/Factions";
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { checkEnum } from "../utils/helpers/checkEnum";
import { LocationName } from "../Locations/data/LocationNames";
export function NetscriptInfiltration( export function NetscriptInfiltration(player: IPlayer): InternalAPI<IInfiltration> {
player: IPlayer,
workerScript: WorkerScript,
helper: INetscriptHelper,
): IInfiltration {
const getLocationsWithInfiltrations = Object.values(Locations).filter( const getLocationsWithInfiltrations = Object.values(Locations).filter(
(location: Location) => location.infiltrationData, (location: Location) => location.infiltrationData,
); );
const calculateInfiltrationData = (location: Location | undefined): InfiltrationLocation => { const calculateInfiltrationData = (ctx: NetscriptContext, locationName: string): InfiltrationLocation => {
if (location === undefined) if (!checkEnum(LocationName, locationName)) throw new Error(`Location '${locationName}' does not exists.`);
throw helper.makeRuntimeErrorMsg( const location = Locations[locationName];
`infiltration.calculateReward`, if (location === undefined) throw ctx.makeRuntimeErrorMsg(`Location '${location}' does not exists.`);
"The provided location does not exist or does not provide infiltrations",
);
if (location.infiltrationData === undefined) if (location.infiltrationData === undefined)
throw helper.makeRuntimeErrorMsg( throw ctx.makeRuntimeErrorMsg(`Location '${location}' does not provide infiltrations.`);
`infiltration.calculateReward`,
"The provided location does not exist or does not provide infiltrations",
);
const startingSecurityLevel = location.infiltrationData.startingSecurityLevel; const startingSecurityLevel = location.infiltrationData.startingSecurityLevel;
const difficulty = calculateDifficulty(player, startingSecurityLevel); const difficulty = calculateDifficulty(player, startingSecurityLevel);
const reward = calculateReward(player, startingSecurityLevel); const reward = calculateReward(player, startingSecurityLevel);
@ -49,29 +42,20 @@ export function NetscriptInfiltration(
reward: { reward: {
tradeRep: calculateTradeInformationRepReward(player, reward, maxLevel, difficulty), tradeRep: calculateTradeInformationRepReward(player, reward, maxLevel, difficulty),
sellCash: calculateSellInformationCashReward(player, reward, maxLevel, difficulty), sellCash: calculateSellInformationCashReward(player, reward, maxLevel, difficulty),
infiltratorRep: calculateInfiltratorsRepReward(player, Factions[FactionNames.ShadowsOfAnarchy], difficulty), SoARep: calculateInfiltratorsRepReward(player, Factions[FactionNames.ShadowsOfAnarchy], difficulty),
}, },
difficulty: difficulty, difficulty: difficulty,
}; };
}; };
return { return {
calculateDifficulty: function (locationName: string): number { getPossibleLocations: () => (): string[] => {
const location = getLocationsWithInfiltrations.find((infilLocation) => infilLocation.name === locationName); return getLocationsWithInfiltrations.map((l) => l + "");
helper.updateDynamicRam("calculateDifficulty", getRamCost(player, "infiltration", "calculateDifficulty"));
return calculateInfiltrationData(location).difficulty;
},
calculateRewards: function (locationName: string): InfiltrationReward {
const location = getLocationsWithInfiltrations.find((infilLocation) => infilLocation.name === locationName);
helper.updateDynamicRam("calculateReward", getRamCost(player, "infiltration", "calculateReward"));
return calculateInfiltrationData(location).reward;
},
getLocations: function (): Location[] {
helper.updateDynamicRam("getLocations", getRamCost(player, "infiltration", "getLocations"));
return getLocationsWithInfiltrations;
},
getInfiltrations: function (): InfiltrationLocation[] {
helper.updateDynamicRam("getInfiltrations", getRamCost(player, "infiltration", "getInfiltrations"));
return getLocationsWithInfiltrations.map(calculateInfiltrationData);
}, },
getInfiltration:
(ctx: NetscriptContext) =>
(_location: unknown): InfiltrationLocation => {
const location = ctx.helper.string("location", _location);
return calculateInfiltrationData(ctx, location);
},
}; };
} }

@ -4276,7 +4276,7 @@ interface Stanek {
export interface InfiltrationReward { export interface InfiltrationReward {
tradeRep: number; tradeRep: number;
sellCash: number; sellCash: number;
infiltratorRep: number; SoARep: number;
} }
export interface InfiltrationLocation { export interface InfiltrationLocation {
@ -4290,24 +4290,6 @@ export interface InfiltrationLocation {
* @public * @public
*/ */
interface Infiltration { interface Infiltration {
/**
* Calculate the difficulty of performing an infiltration.
* @remarks
* RAM cost: 2.5 GB
*
* @param locationName - name of the location to check.
* @returns the difficulty.
*/
calculateDifficulty(locationName: string): number;
/**
* Calculate the rewards for trading and selling information for completing an infiltration.
* @remarks
* RAM cost: 2.5 GB
*
* @param locationName - name of the location to check.
* @returns the trade reputation, sell cash and infiltrators rep reward.
*/
calculateRewards(locationName: string): InfiltrationReward;
/** /**
* Get all locations that can be infiltrated. * Get all locations that can be infiltrated.
* @remarks * @remarks
@ -4315,15 +4297,15 @@ interface Infiltration {
* *
* @returns all locations that can be infiltrated. * @returns all locations that can be infiltrated.
*/ */
getLocations(): any[]; getPossibleLocations(): string[];
/** /**
* Get all infiltrations with difficulty, location and rewards. * Get all infiltrations with difficulty, location and rewards.
* @remarks * @remarks
* RAM cost: 15 GB * RAM cost: 15 GB
* *
* @returns all infiltrations with difficulty, location and rewards. * @returns Infiltration data for given location.
*/ */
getInfiltrations(): InfiltrationLocation[]; getInfiltration(location: string): InfiltrationLocation;
} }
/** /**