tighten messages types wth enums

This commit is contained in:
Olivier Gagnon 2022-04-13 11:10:56 -04:00
parent c21223ca40
commit 501b69bbc2
3 changed files with 10 additions and 25 deletions

@ -1,31 +1,14 @@
import { Reviver, Generic_toJSON, Generic_fromJSON } from "../utils/JSONReviver"; import { MessageFilenames } from "./MessageHelpers";
export class Message { export class Message {
// Name of Message file // Name of Message file
filename = ""; filename: MessageFilenames;
// The text contains in the Message // The text contains in the Message
msg = ""; msg: string;
// Flag indicating whether this Message has been received by the player constructor(filename: MessageFilenames, msg: string) {
recvd = false;
constructor(filename = "", msg = "") {
this.filename = filename; this.filename = filename;
this.msg = msg; 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;

@ -19,7 +19,7 @@ function sendMessage(msg: Message, forced = false): void {
addMessageToServer(msg); addMessageToServer(msg);
} }
function showMessage(name: string): void { function showMessage(name: MessageFilenames): void {
const msg = Messages[name]; const msg = Messages[name];
if (!(msg instanceof Message)) throw new Error("trying to display unexistent message"); if (!(msg instanceof Message)) throw new Error("trying to display unexistent message");
const txt = const txt =
@ -107,7 +107,7 @@ function checkForMessagesToSend(): void {
} }
} }
enum MessageFilenames { export enum MessageFilenames {
Jumper0 = "j0.msg", Jumper0 = "j0.msg",
Jumper1 = "j1.msg", Jumper1 = "j1.msg",
Jumper2 = "j2.msg", Jumper2 = "j2.msg",
@ -121,7 +121,7 @@ enum MessageFilenames {
} }
//Reset //Reset
const Messages: Record<string, Message> = { const Messages: Record<MessageFilenames, Message> = {
//jump3R Messages //jump3R Messages
[MessageFilenames.Jumper0]: new Message( [MessageFilenames.Jumper0]: new Message(
MessageFilenames.Jumper0, MessageFilenames.Jumper0,

@ -2,9 +2,10 @@ import { ITerminal } from "../ITerminal";
import { IRouter } from "../../ui/Router"; import { IRouter } from "../../ui/Router";
import { IPlayer } from "../../PersonObjects/IPlayer"; import { IPlayer } from "../../PersonObjects/IPlayer";
import { BaseServer } from "../../Server/BaseServer"; import { BaseServer } from "../../Server/BaseServer";
import { showMessage } from "../../Message/MessageHelpers"; import { MessageFilenames, showMessage } from "../../Message/MessageHelpers";
import { showLiterature } from "../../Literature/LiteratureHelpers"; import { showLiterature } from "../../Literature/LiteratureHelpers";
import { dialogBoxCreate } from "../../ui/React/DialogBox"; import { dialogBoxCreate } from "../../ui/React/DialogBox";
import { checkEnum } from "../../utils/helpers/checkEnum";
export function cat( export function cat(
terminal: ITerminal, terminal: ITerminal,
@ -43,6 +44,7 @@ export function cat(
} else if (filename.endsWith(".msg")) { } else if (filename.endsWith(".msg")) {
const file = server.messages[i]; const file = server.messages[i];
if (file !== filename) continue; if (file !== filename) continue;
if (!checkEnum(MessageFilenames, file)) return;
showMessage(file); showMessage(file);
return; return;
} }