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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
var CAO = function() {
this.on_socket_open = function() { /* to override */ };
this.on_socket_close = function() { /* to override */ };
this.on_socket_error = function(evt) { /* to override */};
this.on_join_game_ok = function() { /* to override */ };
this.on_show_white_card = function(idx, desc) { /* to override */ };
this.on_pick_black_card_ok = function() { /* to override */ };
this.on_show_black_card = function(desc) { /* to override */ };
this.on_play_white_card_ok = function(idx) { /* to override */ };
this.on_collect_cards_ok = function() { /* to override */ };
this.on_show_played_card = function(idx, desc) { /* to override */ };
this.on_designate_card_ok = function(idx) { /* to override */ };
var request_queue = [];
var self = this;
var ws;
var played_cards = [];
var white_cards = {};
var black_card;
this.run = function() {
ws = new WebSocket('ws://' + document.location.hostname + ':1236');
ws.onopen = function() {
console.log('connection established');
self.on_socket_open();
};
ws.onclose = function() {
self.on_socket_close();
};
ws.onmessage = function(evt) {
var message = JSON.parse(evt.data);
if (message['type'] == 'notification') {
handle_notification(message['content']);
} else {
handle_response(message['content']);
}
};
ws.onerror = function(evt) {
console.log(evt);
self.on_socket_error(evt);
};
};
var handle_notification = function(msg) {
console.log('notification:');
console.log(msg);
};
var handle_response = function(response) {
var rq = request_queue.shift();
if (response['status'] != 0) {
alert(response['info']);
}
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);
}
};
this.gen_callback_white_card = function(index) {
return function() {
var request = {
'op': 'play_white_card',
'card_id': index,
};
request_queue.push('play_white_card');
ws.send(JSON.stringify(request));
};
};
this.gen_callback_played_card = function(index) {
return function() {
var request = {
'op': 'designate_card',
'card_id': index,
};
request_queue.push('designate_card');
ws.send(JSON.stringify(request));
};
};
$('#btn_join').click(function() {
var game_name = prompt('Name of the game');
var request = {
'op': 'join_game',
'game_name': game_name,
};
request_queue.push('join_game');
ws.send(JSON.stringify(request));
});
$('#btn_pick_black').click(function() {
var request = {
'op': 'pick_black_card',
};
request_queue.push('pick_black_card');
ws.send(JSON.stringify(request));
});
$('#btn_collect').click(function() {
var request = {
'op': 'collect_cards',
};
request_queue.push('collect_cards');
ws.send(JSON.stringify(request));
});
this.request_show_cards = function() {
var request = {
'op': 'view_player_cards',
};
request_queue.push('view_player_cards');
console.log(request);
ws.send(JSON.stringify(request));
};
this.request_show_black_card = function() {
var request = {
'op': 'view_black_card',
};
request_queue.push('view_black_card');
ws.send(JSON.stringify(request));
};
this.request_show_played_cards = function() {
var request = {
'op': 'view_played_cards',
};
request_queue.push('view_played_cards');
ws.send(JSON.stringify(request));
};
};
var cao;
$(document).ready(function() {
cao = new CAO();
});
|