From 51edac414ed819998d1486a60e6ab09bb73cb82d Mon Sep 17 00:00:00 2001 From: daan flore Date: Mon, 10 Jan 2022 22:31:30 +0100 Subject: [PATCH 1/3] Adding IPort interface and correcting return value of getPortHandle in NetscriptDefinitions file --- src/ScriptEditor/NetscriptDefinitions.d.ts | 32 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 058a46a42..b5d826aeb 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -1,7 +1,7 @@ /** * @public */ -interface Player { + interface Player { hacking: number; hp: number; max_hp: number; @@ -109,6 +109,32 @@ interface RunningScript { threads: number; } +/** + * Interface of a netscript port + * @public + */ + export interface IPort { + /** write data to the port and removes and returns first element if full */ + write: (value: any) => any; + /** add data to port if not full. + * @returns true if added and false if full and not added */ + tryWrite: (value: any) => boolean; + /** reads and removes first element from port + * if no data in port returns "NULL PORT DATA" + */ + read: () => any; + /** reads first element without removing it from port + * if no data in port returns "NULL PORT DATA" + */ + peek: () => any; + /** check if port is full */ + full: () => boolean; + /** check if port is empty */ + empty: () => boolean; + /** removes all data from port */ + clear: () => void; +} + /** * Data representing the internal values of a crime. * @public @@ -5404,12 +5430,12 @@ export interface NS extends Singularity { * Get a handle to a Netscript Port. * * WARNING: Port Handles only work in NetscriptJS (Netscript 2.0). They will not work in Netscript 1.0. - * + * * @see https://bitburner.readthedocs.io/en/latest/netscript/netscriptmisc.html#netscript-ports * @param port - Port number. Must be an integer between 1 and 20. * @returns Data in the specified port. */ - getPortHandle(port: number): any[]; + getPortHandle(port: number): IPort; /** * Delete a file. From 66fbec536590e6acda29d0ab82784e865289719f Mon Sep 17 00:00:00 2001 From: daan flore Date: Mon, 10 Jan 2022 22:33:50 +0100 Subject: [PATCH 2/3] Cleanup of code --- src/ScriptEditor/NetscriptDefinitions.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index b5d826aeb..99068487a 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -1,7 +1,7 @@ /** * @public */ - interface Player { +interface Player { hacking: number; hp: number; max_hp: number; @@ -113,7 +113,7 @@ interface RunningScript { * Interface of a netscript port * @public */ - export interface IPort { +export interface IPort { /** write data to the port and removes and returns first element if full */ write: (value: any) => any; /** add data to port if not full. @@ -5430,7 +5430,7 @@ export interface NS extends Singularity { * Get a handle to a Netscript Port. * * WARNING: Port Handles only work in NetscriptJS (Netscript 2.0). They will not work in Netscript 1.0. - * + * * @see https://bitburner.readthedocs.io/en/latest/netscript/netscriptmisc.html#netscript-ports * @param port - Port number. Must be an integer between 1 and 20. * @returns Data in the specified port. From 0d006d0a64f04a2a35c614f4c1321bacfc042f6a Mon Sep 17 00:00:00 2001 From: daan flore Date: Mon, 10 Jan 2022 22:49:42 +0100 Subject: [PATCH 3/3] Adding correct type to getPortHandle function --- src/NetscriptFunctions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 7e1df5207..ea97083a9 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1901,7 +1901,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { const iport = helper.getValidPort("clearPort", port); return iport.clear(); }, - getPortHandle: function (port: any): any { + getPortHandle: function (port: any): IPort { updateDynamicRam("getPortHandle", getRamCost(Player, "getPortHandle")); const iport = helper.getValidPort("getPortHandle", port); return iport;