From 501b69bbc2291eb466ffeb49c0d27d20b67b0efe Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Wed, 13 Apr 2022 11:10:56 -0400 Subject: [PATCH] tighten messages types wth enums --- src/Message/Message.ts | 25 ++++--------------------- src/Message/MessageHelpers.ts | 6 +++--- src/Terminal/commands/cat.ts | 4 +++- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/Message/Message.ts b/src/Message/Message.ts index 4e93c0b59..30aa419ab 100644 --- a/src/Message/Message.ts +++ b/src/Message/Message.ts @@ -1,31 +1,14 @@ -import { Reviver, Generic_toJSON, Generic_fromJSON } from "../utils/JSONReviver"; +import { MessageFilenames } from "./MessageHelpers"; export class Message { // Name of Message file - filename = ""; + filename: MessageFilenames; // The text contains in the Message - msg = ""; + msg: string; - // Flag indicating whether this Message has been received by the player - recvd = false; - - constructor(filename = "", msg = "") { + constructor(filename: MessageFilenames, msg: string) { this.filename = filename; this.msg = msg; - this.recvd = false; - } - - // Serialize the current object to a JSON save state - toJSON(): any { - return Generic_toJSON("Message", this); - } - - // Initializes a Message Object from a JSON save state - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - static fromJSON(value: any): Message { - return Generic_fromJSON(Message, value.data); } } - -Reviver.constructors.Message = Message; diff --git a/src/Message/MessageHelpers.ts b/src/Message/MessageHelpers.ts index 4d3858ee1..9236b8664 100644 --- a/src/Message/MessageHelpers.ts +++ b/src/Message/MessageHelpers.ts @@ -19,7 +19,7 @@ function sendMessage(msg: Message, forced = false): void { addMessageToServer(msg); } -function showMessage(name: string): void { +function showMessage(name: MessageFilenames): void { const msg = Messages[name]; if (!(msg instanceof Message)) throw new Error("trying to display unexistent message"); const txt = @@ -107,7 +107,7 @@ function checkForMessagesToSend(): void { } } -enum MessageFilenames { +export enum MessageFilenames { Jumper0 = "j0.msg", Jumper1 = "j1.msg", Jumper2 = "j2.msg", @@ -121,7 +121,7 @@ enum MessageFilenames { } //Reset -const Messages: Record = { +const Messages: Record = { //jump3R Messages [MessageFilenames.Jumper0]: new Message( MessageFilenames.Jumper0, diff --git a/src/Terminal/commands/cat.ts b/src/Terminal/commands/cat.ts index 482b9f672..5d155dedf 100644 --- a/src/Terminal/commands/cat.ts +++ b/src/Terminal/commands/cat.ts @@ -2,9 +2,10 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; -import { showMessage } from "../../Message/MessageHelpers"; +import { MessageFilenames, showMessage } from "../../Message/MessageHelpers"; import { showLiterature } from "../../Literature/LiteratureHelpers"; import { dialogBoxCreate } from "../../ui/React/DialogBox"; +import { checkEnum } from "../../utils/helpers/checkEnum"; export function cat( terminal: ITerminal, @@ -43,6 +44,7 @@ export function cat( } else if (filename.endsWith(".msg")) { const file = server.messages[i]; if (file !== filename) continue; + if (!checkEnum(MessageFilenames, file)) return; showMessage(file); return; }