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

View File

@ -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;

View File

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

View File

@ -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() {