blob: df42d9c7353617415a02d49ce786814b840039f6 (
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
$(document).ready(function() {
request_queue = []
var map_white_idx_row = {};
var ws = new WebSocket('ws://' + document.location.hostname + ':1236');
var self = this;
ws.onopen = function() {
console.log('connection established');
$('#btn_join').show();
};
ws.onclose = function() {
};
ws.onmessage = function(evt) {
message = JSON.parse(evt.data);
if (message['type'] == 'notification') {
handle_notification(message['content']);
} else {
handle_response(message['content']);
}
};
var handle_notification = function(msg) {
console.log('notification:');
console.log(msg);
};
var handle_response = function(response) {
rq = request_queue.shift();
if (response['status'] != 0) {
alert(response['info']);
}
switch (rq) {
case 'join_game':
console.log(response);
if (response['status'] != 0) {
break;
}
$('#btn_join').hide();
$('#btn_pick_black').show();
console.log('just joined the game');
/* self.request_show_cards(); */
/* XXX intentional fallback */
case 'view_player_cards':
if (response['status'] == 0) {
$('#white_cards').show();
for (i in response['result']) {
element = $('.card_desc').eq(i);
element.html(response['result'][i][1]);
element.dblclick(gen_callback_white_card(i));
map_white_idx_row[response['result'][i][0]] = i;
}
}
break;
case 'pick_black_card':
if (response['status'] != 0) {
break;
}
$('#btn_collect').show();
$('#btn_pick_black').hide();
/* self.request_show_black_card(); */
/* XXX intentional fallback */
case 'view_black_card':
if (response['status'] == 0) {
$('#black_card').show();
$('#black_card').html(response['result']);
}
break;
case 'play_white_card':
if (response['status'] == 0) {
row = map_white_idx_row[response['result']['card_id']];
element = $('.card_desc').eq(row);
element.empty();
element.dblclick(null)
}
break;
case 'collect_cards':
if (response['status'] != 0) {
break;
}
$('#btn_collect').hide();
/* XXX intentional fallback */
case 'view_played_cards':
if (response['status'] == 0) {
for (i in response['result']) {
console.log(response['result'][i]);
}
}
break;
default:
console.log(evt);
}
};
ws.onerror = function(evt) {
alert(evt);
};
var gen_callback_white_card = function(index) {
return function() {
request = {
'op': 'play_white_card',
'card_id': index,
};
request_queue.push('play_white_card');
ws.send(JSON.stringify(request));
};
};
$('#btn_join').click(function() {
game_name = prompt('Name of the game');
request = {
'op': 'join_game',
'game_name': game_name,
};
request_queue.push('join_game');
ws.send(JSON.stringify(request));
});
$('#btn_pick_black').click(function() {
request = {
'op': 'pick_black_card',
};
request_queue.push('pick_black_card');
ws.send(JSON.stringify(request));
});
$('#btn_collect').click(function() {
request = {
'op': 'collect_cards',
};
request_queue.push('collect_cards');
ws.send(JSON.stringify(request));
});
this.request_show_cards = function() {
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() {
request = {
'op': 'view_black_card',
};
request_queue.push('view_black_card');
ws.send(JSON.stringify(request));
};
this.request_show_played_cards = function() {
request = {
'op': 'view_played_cards',
};
request_queue.push('view_played_cards');
ws.send(JSON.stringify(request));
};
});
|