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 {
// 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;

@ -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<string, Message> = {
const Messages: Record<MessageFilenames, Message> = {
//jump3R Messages
[MessageFilenames.Jumper0]: new Message(
MessageFilenames.Jumper0,

@ -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;
}