Stuff
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
import { isIdSane, miniUid } from "../utils/id.js";
|
||||
import { Xorshift32 } from "../utils/random.js";
|
||||
import { Security } from "../utils/security.js";
|
||||
import { ItemBlueprint } from "./item.js";
|
||||
import { Player } from "./player.js";
|
||||
|
||||
@@ -83,18 +84,17 @@ export class Game {
|
||||
* @param {string?} passwordHash
|
||||
* @param {string?} salt
|
||||
*
|
||||
* @returns {Player|null} Returns the player if username wasn't already taken, or null otherwise.
|
||||
* @returns {Player|false} Returns the player if username wasn't already taken, or null otherwise.
|
||||
*/
|
||||
createPlayer(username, passwordHash = undefined, salt = undefined) {
|
||||
if (this.#players.has(username)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const player = new Player(
|
||||
username,
|
||||
typeof passwordHash === "string" ? passwordHash : "",
|
||||
typeof salt === "string" && salt.length > 0 ? salt : miniUid(),
|
||||
);
|
||||
passwordHash ??= "";
|
||||
salt ??= Security.generateHash(miniUid());
|
||||
|
||||
const player = new Player(username, passwordHash, salt);
|
||||
|
||||
this.#players.set(username, player);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Player } from "./player.js";
|
||||
import { mustBeString, mustBe } from "../utils/mustbe.js";
|
||||
import { Scene } from "../scenes/scene.js";
|
||||
import { formatMessage, MessageType } from "../utils/messages.js";
|
||||
import * as Messages from "../utils/messages.js";
|
||||
|
||||
/** @typedef {import("ws").WebSocket} WebSocket */
|
||||
|
||||
@@ -42,7 +42,7 @@ export class Session {
|
||||
* @param {Scene} scene
|
||||
*/
|
||||
setScene(scene) {
|
||||
console.debug("changing scene", scene.constructor.name);
|
||||
console.debug("Changing scene", { scene: scene.constructor.name });
|
||||
if (!(scene instanceof Scene)) {
|
||||
throw new Error(`Expected instance of Scene, got a ${typeof scene}: >>${scene}<<`);
|
||||
}
|
||||
@@ -91,7 +91,7 @@ export class Session {
|
||||
console.error("Trying to send a message without a valid websocket", messageType, args);
|
||||
return;
|
||||
}
|
||||
this._websocket.send(formatMessage(messageType, ...args));
|
||||
this._websocket.send(Messages.formatMessage(messageType, ...args));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,7 +112,7 @@ export class Session {
|
||||
}
|
||||
|
||||
this.send(
|
||||
MessageType.PROMPT, // message type
|
||||
Messages.PROMPT, // message type
|
||||
text, // TODO: prompt text must be string or an array of strings
|
||||
mustBe(options, "object"),
|
||||
);
|
||||
@@ -125,12 +125,17 @@ export class Session {
|
||||
* @param {object?} options message options for the client.
|
||||
*/
|
||||
sendText(text, options = {}) {
|
||||
this.send(MessageType.TEXT, text, options);
|
||||
this.send(Messages.TEXT, text, options);
|
||||
}
|
||||
|
||||
/** @param {string|string[]} errorMessage */
|
||||
sendError(errorMessage, options = { verbatim: true, error: true }) {
|
||||
this.send(MessageType.ERROR, mustBeString(errorMessage), options);
|
||||
this.send(Messages.ERROR, mustBeString(errorMessage), options);
|
||||
}
|
||||
|
||||
/** @param {string|string[]} debugMessage */
|
||||
sendDebug(debugMessage, options = { verbatim: true, debug: true }) {
|
||||
this.send(Messages.DEBUG, debugMessage, options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,7 +146,7 @@ export class Session {
|
||||
//
|
||||
// The client should know not to format calamaties anyway, but we add “preformatted” anyway
|
||||
console.info("CALAMITY", errorMessage);
|
||||
this.send(MessageType.CALAMITY, errorMessage, { verbatim: true, calamity: true });
|
||||
this.send(Messages.CALAMITY, errorMessage, { verbatim: true, calamity: true });
|
||||
this.close();
|
||||
}
|
||||
|
||||
@@ -150,6 +155,6 @@ export class Session {
|
||||
* @param {any?} value
|
||||
*/
|
||||
sendSystemMessage(systemMessageType, value = undefined) {
|
||||
this.send(MessageType.SYSTEM, mustBeString(systemMessageType), value);
|
||||
this.send(Messages.SYSTEM, mustBeString(systemMessageType), value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user