-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathInfiltration.ts
60 lines (58 loc) · 2.67 KB
/
Infiltration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Infiltration as IInfiltration, InfiltrationLocation } from "@nsdefs";
import { Location } from "../Locations/Location";
import { Locations } from "../Locations/Locations";
import { calculateDifficulty, calculateReward } from "../Infiltration/formulas/game";
import {
calculateInfiltratorsRepReward,
calculateSellInformationCashReward,
calculateTradeInformationRepReward,
} from "../Infiltration/formulas/victory";
import { FactionNames } from "../Faction/data/FactionNames";
import { Factions } from "../Faction/Factions";
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { checkEnum } from "../utils/helpers/enum";
import { LocationName } from "../Enums";
import { helpers } from "../Netscript/NetscriptHelpers";
import { filterTruthy } from "../utils/helpers/ArrayHelpers";
export function NetscriptInfiltration(): InternalAPI<IInfiltration> {
const getLocationsWithInfiltrations = Object.values(Locations).filter(
(location: Location) => location.infiltrationData,
);
const calculateInfiltrationData = (ctx: NetscriptContext, locationName: string): InfiltrationLocation => {
if (!checkEnum(LocationName, locationName)) throw new Error(`Location '${locationName}' does not exists.`);
const location = Locations[locationName];
if (location === undefined) throw helpers.makeRuntimeErrorMsg(ctx, `Location '${location}' does not exists.`);
if (location.infiltrationData === undefined)
throw helpers.makeRuntimeErrorMsg(ctx, `Location '${location}' does not provide infiltrations.`);
const startingSecurityLevel = location.infiltrationData.startingSecurityLevel;
const difficulty = calculateDifficulty(startingSecurityLevel);
const reward = calculateReward(startingSecurityLevel);
const maxLevel = location.infiltrationData.maxClearanceLevel;
return {
location: JSON.parse(JSON.stringify(location)),
reward: {
tradeRep: calculateTradeInformationRepReward(reward, maxLevel, startingSecurityLevel),
sellCash: calculateSellInformationCashReward(reward, maxLevel, startingSecurityLevel),
SoARep: calculateInfiltratorsRepReward(Factions[FactionNames.ShadowsOfAnarchy], startingSecurityLevel),
},
difficulty: difficulty,
};
};
return {
getPossibleLocations: () => () => {
return filterTruthy(
getLocationsWithInfiltrations.map((l) => {
if (!l.city) return false;
return {
city: l.city,
name: l.name,
};
}),
);
},
getInfiltration: (ctx) => (_location) => {
const location = helpers.string(ctx, "location", _location);
return calculateInfiltrationData(ctx, location);
},
};
}