i am trying to use a Map
to store some data in a class. On creation I am initializing the Map
with initial data. The Problem is that after the initBoard()
function the Map
is still empty.
The Map
which makes the problem is located inside the Scoreboard
class. During the initBoard()
the Map
should be filled with the player names as a key and the value should be an array of ScoreEntry
. The problem is that the Map
stays empty.
A minimal working sample:
player.ts
import { log } from './index';
export class Player {
private id: string;
constructor(inID: string) {
this.id = inID;
}
getID(): string {
log.silly(`[Player (${this.id})] Returning ID.`);
return this.id;
}
}
scoreboard.ts
import { log } from '.';
import { Player } from './player';
export class Scoreboard {
private players: Array<Player>;
private board: Map<string, Array<ScoreEntry>>;
constructor(plrs: Array<Player>) {
this.players = plrs;
this.board = new Map<string, Array<ScoreEntry>>();
this.initBoard();
}
initBoard(): void {
log.info(`[ScoreBoard] Initializing.`);
const max = this.getMaxRoundNumber();
log.debug(`[ScoreBoard] Maximum Rounds: ${max}.`);
this.players.forEach((player: Player) => {
let temp = new Array<ScoreEntry>(max);
for (let i = 0; i < max; i++) {
temp[i] = new ScoreEntry();
}
this.board.set(player.getID(), temp);
});
}
getMaxRoundNumber(): number {
switch (this.players.length) {
case 3:
return 20;
case 4:
return 15;
case 5:
return 12;
case 6:
return 10;
default:
return 0;
}
}
}
class ScoreEntry {
private target!: number;
private score: number;
constructor() {
this.score = 0;
}
getScore(): number {
return this.score;
}
getTarget(): number {
return this.target;
}
setScore(val: number) {
this.score = val;
}
setTarget(val: number) {
this.target = val;
}
}
index.ts
import { Logger, TLogLevelName } from 'tslog';
import { Player } from './player';
import { Scoreboard } from './scoreboard';
const loglevel = process.env.LOG_LEVEL || 'silly';
export const log: Logger = new Logger({
name: 'min_example',
minLevel: loglevel as TLogLevelName,
dateTimeTimezone: 'Europe/Berlin',
});
const a = new Player('a')
const b = new Player('b')
const c = new Player('c')
const players = [a, b, c]
const scoreboard = new Scoreboard(players)
console.log(JSON.stringify(scoreboard))
log.debug(`Board`, scoreboard)
Thoses classes ran with node and TypeScript compiler targeted at ES2020
produce the following output for me: I used my logger for the project as well as console.log
to show that it isn't a problem with the logger.
2021-06-14 15:10:53.302 INFO [wizard_backend build/scoreboard.js:12 Scoreboard.initBoard] [ScoreBoard] Initializing.
2021-06-14 15:10:53.306 DEBUG [wizard_backend build/scoreboard.js:14 Scoreboard.initBoard] [ScoreBoard] Maximum Rounds: 20.
2021-06-14 15:10:53.306 SILLY [wizard_backend build/player.js:10 Player.getID] [Player (a)] Returning ID.
2021-06-14 15:10:53.308 SILLY [wizard_backend build/player.js:10 Player.getID] [Player (b)] Returning ID.
2021-06-14 15:10:53.308 SILLY [wizard_backend build/player.js:10 Player.getID] [Player (c)] Returning ID.
{"players":[{"id":"a"},{"id":"b"},{"id":"c"}],"board":{}}
2021-06-14 15:10:53.310 DEBUG [wizard_backend build/index.js:22 Object.<anonymous>] Board
Scoreboard {
players: [
Player {
id: 'a'
},
Player {
id: 'b'
},
Player {
id: 'c'
}
],
board: Map {}
}