-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
354 lines (307 loc) · 8.42 KB
/
test.html
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>吹牛逼测试网站</title>
<style>
#myCards {
padding: 40px;
position: absolute;
bottom: 0;
}
#statementSelector {
width: 300px;
padding: 10px;
margin-top: 30px;
border: 1px #ccc solid;
border-radius: 10px;
}
#statementSelector ul {
display: flex;
list-style: none;
padding: 0;
}
#statementSelector ul li {
cursor: pointer;
margin: 5px;
}
#statementSelector ul li:hover, .statementSelected {
font-weight: bold;
text-decoration: underline;
background: #f5f5f5;
}
#notification {
width: 50%;
top: 130px;
margin-left: 25%;
display: none;
position: fixed;
justify-content: center;
}
#notification div {
width: 200px;
text-align: center;
border: 1px #ccc solid;
border-radius: 10px;
padding: 10px;
color: crimson;
font-weight: bold;
}
#btnWrapper {
/*display: none;*/
width: 105px !important;
border: none !important;
}
button {
width: 50px;
height: 30px;
border-radius: 4px;
border: none;
}
.card {
width: 50px;
height: 80px;
border: 1px #555 solid;
padding: 5px;
border-radius: 4px;
float: left;
background: #fff;
margin-left: -20px;
}
.selected {
margin-top: -10px;
}
</style>
</head>
<body>
<h3>树新蜂纸牌游戏 WebSocket 调试阶段</h3>
<div id="myCards">
<!--牌将会作为子节点出现这这里-->
</div>
<div id="userInfo">
<!--玩家信息将会出现在这里-->
</div>
<button id="getReadyBtn">准备</button>
<div id="statementSelector" style="display: none">
<!--从这里选择声称出了什么牌-->
请选择你要声称你出的是什么:
<ul>
<li>A</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>J</li>
<li>Q</li>
<li>K</li>
</ul>
<button style="display: none" id="submit">出牌</button>
</div>
<div id="notification">
<!--这里显示提示-->
<div>
</div>
<div id="btnWrapper">
<button id="challengeBtn">质疑</button>
<button id="passBtn">Pass</button>
</div>
</div>
</body>
<script src="main.js"></script>
<script>
function notify(content, status=true) {
const notification = document.querySelector('#notification');
notification.children[0].innerHTML = content;
notification.style.display = "flex";
const btnWrapper = document.querySelector('#btnWrapper');
if (status) {
btnWrapper.style.display = "block";
} else {
btnWrapper.style.display = "none";
}
}
function unselectAll() {
for (let j = 0; j < statements.length; j+=1) {
statements[j].setAttribute("class", "");
}
}
function clearPlay() {
statement = "";
document.querySelector('#statementSelector').style.display = "none";
unselectAll();
submitBtn.style.display = "none";
}
let username = prompt("请输入你的名字:");
let player;
let otherPlayers = [];
let gameMode;
let statement = "";
const statements = document.querySelectorAll('#statementSelector li');
for (let i = 0; i < statements.length; i+=1) {
statements[i].onclick = e => {
statement = `${i+1}`;
if (statement !== "") {
submitBtn.style.display = "block";
} else {
submitBtn.style.display = "none";
}
unselectAll();
e.target.setAttribute("class", "statementSelected");
}
}
const submitBtn = document.querySelector('#submit');
const challengeBtn = document.querySelector('#challengeBtn');
const passBtn = document.querySelector('#passBtn');
submitBtn.onclick = () => {
if (player.cardsTemp.length > 0 &&
statement !== "") {
gameMode = "play";
sendCanIPlay({
id: player.id,
});
} else {
alert("出牌错误!");
}
};
challengeBtn.onclick = () => {
gameMode = "challenge";
sendCanIPlay({
id: player.id,
});
};
passBtn.onclick = () => {
gameMode = "pass";
sendCanIPlay({
id: player.id,
});
};
const getReadyBtn = document.querySelector('#getReadyBtn');
getReadyBtn.onclick = () => {
sendReady({ id: player.id });
};
const ws = new WebSocket('ws://119.28.89.164:5002/cnb');
ws.onmessage = response => {
if (response && response.data) {
const data = JSON.parse(response.data);
switch (data.type) {
case "accessible":
handleAccessible(data);
break;
case "room_info":
handleRoomInfo(data);
break;
case "game_begins":
handleGameBegins(data);
break;
case "play_status":
handlePlayStatus(data);
break;
case "play_result":
handlePlayResult(data);
}
} else {
alert("连接服务器失败!");
}
};
function updateOtherPlayers(data) {
otherPlayers = data;
}
function sendEnterRoom(data) {
ws.send(JSON.stringify({
type: "enter_room",
data,
}));
}
function sendReady(data) {
ws.send(JSON.stringify({
type: "ready",
data,
}));
}
function sendCanIPlay(data) {
ws.send(JSON.stringify({
type: "can_i_play",
data,
}));
}
function sendPlayInfo(data) {
ws.send(JSON.stringify({
type: "play",
data,
}));
}
function handlePlayResult(data) {
const { result } = data.data;
notify(result);
}
function handlePlayStatus(data) {
let { status } = data.data;
status = JSON.parse(status);
if (status) {
switch (gameMode) {
case "play":
handlePlay();
break;
case "challenge":
handleChallenge();
break;
case "pass":
handlePass();
break;
}
} else {
alert("还没轮到你!");
}
}
function handlePass() {
ws.send(JSON.stringify({
type: "pass",
id: player.id,
}));
}
function handleChallenge() {
ws.send(JSON.stringify({
type: "challenge",
id: player.id,
}));
}
function handlePlay() {
const cards = player.play();
sendPlayInfo({
id: player.id,
cards,
statement,
});
clearPlay();
}
function handleGameBegins(data) {
const { cards, others } = data.data;
player.getCards(cards);
player.showCards();
updateOtherPlayers(others);
getReadyBtn.style.display = "none";
notify("游戏开始!", false)
}
function handleRoomInfo(data) {
const { id, others } = data.data;
player = new Player(id, username);
player.showInfo();
updateOtherPlayers(others);
}
function handleAccessible(data) {
const { accessible } = data.data;
if (accessible) {
sendEnterRoom({
name: username,
});
} else {
alert("你不能进入此房间!");
}
}
</script>
</html>