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
+10 -8
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
}
}
}
+29 -14
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`,
);
}
+1 -7
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");
}