things + stff

This commit is contained in:
Kim Ravn Hansen
2025-09-07 23:24:50 +02:00
parent fb915f2681
commit 8a4eb25507
27 changed files with 1991 additions and 630 deletions

View File

@@ -1,103 +1,48 @@
import WebSocket from "ws";
import { Character } from "./character.js";
/**
* Player Account.
*
* 1. Contain persistent player account info.
* 2. Contain the connection to the client machine if the player is currently playing the game.
* 3. Contain session information.
*
* We can do this because we only allow a single websocket per player account.
* You are not allowed to log in if a connection/socket is already open.
*
* We regularly ping and pong to ensure that stale connections are closed.
*
* Contain persistent player account info.
*/
export class Player {
/** @protected @type {string} unique username */
/**
* @protected
* @type {string} unique username
*/
_username;
get username() {
return this._username;
}
/** @protected @type {string} */
/**
* @protected
* @type {string}
*/
_passwordHash;
get passwordHash() {
return this._passwordHash;
}
/** @protected @type {WebSocket} Player's current and only websocket. If undefined, the player is not logged in. */
_websocket;
get websocket() {
return this._websocket;
/** @protected @type {Set<Character>} */
_characters = new Set();
get characters() {
return this._characters;
}
/** @protected @type {Date} */
_latestSocketReceived;
/**
* @param {string} username
* @param {string} passwordHash
*/
constructor(username, passwordHash) {
this._username = username;
this._passwordHash = passwordHash;
this.createdAt = new Date();
}
/** @param {WebSocket} websocket */
clientConnected(websocket) {
this._websocket = websocket;
}
/***
* Send a message back to the client via the WebSocket.
*
* @param {string} message
* @return {boolean} success
*/
_send(data) {
if (!this._websocket) {
console.error(
"Trying to send a message to an uninitialized websocket",
this,
data,
);
return false;
}
if (this._websocket.readyState === WebSocket.OPEN) {
this._websocket.send(JSON.stringify(data));
return true;
}
if (this._websocket.readyState === WebSocket.CLOSED) {
console.error(
"Trying to send a message through a CLOSED websocket",
this,
data,
);
return false;
}
if (this._websocket.readyState === WebSocket.CLOSING) {
console.error(
"Trying to send a message through a CLOSING websocket",
this,
data,
);
return false;
}
if (this._websocket.readyState === WebSocket.CONNECTING) {
console.error(
"Trying to send a message through a CONNECTING (not yet open) websocket",
this,
data,
);
return false;
}
console.error(
"Trying to send a message through a websocket with an UNKNOWN readyState (%d)",
this.websocket.readyState,
this,
data,
);
return false;
}
sendPrompt() {
this.sendMessage(`\n[${this.currentRoom}] > `);
setPasswordHash(hashedPassword) {
this._passwordHash = hashedPassword;
}
}