Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Grow log shows invalid values in edge cases #1872

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/NetscriptFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,26 @@ export const ns: InternalAPI<NSFull> = {
if (host === null) {
throw helpers.errorMessage(ctx, `Cannot find host of WorkerScript. Hostname: ${ctx.workerScript.hostname}.`);
}
const moneyBefore = server.moneyAvailable <= 0 ? 1 : server.moneyAvailable;
processSingleServerGrowth(server, threads, host.cpuCores);
const moneyBefore = server.moneyAvailable;
const growth = processSingleServerGrowth(server, threads, host.cpuCores);
const growthPercentForLogging = growth !== 0 ? growth - 1 : 0;
const moneyAfter = server.moneyAvailable;
ctx.workerScript.scriptRef.recordGrow(server.hostname, threads);
const expGain = calculateHackingExpGain(server, Player) * threads;
const logGrowPercent = moneyAfter / moneyBefore - 1;
helpers.log(
ctx,
() =>
`Available money on '${server.hostname}' grown by ${formatPercent(logGrowPercent, 6)}. Gained ${formatExp(
expGain,
)} hacking exp (t=${formatThreads(threads)}).`,
`Available money on '${server.hostname}' grown by ${formatPercent(
growthPercentForLogging,
6,
)}. Gained ${formatExp(expGain)} hacking exp (t=${formatThreads(threads)}).`,
);
ctx.workerScript.scriptRef.onlineExpGained += expGain;
Player.gainHackingExp(expGain);
if (stock) {
influenceStockThroughServerGrow(server, moneyAfter - moneyBefore);
}
return Promise.resolve(moneyAfter / moneyBefore);
return Promise.resolve(growth);
});
},
growthAnalyze:
Expand Down
19 changes: 12 additions & 7 deletions src/Script/ScriptHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,24 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript):
if (runningScript.dataMap[hostname][2] == 0 || runningScript.dataMap[hostname][2] == null) {
continue;
}
const serv = GetServer(hostname);
if (serv == null) {
const server = GetServer(hostname);
if (server == null) {
continue;
}
const timesGrown = Math.round(
((0.5 * runningScript.dataMap[hostname][2]) / runningScript.onlineRunningTime) * timePassed,
);
runningScript.log(`Called on ${serv.hostname} ${timesGrown} times while offline`);
runningScript.log(`Called on ${server.hostname} ${timesGrown} times while offline`);
const host = GetServer(runningScript.server);
if (host === null) throw new Error("getServer of null key?");
if (!(serv instanceof Server)) throw new Error("trying to grow a non-normal server");
const growth = processSingleServerGrowth(serv, timesGrown, host.cpuCores);
runningScript.log(`'${serv.hostname}' grown by ${formatPercent(growth - 1, 6)} while offline`);
if (host === null) {
throw new Error("getServer of null key?");
}
if (!(server instanceof Server)) {
throw new Error("trying to grow a non-normal server");
}
const growth = processSingleServerGrowth(server, timesGrown, host.cpuCores);
const growthPercentForLogging = growth !== 0 ? growth - 1 : 0;
runningScript.log(`'${server.hostname}' grown by ${formatPercent(growthPercentForLogging, 6)} while offline`);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Server/ServerHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ export function processSingleServerGrowth(server: Server, threads: number, cores
usedCycles = Math.min(Math.max(0, Math.ceil(usedCycles)), threads);
server.fortify(2 * ServerConstants.ServerFortifyAmount * usedCycles);
}
// Prevent returning NaN. This happens when server.moneyMax is 0.
if (server.moneyAvailable === 0 && oldMoneyAvailable === 0) {
return 0;
catloversg marked this conversation as resolved.
Show resolved Hide resolved
}
// Prevent returning Infinity. If server's money before growing is 0, we "pretend" that it's 1.
if (oldMoneyAvailable === 0) {
return server.moneyAvailable;
}
return server.moneyAvailable / oldMoneyAvailable;
}

Expand Down
10 changes: 6 additions & 4 deletions src/Terminal/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,16 @@ export class Terminal {
if (!(server instanceof Server)) throw new Error("server should be normal server");
const expGain = calculateHackingExpGain(server, Player);
const oldSec = server.hackDifficulty;
const growth = processSingleServerGrowth(server, 25, server.cpuCores) - 1;
const growth = processSingleServerGrowth(server, 25, server.cpuCores);
const growthPercentForLogging = growth !== 0 ? growth - 1 : 0;
const newSec = server.hackDifficulty;

Player.gainHackingExp(expGain);
this.print(
`Available money on '${server.hostname}' grown by ${formatPercent(growth, 6)}. Gained ${formatExp(
expGain,
)} hacking exp.`,
`Available money on '${server.hostname}' grown by ${formatPercent(
growthPercentForLogging,
6,
)}. Gained ${formatExp(expGain)} hacking exp.`,
);
this.print(
`Security increased on '${server.hostname}' from ${formatSecurity(oldSec)} to ${formatSecurity(newSec)}`,
Expand Down
Loading