-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
734 lines (610 loc) · 15.9 KB
/
app.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/**
* Created by yuanfei on 15/8/14.
*/
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8080 });
var server_msg = new Messages(wss);
var gameMain = new Game(8);
//-------------Message
function Messages(wss)
{
this.wss_ = wss;
this.command_=new Object();
}
Messages.prototype.decode = function(data,ws)
{
var decodeMsg=eval ("(" + data + ")");
var cmd = decodeMsg.cmd;
var data = decodeMsg.data;
var fun=this.command_[cmd];
if(fun){
fun.call(null,data,ws);
}
}
Messages.prototype.register=function(cmd,fun)
{
this.command_[cmd] = fun;
}
Messages.prototype.send = function(cmd,data,ws)
{
var send_data=new Object();
send_data.cmd = cmd;
send_data.data = (data)?data:new Object();;
var d=JSON.stringify(send_data);
console.log("Send Data:%s",d);
ws.send(d)
}
//-----角色类------
function Role(ws)
{
this._ws=ws;
this.id=null;
this.type=null;
this.isDead = false;
this.num= null;
//是否为警长
this.sheriff = false;
//是否投票
this.isVote = false;
//是否准备
this.isReady = false;
//是否被守卫保护
this.protection = false;
//狼人是否杀过人
this.killed = false;
}
//--平民,守卫,预言家,女巫,狼人
Role.TYPE = {"PM":1,"SW":2,"YY":3,"NW":4,"LR":5};
//神的列表
Role.GOD=[Role.TYPE.YY,Role.TYPE.SW,Role.TYPE.NW];
Role.prototype.setData = function(value)
{
this.id = value.id;
this.type = value.type;
this.num = value.num;
}
Role.prototype.getData = function()
{
return {id:this.id,
type:this.type,
num:this.num,
isDead:this.isDead};
}
Role.prototype.getSocket= function () {
return this._ws
}
//重置状态
Role.prototype.resetState=function()
{
if(!this.isDead)
{
this.isDead=false;
this.isReady=false;
this.protection = false;
}
}
//----------游戏逻辑-----
function Game(num)
{
this.isStartGame=false
//初始化游戏角色人数
this.role_num = num;
this.client = new Array();
this._clientKey = new Object();
//是否选过警长
this.isChoosePoilceman = false;
this.wolfList = new Array();
//被杀死角色列表
this.roleDeadList = new Array();
//游戏中角色的ID
this.gameRoleId=new Array();
//投票结果
this.voteResult=new Object();
//已经投票了的角色
this.hasVoteRole = new Array();
//死亡角色类型列表
this.deadRoleTypelist = new Array();
this.Rounds=0;
this.prototypeRoleList= new Object()
//被杀人ID
this.killRoleID = null;
this.clickWolfNum=0;
}
Game.prototype.isStart = function()
{
return this.isStartGame;
}
Game.prototype.cleanVote=function()
{
this.voteResult=null;
this.voteResult=new Object();
this.hasVoteRole.length=0;
}
//新游戏开始清除数据
Game.prototype.cleanAll=function()
{
for(var key in this.client)
{
this.client[key]=null;
}
this.client.length=0
this._clientKey=null;
this._clientKey=new Object();
this.isChoosePoilceman=false;
this.wolfList.length=0;
this.roleDeadList.length=0;
this.gameRoleId.length=0;
this.deadRoleTypelist.length=0;
this.prototypeRoleList = null;
this.prototypeRoleList = new Object();
this.killRoleID = null;
this.clickWolfNum=0;
}
//--主动关闭客户端的成员删除
Game.prototype.closeRole = function(ws)
{
var index=0;
for(var key in this.client)
{
var role = this.client[key];
if(role.getSocket()==ws)
{
this.client[key]=null;
this.client.splice(index,1);
break;
}
index++;
}
if(this.client.length==0)
{
this.cleanAll();
this.isStartGame=false;
}
}
Game.prototype.getRoleByWS=function(ws)
{
for(var key in this.client)
{
var role = this.client[key];
if(role.getSocket()==ws)
{
return role;
}
}
}
//当前正在游戏的狼人角色排出被杀的
Game.prototype.wolfGameRole = function()
{
var result=new Array();
for(var key in this.client)
{
var role = this.client[key];
if(role.type==Role.TYPE.LR && !role.isDead)
{
result.push(role);
}
}
return result;
}
Game.prototype.isGameOver = function()
{
//判断角色类型抱团的是否都被杀死了,然后游戏结束了
for(var key in this.deadRoleTypelist){
var val = this.deadRoleTypelist[key];
if(val<1)
{
//根据类型返回游戏结果
this.gameOver((key==2)?2:1);
return false;
}
}
return true;
}
Game.prototype.pushRole=function(role)
{
this.client.push(role)
var role_num = this.getRoleSize();
if(role_num>=this.role_num && !this.isStartGame)
{
this.isStartGame = true;
this.startGame();
}
}
Game.prototype.getRoleSize=function()
{
var role_num = this.client.length;
console.log("当前请求开始游戏人数:%s",role_num);
return role_num
}
Game.prototype.getRoleById=function(roleId)
{
return this._clientKey[roleId];
}
Game.prototype.startGame=function()
{
console.log("startGame..");
var type_list = [Role.TYPE.SW,Role.TYPE.LR,Role.TYPE.NW,Role.TYPE.YY,Role.TYPE.LR];//,Role.TYPE.LR
var pm_num = 0;
var len = (type_list.length<this.role_num)?(this.role_num-type_list.length):0;
//插入平民到队列
for(var i=0;i<len;i++)
{
type_list.push(Role.TYPE.PM);
pm_num++;
}
this.deadRoleTypelist[0] =pm_num;//平民数量
this.deadRoleTypelist[1] = Role.GOD.length;//神的数量
this.deadRoleTypelist[2] = 2;//狼人数量
this.Rounds=0;
//角色分配数据
var index_num=1;
var sendData =new Array();
for(var i=0;i<4;i++)
{
var index = parseInt(Math.random()*4);
console.log("%s",i)
}
var tmpindex=0;
for(var key in this.client)
{
var role = this.client[key];
var data=new Object();
var index = parseInt(Math.random()*type_list.length);
//var index =tmpindex;
data.id = parseInt(index_num+1000);
data.type = type_list[index];
data.num = parseInt(index_num);
type_list.splice(index,1);
role.setData(data);
sendData.push(role.getData());
this._clientKey[data.id]=role;
this.gameRoleId.push(role.id);
index_num++;
tmpindex+=1;
}
this.client.forEach(function(role){
var data = role.getData();
data.roleList = sendData;
server_msg.send(1002,data,role.getSocket());
})
}
//判断客户都准备好了则开始天黑
Game.prototype.startNight = function()
{
//判断所有客户端都准备好了
for(var key in this.gameRoleId)
{
var role = this.getRoleById(this.gameRoleId[key]);
if(!role.isReady)
{
return false;
}
}
//重置角色的状态
var gameInstance=this
this.gameRoleId.forEach(function(roleid){
var role = gameInstance.getRoleById(roleid);
role.resetState();
});
this.Rounds+=1;
//开始播放守卫信息
this.broadcast(1003,{});
}
//广播消息
Game.prototype.broadcast=function(cmd,data)
{
this.client.forEach(function(role){
server_msg.send(cmd,data,role.getSocket());
});
}
Game.prototype.gameOver=function(value)
{
this.broadcast(1014,{result:value});
gameMain.cleanAll();
gameMain.isStartGame=false;
}
//获取死亡列表
Game.prototype.getDeadRoles=function()
{
var roles=[];
var roles_type=new Object();
var gameList = this.gameRoleId
var deadRoleTypeList = this.deadRoleTypelist;
for(var key in this.gameRoleId)
{
var role = this.getRoleById(this.gameRoleId[key])
if(role.isDead)
{
roles.push(role.getData());
var type=role.getData().type
if(type==Role.TYPE.PM)
{
deadRoleTypeList[0] -=1;
}else if(Role.GOD.indexOf(type)>-1){
deadRoleTypeList[1]-=1;
}else if(type==Role.TYPE.LR){
deadRoleTypeList[2]-=1;
}
}
}
//移除死亡角色
for(var key in roles)
{
var role= roles[key]
var index=gameList.indexOf(role.id)
if(index>-1)
{
gameList.splice(index,1)
}
}
this.roleDeadList=roles;
return roles;
}
//投票选警长
Game.prototype.vote = function()
{
var result = false;
if(!this.isChoosePoilceman)
{
result = true;
this.isChoosePoilceman = true;
}
//是否选警长
this.broadcast(1011,{result:result});
}
//统计结果
Game.prototype.hasVote=function(roleID)
{
this.hasVoteRole.push(roleID);
if(this.hasVoteRole.length==this.gameRoleId.length)
{
var result_id = 0;
var tmpVoteNum=0;
for(var key in this.voteResult)
{
var role_vote_num = this.voteResult[key];
if(role_vote_num>tmpVoteNum)
{
tmpVoteNum=role_vote_num;
result_id=parseInt(key);
}
}
//清理投票结果
this.cleanVote();
return result_id;
}
return -1;
}
//---请求登录
server_msg.register(1001,function(data,ws){
//游戏没有开始则加入角色到游戏队列
if(!gameMain.isStart())
{
var role = new Role(ws)
gameMain.pushRole(role);
}
});
//----点击准备
server_msg.register(1003,function(data,ws){
var role= gameMain.getRoleById(data.id);
role.isReady = true;
gameMain.startNight();
})
//--接收到守卫的请求
server_msg.register(1004,function(data,ws){
var sendData = {};
if(data.id)
{
var role= gameMain.getRoleById(data.id);
var round=gameMain.prototypeRoleList[role.id]
if(round && (round+1)==this.Rounds)
{
console.log("已经被守卫守过的人!!!!");
return false;
}
role.protection=true;
gameMain.prototypeRoleList[role.id]=this.Rounds;
sendData.id = role.id
}
//--广播守卫完成
gameMain.broadcast(1004,sendData);
});
//预言家确认验证的角色
function clickRole()
{
gameMain.broadcast(1006,{});
}
//--预言家验证角色
server_msg.register(1005,function(data,ws){
if(data.id)
{
var role= gameMain.getRoleById(data.id);
server_msg.send(1005,role.getData(),ws);
}else{
clickRole();
}
});
//--预言家确认,广播下一步
server_msg.register(1006,clickRole);
//--狼人开始杀人--
server_msg.register(1007,function(data,ws){
var wolf = gameMain.getRoleByWS(ws);
gameMain.clickWolfNum+=1;
if(data.id){
//狼人意见不统一
if(gameMain.killRoleID && gameMain.killRoleID!=data.id)
{
gameMain.clickWolfNum=0;
gameMain.killRoleID=null;
for(var key in gameMain.client)
{
var role = gameMain.client[key]
if(role.type==Role.TYPE.LR)
{
server_msg.send(1007,{result:0,deadRole:gameMain.killRoleID},role.getSocket());
}
}
}else{
gameMain.killRoleID=data.id;
}
}
console.log("狼人数量:%s,当前确认狼人数量%s",gameMain.wolfGameRole().length,gameMain.clickWolfNum)
if(gameMain.clickWolfNum>=gameMain.wolfGameRole().length )
{
var senddata = {result:1}
if(data.id)
{
var role= gameMain.getRoleById(data.id);
if(!role.protection)
{
role.isDead = true;
}
senddata.deadRole=gameMain.killRoleID
}
gameMain.killRoleID=null;
gameMain.clickWolfNum=0;
//result: 0是统一意见,1:杀人成功
//deadRole:被杀人ID
gameMain.broadcast(1007,senddata);
}
});
//--女巫环节
server_msg.register(1008,function(data,ws){
if(data.id)
{
var role = gameMain.getRoleById(data.id);
//毒人判断
if(data.type==1 && !role.isDead)
{
role.isDead = true;
}
//救人判断
if(data.type==2 && role.isDead)
{
role.isDead = false;
}
//判断女巫使用解药和守卫守卫是否对同一个人
if(data.type==2 && role.protection)
{
role.isDead=true;
}
}
if(data.type==1)
{
//女巫先救人后毒人
//返回死亡列表,不为空则不需要留遗言
var roles_dead=gameMain.getDeadRoles();
if(!gameMain.isGameOver()){
return false;
}
//广播被死亡角色列表
gameMain.broadcast(1009,{roles:roles_dead});
if(roles_dead.length<1)
{
//执行其他命令 选警长 或投票
gameMain.vote();
}
}
});
//--遗言确认,所有死亡人确认后在接下来游戏
server_msg.register(1010,function(data,ws){
var role = gameMain.getRoleByWS(ws);
var role_id = role.id;
var index=0;
for(var key in gameMain.roleDeadList)
{
var role = gameMain.roleDeadList[key];
if(role.id==role_id)
{
gameMain.roleDeadList.splice(key,1);
break;
}
index++;
}
if(gameMain.roleDeadList.length<1)
{
//--投票选警长还是投票杀人
gameMain.vote();
}
});
//投票选警长
server_msg.register(1012,function(data,ws){
if(data.id)
{
var roleVote = gameMain.voteResult[data.id];
var vote_num=(roleVote==null)?1:roleVote+=1;
gameMain.voteResult[data.id]=vote_num;
}
var voteRole = gameMain.getRoleByWS(ws);
var result_roleId=gameMain.hasVote(voteRole.id);
if(result_roleId>-1)
{
gameMain.broadcast(1012,{roleID:result_roleId});
}
});
//投票杀人
server_msg.register(1013,function(data,ws){
if(data.id) {
var roleVote = gameMain.voteResult[data.id];
var vote_num = (roleVote == null) ? 1 : roleVote += 1;
//警长多0.5票
var roleId = data.id;
var role = gameMain.getRoleById(roleId);
if (role.sheriff) {
vote_num += 0.5;
}
gameMain.voteResult[data.id] = vote_num;
}
var voteRole = gameMain.getRoleByWS(ws);
var result_id= gameMain.hasVote(voteRole.id);
var data = {result:0};
if(result_id>-1)
{
var roleId = gameMain.getRoleById(result_id);
roleId.isDead=true;
gameMain.getDeadRoles()
if(!gameMain.isGameOver()) return false;
if(result_id>0)
{
data.result=1;
data.roleID=result_id;
for(var key in gameMain.gameRoleId)
{
var roleId = gameMain.gameRoleId[key];
if(roleId==result_id)
{
gameMain.gameRoleId.splice(key,1);
break;
}
}
}
//0没有人死 1有人死
gameMain.broadcast(1013,data);
}
});
//移交警长
server_msg.register(1015,function(data,ws){
var role=gameMain.getRoleById(data.id);
role.sheriff=true;
});
var connlen=0;
wss.on('connection', function connection(ws) {
connlen+=1;
console.log("连接的客户端数量:%s",connlen);
gameMain.getRoleSize();
ws.on('message', function incoming(message) {
try{
console.log('received: %s', message);
server_msg.decode(message,ws);
}catch(e){
console.log("runing error:%s", e.toString());
}
});
ws.on('close',function close(){
console.log("客户端离开");
connlen-=1;
gameMain.closeRole(ws);
gameMain.getRoleSize();
gameMain.broadcast(10016,{})
});
});