stuff and junk and things
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
# Ignore artifacts:
|
||||
build
|
||||
coverage
|
||||
node_modules
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
{}
|
||||
{
|
||||
"tabWidth": 4
|
||||
}
|
||||
|
||||
@@ -7,25 +7,32 @@ import * as id from "../utils/id.js";
|
||||
* @class
|
||||
*/
|
||||
export class Character {
|
||||
|
||||
/** @type {string} character's name */
|
||||
name;
|
||||
|
||||
/** @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. */
|
||||
_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 */
|
||||
_id;
|
||||
get id() { return this._id; }
|
||||
get id() {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
/** @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;
|
||||
@@ -57,7 +64,6 @@ export class Character {
|
||||
* @param {boolean} initialize Should we initialize the character
|
||||
*/
|
||||
constructor(playerUname, name, initialize) {
|
||||
|
||||
this.name = name;
|
||||
|
||||
// Initialize the unique name if this character.
|
||||
@@ -138,7 +144,9 @@ export class Character {
|
||||
this.meleeCombat = Math.max(this.skulduggery, 10);
|
||||
break;
|
||||
default:
|
||||
throw new Error('Logic error, ancestry d8() roll was out of scope');
|
||||
throw new Error(
|
||||
"Logic error, ancestry d8() roll was out of scope",
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -167,7 +175,7 @@ export class Character {
|
||||
this.equipment
|
||||
.set("sickle", 1)
|
||||
.set("poisoner's kit", 1)
|
||||
.set("healer's kit", 1)
|
||||
.set("healer's kit", 1);
|
||||
default:
|
||||
this.foundation = "debug";
|
||||
this.proficiencies.add("heavy_armor");
|
||||
|
||||
@@ -11,8 +11,7 @@ import WebSocket from "ws";
|
||||
import { Character } from "./character.js";
|
||||
import { ItemTemplate } from "./item.js";
|
||||
|
||||
export class Game{
|
||||
|
||||
export class Game {
|
||||
/** @type {Map<string,ItemTemplate>} List of all item templates in the game */
|
||||
_itemTemplates = new Map();
|
||||
|
||||
@@ -33,5 +32,8 @@ export class Game{
|
||||
* @protected
|
||||
* @type {Map<string,Player>} Map of users in the game username->Player
|
||||
*/
|
||||
_players = new Map(); get players() { return this._players; }
|
||||
_players = new Map();
|
||||
get players() {
|
||||
return this._players;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import { cleanName } from "../utils/id.js";
|
||||
* generate these CharacterItems.
|
||||
*/
|
||||
export class ItemTemplate {
|
||||
|
||||
_id;
|
||||
_name;
|
||||
_description;
|
||||
@@ -42,15 +41,18 @@ export class ItemTemplate {
|
||||
* @param {string=} id Item's machine-friendly name.
|
||||
*/
|
||||
constructor(name, itemSlots, description, id) {
|
||||
|
||||
if (typeof name !== "string") {
|
||||
throw new Error("Name must be a string, but " + typeof name + " given.");
|
||||
throw new Error(
|
||||
"Name must be a string, but " + typeof name + " given.",
|
||||
);
|
||||
}
|
||||
if (typeof description === "undefined") {
|
||||
description = "";
|
||||
}
|
||||
if (typeof description !== "string") {
|
||||
throw new Error("Name must be a string, but " + typeof name + " given.");
|
||||
throw new Error(
|
||||
"Name must be a string, but " + typeof name + " given.",
|
||||
);
|
||||
}
|
||||
if (!Number.isFinite(itemSlots)) {
|
||||
throw new Error("itemSlots must be a finite number!");
|
||||
@@ -66,11 +68,15 @@ export class ItemTemplate {
|
||||
this._id = id;
|
||||
this._itemSlots = Number(itemSlots);
|
||||
this._description = "";
|
||||
|
||||
}
|
||||
|
||||
createItem() {
|
||||
return new ChracterItem(this._id, this._name, this._description, this._itemSlots);
|
||||
return new ChracterItem(
|
||||
this._id,
|
||||
this._name,
|
||||
this._description,
|
||||
this._itemSlots,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,9 +100,8 @@ export class ItemTemplate {
|
||||
* Another bonus is, that the game can spawn custom items that arent even in the ItemTemplate Set.
|
||||
*/
|
||||
export class CharacterItem {
|
||||
|
||||
/** @type {string?} The unique name if the ItemTemplate this item is based on. May be null. */
|
||||
templateItemId; // We use the id instead of a pointer, could make garbage collection better.
|
||||
templateItemId; // We use the id instead of a pointer, could make garbage collection better.
|
||||
|
||||
/** @type {string} The player's name for this item. */
|
||||
name;
|
||||
@@ -119,7 +124,3 @@ const i = new ItemTemplate("knife", 10000);
|
||||
|
||||
const ci = new CharacterItem();
|
||||
console.log(ci);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Portal } from "./portal";
|
||||
|
||||
/**
|
||||
* Location in the world.
|
||||
*
|
||||
* Can contain characters, quests, monsters, loot, NPCs and more.
|
||||
*
|
||||
* Can contain mundane portals (such as doors or pathways) to adjacent rooms/locations,
|
||||
* or magical portals to distant locations.
|
||||
*/
|
||||
* Location in the world.
|
||||
*
|
||||
* Can contain characters, quests, monsters, loot, NPCs and more.
|
||||
*
|
||||
* Can contain mundane portals (such as doors or pathways) to adjacent rooms/locations,
|
||||
* or magical portals to distant locations.
|
||||
*/
|
||||
export class Location {
|
||||
/** @protected @type string */
|
||||
_id;
|
||||
@@ -34,7 +34,7 @@ export class Location {
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
*/
|
||||
constructor(id, name, description) {
|
||||
this._id = id;
|
||||
this._name = name;
|
||||
|
||||
@@ -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}] > `);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
* @todo Add encounters to portals
|
||||
*/
|
||||
export class Portal {
|
||||
|
||||
/**
|
||||
* Target Location.
|
||||
*/
|
||||
@@ -23,5 +22,4 @@ export class Portal {
|
||||
* Description shown to the player when they traverse the portal.
|
||||
*/
|
||||
_traversalDescription;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,269 +1,300 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>WebSocket MUD</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'Courier New', monospace;
|
||||
background-color: #1a1a1a;
|
||||
color: #00ff00;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WebSocket MUD</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: "Courier New", monospace;
|
||||
background-color: #1a1a1a;
|
||||
color: #00ff00;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
#container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 10px;
|
||||
}
|
||||
#container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
#output {
|
||||
flex: 1;
|
||||
background-color: #000;
|
||||
border: 2px solid #333;
|
||||
padding: 15px;
|
||||
overflow-y: auto;
|
||||
white-space: pre-wrap;
|
||||
font-size: 14px;
|
||||
line-height: 1.4;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
#output {
|
||||
flex: 1;
|
||||
background-color: #000;
|
||||
border: 2px solid #333;
|
||||
padding: 15px;
|
||||
overflow-y: auto;
|
||||
white-space: pre-wrap;
|
||||
font-size: 14px;
|
||||
line-height: 1.4;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
#input-container {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
#input-container {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
#input {
|
||||
flex: 1;
|
||||
background-color: #222;
|
||||
border: 2px solid #333;
|
||||
color: #00ff00;
|
||||
padding: 10px;
|
||||
font-family: 'Courier New', monospace;
|
||||
font-size: 14px;
|
||||
}
|
||||
#input {
|
||||
flex: 1;
|
||||
background-color: #222;
|
||||
border: 2px solid #333;
|
||||
color: #00ff00;
|
||||
padding: 10px;
|
||||
font-family: "Courier New", monospace;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
#input:focus {
|
||||
outline: none;
|
||||
border-color: #00ff00;
|
||||
}
|
||||
#input:focus {
|
||||
outline: none;
|
||||
border-color: #00ff00;
|
||||
}
|
||||
|
||||
#send {
|
||||
background-color: #333;
|
||||
border: 2px solid #555;
|
||||
color: #00ff00;
|
||||
padding: 10px 20px;
|
||||
font-family: 'Courier New', monospace;
|
||||
cursor: pointer;
|
||||
}
|
||||
#send {
|
||||
background-color: #333;
|
||||
border: 2px solid #555;
|
||||
color: #00ff00;
|
||||
padding: 10px 20px;
|
||||
font-family: "Courier New", monospace;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#send:hover {
|
||||
background-color: #444;
|
||||
}
|
||||
#send:hover {
|
||||
background-color: #444;
|
||||
}
|
||||
|
||||
#status {
|
||||
background-color: #333;
|
||||
padding: 5px 15px;
|
||||
margin-bottom: 10px;
|
||||
border-radius: 3px;
|
||||
}
|
||||
#status {
|
||||
background-color: #333;
|
||||
padding: 5px 15px;
|
||||
margin-bottom: 10px;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.connected {
|
||||
color: #00ff00;
|
||||
}
|
||||
.connected {
|
||||
color: #00ff00;
|
||||
}
|
||||
|
||||
.disconnected {
|
||||
color: #ff4444;
|
||||
}
|
||||
.disconnected {
|
||||
color: #ff4444;
|
||||
}
|
||||
|
||||
.connecting {
|
||||
color: #ffaa00;
|
||||
}
|
||||
.connecting {
|
||||
color: #ffaa00;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #ff4444;
|
||||
}
|
||||
.error {
|
||||
color: #ff4444;
|
||||
}
|
||||
|
||||
.system {
|
||||
color: #aaaaaa;
|
||||
}
|
||||
.system {
|
||||
color: #aaaaaa;
|
||||
}
|
||||
|
||||
.prompt {
|
||||
color: #00ccff;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="container">
|
||||
<div id="status" class="connecting">Connecting...</div>
|
||||
<div id="output"></div>
|
||||
<div id="input-container">
|
||||
<input type="text" id="input" placeholder="Enter command..." disabled>
|
||||
<button id="send" disabled>Send</button>
|
||||
.prompt {
|
||||
color: #00ccff;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="container">
|
||||
<div id="status" class="connecting">Connecting...</div>
|
||||
<div id="output"></div>
|
||||
<div id="input-container">
|
||||
<input
|
||||
type="text"
|
||||
id="input"
|
||||
placeholder="Enter command..."
|
||||
disabled
|
||||
/>
|
||||
<button id="send" disabled>Send</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
class MUDClient {
|
||||
constructor() {
|
||||
this.ws = null;
|
||||
this.output = document.getElementById('output');
|
||||
this.input = document.getElementById('input');
|
||||
this.sendButton = document.getElementById('send');
|
||||
this.status = document.getElementById('status');
|
||||
<script>
|
||||
class MUDClient {
|
||||
constructor() {
|
||||
this.ws = null;
|
||||
this.output = document.getElementById("output");
|
||||
this.input = document.getElementById("input");
|
||||
this.sendButton = document.getElementById("send");
|
||||
this.status = document.getElementById("status");
|
||||
|
||||
this.setupEventListeners();
|
||||
this.connect();
|
||||
}
|
||||
|
||||
connect() {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsUrl = `${protocol}//${window.location.host}`;
|
||||
|
||||
this.updateStatus('Connecting...', 'connecting');
|
||||
|
||||
try {
|
||||
this. s = new WebSocket(wsUrl);
|
||||
|
||||
this.ws.onopen = () => {
|
||||
this.updateStatus('Connected', 'connected');
|
||||
this.input.disabled = false;
|
||||
this.sendButton.disabled = false;
|
||||
this.input.focus();
|
||||
};
|
||||
|
||||
this.ws.onmessage = (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.handleMessage(data);
|
||||
};
|
||||
|
||||
this.ws.onclose = () => {
|
||||
this.updateStatus('Disconnected', 'disconnected');
|
||||
this.input.disabled = true;
|
||||
this.sendButton.disabled = true;
|
||||
|
||||
// Attempt to reconnect after 3 seconds
|
||||
setTimeout(() => this.connect(), 3000);
|
||||
};
|
||||
|
||||
this.ws.onerror = (error) => {
|
||||
this.updateStatus('Connection Error', 'error');
|
||||
this.appendOutput('Connection error occurred. Retrying...', 'error');
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
this.updateStatus('Connection Failed', 'error');
|
||||
setTimeout(() => this.connect(), 3000);
|
||||
this.setupEventListeners();
|
||||
this.connect();
|
||||
}
|
||||
}
|
||||
|
||||
setupEventListeners() {
|
||||
this.input.addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
connect() {
|
||||
const protocol =
|
||||
window.location.protocol === "https:" ? "wss:" : "ws:";
|
||||
const wsUrl = `${protocol}//${window.location.host}`;
|
||||
|
||||
this.updateStatus("Connecting...", "connecting");
|
||||
|
||||
try {
|
||||
this.s = new WebSocket(wsUrl);
|
||||
|
||||
this.ws.onopen = () => {
|
||||
this.updateStatus("Connected", "connected");
|
||||
this.input.disabled = false;
|
||||
this.sendButton.disabled = false;
|
||||
this.input.focus();
|
||||
};
|
||||
|
||||
this.ws.onmessage = (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.handleMessage(data);
|
||||
};
|
||||
|
||||
this.ws.onclose = () => {
|
||||
this.updateStatus("Disconnected", "disconnected");
|
||||
this.input.disabled = true;
|
||||
this.sendButton.disabled = true;
|
||||
|
||||
// Attempt to reconnect after 3 seconds
|
||||
setTimeout(() => this.connect(), 3000);
|
||||
};
|
||||
|
||||
this.ws.onerror = (error) => {
|
||||
this.updateStatus("Connection Error", "error");
|
||||
this.appendOutput(
|
||||
"Connection error occurred. Retrying...",
|
||||
"error",
|
||||
);
|
||||
};
|
||||
} catch (error) {
|
||||
this.updateStatus("Connection Failed", "error");
|
||||
setTimeout(() => this.connect(), 3000);
|
||||
}
|
||||
}
|
||||
|
||||
setupEventListeners() {
|
||||
this.input.addEventListener("keypress", (e) => {
|
||||
if (e.key === "Enter") {
|
||||
this.sendMessage();
|
||||
}
|
||||
});
|
||||
|
||||
this.sendButton.addEventListener("click", () => {
|
||||
this.sendMessage();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
this.sendButton.addEventListener('click', () => {
|
||||
this.sendMessage();
|
||||
});
|
||||
|
||||
// Command history
|
||||
this.commandHistory = [];
|
||||
this.historyIndex = -1;
|
||||
|
||||
this.input.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
if (this.historyIndex < this.commandHistory.length - 1) {
|
||||
this.historyIndex++;
|
||||
this.input.value = this.commandHistory[this.commandHistory.length - 1 - this.historyIndex];
|
||||
}
|
||||
} else if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
if (this.historyIndex > 0) {
|
||||
this.historyIndex--;
|
||||
this.input.value = this.commandHistory[this.commandHistory.length - 1 - this.historyIndex];
|
||||
} else if (this.historyIndex === 0) {
|
||||
this.historyIndex = -1;
|
||||
this.input.value = '';
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
sendMessage() {
|
||||
const message = this.input.value.trim();
|
||||
if (message && this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||
// Add to command history
|
||||
if (this.commandHistory[this.commandHistory.length - 1] !== message) {
|
||||
this.commandHistory.push(message);
|
||||
if (this.commandHistory.length > 50) {
|
||||
this.commandHistory.shift();
|
||||
}
|
||||
}
|
||||
// Command history
|
||||
this.commandHistory = [];
|
||||
this.historyIndex = -1;
|
||||
|
||||
this.ws.send(JSON.stringify({
|
||||
type: 'command',
|
||||
content: message
|
||||
}));
|
||||
this.input.addEventListener("keydown", (e) => {
|
||||
if (e.key === "ArrowUp") {
|
||||
e.preventDefault();
|
||||
if (
|
||||
this.historyIndex <
|
||||
this.commandHistory.length - 1
|
||||
) {
|
||||
this.historyIndex++;
|
||||
this.input.value =
|
||||
this.commandHistory[
|
||||
this.commandHistory.length -
|
||||
1 -
|
||||
this.historyIndex
|
||||
];
|
||||
}
|
||||
} else if (e.key === "ArrowDown") {
|
||||
e.preventDefault();
|
||||
if (this.historyIndex > 0) {
|
||||
this.historyIndex--;
|
||||
this.input.value =
|
||||
this.commandHistory[
|
||||
this.commandHistory.length -
|
||||
1 -
|
||||
this.historyIndex
|
||||
];
|
||||
} else if (this.historyIndex === 0) {
|
||||
this.historyIndex = -1;
|
||||
this.input.value = "";
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.input.value = '';
|
||||
sendMessage() {
|
||||
const message = this.input.value.trim();
|
||||
if (
|
||||
message &&
|
||||
this.ws &&
|
||||
this.ws.readyState === WebSocket.OPEN
|
||||
) {
|
||||
// Add to command history
|
||||
if (
|
||||
this.commandHistory[
|
||||
this.commandHistory.length - 1
|
||||
] !== message
|
||||
) {
|
||||
this.commandHistory.push(message);
|
||||
if (this.commandHistory.length > 50) {
|
||||
this.commandHistory.shift();
|
||||
}
|
||||
}
|
||||
this.historyIndex = -1;
|
||||
|
||||
this.ws.send(
|
||||
JSON.stringify({
|
||||
type: "command",
|
||||
content: message,
|
||||
}),
|
||||
);
|
||||
|
||||
this.input.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
handleMessage(data) {
|
||||
switch (data.type) {
|
||||
case "message":
|
||||
this.appendOutput(data.content);
|
||||
break;
|
||||
case "error":
|
||||
this.appendOutput(data.content, "error");
|
||||
break;
|
||||
case "system":
|
||||
this.appendOutput(data.content, "system");
|
||||
break;
|
||||
default:
|
||||
this.appendOutput(data.content);
|
||||
}
|
||||
}
|
||||
|
||||
appendOutput(text, className = "") {
|
||||
const div = document.createElement("div");
|
||||
if (className) {
|
||||
div.className = className;
|
||||
}
|
||||
|
||||
// Check if this looks like a prompt
|
||||
if (text.includes("] > ")) {
|
||||
div.className = "prompt";
|
||||
}
|
||||
|
||||
div.textContent = text;
|
||||
this.output.appendChild(div);
|
||||
this.output.scrollTop = this.output.scrollHeight;
|
||||
}
|
||||
|
||||
updateStatus(message, className) {
|
||||
this.status.textContent = `Status: ${message}`;
|
||||
this.status.className = className;
|
||||
}
|
||||
}
|
||||
|
||||
handleMessage(data) {
|
||||
switch (data.type) {
|
||||
case 'message':
|
||||
this.appendOutput(data.content);
|
||||
break;
|
||||
case 'error':
|
||||
this.appendOutput(data.content, 'error');
|
||||
break;
|
||||
case 'system':
|
||||
this.appendOutput(data.content, 'system');
|
||||
break;
|
||||
default:
|
||||
this.appendOutput(data.content);
|
||||
}
|
||||
}
|
||||
|
||||
appendOutput(text, className = '') {
|
||||
const div = document.createElement('div');
|
||||
if (className) {
|
||||
div.className = className;
|
||||
}
|
||||
|
||||
// Check if this looks like a prompt
|
||||
if (text.includes('] > ')) {
|
||||
div.className = 'prompt';
|
||||
}
|
||||
|
||||
div.textContent = text;
|
||||
this.output.appendChild(div);
|
||||
this.output.scrollTop = this.output.scrollHeight;
|
||||
}
|
||||
|
||||
updateStatus(message, className) {
|
||||
this.status.textContent = `Status: ${message}`;
|
||||
this.status.className = className;
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the MUD client when the page loads
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
new MUDClient();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
// Initialize the MUD client when the page loads
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
new MUDClient();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -4,7 +4,7 @@ import path from "path";
|
||||
import fs from "fs";
|
||||
import { Player } from "./models/player.js";
|
||||
import { Game } from "./models/game.js";
|
||||
import { ClientMessage, MSG_ERROR, MSG_MESSAGE, MSG_PROMPT, MSG_CALAMITY } from "./utils/messages.js";
|
||||
import { ClientMessage, MSG_ERROR, MSG_MESSAGE, MSG_PROMPT, MSG_CALAMITY, } from "./utils/messages.js";
|
||||
|
||||
class Session {
|
||||
/** @type {boolean} */
|
||||
@@ -49,8 +49,12 @@ class MudServer {
|
||||
console.log("New connection established");
|
||||
this.sessions[websocket] = new Session();
|
||||
|
||||
websocket.on("message", (data) => { this.onIncomingMessage(websocket, data) });
|
||||
websocket.on("close", () => { this.onConnectionClosed(websocket); });
|
||||
websocket.on("message", (data) => {
|
||||
this.onIncomingMessage(websocket, data);
|
||||
});
|
||||
websocket.on("close", () => {
|
||||
this.onConnectionClosed(websocket);
|
||||
});
|
||||
|
||||
this.send(websocket, MSG_MESSAGE, "Welcome to MUUUHD", "big");
|
||||
this.send(websocket, MSG_PROMPT, "Please enter your username");
|
||||
@@ -64,10 +68,18 @@ class MudServer {
|
||||
const session = this.sessions.get(websocket);
|
||||
|
||||
if (!session) {
|
||||
console.error("Incoming message from a client without a session!", data);
|
||||
this.send(websocket, MSG_ERROR, "terminal", "You do not have an active session. Go away!")
|
||||
console.error(
|
||||
"Incoming message from a client without a session!",
|
||||
data,
|
||||
);
|
||||
this.send(
|
||||
websocket,
|
||||
MSG_ERROR,
|
||||
"terminal",
|
||||
"You do not have an active session. Go away!",
|
||||
);
|
||||
websocket.close();
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
let message;
|
||||
@@ -76,9 +88,14 @@ class MudServer {
|
||||
message = new ClientMessage(data);
|
||||
} catch (error) {
|
||||
console.error("Bad websocket message", data, error);
|
||||
this.send(websocket, MSG_ERROR, "terminal", "You sent me a bad message! Goodbye...")
|
||||
this.send(
|
||||
websocket,
|
||||
MSG_ERROR,
|
||||
"terminal",
|
||||
"You sent me a bad message! Goodbye...",
|
||||
);
|
||||
websocket.close();
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
if (!session.usernameProcessed) {
|
||||
@@ -87,8 +104,14 @@ class MudServer {
|
||||
// We haven"t gotten a username yet, so we expect one.
|
||||
//----------------------------------------------------
|
||||
if (!message.hasUsername()) {
|
||||
console.error("User should have sent a “username” message, but sent something else instead")
|
||||
this.send(websocket, MSG_CALAMITY, "I expected you to send me a username, but you sent me something else instead. You bad! Goodbye...")
|
||||
console.error(
|
||||
"User should have sent a “username” message, but sent something else instead",
|
||||
);
|
||||
this.send(
|
||||
websocket,
|
||||
MSG_CALAMITY,
|
||||
"I expected you to send me a username, but you sent me something else instead. You bad! Goodbye...",
|
||||
);
|
||||
|
||||
// for now, just close the socket.
|
||||
websocket.close();
|
||||
@@ -100,7 +123,11 @@ class MudServer {
|
||||
// player not found - for now, just close the connection - make a better
|
||||
console.log("Invalid username sent during login: %s", username);
|
||||
this.send(websocket, MSG_ERROR, "Invalid username");
|
||||
this.send(websocket, MSG_PROMPT, "Please enter a valid username");
|
||||
this.send(
|
||||
websocket,
|
||||
MSG_PROMPT,
|
||||
"Please enter a valid username",
|
||||
);
|
||||
}
|
||||
|
||||
// correct username, tentatively assign player to session
|
||||
@@ -120,11 +147,13 @@ class MudServer {
|
||||
//----------------------------------------------------
|
||||
if (!session.passwordProcessed) {
|
||||
if (!message.hasPassword) {
|
||||
console.error("Youser should have sent a “password” message, but sent this instead: %s", message.type);
|
||||
console.error(
|
||||
"Youser should have sent a “password” message, but sent this instead: %s",
|
||||
message.type,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
//----------------------------------------------------
|
||||
// Process the player's commands
|
||||
@@ -134,7 +163,10 @@ class MudServer {
|
||||
return;
|
||||
}
|
||||
|
||||
console.error("We have received a message we couldn't handle!!!", message);
|
||||
console.error(
|
||||
"We have received a message we couldn't handle!!!",
|
||||
message,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,7 +258,9 @@ class MudServer {
|
||||
break;
|
||||
|
||||
default:
|
||||
player.sendMessage(`Unknown command: ${command}. Type "help" for available commands.`);
|
||||
player.sendMessage(
|
||||
`Unknown command: ${command}. Type "help" for available commands.`,
|
||||
);
|
||||
}
|
||||
|
||||
player.sendPrompt();
|
||||
@@ -257,7 +291,10 @@ class MudServer {
|
||||
// Create HTTP server for serving the client
|
||||
const server = http.createServer((req, res) => {
|
||||
// let filePath = path.join(__dirname, "public", req.url === "/" ? "index.html" : req.url);
|
||||
let filePath = path.join("public", req.url === "/" ? "index.html" : req.url);
|
||||
let filePath = path.join(
|
||||
"public",
|
||||
req.url === "/" ? "index.html" : req.url,
|
||||
);
|
||||
const ext = path.extname(filePath);
|
||||
|
||||
const contentTypes = {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export function withSides(sides) {
|
||||
const r = Math.random()
|
||||
const r = Math.random();
|
||||
return Math.floor(r * sides) + 1;
|
||||
}
|
||||
|
||||
@@ -10,4 +10,3 @@ export function d6() {
|
||||
export function d8() {
|
||||
return withSides(8);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ export function cleanName(s) {
|
||||
if (typeof s !== "string") {
|
||||
throw new Error("String expected, but got a ", typeof s);
|
||||
}
|
||||
return s.toLowerCase().replace(" ", "_").replace(/[^a-zA-Z0-9_]/, "_");
|
||||
return s
|
||||
.toLowerCase()
|
||||
.replace(" ", "_")
|
||||
.replace(/[^a-zA-Z0-9_]/, "_");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -65,28 +65,36 @@ export class ClientMessage {
|
||||
return this._arr[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
|
||||
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._arr = JSON.parse(msgData);
|
||||
} catch (_) {
|
||||
throw new Error(`Could not create client message. Attempting to parse json, but data was invalid json: >>> ${msgData} <<<`);
|
||||
throw new Error(
|
||||
`Could not create client message. Attempting to parse json, but data was invalid json: >>> ${msgData} <<<`,
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof this._arr !== "array") {
|
||||
throw new Error(`Could not create client message. Excpected an array, but got a ${typeof this._arr}`);
|
||||
throw new Error(
|
||||
`Could not create client message. Excpected an array, but got a ${typeof this._arr}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (this._arr.length < 1) {
|
||||
throw new Error("Could not create client message. Excpected an array with at least 1 element, but got an empty one");
|
||||
throw new Error(
|
||||
"Could not create client message. Excpected an array with at least 1 element, but got an empty one",
|
||||
);
|
||||
}
|
||||
|
||||
this._arr = arr;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { randomBytes, pbkdf2Sync, randomInt } from 'node:crypto';
|
||||
import { randomBytes, pbkdf2Sync, randomInt } from "node:crypto";
|
||||
|
||||
// Settings (tune as needed)
|
||||
const ITERATIONS = 100_000; // Slow enough to deter brute force
|
||||
const KEYLEN = 64; // 512-bit hash
|
||||
const DIGEST = 'sha512';
|
||||
const DIGEST = "sha512";
|
||||
|
||||
/**
|
||||
* Generate a hash from a plaintext password.
|
||||
@@ -11,9 +11,15 @@ const DIGEST = 'sha512';
|
||||
* @returns String
|
||||
*/
|
||||
export function hash(password) {
|
||||
const salt = randomBytes(16).toString('hex'); // 128-bit salt
|
||||
const hash = pbkdf2Sync(password, salt, ITERATIONS, KEYLEN, DIGEST).toString('hex');
|
||||
return `${ITERATIONS}:${salt}:${hash}`;
|
||||
const salt = randomBytes(16).toString("hex"); // 128-bit salt
|
||||
const hash = pbkdf2Sync(
|
||||
password,
|
||||
salt,
|
||||
ITERATIONS,
|
||||
KEYLEN,
|
||||
DIGEST,
|
||||
).toString("hex");
|
||||
return `${ITERATIONS}:${salt}:${hash}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,7 +30,13 @@ export function hash(password) {
|
||||
* @returns Boolean
|
||||
*/
|
||||
export function verify(password, hashed_password) {
|
||||
const [iterations, salt, hash] = hashed_password.split(':');
|
||||
const derived = pbkdf2Sync(password, salt, Number(iterations), KEYLEN, DIGEST).toString('hex');
|
||||
return hash === derived;
|
||||
const [iterations, salt, hash] = hashed_password.split(":");
|
||||
const derived = pbkdf2Sync(
|
||||
password,
|
||||
salt,
|
||||
Number(iterations),
|
||||
KEYLEN,
|
||||
DIGEST,
|
||||
).toString("hex");
|
||||
return hash === derived;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user