stuff and junk and things

This commit is contained in:
Kim Ravn Hansen
2025-09-05 17:57:09 +02:00
parent 8bcbdbfe35
commit 1720db9eb7
14 changed files with 460 additions and 333 deletions

View File

@@ -1,4 +1,4 @@
import WebSocket from 'ws';
import WebSocket from "ws";
/**
* Player Account.
@@ -13,18 +13,24 @@ import WebSocket from 'ws';
* We regularly ping and pong to ensure that stale connections are closed.
*
*/
export class Player{
export class Player {
/** @protected @type {string} unique username */
_username;
get username() { return this._username; }
get username() {
return this._username;
}
/** @protected @type {string} */
_passwordHash;
get passwordHash() { return this._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; }
get websocket() {
return this._websocket;
}
/** @protected @type {Date} */
_latestSocketReceived;
@@ -46,7 +52,11 @@ export class Player{
*/
_send(data) {
if (!this._websocket) {
console.error("Trying to send a message to an uninitialized websocket", this, data)
console.error(
"Trying to send a message to an uninitialized websocket",
this,
data,
);
return false;
}
if (this._websocket.readyState === WebSocket.OPEN) {
@@ -54,19 +64,36 @@ export class Player{
return true;
}
if (this._websocket.readyState === WebSocket.CLOSED) {
console.error("Trying to send a message through a CLOSED websocket", this, data);
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);
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);
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);
console.error(
"Trying to send a message through a websocket with an UNKNOWN readyState (%d)",
this.websocket.readyState,
this,
data,
);
return false;
}
@@ -74,5 +101,3 @@ export class Player{
this.sendMessage(`\n[${this.currentRoom}] > `);
}
}