fixed a few things

This commit is contained in:
Olivier Gagnon 2021-10-14 02:07:05 -04:00
parent 3f1d4875e7
commit e245c2d3a7
11 changed files with 60 additions and 32 deletions

@ -0,0 +1,15 @@
alert() Netscript Function
============================
.. js:function:: alert(message)
:RAM cost: 0 GB
:param string message: message to display
Spawns an alert box.
Example:
.. code-block:: javascript
alert("Reached $1b");

@ -0,0 +1,17 @@
toast() Netscript Function
============================
.. js:function:: toast(message[, variant])
:RAM cost: 0 GB
:param string message: message to display
:param success|info|warning|error variant: color of the toast
Spawns a toast (those bottom left notifications).
Example:
.. code-block:: javascript
toast("Reached $1b");
toast("Failed to hack home", "error");

@ -13,12 +13,14 @@ import { Reviver } from "../utils/JSONReviver";
function sendMessage(msg: Message, forced = false): void {
msg.recvd = true;
if (forced || !Settings.SuppressMessages) {
showMessage(msg);
showMessage(msg.filename);
}
addMessageToServer(msg, "home");
}
function showMessage(msg: Message): void {
function showMessage(name: string): void {
const msg = Messages[name];
if (!msg) throw new Error("trying to display unexistent message");
const txt =
"Message received from unknown sender: <br><br>" +
"<i>" +
@ -39,12 +41,11 @@ function addMessageToServer(msg: Message, serverHostname: string): void {
}
for (let i = 0; i < server.messages.length; ++i) {
const other = server.messages[i];
if (typeof other === "string") continue;
if (msg.filename === other.filename) {
if (msg.filename === other) {
return; //Already exists
}
}
server.messages.push(msg);
server.messages.push(msg.filename);
}
//Checks if any of the 'timed' messages should be sent

@ -1336,7 +1336,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (scriptname.endsWith(".lit")) {
let found = false;
for (let i = 0; i < currServ.messages.length; ++i) {
if (!(currServ.messages[i] instanceof Message) && currServ.messages[i] == scriptname) {
if (currServ.messages[i] == scriptname) {
found = true;
break;
}
@ -1462,20 +1462,11 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
for (let i = 0; i < server.messages.length; i++) {
if (filter) {
const msg = server.messages[i];
if (msg instanceof Message) {
if (msg.filename.includes(filter)) {
allFiles.push(msg.filename);
}
} else if (msg.includes(filter)) {
if (msg.includes(filter)) {
allFiles.push(msg);
}
} else {
const msg = server.messages[i];
if (msg instanceof Message) {
allFiles.push(msg.filename);
} else {
allFiles.push(msg);
}
allFiles.push(server.messages[i]);
}
}
@ -1770,7 +1761,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
}
}
for (let i = 0; i < server.messages.length; ++i) {
if (!(server.messages[i] instanceof Message) && filename.toLowerCase() === server.messages[i]) {
if (filename.toLowerCase() === server.messages[i]) {
return true;
}
}

@ -164,6 +164,13 @@ function evaluateVersionCompatibility(ver: string): void {
if (anyPlayer.corporation === 0) {
anyPlayer.corporation = null;
}
// convert all Messages to just filename to save space.
const home = anyPlayer.getHomeComputer();
for (let i = 0; i < home.messages.length; i++) {
if (home.messages[i].filename) {
home.messages[i] = home.messages[i].filename;
}
}
}
}

@ -61,7 +61,7 @@ export class BaseServer {
// For Literature files, this array contains only the filename (string)
// For Messages, it contains the actual Message object
// TODO Separate literature files into its own property
messages: (Message | string)[] = [];
messages: string[] = [];
// Name of company/faction/etc. that this server belongs to.
// Optional, not applicable to all Servers

@ -3,7 +3,6 @@ import { IRouter } from "../../ui/Router";
import { IPlayer } from "../../PersonObjects/IPlayer";
import { BaseServer } from "../../Server/BaseServer";
import { showMessage } from "../../Message/MessageHelpers";
import { Message } from "../../Message/Message";
import { showLiterature } from "../../Literature/LiteratureHelpers";
export function cat(
@ -29,12 +28,12 @@ export function cat(
for (let i = 0; i < server.messages.length; ++i) {
if (filename.endsWith(".lit") && server.messages[i] === filename) {
const file = server.messages[i];
if (file instanceof Message) throw new Error(".lit file should not be a .msg");
if (file.endsWith(".msg")) throw new Error(".lit file should not be a .msg");
showLiterature(file);
return;
} else if (filename.endsWith(".msg")) {
const file = server.messages[i] as Message;
if (file.filename !== filename) continue;
const file = server.messages[i];
if (file !== filename) continue;
showMessage(file);
return;
}

@ -2,7 +2,6 @@ import { ITerminal } from "../ITerminal";
import { IRouter } from "../../ui/Router";
import { IPlayer } from "../../PersonObjects/IPlayer";
import { BaseServer } from "../../Server/BaseServer";
import { Message } from "../../Message/Message";
import { getFirstParentDirectory, isValidDirectoryPath, evaluateDirectoryPath } from "../../Terminal/DirectoryHelpers";
export function ls(
@ -103,8 +102,7 @@ export function ls(
for (const script of s.scripts) handleFn(script.filename, allScripts);
for (const txt of s.textFiles) handleFn(txt.fn, allTextFiles);
for (const contract of s.contracts) handleFn(contract.fn, allContracts);
for (const msgOrLit of s.messages)
msgOrLit instanceof Message ? handleFn(msgOrLit.filename, allMessages) : handleFn(msgOrLit, allMessages);
for (const msgOrLit of s.messages) handleFn(msgOrLit, allMessages);
// Sort the files/folders alphabetically then print each
allPrograms.sort();

@ -34,7 +34,7 @@ export function scp(
if (scriptname.endsWith(".lit")) {
let found = false;
for (let i = 0; i < server.messages.length; ++i) {
if (!(server.messages[i] instanceof Message) && server.messages[i] == scriptname) {
if (server.messages[i] == scriptname) {
found = true;
break;
}

@ -71,7 +71,7 @@ export function determineAllPossibilitiesForTabCompletion(
function addAllLitFiles(): void {
for (const file of currServ.messages) {
if (!(file instanceof Message)) {
if (!file.endsWith(".msg")) {
allPos.push(file);
}
}
@ -79,8 +79,8 @@ export function determineAllPossibilitiesForTabCompletion(
function addAllMessages(): void {
for (const file of currServ.messages) {
if (file instanceof Message) {
allPos.push(file.filename);
if (file.endsWith(".msg")) {
allPos.push(file);
}
}
}

@ -95,7 +95,7 @@ describe("determineAllPossibilitiesForTabCompletion", function () {
Player.getHomeComputer().writeToTextFile("note.txt", "oh hai mark");
Player.getHomeComputer().writeToScriptFile("/www/script.js", "oh hai mark");
Player.getHomeComputer().contracts.push(new CodingContract("linklist.cct"));
Player.getHomeComputer().messages.push(new Message("asl.msg"));
Player.getHomeComputer().messages.push("asl.msg");
Player.getHomeComputer().messages.push("af.lit");
expect(determineAllPossibilitiesForTabCompletion(Player, "rm ", 0)).equal([
"/www/script.js",
@ -120,7 +120,7 @@ describe("determineAllPossibilitiesForTabCompletion", function () {
it("completes the cat command", () => {
Player.getHomeComputer().writeToTextFile("/www/note.txt", "oh hai mark");
Player.getHomeComputer().messages.push(new Message("asl.msg"));
Player.getHomeComputer().messages.push("asl.msg");
Player.getHomeComputer().messages.push("af.lit");
expect(determineAllPossibilitiesForTabCompletion(Player, "cat ", 0)).equal([
"asl.msg",