-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.ts
38 lines (33 loc) · 698 Bytes
/
user.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
import { Game } from './game.ts';
export class User {
readonly uuid: string;
constructor(
readonly game: Game,
public name = '',
public hue = Math.floor(Math.random() * 360)
) {
this.uuid = crypto.randomUUID();
}
get based() {
return this.game.cells.some((c) => this.is(c.owner));
}
get attacking() {
return this.game.cells.some((c) => this.is(c.attacker));
}
get energy() {
return this.game.cells.reduce(
(acc, c) => acc + Number(this.is(c.owner) && c.energy),
0
);
}
is(x?: User) {
return x?.uuid === this.uuid;
}
toJSON(): IUser {
return {
u: this.uuid,
n: this.name,
h: this.hue,
};
}
}