This commit is contained in:
Kim Ravn Hansen
2025-09-05 13:38:17 +02:00
parent 438f4fe59e
commit 3835ad1de3
9 changed files with 613 additions and 622 deletions

View File

@@ -11,52 +11,21 @@ export class Character {
/** @type {string} character's name */
name;
/**
* Alive?
*
* @protected
* @type {boolean}
*/
_alive = true;
get alive() {
return _alive;
}
/**
* @protected
* @type {number} The number of XP the character has.
*/
/** @protected @type {number} The number of XP the character has. */
_xp = 0;
get xp() {
return this._xp;
}
get xp() { return this._xp; }
/**
* @protected
* @type {number} The character's level.
*/
/** @protected @type {number} The character's level. */
_level = 1;
get level() {
return this._level;
}
get level() { return this._level; }
/**
* @protected
* @type {string} unique name used for chats when there's a name clash and also other things that require a unique character id
*/
/** @protected @type {string} unique name used for chats when there's a name clash and also other things that require a unique character id */
_id;
get id() {
return this._id;
}
get id() { return this._id; }
/**
* @protected
* @type {string} username of the player that owns this character.
*/
/** @protected @type {string} username of the player that owns this character. */
_username;
get username() {
return this._username;
}
get username() { return this._username; }
/** @type {string} Bloodline background */
ancestry;
@@ -83,7 +52,7 @@ export class Character {
equipment = new Map();
/**
* @param {string} playerUname The name of player who owns this character. Note that the game can own a character - somehow.
* @param {string} username The name of player who owns this character. Note that the game can own a character - somehow.
* @param {string} name The name of the character
* @param {boolean} initialize Should we initialize the character
*/

View File

@@ -7,11 +7,12 @@
* Serializing this object effectively saves the game.
*/
import WebSocket from "ws";
import { Character } from "./character";
import { ItemTemplate } from "./item";
class Game{
/** @type {Map<string,ItemTemplate>} List of all item templates in the game */
_itemTemplates = new Map();
@@ -26,9 +27,15 @@ class Game{
*/
_characters = new Map();
/**
/**
* @protected
* @type {Map<string,Player>} The list of users in the game
* @type {Map<string,Player>} Map of users in the game username->Player
*/
_players = new Map();
_playersByName = new Map();
/**
* @protected
* @type {Map<WebSocket,Player>} Map of users in the game username->Player
*/
_playersBySocket = new Map();
}

View File

@@ -1,5 +1,78 @@
import WebSocket from 'ws';
/**
* 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.
*
*/
export class Player{
/** @protected @type {string} unique username */
_username;
get username() { return this._username; }
/** @protected @type {string} */
_passwordHash;
alias;
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 {Date} */
_latestSocketReceived;
constructor(username, passwordHash) {
this._username = username;
}
/** @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}] > `);
}
}