blob: ca92e48d055369ca3057983596fbf2af82724fdb (
plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
// ==============================================================================
// Copyright (C) 2019 - Philip Paquette, Steven Bocco
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License along
// with this program. If not, see <https://www.gnu.org/licenses/>.
// ==============================================================================
import {STRINGS} from "../utils/strings";
import {UTILS} from "../utils/utils";
export class GameInstanceSet {
constructor(gameID) {
this.__game_id = gameID;
this.__games = {};
}
getGames() {
return Object.values(this.__games);
}
has(role) {
return this.__games.hasOwnProperty(role);
}
get(role) {
return this.__games[role] || null;
}
getSpecial() {
if (this.__games.hasOwnProperty(STRINGS.OBSERVER_TYPE))
return this.__games[STRINGS.OBSERVER_TYPE];
if (this.__games.hasOwnProperty(STRINGS.OMNISCIENT_TYPE))
return this.__games[STRINGS.OMNISCIENT_TYPE];
return null;
}
remove(role) {
let old = null;
if (this.__games[role]) {
old = this.__games[role];
delete this.__games[role];
}
return old;
}
removeSpecial() {
if (this.__games.hasOwnProperty(STRINGS.OBSERVER_TYPE))
delete this.__games[STRINGS.OBSERVER_TYPE];
if (this.__games.hasOwnProperty(STRINGS.OMNISCIENT_TYPE))
delete this.__games[STRINGS.OMNISCIENT_TYPE];
}
add(game) {
if (game.local.game_id !== this.__game_id)
throw new Error('game ID to add does not match game instance set ID.');
if (this.__games.hasOwnProperty(game.local.role))
throw new Error('Role already in game instance set.');
if (!game.local.isPlayerGame() && (
this.__games.hasOwnProperty(STRINGS.OBSERVER_TYPE) || this.__games.hasOwnProperty(STRINGS.OMNISCIENT_TYPE)))
throw new Error('Previous special game must be removed before adding new one.');
this.__games[game.local.role] = game;
}
size() {
return UTILS.javascript.count(this.__games);
}
}
|