diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-06-05 00:13:41 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-06-05 00:13:41 +0100 |
commit | 100db155e1d4dffeb9d30cdeab24fa9919a8aa4e (patch) | |
tree | 8ad070fc7704fb6149943c87691ca59f068a48ea /cao-common.js | |
parent | ad92a8c1289ce11baed1f59ddf964ca07906846f (diff) |
use a map to handle the response handling
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
Diffstat (limited to 'cao-common.js')
-rw-r--r-- | cao-common.js | 136 |
1 files changed, 66 insertions, 70 deletions
diff --git a/cao-common.js b/cao-common.js index 1047845..8fa1a1e 100644 --- a/cao-common.js +++ b/cao-common.js @@ -21,6 +21,67 @@ var CAO = function() { var white_cards = {}; var black_card; + var handle_response_ok = {}; + + /* handle_response_ok {{{ */ + + handle_response_ok['join_game'] = function(result) { + self.on_join_game_ok(); + handle_response_ok['view_player_cards'](result); + }; + + handle_response_ok['view_player_cards'] = function(result) { + for (var i in result) { + var idx = result[i][0]; + var desc = result[i][1]; + + white_cards[idx] = desc; + + self.on_show_white_card(idx, desc) + } + }; + + handle_response_ok['pick_black_card'] = function(result) { + self.on_pick_black_card_ok(); + handle_response_ok['view_black_card'](result); + }; + + handle_response_ok['view_black_card'] = function(result) { + black_card = result; + self.on_show_black_card(black_card); + }; + + handle_response_ok['play_white_card'] = function(result) { + idx = result['card_id']; + + self.on_play_white_card_ok(idx); + + delete white_cards[idx]; + }; + + handle_response_ok['collect_cards'] = function(result) { + self.on_collect_cards_ok(); + handle_response_ok['view_played_cards'](result); + }; + + handle_response_ok['view_played_cards'] = function(result) { + for (var i in result) { + var desc = result[i]; + + played_cards.push(desc); + self.on_show_played_card(i, desc); + } + }; + + handle_response_ok['designate_card'] = function(result) { + /* TODO check what is `idx' */ + self.on_designate_card_ok(idx); + + played_cards = []; + }; + + /* }}} */ + this.run = function() { ws = new WebSocket('ws://' + document.location.hostname + ':1236'); @@ -60,78 +121,13 @@ var CAO = function() { if (response['status'] != 0) { alert(response['info']); + return; } - switch (rq) { - case 'join_game': - console.log(response); - if (response['status'] != 0) { - break; - } - console.log('just joined the game'); - self.on_join_game_ok(); - /* self.request_show_cards(); */ - /* XXX intentional fallback */ - case 'view_player_cards': - if (response['status'] == 0) { - for (var i in response['result']) { - var idx = response['result'][i][0]; - var desc = response['result'][i][1]; - - white_cards[idx] = desc; - - self.on_show_white_card(idx, desc); - } - } - break; - case 'pick_black_card': - if (response['status'] != 0) { - break; - } - self.on_pick_black_card_ok(); - /* self.request_show_black_card(); */ - /* XXX intentional fallback */ - - case 'view_black_card': - if (response['status'] == 0) { - black_card = response['result']; - self.on_show_black_card(black_card); - } - break; - case 'play_white_card': - if (response['status'] == 0) { - idx = response['result']['card_id']; - - self.on_played_white_card_ok(idx); - - delete white_cards[idx]; - } - break; - case 'collect_cards': - if (response['status'] != 0) { - break; - } - self.on_collect_cards_ok(); - /* XXX intentional fallback */ - case 'view_played_cards': - if (response['status'] == 0) { - for (var i in response['result']) { - var desc = response['result'][i]; - - played_cards.push(desc); - self.on_show_played_card(i, desc); - } - } - break; - case 'designate_card': - if (response['status'] == 0) { - self.on_designate_card_ok(idx); - - played_cards = []; - } - break; - default: - console.log(evt); + if (handle_response_ok[rq]) { + handle_response_ok[rq](response['result']) + } else { + console.log(evt); } }; |