-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.mjs
42 lines (36 loc) · 817 Bytes
/
player.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Game } from './game.mjs';
/**
* Player class
* @param {number} id player id (number)
* @param {string} name player name (string)
* @param {number} money player money (number)
* @param {Object} stocks player stocks (Object)
*/
export class Player {
constructor(id, name, money) {
this.id = id;
this.name = name;
this.money = money;
this.stocks = {};
}
static generateId() {
return Game.generateId();
}
toObject() {
return {
id: this.id,
name: this.name,
money: this.money,
stocks: this.stocks,
};
}
static fromObject(obj) {
return new Player(obj.id, obj.name, obj.money, obj.stocks);
}
toJSON() {
return JSON.stringify(this.toObject());
}
static fromJSON(json) {
return this.fromObject(JSON.parse(json));
}
}