diff options
-rw-r--r-- | cao-common.js | 39 | ||||
-rw-r--r-- | cao-mobile.js | 68 |
2 files changed, 67 insertions, 40 deletions
diff --git a/cao-common.js b/cao-common.js index 050490c..56ada43 100644 --- a/cao-common.js +++ b/cao-common.js @@ -18,6 +18,7 @@ var CAO = function() { this.on_card_played = function() { /* to override */ }; this.on_cards_collected = function() { /* to override */ }; this.on_updated_score = function(new_score) { /* to override */ }; + this.on_change_state = function(state) { /* to override */ }; this.on_change_nbr_played_cards = function(nbr) { /* to override */ }; var request_queue = []; @@ -54,10 +55,33 @@ var CAO = function() { self.on_change_nbr_played_cards(nbr_played_cards); }; + this.change_state = function(state) { + game_state = state; + + switch (state) { + case 'waiting_judge': + self.reset_nbr_played_cards(); + judge = false; + + self.on_judge_needed(); + + break; + case 'waiting_collection': + break; + case 'waiting_designation': + break; + } + + this.on_change_state(state); + }; + /* map_handle_response_ok {{{ */ map_handle_response_ok['join_game'] = function(result) { - self.on_join_game_ok(result['game_state']); + self.on_join_game_ok(); + + self.change_state(result['game_state']); + map_handle_response_ok['view_player_cards'](result); }; @@ -77,6 +101,7 @@ var CAO = function() { map_handle_response_ok['pick_black_card'] = function(result) { judge = true; + self.change_state('waiting_collection'); self.on_pick_black_card_ok(); map_handle_response_ok['view_black_card'](result); @@ -96,6 +121,8 @@ var CAO = function() { }; map_handle_response_ok['collect_cards'] = function(result) { + self.change_state('waiting_designation'); + self.on_collect_cards_ok(); map_handle_response_ok['view_played_cards'](result); }; @@ -110,6 +137,8 @@ var CAO = function() { }; map_handle_response_ok['designate_card'] = function(result) { + self.change_state('waiting_judge'); + self.on_designate_card_ok(); played_cards = []; @@ -119,6 +148,8 @@ var CAO = function() { /* handle_notif {{{ */ map_handle_notif['judge_designed'] = function(result) { + self.change_state('waiting_collection'); + self.on_judge_designed(); }; @@ -135,7 +166,7 @@ var CAO = function() { }; map_handle_notif['judge_needed'] = function(result) { - self.on_judge_needed(); + self.change_state('waiting_judge'); }; map_handle_notif['player_joined_game'] = function(result) { @@ -143,10 +174,14 @@ var CAO = function() { }; map_handle_notif['card_played'] = function(result) { + self.incr_nbr_played_cards(); + self.on_card_played(); }; map_handle_notif['cards_collected'] = function(result) { + self.change_state('waiting_designation'); + self.on_cards_collected(); }; diff --git a/cao-mobile.js b/cao-mobile.js index 8d39169..2db978f 100644 --- a/cao-mobile.js +++ b/cao-mobile.js @@ -20,6 +20,10 @@ $(document).ready(function() { window.location.reload(); }); + $become_judge_btn.click(function() { + cao.pick_black_card(); + }); + cao.on_socket_open = function() { $join_btn.show(); $join_btn.on("click", function () { @@ -27,25 +31,38 @@ $(document).ready(function() { }); }; - cao.on_join_game_ok = function(game_state) { + cao.on_join_game_ok = function() { $header.show(); $home.removeClass("current"); $game.addClass("current"); $all.hide(); - switch (game_state) { - case "waiting_collection": - $player_choose.show(); - $white_cards.removeClass("read-only"); + }; + + cao.on_change_state = function(state) { + $all.hide(); + + switch (state) { + case 'waiting_judge': + $become_judge.show(); break; - case "waiting_designation": - $player_wait.show(); - $white_cards.addClass("read-only"); + case 'waiting_designation': + if (cao.is_judge()) { + $judge_choose.show(); + } else { + $player_wait.show(); + $white_cards.addClass("read-only"); + } break; - case "waiting_judge": - $become_judge.show(); - $become_judge_btn.on("click", function() { - cao.pick_black_card(); - }); + case 'waiting_collection': + if (cao.is_judge()) { + $judge_collect.show(); + } else { + $player_choose.show(); + $white_cards.removeClass("read-only"); + } + break; + default: + console.log('unhandled state'); break; } }; @@ -70,39 +87,14 @@ $(document).ready(function() { cao.on_show_played_card = cao.on_show_white_card; - - cao.on_pick_black_card_ok = function() { - $all.hide(); - $judge_collect.show(); - }; - cao.on_show_black_card = function(desc) { $('#black-card').html(desc); }; - cao.on_play_white_card_ok = function(idx) { $('#white-card-' + identifier).remove(); }; - cao.on_designate_card_ok = function() { - // TODO - }; - - cao.on_collect_cards_ok = function() { - $all.hide(); - $judge_choose.show(); - }; - - cao.on_judge_designed = function() { - $all.hide(); - $player_choose.show(); - $white_cards.removeClass("read-only"); - }; - - cao.on_judge_needed = function() { - $all.hide(); - $become_judge.show(); cao.on_updated_score = function(score) { $score_value.text(score); }; |