[refactor] Converted TextFile to TypeScript

This commit is contained in:
Steven Evans 2018-06-14 09:11:28 -04:00
parent 3e1ec388e0
commit 67cee8642b
6 changed files with 107 additions and 95 deletions

@ -36,7 +36,7 @@ import {StockMarket, StockSymbols, SymbolToStockMap, initStockSymbols,
Stock, shortStock, sellShort, OrderTypes, Stock, shortStock, sellShort, OrderTypes,
PositionTypes, placeOrder, cancelOrder} from "./StockMarket.js"; PositionTypes, placeOrder, cancelOrder} from "./StockMarket.js";
import {post} from "./Terminal.js"; import {post} from "./Terminal.js";
import {TextFile, getTextFile, createTextFile} from "./TextFile.js"; import {TextFile, getTextFile, createTextFile} from "./TextFile";
import {WorkerScript, workerScripts, import {WorkerScript, workerScripts,
killWorkerScript, NetscriptPorts} from "./NetscriptWorker.js"; killWorkerScript, NetscriptPorts} from "./NetscriptWorker.js";

@ -29,7 +29,7 @@ import {Player} from "./Player.js";
import {AllServers, processSingleServerGrowth} from "./Server.js"; import {AllServers, processSingleServerGrowth} from "./Server.js";
import {Settings} from "./Settings.js"; import {Settings} from "./Settings.js";
import {post, Terminal} from "./Terminal.js"; import {post, Terminal} from "./Terminal.js";
import {TextFile} from "./TextFile.js"; import {TextFile} from "./TextFile";
import {parse, Node} from "../utils/acorn.js"; import {parse, Node} from "../utils/acorn.js";
import {dialogBoxCreate} from "../utils/DialogBox.js"; import {dialogBoxCreate} from "../utils/DialogBox.js";

@ -29,8 +29,7 @@ import {AllServers, GetServerByHostname,
import {Settings} from "./Settings.js"; import {Settings} from "./Settings.js";
import {SpecialServerIps, import {SpecialServerIps,
SpecialServerNames} from "./SpecialServerIps.js"; SpecialServerNames} from "./SpecialServerIps.js";
import {TextFile, getTextFile, import {TextFile, getTextFile} from "./TextFile";
createTextFile} from "./TextFile.js";
import {containsAllStrings, longestCommonStart, import {containsAllStrings, longestCommonStart,
formatNumber, isString} from "../utils/StringHelperFunctions.js"; formatNumber, isString} from "../utils/StringHelperFunctions.js";

@ -1,90 +0,0 @@
import {Server} from "./Server.js";
import {dialogBoxCreate} from "../utils/DialogBox.js";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js";
function TextFile(fn="", txt="") {
this.fn = fn.endsWith(".txt") ? fn : fn + ".txt";
this.fn = this.fn.replace(/\s+/g, '');
this.text = String(txt);
}
TextFile.prototype.append = function(txt) {
this.text += String(txt);
}
TextFile.prototype.write = function(txt) {
this.text = String(txt);
}
TextFile.prototype.read = function() {
return this.txt;
}
TextFile.prototype.show = function() {
dialogBoxCreate(this.fn + "<br><br>" + this.text, true);
}
TextFile.prototype.download = function() {
var filename = this.fn;
var file = new Blob([this.text], {type: 'text/plain'});
if (window.navigator.msSaveOrOpenBlob) {// IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
} else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = this.fn;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
TextFile.prototype.toJSON = function() {
return Generic_toJSON("TextFile", this);
}
TextFile.fromJSON = function(value) {
return Generic_fromJSON(TextFile, value.data);
}
Reviver.constructors.TextFile = TextFile;
function getTextFile(fn, server) {
if (!fn.endsWith(".txt")) {fn += ".txt";}
for (var i = 0; i < server.textFiles.length; ++i) {
if (server.textFiles[i].fn === fn) {
return server.textFiles[i];
}
}
return null;
}
//Returns the TextFile object that was just created
function createTextFile(fn, txt, server) {
if (getTextFile(fn, server) !== null) {
console.log("ERROR: createTextFile failed because the specified " +
"server already has a text file with the same fn");
return;
}
var file = 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) {
server.textFiles.splice(i, 1);
return true;
}
}
return false;
}
export {TextFile, getTextFile, createTextFile};

103
src/TextFile.ts Normal file

@ -0,0 +1,103 @@
import {dialogBoxCreate} from "../utils/DialogBox";
import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver";
export class TextFile {
fn: string;
text: string;
constructor(fn = "", txt = "") {
this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, '');
this.text = txt;
}
append(txt: string) {
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' });
if (window.navigator.msSaveOrOpenBlob) {
// IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
} else {
// Others
const a = document.createElement("a");
const url = 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);
}
}
toJSON() {
return Generic_toJSON("TextFile", this);
}
static fromJSON(value: any) {
return Generic_fromJSON(TextFile, value.data);
}
}
Reviver.constructors.TextFile = TextFile;
export function getTextFile(fn: string, server: any): string | null {
if (!fn.endsWith(".txt")) {
fn += ".txt";
}
for (let i = 0; i < server.textFiles.length; i++) {
if (server.textFiles[i].fn === fn) {
return server.textFiles[i];
}
}
return 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.
*/
export function createTextFile(fn: string, txt: string, server: any): TextFile {
if (getTextFile(fn, server) !== null) {
console.error(`A file named "${fn}" already exists on server ${server.hostname}.`);
return;
}
const file = 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) {
server.textFiles.splice(i, 1);
return true;
}
}
return false;
}

@ -1,7 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"target": "es5", "target": "es6",
"sourceMap": true "sourceMap": true
}, },
"exclude": [ "exclude": [