From 335eab0f1423d3bfba4f4eff0a6440030c80775b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Hoekstra?= Date: Fri, 2 Sep 2022 19:14:56 +0200 Subject: [PATCH] tryWritePort now also uses helper function --- src/NetscriptFunctions.ts | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 91c43a637..d941efd01 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1518,34 +1518,15 @@ const base: InternalAPI = { tryWritePort: (ctx: NetscriptContext) => (_port: unknown, data: unknown = ""): Promise => { - let port = helpers.number(ctx, "port", _port); + const port = helpers.number(ctx, "port", _port); if (typeof data !== "string" && typeof data !== "number") { throw helpers.makeRuntimeErrorMsg( ctx, `Trying to write invalid data to a port: only strings and numbers are valid.`, ); } - if (!isNaN(port)) { - port = Math.round(port); - if (port < 1 || port > CONSTANTS.NumNetscriptPorts) { - throw helpers.makeRuntimeErrorMsg( - ctx, - `Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`, - ); - } - let iport = NetscriptPorts.get(port); - if (iport == null || !(iport instanceof Object)) { - NetscriptPorts.set(port, NetscriptPort()); - } - // Try again. - iport = NetscriptPorts.get(port); - if (iport == null || !(iport instanceof Object)) { - throw helpers.makeRuntimeErrorMsg(ctx, `Could not find port: ${port}. This is a bug. Report to dev.`); - } - return Promise.resolve(iport.tryWrite(data)); - } else { - throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${port}`); - } + const iport = helpers.getValidPort(ctx, port); + return Promise.resolve(iport.tryWrite(data)); }, readPort: (ctx: NetscriptContext) =>