-
Notifications
You must be signed in to change notification settings - Fork 3
/
room.js
545 lines (479 loc) · 14.5 KB
/
room.js
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
document.addEventListener("WeixinJSBridgeReady", function () {
document.getElementById("ctrl").load();
document.getElementById("ready").load();
document.getElementById("suc").load();
document.getElementById("failed").load();
}, false);
var ROOM_ID,
PLAYER,
URL,
READY_TIMER,
READY_SECONDS = 10,
PLAY_TIMER,
PLAY_SECONDS = 30,
STATE = 0, // 0未排队 1队列中
CANVAS = document.getElementById("wawa_video");
//
var arrParams = params || window.params;
if (Object.keys(arrParams).length !== 0) {
ROOM_ID = arrParams.id;
document.querySelector("#waajiID").innerHTML = ROOM_ID;
document.title = "anyRTC娃娃机-房间号"+ ROOM_ID;
var WaWaClient = AnyRTCWaWaClient || window.AnyRTCWaWaClient;
var anyClient = new WaWaClient();
/**
* 配置开发者信息
* @anyrtc_developerid
* @anyrtc_appid
* @anyrtc_appkey
* @anyrtc_apptoken
* */
anyClient.initEngineWithAnyRTCInfo(DEV_ID, APP_ID, APP_KEY, APP_TOKEN);
/**
* 开启服务并连接远程娃娃机
**/
anyClient.openServer();
/**
* 连接娃娃机成功
* */
anyClient.on("onConnectServerSuccess", function () {
/*********************************/
// 预约
document.querySelector("#appointment").onclick = function () {
STATE = 1;
anyClient.book();
// 隐藏预约、显现取消预约按钮
document.querySelector("#bookBtn").style.display = 'none';
document.querySelector("#unBookBtn").style.display = 'inline-block';
};
// 取消预约
document.querySelector("#cancelAppointment").onclick = function () {
STATE = 0;
anyClient.unbook();
document.querySelector("#bookBtn").style.display = 'inline-block';
document.querySelector("#unBookBtn").style.display = 'none';
};
// 开始游戏
document.querySelector("#startBtn").onclick = function () {
// 隐藏开始游戏蒙层
document.querySelector("#mask").style.display = "none";
// 隐藏预约、显示控制
document.querySelector('#apply').style.display = 'none';
document.querySelector('#controller').style.display = 'block';
// 显示切换视角按钮
document.querySelector('#transformCamera').style.display = 'block';
//
playReadyAudio();
// 开始游戏 GO
anyClient.play();
// 游戏倒计时
PLAY_TIMER = setInterval(function () {
if (PLAY_SECONDS === 0) {
clearInterval(PLAY_TIMER);
PLAY_SECONDS = 30;
PLAY_TIMER = null;
document.querySelector('#leftActionSeconds').innerHTML = "";
return
}
PLAY_SECONDS--;
document.querySelector('#leftActionSeconds').innerHTML = (PLAY_SECONDS === 1) ? "" : "("+ PLAY_SECONDS +"s)";
}, 1000);
};
// 向上
document.querySelector("#moveTop").onclick = function () {
if (anyClient.canGet) {
playCtrlAudio();
}
anyClient.sendControlCmd(0);
};
// 向右
document.querySelector("#moveRight").onclick = function () {
if (anyClient.canGet) {
playCtrlAudio();
}
anyClient.sendControlCmd(3);
};
// 向左
document.querySelector("#moveLeft").onclick = function () {
if (anyClient.canGet) {
playCtrlAudio();
}
anyClient.sendControlCmd(2);
};
// 向下
document.querySelector("#moveBottom").onclick = function () {
if (anyClient.canGet) {
playCtrlAudio();
}
anyClient.sendControlCmd(1);
};
// 抓取
document.querySelector("#getIt").onclick = function () {
if (anyClient.canGet) {
playCtrlAudio();
}
anyClient.sendControlCmd(4);
// 清除定时器
if (PLAY_TIMER) {
clearInterval(PLAY_TIMER);
PLAY_SECONDS = 30;
PLAY_TIMER = null;
document.querySelector('#leftActionSeconds').innerHTML = "";
}
};
// 旋转摄像头
document.querySelector("#transformCamera").onclick = function () {
if (anyClient.canGet) {
playCtrlAudio();
}
anyClient.sendControlCmd(5);
};
// 取消游戏
document.querySelector("#cancelBtn").onclick = function () {
// 隐藏蒙层
document.querySelector("#mask").style.display = "none";
// 清除准备定时器
if (READY_TIMER) {
clearInterval(READY_TIMER);
READY_SECONDS = 10;
READY_TIMER = null;
document.querySelector('#leftGoSeconds').innerHTML = "";
}
//
anyClient.canclePlay();
};
// 返回列表
document.querySelector("#backBtn").onclick = function () {
window.location.href = "./";
};
// 在玩一次
document.querySelector("#againBtn").onclick = function playAgain () {
// 隐藏蒙层
document.querySelector("#mask").style.display = "none";
// 重置结果页面按钮
document.querySelector("#cancelBtn").style.display = 'inline-block';
document.querySelector("#startBtn").style.display = 'inline-block';
document.querySelector("#backBtn").style.display = 'none';
document.querySelector("#againBtn").style.display = 'none';
// 显示预约、隐藏控制
document.querySelector('#apply').style.display = 'block';
document.querySelector('#controller').style.display = 'none';
}
});
//初始化anyrtc成功
anyClient.on("onInitAnyRTCSuccess", function () {
/**
* 进入房间
* @strAnyRTCId
* @strUserId
* @strUserName
* @strUserIcon
* */
anyClient.joinRoom(ROOM_ID, 'web_' + randomNumber(), '玩家'+ randomNumber(6), '');
});
//初始化anyRtc失败
anyClient.on("onInitAnyRTCFaild", function (data) {
console.log(data);
});
/**
* 加入房间
* @param nCode 状态码
* @param videoInfo 视频流信息
* @param strMemberNum 房间内人数
*/
anyClient.on("onJoinRoom", function (nCode, videoInfo, strMemberNum) {
//console.log(nCode, videoInfo, strMemberNum);
if (nCode === 0) {
try {
// 在线人数
document.querySelector("#onlineMember").innerHTML = strMemberNum;
if (videoInfo !== "" && typeof videoInfo === "string") {
var URL = JSON.parse(videoInfo) || '';
if (URL !== "") {
// 选择方案RTCP 还是 WS
function hasGetUserMedia() {
// Note: Opera builds are unprefixed.
return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia);
}
var ua = navigator.userAgent.toLocaleLowerCase();
if (
(ua.indexOf('android')!==-1 && ua.indexOf('mobile')!==-1 && ua.indexOf('linux')!==-1) ||
ua.indexOf('micromessenger')!==-1 ||
!hasGetUserMedia()
) {
// alert('微信 '+ URL.H5LiveUrl);
PLAYER = new JSMpeg.Player(URL.H5LiveUrl, {
canvas: CANVAS
});
} else {
if (ua.indexOf('chrome')!== -1) {
// alert('chrome '+ URL.RtcpUrl);
document.getElementById("wawa_video").style.display = "none";
var RtcSub = RtcpKit || window.RtcpKit;
var rtcSub = new RtcSub();
// rtcSub.configServerForPriCloud("teameeting.anyrtc.io", 9091);
rtcSub.initEngineWithAnyRTCInfo(DEV_ID, APP_ID, APP_KEY, APP_TOKEN);
//创建界面视频
rtcSub.on('onRTCOpenVideoRender', function (strRtcpId) {
var video = document.createElement('video');
video.id = strRtcpId;
video.autoplay = "autoplay";
video.style.width = "100%";
video.style.height = "auto";
document.getElementById("video_view").appendChild(video);
});
//设置远程流
rtcSub.on('onRemoteStream', function (stream, strRtcpId) {
rtcSub.setRTCVideoRender(stream, document.getElementById(strRtcpId));
});
//订阅
rtcSub.subscribe(URL.RtcpUrl);
rtcSub.on("onSubscribeOK", function (strRtcpId) { });
rtcSub.on("onSubscribeFailed", function (nCode) { });
} else {
// alert('!chrome '+ URL.H5LiveUrl);
PLAYER = new JSMpeg.Player(URL.H5LiveUrl, {
canvas: CANVAS
});
}
}
} else {
alert('服务错误!ERR::MSG没有找到视频流');
}
}
} catch (e) {
console.log(e);
}
}
});
/**
* 获取房间列表
* @param strRoomList 房间列表Json
*/
// anyClient.on("onGetRoomList", function (roomList) {
// console.log(roomList);
// });
/**
* 预约结果
* @param nCode 状态码
* @param strBookNum 预约人数
*/
anyClient.on("onBookResult", function (nCode, strBookNum) {
if (nCode === 0) {
document.querySelector("#bookBtn").style.display = 'none';
document.querySelector("#unBookBtn").style.display = 'inline-block';
if (strBookNum === 0 && strBookNum === undefined) {
document.querySelector("#appointmentTip").innerHTML = '';
} else {
document.querySelector("#appointmentTip").innerHTML = '当前排队人数: ' + strBookNum;
}
}
});
/**
* 取消预约结果
* @param nCode 状态码
* @param strBookNum 预约人数
*/
anyClient.on("onUnBookResult",function (nCode, strBookNum) {
if (nCode === 0) {
document.querySelector("#bookBtn").style.display = 'inline-block';
document.querySelector("#unBookBtn").style.display = 'none';
if (strBookNum === 0 && strBookNum === undefined) {
document.querySelector("#appointmentTip").innerHTML = '';
} else {
document.querySelector("#appointmentTip").innerHTML = '当前排队人数: ' + strBookNum;
}
}
});
/**
* 预约人数更新
* @param strBookMemberNum 预约人数
*/
anyClient.on("onRoomMemberUpdate", function (strBookMemberNum) {
document.querySelector("#appointmentTip").innerHTML = '当前排队人数: ' + strBookMemberNum;
});
/**
* 指令回掉
* @param nCode 状态码
* @param strCmd 指令 left up ...
*/
anyClient.on("onControlCmd", function (nCode, strCmd) {
console.log("CMD:: "+ strCmd);
});
/**
* 准备开始通知
*/
anyClient.on('onReadyStart', function () {
// 显现预约、隐藏取消预约按钮
document.querySelector("#bookBtn").style.display = 'inline-block';
document.querySelector("#unBookBtn").style.display = 'none';
// 倒计时
READY_TIMER = setInterval(function () {
if (READY_SECONDS === 0) {
clearInterval(READY_TIMER);
READY_SECONDS = 10;
READY_TIMER = null;
document.querySelector('#leftGoSeconds').innerHTML = "";
return
}
READY_SECONDS--;
document.querySelector('#leftGoSeconds').innerHTML = (READY_SECONDS === 1) ? "" : "("+ READY_SECONDS +"s)";
}, 1000);
// 打开开始游戏页面
changeState('go');
});
/**
* 抓娃娃结果
* @param result true get false not get
*/
anyClient.on('onResult', function (bResult) {
// 隐藏切换视角按钮
document.querySelector('#transformCamera').style.display = 'none';
if (bResult) {//抓取成功
changeState('win');
playSucAudio();
} else {//抓取失败
changeState('lose');
playFailedAudio();
}
// 清除定时器
if (READY_TIMER) {
clearInterval(READY_TIMER);
READY_SECONDS = 10;
READY_TIMER = null;
document.querySelector('#leftGoSeconds').innerHTML = "";
}
});
/**
* 排队人数更新
* @param strBookMemberNum 房间人数
*/
anyClient.on("onBookMemberUpdate", function (strBookMember) {
document.querySelector("#appointmentTip").innerHTML = strBookMember;
});
/**
* 房间人数更新
* @param strMemberNum 房间人数
*/
anyClient.on("onRoomMemberUpdate", function (strRoomMember) {
document.querySelector("#onlineMember").innerHTML = strRoomMember;
});
/**
* 抓娃娃超时
* @param nCode 状态码
* @param jData 返回错误
*/
anyClient.on("onPlayTimeout", function () {
alert('服务器故障');
window.location.href = "./";
});
/**
* 准备超时通知
*/
anyClient.on("onReadyTimeout", function () {
// 清除定时器
if (READY_TIMER) {
clearInterval(READY_TIMER);
READY_SECONDS = 10;
READY_TIMER = null;
}
// 隐藏开始游戏蒙层
document.querySelector("#mask").style.display = "none";
// 隐藏取消预约、限制立即预约
document.querySelector("#bookBtn").style.display = 'inline-block';
document.querySelector("#unBookBtn").style.display = 'none';
});
/**
* 娃娃机视频流变化
* @param nCode 状态码
* @param jData 返回错误
*/
anyClient.on("onRoomUrlUpdate", function (nCode, jData) {
// 待定
});
/**
* 娃娃机离开房间
* @param nCode 状态码
*/
anyClient.on("onWaWaLeave", function () {
alert("娃娃机退出!");
window.location.href = "./";
});
/**
* 与服务断开连接
*/
anyClient.on('onDisconnect', function () {
console.log('和服务器断开连接,正在尝试重连');
});
/**
* 重连服务
*/
anyClient.on('onReconnect', function () {
console.log('正在重连中...');
});
/****************************/
function randomNumber (len) {
var strNumber = '0123456789';
var strId = '';
if (typeof len === 'undefined') {
len = 8;
}
for (var i=0; i<len; i++) {
strId += strNumber.split('')[parseInt(Math.random()*strNumber.length)];
}
return strId
}
// 更改游戏状态
function changeState (state) {
if (state === undefined || state === null || state === "") {
return
}
// 打开蒙层
document.querySelector("#mask").style.display = "block";
// 显示结果图片
var reg = new RegExp('{{\\s([\\w|\\S]*)\\s}}', 'ig');
var resHTML = document.querySelector("#imgResTemp").innerHTML.replace(reg, function (text, key) {
return {state: state}[key]
});
document.querySelector('#resImg').innerHTML = resHTML;
// 打开不同的按钮
if (state === 'go') {
document.querySelector("#startBtn").innerHTML = "开始游戏";
} else if (state === 'win' || state === 'lose') {
// 隐藏取消和开始游戏按钮
document.querySelector("#cancelBtn").style.display = 'none';
document.querySelector("#startBtn").style.display = 'none';
// 显示返回列表和在玩一次按钮
document.querySelector("#backBtn").style.display = 'inline-block';
document.querySelector("#againBtn").style.display = 'inline-block';
// 返回列表字体更改
if (state === "win") {
document.querySelector("#startBtn").innerHTML = "再接再厉";
document.querySelector("#backBtn").innerHTML = "休息一下";
document.querySelector("#backBtn").className = "relax"
} else {
document.querySelector("#startBtn").innerHTML = "再来一局";
document.querySelector("#backBtn").innerHTML = "无力再战";
document.querySelector("#backBtn").className = "cancel"
}
}
}
//
function playCtrlAudio () {
document.querySelector("#ctrl").pause();
document.querySelector("#ctrl").play();
}
function playReadyAudio () {
document.querySelector("#ctrl").pause();
document.querySelector("#ready").play();
}
function playSucAudio () {
document.querySelector("#ctrl").pause();
document.querySelector("#suc").play();
}
function playFailedAudio () {
document.querySelector("#ctrl").pause();
document.querySelector("#failed").play();
}
}