bitburner-official / bitburner-src

Bitburner source code.
Other
847 stars 274 forks source link

BUGFIX: Block hacking-related actions on player-owned servers #1585

Closed catloversg closed 3 months ago

catloversg commented 3 months ago

Implementations of hack, grow, weaken, and backdoor are inconsistent between CLI and API. In CLI, we check server.purchasedByPlayer, but we do not check it in API. This PR fixes it. As we discussed on Discord, this change is pretty harmless.

On Discord, I talked about this case: await ns.singularity.installBackdoor("hacknet-node-0") shows singularity.installBackdoor: Successfully installed backdoor on 'home'. Just ignore it. I forgot that installBackdoor is a no-parameter API, and we need to connect to the targeted server before using it. We cannot connect to a Hacknet Node (not Hacknet Server), so it won't be a problem.

Although the PR's title says "hacking-related actions", I don't touch nuke and port-opener APIs in this PR. It seems unnecessary to change them. If you think we should change them too, I'll do it.

Test code:

/** @param {NS} ns */
export async function main(ns) {
  ns.tail();
  ns.clearLog();
  await ns.hack("home");
  await ns.grow("home");
  await ns.weaken("home");
  ns.singularity.connect("home"); await ns.singularity.installBackdoor();

  await ns.hack("hacknet-node-0");

  await ns.hack("hacknet-server-0");
  await ns.grow("hacknet-server-0");
  await ns.weaken("hacknet-server-0");
  ns.singularity.connect("hacknet-server-0"); await ns.singularity.installBackdoor();
}