summaryrefslogtreecommitdiff
path: root/cao-common.js
diff options
context:
space:
mode:
Diffstat (limited to 'cao-common.js')
-rw-r--r--cao-common.js136
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);
}
};