bitburner-src/src/NetscriptPort.ts

62 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-03-14 07:08:24 +01:00
import { Settings } from "./Settings/Settings";
2021-09-24 23:56:30 +02:00
export interface IPort {
2022-07-18 09:09:19 +02:00
write: (value: unknown) => unknown;
tryWrite: (value: unknown) => boolean;
read: () => unknown;
peek: () => unknown;
2021-09-24 23:56:30 +02:00
full: () => boolean;
empty: () => boolean;
clear: () => void;
}
2021-09-22 02:39:25 +02:00
export function NetscriptPort(): IPort {
2022-07-18 09:00:51 +02:00
const data: unknown[] = [];
2021-09-22 02:39:25 +02:00
return {
2022-07-18 09:00:51 +02:00
write: (value: unknown): unknown => {
2021-09-22 02:39:25 +02:00
data.push(value);
if (data.length > Settings.MaxPortCapacity) {
return data.shift();
}
return null;
},
2022-07-18 09:00:51 +02:00
tryWrite: (value: unknown): boolean => {
2021-09-22 02:39:25 +02:00
if (data.length >= Settings.MaxPortCapacity) {
return false;
}
data.push(value);
return true;
},
2022-07-18 09:00:51 +02:00
read: (): unknown => {
2021-09-22 02:39:25 +02:00
if (data.length === 0) {
return "NULL PORT DATA";
}
return data.shift();
},
2022-07-18 09:00:51 +02:00
peek: (): unknown => {
2021-09-22 02:39:25 +02:00
if (data.length === 0) {
return "NULL PORT DATA";
} else {
const foo = data.slice();
return foo[0];
}
},
full: (): boolean => {
return data.length == Settings.MaxPortCapacity;
},
empty: (): boolean => {
return data.length === 0;
},
clear: (): void => {
data.length = 0;
},
};
2021-03-14 07:08:24 +01:00
}