mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-22 23:53:48 +01:00
DRY: Consolidated the code for retrieving a port so it will give consistent errors.
This commit is contained in:
parent
d99b03d12e
commit
7c846abb65
@ -70,6 +70,7 @@ import { NetscriptCodingContract } from "./NetscriptFunctions/CodingContract";
|
|||||||
import { NetscriptCorporation } from "./NetscriptFunctions/Corporation";
|
import { NetscriptCorporation } from "./NetscriptFunctions/Corporation";
|
||||||
import { NetscriptFormulas } from "./NetscriptFunctions/Formulas";
|
import { NetscriptFormulas } from "./NetscriptFunctions/Formulas";
|
||||||
import { NetscriptStockMarket } from "./NetscriptFunctions/StockMarket";
|
import { NetscriptStockMarket } from "./NetscriptFunctions/StockMarket";
|
||||||
|
import { IPort } from "./NetscriptPort";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
NS as INS,
|
NS as INS,
|
||||||
@ -442,6 +443,26 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
|
|||||||
getServer: safeGetServer,
|
getServer: safeGetServer,
|
||||||
checkSingularityAccess: checkSingularityAccess,
|
checkSingularityAccess: checkSingularityAccess,
|
||||||
hack: hack,
|
hack: hack,
|
||||||
|
getValidPort: (funcName:string, port: any): IPort => {
|
||||||
|
if (isNaN(port)) {
|
||||||
|
throw makeRuntimeErrorMsg(
|
||||||
|
funcName,
|
||||||
|
`Invalid argument. Must be a port number between 1 and ${CONSTANTS.NumNetscriptPorts}, is ${port}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
port = Math.round(port);
|
||||||
|
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
||||||
|
throw makeRuntimeErrorMsg(
|
||||||
|
funcName,
|
||||||
|
`Trying to write to invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const iport = NetscriptPorts[port - 1];
|
||||||
|
if (iport == null || !(iport instanceof Object)) {
|
||||||
|
throw makeRuntimeErrorMsg(funcName, `Could not find port: ${port}. This is a bug. Report to dev.`);
|
||||||
|
}
|
||||||
|
return iport;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const gang = NetscriptGang(Player, workerScript, helper);
|
const gang = NetscriptGang(Player, workerScript, helper);
|
||||||
@ -1737,25 +1758,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
|
|||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
writePort: function (port: any, data: any = ""): any {
|
writePort: function (port: any, data: any = ""): any {
|
||||||
// Write to port
|
const iport = helper.getValidPort("writePort", port);
|
||||||
// Port 1-10
|
|
||||||
if (typeof data !== "string" && typeof data !== "number") {
|
|
||||||
throw makeRuntimeErrorMsg(
|
|
||||||
"writePort",
|
|
||||||
`Trying to write invalid data to a port: only strings and numbers are valid.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
port = Math.round(port);
|
|
||||||
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
||||||
throw makeRuntimeErrorMsg(
|
|
||||||
"writePort",
|
|
||||||
`Trying to write to invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const iport = NetscriptPorts[port - 1];
|
|
||||||
if (iport == null || !(iport instanceof Object)) {
|
|
||||||
throw makeRuntimeErrorMsg("writePort", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
|
||||||
}
|
|
||||||
return Promise.resolve(iport.write(data));
|
return Promise.resolve(iport.write(data));
|
||||||
},
|
},
|
||||||
write: function (port: any, data: any = "", mode: any = "a"): any {
|
write: function (port: any, data: any = "", mode: any = "a"): any {
|
||||||
@ -1832,18 +1835,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
|
|||||||
},
|
},
|
||||||
readPort: function (port: any): any {
|
readPort: function (port: any): any {
|
||||||
// Read from port
|
// Read from port
|
||||||
// Port 1-10
|
const iport = helper.getValidPort("readPort", port);
|
||||||
port = Math.round(port);
|
|
||||||
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
||||||
throw makeRuntimeErrorMsg(
|
|
||||||
"readPort",
|
|
||||||
`Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const iport = NetscriptPorts[port - 1];
|
|
||||||
if (iport == null || !(iport instanceof Object)) {
|
|
||||||
throw makeRuntimeErrorMsg("readPort", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
|
||||||
}
|
|
||||||
const x = iport.read();
|
const x = iport.read();
|
||||||
return x;
|
return x;
|
||||||
},
|
},
|
||||||
@ -1878,23 +1870,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
|
|||||||
},
|
},
|
||||||
peek: function (port: any): any {
|
peek: function (port: any): any {
|
||||||
updateDynamicRam("peek", getRamCost("peek"));
|
updateDynamicRam("peek", getRamCost("peek"));
|
||||||
if (isNaN(port)) {
|
const iport = helper.getValidPort("peek", port);
|
||||||
throw makeRuntimeErrorMsg(
|
|
||||||
"peek",
|
|
||||||
`Invalid argument. Must be a port number between 1 and ${CONSTANTS.NumNetscriptPorts}, is ${port}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
port = Math.round(port);
|
|
||||||
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
||||||
throw makeRuntimeErrorMsg(
|
|
||||||
"peek",
|
|
||||||
`Invalid argument. Must be a port number between 1 and ${CONSTANTS.NumNetscriptPorts}, is ${port}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const iport = NetscriptPorts[port - 1];
|
|
||||||
if (iport == null || !(iport instanceof Object)) {
|
|
||||||
throw makeRuntimeErrorMsg("peek", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
|
||||||
}
|
|
||||||
const x = iport.peek();
|
const x = iport.peek();
|
||||||
return x;
|
return x;
|
||||||
},
|
},
|
||||||
@ -1918,38 +1894,12 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
|
|||||||
},
|
},
|
||||||
clearPort: function (port: any): any {
|
clearPort: function (port: any): any {
|
||||||
// Clear port
|
// Clear port
|
||||||
port = Math.round(port);
|
const iport = helper.getValidPort("clearPort", port);
|
||||||
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
||||||
throw makeRuntimeErrorMsg(
|
|
||||||
"clear",
|
|
||||||
`Trying to clear invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const iport = NetscriptPorts[port - 1];
|
|
||||||
if (iport == null || !(iport instanceof Object)) {
|
|
||||||
throw makeRuntimeErrorMsg("clear", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
|
||||||
}
|
|
||||||
return iport.clear();
|
return iport.clear();
|
||||||
},
|
},
|
||||||
getPortHandle: function (port: any): any {
|
getPortHandle: function (port: any): any {
|
||||||
updateDynamicRam("getPortHandle", getRamCost("getPortHandle"));
|
updateDynamicRam("getPortHandle", getRamCost("getPortHandle"));
|
||||||
if (isNaN(port)) {
|
const iport = helper.getValidPort("getPortHandle", port);
|
||||||
throw makeRuntimeErrorMsg(
|
|
||||||
"getPortHandle",
|
|
||||||
`Invalid port: ${port} Must be an integer between 1 and ${CONSTANTS.NumNetscriptPorts}.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
port = Math.round(port);
|
|
||||||
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
||||||
throw makeRuntimeErrorMsg(
|
|
||||||
"getPortHandle",
|
|
||||||
`Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const iport = NetscriptPorts[port - 1];
|
|
||||||
if (iport == null || !(iport instanceof Object)) {
|
|
||||||
throw makeRuntimeErrorMsg("getPortHandle", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
|
||||||
}
|
|
||||||
return iport;
|
return iport;
|
||||||
},
|
},
|
||||||
rm: function (fn: any, hostname: any): any {
|
rm: function (fn: any, hostname: any): any {
|
||||||
|
Loading…
Reference in New Issue
Block a user