-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.ts
50 lines (43 loc) · 1.71 KB
/
player.ts
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
43
44
45
46
47
48
49
50
import { v4 } from 'uuid';
import { MUDStat } from './stat';
import { staticImplements } from '../utility/class-decorators';
import { Serializable } from '../interfaces/entity.interfaces';
import { MUDStatDefinition } from './stat-definition';
import { mudMessage, MUDMessagePriority } from '../utility/mud-messenger';
import { MUDPlayerSerialized } from "../interfaces/serialized.interfaces";
@staticImplements<Serializable<MUDPlayer, MUDPlayerSerialized>>()
export class MUDPlayer {
public playerID: string;
public playerName: string;
public stats: MUDStat[] = [];
constructor(name: string) {
this.playerID = v4();
this.playerName = name;
}
public AddStat(statDefinition: MUDStatDefinition, startingValue: number): void {
this.stats.push(new MUDStat(statDefinition, startingValue));
}
public GetStatByID(statID: string): MUDStat | null {
const stat = this.stats.find(stat => stat.statID === statID);
if (stat) {
return stat;
} else {
mudMessage(MUDMessagePriority.warning, `No stat found on player ${this.playerName} that matches ID ${statID}`);
return null;
}
}
public static Serialize(player: MUDPlayer): MUDPlayerSerialized {
return {
playerID: player.playerID,
playerName: player.playerName,
stats: player.stats.map(stat => MUDStat.Serialize(stat))
};
}
public static Deserialize(serializedPlayer: MUDPlayerSerialized): MUDPlayer {
const player = new MUDPlayer(serializedPlayer.playerName);
player.stats = serializedPlayer.stats.map(stat => {
return MUDStat.Deserialize(stat);
});
return player;
}
}