This commit is contained in:
Olivier Gagnon 2021-09-21 20:39:25 -04:00
parent 0c932dd4d1
commit dc518e7032
3 changed files with 52 additions and 48 deletions

@ -2354,7 +2354,7 @@ function NetscriptFunctions(workerScript) {
); );
} }
var port = NetscriptPorts[port - 1]; var port = NetscriptPorts[port - 1];
if (port == null || !(port instanceof NetscriptPort)) { if (port == null || !(port instanceof Object)) {
throw makeRuntimeErrorMsg("write", `Could not find port: ${port}. This is a bug. Report to dev.`); throw makeRuntimeErrorMsg("write", `Could not find port: ${port}. This is a bug. Report to dev.`);
} }
return port.write(data); return port.write(data);
@ -2421,7 +2421,7 @@ function NetscriptFunctions(workerScript) {
); );
} }
var port = NetscriptPorts[port - 1]; var port = NetscriptPorts[port - 1];
if (port == null || !(port instanceof NetscriptPort)) { if (port == null || !(port instanceof Object)) {
throw makeRuntimeErrorMsg("tryWrite", `Could not find port: ${port}. This is a bug. Report to dev.`); throw makeRuntimeErrorMsg("tryWrite", `Could not find port: ${port}. This is a bug. Report to dev.`);
} }
return port.tryWrite(data); return port.tryWrite(data);
@ -2442,7 +2442,7 @@ function NetscriptFunctions(workerScript) {
); );
} }
var port = NetscriptPorts[port - 1]; var port = NetscriptPorts[port - 1];
if (port == null || !(port instanceof NetscriptPort)) { if (port == null || !(port instanceof Object)) {
throw makeRuntimeErrorMsg("read", `Could not find port: ${port}. This is a bug. Report to dev.`); throw makeRuntimeErrorMsg("read", `Could not find port: ${port}. This is a bug. Report to dev.`);
} }
return port.read(); return port.read();
@ -2489,7 +2489,7 @@ function NetscriptFunctions(workerScript) {
); );
} }
var port = NetscriptPorts[port - 1]; var port = NetscriptPorts[port - 1];
if (port == null || !(port instanceof NetscriptPort)) { if (port == null || !(port instanceof Object)) {
throw makeRuntimeErrorMsg("peek", `Could not find port: ${port}. This is a bug. Report to dev.`); throw makeRuntimeErrorMsg("peek", `Could not find port: ${port}. This is a bug. Report to dev.`);
} }
return port.peek(); return port.peek();
@ -2506,7 +2506,7 @@ function NetscriptFunctions(workerScript) {
); );
} }
var port = NetscriptPorts[port - 1]; var port = NetscriptPorts[port - 1];
if (port == null || !(port instanceof NetscriptPort)) { if (port == null || !(port instanceof Object)) {
throw makeRuntimeErrorMsg("clear", `Could not find port: ${port}. This is a bug. Report to dev.`); throw makeRuntimeErrorMsg("clear", `Could not find port: ${port}. This is a bug. Report to dev.`);
} }
return port.clear(); return port.clear();
@ -2542,7 +2542,7 @@ function NetscriptFunctions(workerScript) {
); );
} }
var port = NetscriptPorts[port - 1]; var port = NetscriptPorts[port - 1];
if (port == null || !(port instanceof NetscriptPort)) { if (port == null || !(port instanceof Object)) {
throw makeRuntimeErrorMsg("getPortHandle", `Could not find port: ${port}. This is a bug. Report to dev.`); throw makeRuntimeErrorMsg("getPortHandle", `Could not find port: ${port}. This is a bug. Report to dev.`);
} }
return port; return port;

@ -1,51 +1,55 @@
import { Settings } from "./Settings/Settings"; import { Settings } from "./Settings/Settings";
export class NetscriptPort { interface IPort {}
data: any[] = [];
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export function NetscriptPort(): IPort {
write(data: any): any { const data: any[] = [];
this.data.push(data);
if (this.data.length > Settings.MaxPortCapacity) {
return this.data.shift();
}
return null;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types return {
tryWrite(data: any): boolean { // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
if (this.data.length >= Settings.MaxPortCapacity) { write: (value: any): any => {
return false; data.push(value);
} if (data.length > Settings.MaxPortCapacity) {
this.data.push(data); return data.shift();
return true; }
} return null;
},
read(): any { // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
if (this.data.length === 0) { tryWrite: (value: any): boolean => {
return "NULL PORT DATA"; if (data.length >= Settings.MaxPortCapacity) {
} return false;
return this.data.shift(); }
} data.push(value);
return true;
},
peek(): any { read: (): any => {
if (this.data.length === 0) { if (data.length === 0) {
return "NULL PORT DATA"; return "NULL PORT DATA";
} else { }
const foo = this.data.slice(); return data.shift();
return foo[0]; },
}
}
full(): boolean { peek: (): any => {
return this.data.length == Settings.MaxPortCapacity; if (data.length === 0) {
} return "NULL PORT DATA";
} else {
const foo = data.slice();
return foo[0];
}
},
empty(): boolean { full: (): boolean => {
return this.data.length === 0; return data.length == Settings.MaxPortCapacity;
} },
clear(): void { empty: (): boolean => {
this.data.length = 0; return data.length === 0;
} },
clear: (): void => {
data.length = 0;
},
};
} }

@ -35,7 +35,7 @@ import { simple as walksimple } from "acorn-walk";
// Netscript Ports are instantiated here // Netscript Ports are instantiated here
export const NetscriptPorts = []; export const NetscriptPorts = [];
for (var i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) { for (var i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) {
NetscriptPorts.push(new NetscriptPort()); NetscriptPorts.push(NetscriptPort());
} }
export function prestigeWorkerScripts() { export function prestigeWorkerScripts() {