things+stuff

This commit is contained in:
Kim Ravn Hansen
2025-09-10 12:36:42 +02:00
parent 5d0cc61cf9
commit ba293d08b3
39 changed files with 3425 additions and 2713 deletions

View File

@@ -7,8 +7,8 @@
*/
export const CALAMITY = "calamity";
/**
* Tell recipient that an error has occurred
/**
* Tell recipient that an error has occurred
*
* Server-->Client-->Player
*/
@@ -21,7 +21,6 @@ export const ERROR = "e";
*/
export const MESSAGE = "m";
/**
* Player has entered data, and sends it to server.
*
@@ -77,110 +76,126 @@ 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;
/**
* @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];
/** 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;
}
/**
* @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;
}
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} <<<`);
}
if (!Array.isArray(this._attr)) {
throw new Error(`Could not create client message. Excpected an array, but got a ${typeof this._attr}`);
}
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;
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} <<<`,
);
}
/** 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";
if (!Array.isArray(this._attr)) {
throw new Error(
`Could not create client message. Excpected an array, but got a ${typeof this._attr}`,
);
}
/** 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";
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;
}
/** @returns {boolean} does this message indicate the player wants to quit */
isQuitCommand() {
return this._attr[0] === QUIT
}
return Number.parseInt(this._attr[2]);
}
isHelpCommand() {
return this._attr[0] === HELP
}
/** @returns {string|false} Get the username stored in this message */
get username() {
return this.isUsernameResponse() ? this._attr[2] : false;
}
/** @returns {boolean} is this a debug message? */
isDebug() {
return this._attr.length === 2 && this._attr[0] === DEBUG;
}
/** @returns {string|false} Get the password stored in this message */
get password() {
return this.isPasswordResponse() ? this._attr[2] : false;
}
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;
}
return Number.parseInt(this._attr[2]);
}
/** @returns {string|false} Get the username stored in this message */
get username() {
return this.isUsernameResponse() ? this._attr[2] : false;
}
/** @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;
}
/** @returns {string} */
get command() {
return this.hasCommand() ? this._attr[1] : false;
}
}
/**
@@ -190,5 +205,5 @@ export class ClientMessage {
* @param {...any} args
*/
export function prepare(messageType, ...args) {
return JSON.stringify([messageType, ...args]);
return JSON.stringify([messageType, ...args]);
}