aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/web/src/diplomacy/client/game_instance_set.js
blob: b03b027910a2ab4f5a2cce5f422edd39b9ff304e (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 Object.prototype.hasOwnProperty.call(this.__games, role);
    }

    get(role) {
        return this.__games[role] || null;
    }

    getSpecial() {
        if (Object.prototype.hasOwnProperty.call(this.__games, STRINGS.OBSERVER_TYPE))
            return this.__games[STRINGS.OBSERVER_TYPE];
        if (Object.prototype.hasOwnProperty.call(this.__games, 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 (Object.prototype.hasOwnProperty.call(this.__games, STRINGS.OBSERVER_TYPE))
            delete this.__games[STRINGS.OBSERVER_TYPE];
        if (Object.prototype.hasOwnProperty.call(this.__games, 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 (Object.prototype.hasOwnProperty.call(this.__games, game.local.role))
            throw new Error('Role already in game instance set.');
        if (!game.local.isPlayerGame() && (
            Object.prototype.hasOwnProperty.call(this.__games, STRINGS.OBSERVER_TYPE) || Object.prototype.hasOwnProperty.call(this.__games, 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);
    }
}