stuffy
This commit is contained in:
@@ -1,201 +1,186 @@
|
||||
import { mustBe, mustBeInteger, mustBeString, mustMatch } from "./mustbe.js";
|
||||
|
||||
const colonCommandRegex = /^:([a-z0-9_]+)(:?\s*(.*))?$/;
|
||||
|
||||
/**
|
||||
* Very bad logic error. Player must quit game, refresh page, and log in again.
|
||||
* Enum-like object holding placeholder tokens.
|
||||
*
|
||||
* Client-->Server
|
||||
* or
|
||||
* Server-->Client-->Plater
|
||||
* @readonly
|
||||
* @enum {string}
|
||||
*/
|
||||
export const CALAMITY = "calamity";
|
||||
export const MsgContext = Object.freeze({
|
||||
PASSWORD: ":password",
|
||||
USERNAME: ":username",
|
||||
});
|
||||
|
||||
export const MsgTtype = Object.freeze({
|
||||
/**
|
||||
* Very bad logic error. Player must quit game, refresh page, and log in again.
|
||||
*
|
||||
* Client-->Server
|
||||
* or
|
||||
* Server-->Client-->Plater
|
||||
*/
|
||||
CALAMITY: "CALAMITY",
|
||||
|
||||
/**
|
||||
* Tell recipient that an error has occurred
|
||||
*
|
||||
* Server-->Client-->Player
|
||||
*/
|
||||
MsgContext.ERROR: "E",
|
||||
|
||||
/**
|
||||
* Message to be displayed.
|
||||
*
|
||||
* Server-->Client-->Player
|
||||
*/
|
||||
TEXT: "T",
|
||||
|
||||
/**
|
||||
* Player has entered data, and sends it to server.
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
MsgContext.REPLY: "R",
|
||||
|
||||
/**
|
||||
* Player wants to quit.
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
QUIT: "QUIT",
|
||||
|
||||
/**
|
||||
* Player wants help
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
HELP: "HELP",
|
||||
|
||||
/**
|
||||
* Server tells the client to prompt the player for some data
|
||||
*
|
||||
* Server-->Client-->Player
|
||||
*/
|
||||
PROMPT: "P",
|
||||
|
||||
/**
|
||||
* Server tells the client to prompt the player for some data
|
||||
*
|
||||
* Server-->Client-->Player
|
||||
*/
|
||||
SYSTEM: "_",
|
||||
|
||||
/**
|
||||
* Debug message, to be completely ignored in production
|
||||
*
|
||||
* Client-->Server
|
||||
* or
|
||||
* Server-->Client-->Plater
|
||||
*/
|
||||
DEBUG: "dbg",
|
||||
|
||||
/**
|
||||
* Player sent colon-prefixed, an out-of-order, command
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
COLON: ":",
|
||||
});
|
||||
|
||||
/**
|
||||
* Tell recipient that an error has occurred
|
||||
* Represents a message sent to/from client
|
||||
*
|
||||
* Server-->Client-->Player
|
||||
* @property {string?} command
|
||||
* @property {string?} argLine
|
||||
*/
|
||||
export const ERROR = "e";
|
||||
export class WebsocketMessage {
|
||||
/** @protected @type {any[]} _arr The array that contains the message data */
|
||||
_data;
|
||||
|
||||
/**
|
||||
* Message to be displayed.
|
||||
*
|
||||
* Server-->Client-->Player
|
||||
*/
|
||||
export const MESSAGE = "m";
|
||||
/** @constant @readonly @type {string} _arr The array that contains the message data */
|
||||
type;
|
||||
|
||||
/**
|
||||
* Player has entered data, and sends it to server.
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
export const REPLY = "reply";
|
||||
/**
|
||||
* @param {string} msgData the raw text data in the websocket message.
|
||||
*/
|
||||
constructor(msgData) {
|
||||
if (typeof msgData !== "string") {
|
||||
throw new Error(
|
||||
"Could not create client message. Attempting to parse json, but data was not even a string, it was a " +
|
||||
typeof msgData,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Player wants to quit.
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
export const QUIT = "quit";
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(msgData);
|
||||
} catch (_) {
|
||||
throw new Error(
|
||||
`Could not create client message. Attempting to parse json, but data was invalid json: >>> ${msgData} <<<`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Player wants help
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
export const HELP = "help";
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error(`Could not create client message. Excpected an array, but got a ${typeof this._data}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Server tells the client to prompt the player for some data
|
||||
*
|
||||
* Server-->Client-->Player
|
||||
*/
|
||||
export const PROMPT = "prompt";
|
||||
if (data.length < 1) {
|
||||
throw new Error(
|
||||
"Could not create client message. Excpected an array with at least 1 element, but got an empty one",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Player has entered a command, and wants to do something.
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
export const COMMAND = "c";
|
||||
this.type = mustBeString(data[0]);
|
||||
|
||||
/**
|
||||
* Server tells the client to prompt the player for some data
|
||||
*
|
||||
* Server-->Client-->Player
|
||||
*/
|
||||
export const SYSTEM = "_";
|
||||
|
||||
/**
|
||||
* Debug message, to be completely ignored in production
|
||||
*
|
||||
* Client-->Server
|
||||
* or
|
||||
* Server-->Client-->Plater
|
||||
*/
|
||||
export const DEBUG = "dbg";
|
||||
|
||||
/**
|
||||
* Represents a message sent from client to server.
|
||||
*/
|
||||
export class ClientMessage {
|
||||
/**
|
||||
* @protected
|
||||
* @type {any[]} _arr The array that contains the message data
|
||||
*/
|
||||
_attr;
|
||||
|
||||
/** The message type.
|
||||
*
|
||||
* One of the * constants from this document.
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
get type() {
|
||||
return this._attr[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} msgData the raw text data in the websocket message.
|
||||
*/
|
||||
constructor(msgData) {
|
||||
if (typeof msgData !== "string") {
|
||||
throw new Error(
|
||||
"Could not create client message. Attempting to parse json, but data was not even a string, it was a " +
|
||||
typeof msgData,
|
||||
);
|
||||
return;
|
||||
switch (this.type) {
|
||||
case MsgContext.REPLY: // player ==> client ==> server
|
||||
this.text = mustBeString(data[1]);
|
||||
break;
|
||||
case HELP: // player ==> client ==> server
|
||||
this.text = data[1] === undefined ? "" : mustBeString(data[1]).trim();
|
||||
break;
|
||||
case COLON: // player ==> client ==> server
|
||||
this.command = mustMatch(data[1], /^[a-z0-9_]+$/);
|
||||
this.argLine = data[2]; // parse??
|
||||
break;
|
||||
case DEBUG: // server ==> client
|
||||
case MsgContext.ERROR: // server ==> client ==> player
|
||||
case QUIT: // player ==> client ==> server
|
||||
case SYSTEM: // client <==> server
|
||||
case PROMPT: // server ==> client ==> player
|
||||
case TEXT: // server ==> client ==> player
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown message type: >>${typeof this.type}<<`);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
this._attr = JSON.parse(msgData);
|
||||
} catch (_) {
|
||||
throw new Error(
|
||||
`Could not create client message. Attempting to parse json, but data was invalid json: >>> ${msgData} <<<`,
|
||||
);
|
||||
isQuit() {
|
||||
return this.type === QUIT;
|
||||
}
|
||||
|
||||
if (!Array.isArray(this._attr)) {
|
||||
throw new Error(
|
||||
`Could not create client message. Excpected an array, but got a ${typeof this._attr}`,
|
||||
);
|
||||
isHelp() {
|
||||
return this.type === HELP;
|
||||
}
|
||||
|
||||
if (this._attr.length < 1) {
|
||||
throw new Error(
|
||||
"Could not create client message. Excpected an array with at least 1 element, but got an empty one",
|
||||
);
|
||||
}
|
||||
}
|
||||
hasCommand() {
|
||||
return this._attr.length > 1 && this._attr[0] === COMMAND;
|
||||
}
|
||||
|
||||
/** Does this message contain a username-response from the client? */
|
||||
isUsernameResponse() {
|
||||
return (
|
||||
this._attr.length === 4 &&
|
||||
this._attr[0] === REPLY &&
|
||||
this._attr[1] === "username" &&
|
||||
typeof this._attr[2] === "string"
|
||||
);
|
||||
}
|
||||
|
||||
/** Does this message contain a password-response from the client? */
|
||||
isPasswordResponse() {
|
||||
return (
|
||||
this._attr.length === 4 &&
|
||||
this._attr[0] === REPLY &&
|
||||
this._attr[1] === "password" &&
|
||||
typeof this._attr[2] === "string"
|
||||
);
|
||||
}
|
||||
|
||||
/** @returns {boolean} does this message indicate the player wants to quit */
|
||||
isQuitCommand() {
|
||||
return this._attr[0] === QUIT;
|
||||
}
|
||||
|
||||
isHelpCommand() {
|
||||
return this._attr[0] === HELP;
|
||||
}
|
||||
|
||||
/** @returns {boolean} is this a debug message? */
|
||||
isDebug() {
|
||||
return this._attr.length === 2 && this._attr[0] === DEBUG;
|
||||
}
|
||||
|
||||
isIntegerResponse() {
|
||||
return (
|
||||
this._attr.length === 4 &&
|
||||
this._attr[0] === REPLY &&
|
||||
this._attr[1] === "integer" &&
|
||||
(typeof this._attr[2] === "string" ||
|
||||
typeof this._attr[2] === "number") &&
|
||||
Number.isInteger(Number(this._attr[2]))
|
||||
);
|
||||
}
|
||||
|
||||
/** @returns {number} integer */
|
||||
get integer() {
|
||||
if (!this.isIntegerResponse()) {
|
||||
return undefined;
|
||||
isColon() {
|
||||
return this.type === COLON;
|
||||
}
|
||||
|
||||
return Number.parseInt(this._attr[2]);
|
||||
}
|
||||
isReply() {
|
||||
return this.type === MsgContext.REPLY;
|
||||
}
|
||||
|
||||
/** @returns {string|false} Get the username stored in this message */
|
||||
get username() {
|
||||
return this.isUsernameResponse() ? this._attr[2] : false;
|
||||
}
|
||||
isSysMessage() {
|
||||
return this.type === SYSTEM;
|
||||
}
|
||||
|
||||
/** @returns {string|false} Get the password stored in this message */
|
||||
get password() {
|
||||
return this.isPasswordResponse() ? this._attr[2] : false;
|
||||
}
|
||||
|
||||
/** @returns {string} */
|
||||
get command() {
|
||||
return this.hasCommand() ? this._attr[1] : false;
|
||||
}
|
||||
isDebug() {
|
||||
return this.type === DEBUG;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,6 +189,6 @@ export class ClientMessage {
|
||||
* @param {string} messageType
|
||||
* @param {...any} args
|
||||
*/
|
||||
export function prepare(messageType, ...args) {
|
||||
return JSON.stringify([messageType, ...args]);
|
||||
export function prepareToSend(messageType, ...args) {
|
||||
return JSON.stringify([messageType, ...args]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user