summaryrefslogtreecommitdiff
path: root/webapp/swiftstory-mobile.js
blob: b05193cb4e026a7842e59567c79ed5981dde4fc9 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
$(document).ready(function() {
    var $home = $("#home");
    var $game = $("#game");
    var $become_judge = $('[data-state="become-judge"]');
    var $judge_collect = $('[data-state="judge-collect"]');
    var $judge_choose = $('[data-state="judge-choose"]');
    var $player_choose = $('[data-state="player-choose"]');
    var $player_wait = $('[data-state="player-wait"]');
    var $leave_room = $('#leave-room');
    var $all = $("[data-state]");
    var $join_btn = $("#join-btn");
    var $become_judge_btn = $("#become-judge-btn");
    var $judge_collect_btn = $("#judge-collect-btn");
    var $black_card = $("#black-card");
    var $played_card_number = $("#played-card-number");
    var $header = $("header");
    var $white_cards = $('#white-cards');
    var $played_cards = $('#played-cards');
    var $score_value = $('#score-value');

    $leave_room.click(function () {
        window.location.reload();
    });

    $become_judge_btn.click(function() {
        swst.pick_black_card();
    });

    $join_btn.click(function () {
        swst.join_game(prompt('Name of the game'));
    });

    $judge_collect_btn.click(function() {
        swst.collect_cards();
    });

    swst.on_socket_open = function() {
        $join_btn.show();
    };

    swst.on_join_game_ok = function() {
        $header.show();
        $home.removeClass("current");
        $game.addClass("current");
        $all.hide();
    };

    swst.on_change_state = function(state) {
        $all.hide();

        switch (state) {
            case 'waiting_judge':
                $become_judge.show();
                $white_cards.attr('disabled', true);
                $white_cards.addClass('read-only');
                break;
            case 'waiting_designation':
                if (swst.is_judge()) {
                    $judge_choose.show();
                    $played_cards.removeAttr('disabled');
                    $played_cards.removeClass('read-only');
                } else {
                    $player_wait.show();
                    $white_cards.attr('disabled', true);
                    $white_cards.addClass('read-only');
                }
                break;
            case 'waiting_collection':
                if (swst.is_judge()) {
                    $judge_collect.show();
                    $white_cards.attr('disabled', true);
                    $white_cards.addClass('read-only');
                } else {
                    $player_choose.show();
                    $white_cards.removeAttr('disabled');
                    $white_cards.removeClass('read-only');
                }
                break;
            default:
                console.log('unhandled state');
                break;
        }
    };

    swst.on_show_white_card = function(idx, desc) {
        var identifier = 'white-card-' + idx;
        var content = '<button name="' + idx + '" class="read-only card" id="' + identifier + '">' + desc + '</button>';
        $white_cards.append(content);
        var self = this;
        $('#' + identifier).click(function () {
            var $this = $(this);
            if (!$white_cards.attr('disabled')) {
                if ($this.hasClass("active")) {
                    swst.get_white_card_event($this.prop('name'))();
                } else {
                    $white_cards.find("> .card").removeClass("active");
                    $this.addClass("active");
                }
            }
        });
    };

    swst.on_show_played_card = function(idx, desc) {
        var identifier = 'played-card-' + idx;
        var content = '<button name="' + idx + '" class="read-only card" id="' + identifier + '">' + desc + '</button>';
        $played_cards.append(content);
        var self = this;
        $('#' + identifier).click(function () {
            var $this = $(this);
            if (!$played_cards.attr('disabled')) {
                if ($this.hasClass("active")) {
                    swst.get_played_card_event($this.prop('name'))();
                } else {
                    $played_cards.find("> .card").removeClass("active");
                    $this.addClass("active");
                }
            }
        });
    };

    swst.on_show_black_card = function(desc) {
        $('#black-card').html(desc);
    };

    swst.on_play_white_card_ok = function(idx) {
        $white_cards.attr('disabled', true);
        $white_cards.addClass('read-only');

        $player_wait.show();

        $('#white-card-' + idx).remove();
    };

    swst.on_updated_score = function(score) {
        $score_value.text(score);
    };

    swst.on_change_nbr_played_cards = function(nbr) {
        $played_card_number.text(nbr);
    };

    swst.on_collect_cards_ok = function() {
        $white_cards.hide();
        $played_cards.show();
    };

    swst.on_designate_card_ok = function(idx) {
        $played_cards.empty();

        $played_cards.hide();
        $white_cards.show();
    };

    swst.run();
});