refactors
This commit is contained in:
@@ -17,13 +17,22 @@ import { Player } from "./player.js";
|
||||
/** @typedef {import("./item.js").ItemBlueprint} ItemBlueprint */
|
||||
|
||||
export class Game {
|
||||
_counter = 1_000_000;
|
||||
#counter = 1_000_000;
|
||||
get counter() {
|
||||
return this.#counter;
|
||||
}
|
||||
|
||||
/** @type {Map<string,ItemBlueprint>} List of all item blueprints in the game */
|
||||
_itemBlueprints = new Map();
|
||||
#itemBlueprints = new Map();
|
||||
get itemBlueprints() {
|
||||
return this.#itemBlueprints;
|
||||
}
|
||||
|
||||
/** @type {Map<string,Location>} The list of locations in the game */
|
||||
_locations = new Map();
|
||||
#locations = new Map();
|
||||
get locations() {
|
||||
return this.#locations;
|
||||
}
|
||||
|
||||
/**
|
||||
* The characters in the game.
|
||||
@@ -31,34 +40,40 @@ export class Game {
|
||||
* @protected
|
||||
* @type {Map<string,Character>}
|
||||
*/
|
||||
_characters = new Map();
|
||||
#characters = new Map();
|
||||
get characters() {
|
||||
return this.#characters;
|
||||
}
|
||||
|
||||
/*
|
||||
* @protected
|
||||
* @type {Map<string,Player>} Map of users in the game username->Player
|
||||
*/
|
||||
_players = new Map();
|
||||
#players = new Map();
|
||||
get players() {
|
||||
return this.#players;
|
||||
}
|
||||
|
||||
/** @protected @type {Xorshift32} */
|
||||
_random;
|
||||
#random;
|
||||
|
||||
/** @type {Xorshift32} */
|
||||
get random() {
|
||||
return this._random;
|
||||
return this.#random;
|
||||
}
|
||||
|
||||
/** @param {number} rngSeed Seed number used for randomization */
|
||||
constructor() {
|
||||
this.rngSeed = Date.now();
|
||||
constructor(rngSeed) {
|
||||
this.seedRNG(rngSeed);
|
||||
}
|
||||
|
||||
set rngSeed(rngSeed) {
|
||||
this._random = new Xorshift32(rngSeed);
|
||||
seedRNG(rngSeed) {
|
||||
this.#random = new Xorshift32(rngSeed);
|
||||
}
|
||||
|
||||
getPlayerByUsername(username) {
|
||||
console.log("GETTING PLAYER: `%s`", username);
|
||||
return this._players.get(username);
|
||||
return this.#players.get(username);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +86,7 @@ export class Game {
|
||||
* @returns {Player|null} Returns the player if username wasn't already taken, or null otherwise.
|
||||
*/
|
||||
createPlayer(username, passwordHash = undefined, salt = undefined) {
|
||||
if (this._players.has(username)) {
|
||||
if (this.#players.has(username)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -81,7 +96,7 @@ export class Game {
|
||||
typeof salt === "string" && salt.length > 0 ? salt : miniUid(),
|
||||
);
|
||||
|
||||
this._players.set(username, player);
|
||||
this.#players.set(username, player);
|
||||
|
||||
return player;
|
||||
}
|
||||
@@ -99,7 +114,7 @@ export class Game {
|
||||
throw new Error("Invalid blueprintId!");
|
||||
}
|
||||
|
||||
const existing = this._itemBlueprints.get(blueprintId);
|
||||
const existing = this.#itemBlueprints.get(blueprintId);
|
||||
|
||||
if (existing) {
|
||||
console.warn("we tried to create the same item blueprint more than once", blueprintId, attributes);
|
||||
@@ -110,7 +125,7 @@ export class Game {
|
||||
|
||||
const result = new ItemBlueprint(attributes);
|
||||
|
||||
this._itemBlueprints.set(blueprintId, result);
|
||||
this.#itemBlueprints.set(blueprintId, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -123,6 +138,6 @@ export class Game {
|
||||
if (!isIdSane(blueprintId)) {
|
||||
throw new Error(`blueprintId >>${blueprintId}<< is not a valid id`);
|
||||
}
|
||||
return this._itemBlueprints.get(blueprintId);
|
||||
return this.#itemBlueprints.get(blueprintId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Config } from "../config.js";
|
||||
import { Game } from "./game.js";
|
||||
|
||||
/** @constant @readonly @type {Game} Global instance of Game */
|
||||
export const gGame = new Game();
|
||||
export const gGame = new Game(Config.rngSeed);
|
||||
|
||||
@@ -9,28 +9,28 @@
|
||||
* or magical portals to distant locations.
|
||||
*/
|
||||
export class Location {
|
||||
/** @protected @type {string} */
|
||||
_id;
|
||||
/** @type {string} */
|
||||
#id;
|
||||
get id() {
|
||||
return this._id;
|
||||
return this.#id;
|
||||
}
|
||||
|
||||
/** @protected @type {string} */
|
||||
_name;
|
||||
/** @type {string} */
|
||||
#name;
|
||||
get name() {
|
||||
return this._name;
|
||||
return this.#name;
|
||||
}
|
||||
|
||||
/** @protected @type {string} */
|
||||
_description;
|
||||
/** @type {string} */
|
||||
#description;
|
||||
get description() {
|
||||
return this._description;
|
||||
return this.#description;
|
||||
}
|
||||
|
||||
/** @protected @type {Map<string,Portal>} */
|
||||
_portals = new Map();
|
||||
/** @type {Map<string,Portal>} */
|
||||
#portals = new Map();
|
||||
get portals() {
|
||||
return this._portals;
|
||||
return this.#portals;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,8 +39,8 @@ export class Location {
|
||||
* @param {string} description
|
||||
*/
|
||||
constructor(id, name, description) {
|
||||
this._id = id;
|
||||
this._name = name;
|
||||
this._description = description;
|
||||
this.#id = id;
|
||||
this.#name = name;
|
||||
this.#description = description;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user