MISC: Standardize behavior of connect CLI and Singularity API (#1933)

This commit is contained in:
catloversg
2025-02-01 02:20:17 +07:00
committed by GitHub
parent 686bfe5aef
commit a4217b448a
5 changed files with 120 additions and 54 deletions

View File

@@ -44,7 +44,6 @@ import { findEnumMember } from "../utils/helpers/enum";
import { Engine } from "../engine";
import { getEnumHelper } from "../utils/EnumHelper";
import { ScriptFilePath, resolveScriptFilePath } from "../Paths/ScriptFilePath";
import { root } from "../Paths/Directory";
import { getRecordEntries } from "../Types/Record";
import { JobTracks } from "../Company/data/JobTracks";
import { ServerConstants } from "../Server/data/Constants";
@@ -52,6 +51,7 @@ import { blackOpsArray } from "../Bladeburner/data/BlackOperations";
import { calculateEffectiveRequiredReputation } from "../Company/utils";
import { calculateFavorAfterResetting } from "../Faction/formulas/favor";
import { validBitNodes } from "../BitNode/BitNodeUtils";
import { exceptionAlert } from "../utils/helpers/exceptionAlert";
export function NetscriptSingularity(): InternalAPI<ISingularity> {
const runAfterReset = function (cbScript: ScriptFilePath) {
@@ -479,40 +479,34 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
throw helpers.errorMessage(ctx, `Invalid hostname: '${hostname}'`);
}
//Home case
if (hostname === "home") {
Player.getCurrentServer().isConnectedTo = false;
Player.currentServer = Player.getHomeComputer().hostname;
Player.getCurrentServer().isConnectedTo = true;
Terminal.setcwd(root);
return true;
}
//Adjacent server case
// Adjacent servers
const server = Player.getCurrentServer();
for (let i = 0; i < server.serversOnNetwork.length; i++) {
const other = getServerOnNetwork(server, i);
if (other === null) continue;
if (other.hostname == hostname) {
Player.getCurrentServer().isConnectedTo = false;
Player.currentServer = target.hostname;
Player.getCurrentServer().isConnectedTo = true;
Terminal.setcwd(root);
if (other === null) {
exceptionAlert(
new Error(
`${server.serversOnNetwork[i]} is on the network of ${server.hostname}, but we cannot find its data.`,
),
);
return false;
}
if (other.hostname === hostname) {
Terminal.connectToServer(hostname, true);
return true;
}
}
//Backdoor case
const other = GetServer(hostname);
if (other !== null && other instanceof Server && other.backdoorInstalled) {
Player.getCurrentServer().isConnectedTo = false;
Player.currentServer = target.hostname;
Player.getCurrentServer().isConnectedTo = true;
Terminal.setcwd(root);
/**
* Backdoored + owned servers (home, private servers, or hacknet servers). With home computer, purchasedByPlayer
* is true.
*/
if (target.backdoorInstalled || target.purchasedByPlayer) {
Terminal.connectToServer(hostname, true);
return true;
}
//Failure case
// Failure case
return false;
},
manualHack: (ctx) => () => {

View File

@@ -586,19 +586,21 @@ export class Terminal {
printOutput(root);
}
connectToServer(server: string): void {
const serv = GetServer(server);
if (serv == null) {
connectToServer(hostname: string, singularity = false): void {
const server = GetServer(hostname);
if (server === null) {
this.error("Invalid server. Connection failed.");
return;
}
Player.getCurrentServer().isConnectedTo = false;
Player.currentServer = serv.hostname;
Player.getCurrentServer().isConnectedTo = true;
this.print("Connected to " + serv.hostname);
Player.currentServer = hostname;
server.isConnectedTo = true;
this.setcwd(root);
if (Player.getCurrentServer().hostname == "darkweb") {
checkIfConnectedToDarkweb(); // Posts a 'help' message if connecting to dark web
if (!singularity) {
this.print("Connected to " + server.hostname);
if (Player.getCurrentServer().hostname == "darkweb") {
checkIfConnectedToDarkweb(); // Posts a 'help' message if connecting to dark web
}
}
}

View File

@@ -2,6 +2,7 @@ import { Terminal } from "../../Terminal";
import { BaseServer } from "../../Server/BaseServer";
import { getServerOnNetwork } from "../../Server/ServerHelpers";
import { GetServer } from "../../Server/AllServers";
import { exceptionAlert } from "../../utils/helpers/exceptionAlert";
export function connect(args: (string | number | boolean)[], server: BaseServer): void {
// Disconnect from current server in Terminal and connect to new one
@@ -10,27 +11,41 @@ export function connect(args: (string | number | boolean)[], server: BaseServer)
return;
}
const hostname = args[0] + "";
const hostname = String(args[0]);
const target = GetServer(hostname);
if (target === null) {
Terminal.error(`Invalid hostname: '${hostname}'`);
return;
}
// Adjacent servers
for (let i = 0; i < server.serversOnNetwork.length; i++) {
const other = getServerOnNetwork(server, i);
if (other === null) throw new Error(`Server on network should not be null`);
if (other.hostname == hostname) {
if (other === null) {
exceptionAlert(
new Error(
`${server.serversOnNetwork[i]} is on the network of ${server.hostname}, but we cannot find its data.`,
),
);
return;
}
if (other.hostname === hostname) {
Terminal.connectToServer(hostname);
return;
}
}
const other = GetServer(hostname);
if (other !== null) {
if (other.backdoorInstalled || other.purchasedByPlayer) {
Terminal.connectToServer(hostname);
return;
}
Terminal.error(
`Cannot directly connect to ${hostname}. Make sure the server is backdoored or adjacent to your current Server`,
);
} else {
Terminal.error("Host not found");
/**
* Backdoored + owned servers (home, private servers, or hacknet servers). With home computer, purchasedByPlayer is
* true.
*/
if (target.backdoorInstalled || target.purchasedByPlayer) {
Terminal.connectToServer(hostname);
return;
}
Terminal.error(
`Cannot directly connect to ${hostname}. Make sure the server is backdoored or adjacent to your current server`,
);
}

View File

@@ -1,15 +1,9 @@
import { root } from "../../Paths/Directory";
import { Terminal } from "../../Terminal";
import { Player } from "@player";
export function home(args: (string | number | boolean)[]): void {
if (args.length !== 0) {
Terminal.error("Incorrect usage of home command. Usage: home");
return;
}
Player.getCurrentServer().isConnectedTo = false;
Player.currentServer = Player.getHomeComputer().hostname;
Player.getCurrentServer().isConnectedTo = true;
Terminal.print("Connected to home");
Terminal.setcwd(root);
Terminal.connectToServer("home");
}