bitburner-src/src/NetscriptPort.ts

52 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-03-14 07:08:24 +01:00
import { Settings } from "./Settings/Settings";
export class NetscriptPort {
2021-09-05 01:09:30 +02:00
data: any[] = [];
2021-03-14 07:08:24 +01:00
2021-09-05 01:09:30 +02:00
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
write(data: any): any {
this.data.push(data);
if (this.data.length > Settings.MaxPortCapacity) {
return this.data.shift();
2021-03-14 07:08:24 +01:00
}
2021-09-05 01:09:30 +02:00
return null;
}
2021-03-14 07:08:24 +01:00
2021-09-05 01:09:30 +02:00
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
tryWrite(data: any): boolean {
if (this.data.length >= Settings.MaxPortCapacity) {
return false;
2021-03-14 07:08:24 +01:00
}
2021-09-05 01:09:30 +02:00
this.data.push(data);
return true;
}
2021-03-14 07:08:24 +01:00
2021-09-05 01:09:30 +02:00
read(): any {
if (this.data.length === 0) {
return "NULL PORT DATA";
2021-03-14 07:08:24 +01:00
}
2021-09-05 01:09:30 +02:00
return this.data.shift();
}
peek(): any {
if (this.data.length === 0) {
return "NULL PORT DATA";
} else {
const foo = this.data.slice();
return foo[0];
2021-03-14 07:08:24 +01:00
}
2021-09-05 01:09:30 +02:00
}
2021-03-14 07:08:24 +01:00
2021-09-05 01:09:30 +02:00
full(): boolean {
return this.data.length == Settings.MaxPortCapacity;
}
2021-03-14 07:08:24 +01:00
2021-09-05 01:09:30 +02:00
empty(): boolean {
return this.data.length === 0;
}
clear(): void {
this.data.length = 0;
}
2021-03-14 07:08:24 +01:00
}