mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-20 00:04:22 +02:00
[refactor, chore] Including TSLint; fixing most things.
As it currently stands, bringing in TSLint with *all* rules enabled by default and override each as we go. This means that as we upgrade and bring in new rules we might start seeing new rules that the code violates. Because all the rules are enabled by default, the choice to explicitly disable/relax rules can be discussed as part of PRs. This also brought in strict type checking. This means we shouldn't be relying on truthy/falsey value coercion. It also means that we will likely need to create some type definition files or port certain files over. That...will be a bit tedious, but will make the codebase better as a whole.
This commit is contained in:
+93
-49
@@ -1,71 +1,109 @@
|
||||
import {dialogBoxCreate} from "../utils/DialogBox";
|
||||
import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver";
|
||||
import { dialogBoxCreate } from "../utils/DialogBox";
|
||||
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../utils/JSONReviver";
|
||||
|
||||
/**
|
||||
* Represents a plain text file that is typically stored on a server.
|
||||
*/
|
||||
export class TextFile {
|
||||
/**
|
||||
* Initiatizes a TextFile from a JSON save state.
|
||||
*/
|
||||
static fromJSON(value: any): TextFile {
|
||||
return Generic_fromJSON(TextFile, value.data);
|
||||
}
|
||||
|
||||
/**
|
||||
* The full file name.
|
||||
*/
|
||||
fn: string;
|
||||
|
||||
/**
|
||||
* The content of the file.
|
||||
*/
|
||||
text: string;
|
||||
|
||||
constructor(fn = "", txt = "") {
|
||||
this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, '');
|
||||
constructor(fn: string = "", txt: string = "") {
|
||||
this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, "");
|
||||
this.text = txt;
|
||||
}
|
||||
|
||||
append(txt: string) {
|
||||
/**
|
||||
* Concatenates the raw values to the end of current content.
|
||||
*/
|
||||
append(txt: string): void {
|
||||
this.text += txt;
|
||||
}
|
||||
|
||||
write(txt: string) {
|
||||
this.text = txt;
|
||||
}
|
||||
|
||||
read() {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
show() {
|
||||
dialogBoxCreate(`${this.fn}<br /><br />${this.text}`, true);
|
||||
}
|
||||
|
||||
download() {
|
||||
const filename = this.fn;
|
||||
const file = new Blob([ this.text ], { type: 'text/plain' });
|
||||
/**
|
||||
* Serves the file to the user as a downloadable resource through the browser.
|
||||
*/
|
||||
download(): void {
|
||||
const filename: string = this.fn;
|
||||
const file: Blob = new Blob([ this.text ], { type: "text/plain" });
|
||||
/* tslint:disable-next-line:strict-boolean-expressions */
|
||||
if (window.navigator.msSaveOrOpenBlob) {
|
||||
// IE10+
|
||||
window.navigator.msSaveOrOpenBlob(file, filename);
|
||||
} else {
|
||||
// Others
|
||||
const a = document.createElement("a");
|
||||
const url = URL.createObjectURL(file);
|
||||
const a: HTMLAnchorElement = document.createElement("a");
|
||||
const url: string = URL.createObjectURL(file);
|
||||
a.href = url;
|
||||
a.download = this.fn;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(a);
|
||||
window.URL.revokeObjectURL(url);
|
||||
}, 0);
|
||||
setTimeout(
|
||||
() => {
|
||||
document.body.removeChild(a);
|
||||
window.URL.revokeObjectURL(url);
|
||||
},
|
||||
0);
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
/**
|
||||
* Retrieve the content of the file.
|
||||
*/
|
||||
read(): string {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the content to the user via the game's dialog box.
|
||||
*/
|
||||
show(): void {
|
||||
dialogBoxCreate(`${this.fn}<br /><br />${this.text}`, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the current file to a JSON save state.
|
||||
*/
|
||||
toJSON(): any {
|
||||
return Generic_toJSON("TextFile", this);
|
||||
}
|
||||
|
||||
static fromJSON(value: any) {
|
||||
return Generic_fromJSON(TextFile, value.data);
|
||||
/**
|
||||
* Replaces the current content with the text provided.
|
||||
*/
|
||||
write(txt: string): void {
|
||||
this.text = txt;
|
||||
}
|
||||
}
|
||||
|
||||
Reviver.constructors.TextFile = TextFile;
|
||||
|
||||
export function getTextFile(fn: string, server: any): string | null {
|
||||
if (!fn.endsWith(".txt")) {
|
||||
fn += ".txt";
|
||||
}
|
||||
/**
|
||||
* Retrieve the file object for the filename on the specified server.
|
||||
* @param fn The file name to look for
|
||||
* @param server The server object to look in
|
||||
* @returns The file object, or null if it couldn't find it.
|
||||
*/
|
||||
export function getTextFile(fn: string, server: any): TextFile | null {
|
||||
const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
|
||||
|
||||
for (let i = 0; i < server.textFiles.length; i++) {
|
||||
if (server.textFiles[i].fn === fn) {
|
||||
return server.textFiles[i];
|
||||
for (const file of (server.textFiles as TextFile[])) {
|
||||
if (file.fn === filename) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,30 +112,36 @@ export function getTextFile(fn: string, server: any): string | null {
|
||||
|
||||
/**
|
||||
* Creates a TextFile on the target server.
|
||||
* @param {string} fn The file name to create.
|
||||
* @param {string} txt The contents of the file.
|
||||
* @param {*} server The server that the file should be created on.
|
||||
* @returns {TextFile} The instance of the file.
|
||||
* @param fn The file name to create.
|
||||
* @param txt The contents of the file.
|
||||
* @param server The server that the file should be created on.
|
||||
* @returns The instance of the file.
|
||||
*/
|
||||
export function createTextFile(fn: string, txt: string, server: any): TextFile {
|
||||
export function createTextFile(fn: string, txt: string, server: any): TextFile | undefined {
|
||||
if (getTextFile(fn, server) !== null) {
|
||||
// This should probably be a `throw`...
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.error(`A file named "${fn}" already exists on server ${server.hostname}.`);
|
||||
return;
|
||||
|
||||
return undefined;
|
||||
}
|
||||
const file = new TextFile(fn, txt);
|
||||
const file: TextFile = new TextFile(fn, txt);
|
||||
server.textFiles.push(file);
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
function deleteTextFile(fn, server) {
|
||||
if (!fn.endsWith(".txt")) {
|
||||
fn += ".txt";
|
||||
}
|
||||
for (var i = 0; i < server.textFiles.length; ++i) {
|
||||
if (server.textFiles[i].fn === fn) {
|
||||
/* tslint:disable-next-line:no-unused-variable */
|
||||
function deleteTextFile(fn: string, server: any): boolean {
|
||||
const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
|
||||
/* tslint:disable-next-line:typedef */
|
||||
for (let i = 0; i < server.textFiles.length; ++i) {
|
||||
if (server.textFiles[i].fn === filename) {
|
||||
server.textFiles.splice(i, 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user