Add dummy function to generate a mock server or player for formulas stuff

This commit is contained in:
Olivier Gagnon 2022-08-23 16:01:34 -04:00
parent 895944aa19
commit 89aa23f4b3
6 changed files with 103 additions and 6 deletions

@ -1,18 +1,19 @@
import { Typography } from "@mui/material";
import React, { useState, useEffect } from "react";
interface baubleProps {
callback: () => boolean;
isConnected: () => boolean;
}
export const ConnectionBauble = (props: baubleProps): React.ReactElement => {
const [connection, setConnection] = useState(props.callback());
const [connection, setConnection] = useState(props.isConnected());
useEffect(() => {
const timer = setInterval(() => {
setConnection(props.callback());
setConnection(props.isConnected());
}, 1000);
return () => clearInterval(timer);
});
return <div className="ConnectionBauble">{connection ? "Connected" : "Disconnected"}</div>;
return <Typography>{connection ? "Connected" : "Disconnected"}</Typography>;
};

@ -389,7 +389,7 @@ export const CurrentOptionsPage = (props: IProps): React.ReactElement => {
endAdornment: (
<Box>
<Button onClick={newRemoteFileApiConnection}>Reconnect</Button>
<ConnectionBauble callback={isRemoteFileApiConnectionLive} />
<ConnectionBauble isConnected={isRemoteFileApiConnectionLive} />
</Box>
),
}}

@ -500,12 +500,39 @@ function player(ctx: NetscriptContext, p: unknown): IPlayer {
}
function server(ctx: NetscriptContext, s: unknown): Server {
if (!roughlyIs(new Server(), s)) throw makeRuntimeErrorMsg(ctx, `server should be a Server.`);
const fakeServer = {
cpuCores: undefined,
ftpPortOpen: undefined,
hasAdminRights: undefined,
hostname: undefined,
httpPortOpen: undefined,
ip: undefined,
isConnectedTo: undefined,
maxRam: undefined,
organizationName: undefined,
ramUsed: undefined,
smtpPortOpen: undefined,
sqlPortOpen: undefined,
sshPortOpen: undefined,
purchasedByPlayer: undefined,
backdoorInstalled: undefined,
baseDifficulty: undefined,
hackDifficulty: undefined,
minDifficulty: undefined,
moneyAvailable: undefined,
moneyMax: undefined,
numOpenPortsRequired: undefined,
openPortCount: undefined,
requiredHackingSkill: undefined,
serverGrowth: undefined,
};
if (!roughlyIs(fakeServer, s)) throw makeRuntimeErrorMsg(ctx, `server should be a Server.`);
return s as Server;
}
function roughlyIs(expect: object, actual: unknown): boolean {
if (typeof actual !== "object" || actual == null) return false;
const expects = Object.keys(expect);
const actuals = Object.keys(actual);
for (const expect of expects)

@ -546,6 +546,8 @@ const SourceRamCosts = {
},
formulas: {
mockServer: 0,
mockPlayer: 0,
reputation: {
calculateFavorToRep: 0,
calculateRepToFavor: 0,

@ -52,6 +52,9 @@ import { LocationName } from "../Locations/data/LocationNames";
import { calculateFactionExp, calculateFactionRep } from "../Work/formulas/Faction";
import { FactionWorkType } from "../Work/data/FactionWorkType";
import { Player as INetscriptPlayer, Server as IServerDef } from "../ScriptEditor/NetscriptDefinitions";
import { defaultMultipliers } from "../PersonObjects/Multipliers";
export function NetscriptFormulas(): InternalAPI<IFormulas> {
const checkFormulasAccess = function (ctx: NetscriptContext): void {
if (!player.hasProgram(Programs.Formulas.name)) {
@ -59,6 +62,68 @@ export function NetscriptFormulas(): InternalAPI<IFormulas> {
}
};
return {
mockServer: () => (): IServerDef => ({
cpuCores: 0,
ftpPortOpen: false,
hasAdminRights: false,
hostname: "",
httpPortOpen: false,
ip: "",
isConnectedTo: false,
maxRam: 0,
organizationName: "",
ramUsed: 0,
smtpPortOpen: false,
sqlPortOpen: false,
sshPortOpen: false,
purchasedByPlayer: false,
backdoorInstalled: false,
baseDifficulty: 0,
hackDifficulty: 0,
minDifficulty: 0,
moneyAvailable: 0,
moneyMax: 0,
numOpenPortsRequired: 0,
openPortCount: 0,
requiredHackingSkill: 0,
serverGrowth: 0,
}),
mockPlayer: () => (): INetscriptPlayer => ({
hp: { current: 0, max: 0 },
skills: {
hacking: 0,
strength: 0,
defense: 0,
dexterity: 0,
agility: 0,
charisma: 0,
intelligence: 0,
},
exp: {
hacking: 0,
strength: 0,
defense: 0,
dexterity: 0,
agility: 0,
charisma: 0,
intelligence: 0,
},
mults: defaultMultipliers(),
numPeopleKilled: 0,
money: 0,
city: "",
location: "",
bitNodeN: 0,
totalPlaytime: 0,
playtimeSinceLastAug: 0,
playtimeSinceLastBitnode: 0,
jobs: {},
factions: [],
tor: false,
hasCorporation: false,
inBladeburner: false,
entropy: 0,
}),
reputation: {
calculateFavorToRep:
(ctx: NetscriptContext) =>

@ -4149,6 +4149,8 @@ interface GangFormulas {
* @public
*/
export interface Formulas {
mockServer(): Server;
mockPlayer(): Player;
/** Reputation formulas */
reputation: ReputationFormulas;
/** Skills formulas */