-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.js
806 lines (682 loc) · 23.2 KB
/
game.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*
Requires a Game was already created with api.js and records exist in DB
*/
var ConflictHandler = require('./conflictHandler.js');
var AI = require('./ai.js');
var Utils = require('./utils.js');
var Player = require('./player.js');
var PlayerUser = require('./playerUser.js');
var PlayerAINaive = require('./playerAINaive.js');
function Game(game_manager, state, options) {
this.id = state.id;
this.map = state.map;
this.gm = game_manager;
this.io = game_manager.io;
this.db = game_manager.db;
this.campus = this.gm.cm.getCampusData(state.campus);
this.ai = new AI(this.campus, state.state);
this.state = state.state;
this.turn = state.turn;
this.options = {
quick_start : options && options.quick_start,
};
this.stage = state.stage;
this.stages = {
START : "start",
GRAB : "grab",
REINFORCEMENT : "reinforcement",
ORDERS : "orders",
END : "end",
};
// ordering of teams, e.g. ["eng","ilr", ...]
this.team_order = state.team_order;
// list of indices of teams that were destroyed
this.eliminated = [];
this.current_team_index = state.current_team;
// teams[team_id <string>] contains team data
// players: player list returns player socket list
this.teams = {};
for (var i = 0; i < this.team_order.length; i++) {
var team_id = this.team_order[i];
var team = new Team(team_id);
team.setIndex(i);
this.addTeam(team);
}
// who is left to move this turn
// boolean array: index = team index
this.waiting_on = new Array(this.team_order.length);
for (var i = 0; i < this.team_order.length; i++) {
this.waiting_on[i] = true;
}
// {<string> socketID : Player}
this.socket_player_map = {};
// all move data for this turn
// gets appended to as players contribute their moves
this.all_move_data = [];
this.initStage(this.stage);
// humec always ai for now
// players must be added after initializing stage
if("humec" in this.teams) {
this.addPlayerAI("humec");
}
}
Game.prototype = {
/**
* @param id <string> : id of team, e.g. 'cals' or 'eng'
*/
addTeam : function(team) {
this.teams[team.id] = team;
},
/**
* Adds a player to the game
* @param socket : client that is connecting
* @param team_id : team that they are joining
*/
addPlayerUser : function(socket, team_id) {
if (!(team_id in this.teams)) {
throw new Error('Player joining invalid team');
}
// subscribe the player to updates to the room
socket.join(this.id);
var team = this.teams[team_id];
var player = new PlayerUser({
team : team
}, socket);
this.socket_player_map[socket.id] = player;
this.initPlayer(player);
},
addPlayerAI : function(team_id) {
if (!(team_id in this.teams)) {
throw new Error('Player joining invalid team');
}
var team = this.teams[team_id];
var player = new PlayerAINaive({
team : team,
}, this.campus, this.state);
this.initPlayer(player);
},
initPlayer : function(player) {
console.log('player joined game ' + this.id);
this.teams[player.team.id].players.push(player);
// puts this client into being served by different stage handlers
switch (this.stage) {
case this.stages.START:
break;
case this.stages.GRAB:
player.getGrabMove(this.handleGrabMove.bind(this));
break;
case this.stages.REINFORCEMENT:
player.getReinforcementMove(this.getReinforcementCount(player.team.index), this.handleReinforcementMove.bind(this));
break;
case this.stages.ORDERS:
player.getOrdersMove(this.handleOrdersMove.bind(this));
break;
case this.stages.END:
break;
}
},
getTeamIndexFromId : function(team_id) {
return this.team_order.indexOf(team_id);
},
/**
* Sets the stage for all clients
*/
initStage : function(new_stage) {
var old_listener;
switch (this.stage) {
case this.stages.START:
old_listener = "";
break;
case this.stages.GRAB:
old_listener = "grab move";
break;
case this.stages.REINFORCEMENT:
old_listener = "reinforcement move";
break;
case this.stages.ORDERS:
old_listener = "orders move";
break;
case this.stages.END:
old_listener = "";
break;
}
var socket_ids = Object.keys(this.socket_player_map);
for (var i = 0; i < socket_ids.length; i++) {
var socket_id = socket_ids[i];
var socket = this.io.sockets.connected[socket_id];
socket.removeAllListeners(old_listener);
}
switch (new_stage) {
case this.stages.START:
break;
case this.stages.GRAB:
if (this.options.quick_start) {
this.randomizeRemaining();
var query_string = 'UPDATE global.games SET stage = \'reinforcement\'WHERE id = \'' + this.id + '\'';
this.db.query(query_string);
this.io.to(this.id).emit('stage update', {
stage : 'reinforcement',
});
// tell clients to pull the new data
this.io.to(this.id).emit('server sync');
this.initStage(this.stages.REINFORCEMENT);
return;
}
var team_ids = Object.keys(this.teams);
for (var i = 0; i < team_ids.length; i++) {
var team_id = team_ids[i];
var players = this.teams[team_id].players;
for (var j = 0; j < players.length; j++) {
var player = players[j];
player.getGrabMove(this.handleGrabMove.bind(this));
}
}
break;
case this.stages.REINFORCEMENT:
var team_ids = Object.keys(this.teams);
for (var i = 0; i < team_ids.length; i++) {
var team_id = team_ids[i];
var players = this.teams[team_id].players;
for (var j = 0; j < players.length; j++) {
var player = players[j];
player.getReinforcementMove(
this.getReinforcementCount(this.teams[team_id].index),
this.handleReinforcementMove.bind(this)
);
}
}
break;
case this.stages.ORDERS:
var team_ids = Object.keys(this.teams);
for (var i = 0; i < team_ids.length; i++) {
var team_id = team_ids[i];
var players = this.teams[team_id].players;
for (var j = 0; j < players.length; j++) {
var player = players[j];
player.getOrdersMove(this.handleOrdersMove.bind(this));
}
}
break;
case this.stages.END:
break;
}
this.stage = new_stage;
},
/**
* Takes a client and their move and handles the grab turn
*/
handleGrabMove : function (player, move_data) {
console.log(move_data);
var team_index = move_data.team_index || this.team_order.indexOf(move_data.team_id);
if (this.current_team_index != team_index|| this.state[move_data.piece].team != -1) {
console.log('invalid move data');
console.log("this.current_team_index: " , this.current_team_index);
console.log("this.state[move_data.piece]: ", this.state[move_data.piece].team);
return false;
}
console.log("game " + this.id + " update " + move_data.piece + " to team " + team_index);
this.nextTeamIndex();
this.turn++;
var query_string = 'UPDATE "state"."' + this.id + '" SET team =\'' + team_index + '\', units=1, player =\'Bryce\' WHERE piece_name = \'' + move_data.piece + '\'';
// update server state
this.state[move_data.piece].team = team_index;
this.state[move_data.piece].units = 1;
this.db.query(query_string);
var query_string = 'UPDATE global.games SET turn = ' + this.turn + ', cur_team = ' + this.current_team_index + ' WHERE id = \'' + this.id + '\'';
this.db.query(query_string, function(err, results) {
move_data.current_team = this.current_team_index;
move_data.turn = this.turn;
move_data.stage = this.stage;
move_data.team_index = team_index;
this.io.to(this.id).emit('grab update', move_data);
this.db.query('SELECT EXISTS(SELECT 1 FROM state."' + this.id + '" WHERE -1=team)', function(err, result) {
// STAGE CONTINUES
if (result.rows[0]['?column?']) {
var players = this.teams[this.team_order[this.current_team_index]].players;
for (var i = 0; i < players.length; i++) {
players[i].getGrabMove(this.handleGrabMove.bind(this));
}
// END OF STAGE
} else {
console.log(result);
var query_string = 'UPDATE global.games SET stage = \'reinforcement\'WHERE id = \'' + this.id + '\'';
this.db.query(query_string);
this.io.to(this.id).emit('stage update', {
stage : 'reinforcement',
});
// tell clients to pull the new data
this.io.to(this.id).emit('server sync');
this.initStage(this.stages.REINFORCEMENT);
}
}.bind(this));
}.bind(this));
},
/**
* Takes a client and their move and adds the reinforcements to the move list
*/
handleReinforcementMove : function (player, move_data) {
var team = move_data.meta.team;
var team_index = move_data.meta.team_index;
var coms = move_data.commands;
//var reinforcements_remaining = api.getReinforcementsFromState(this.state,team);
// XXX WILL FAIL IF GET_REINFORCEMENTS BECOMES ASYNC
// var reinforcements_remaining = this.gm.api.getReinforcements(this.id, team, function(x) {
// return x;
// });
var all_moves_valid = true;
for (var i = 0; i < coms.length; i++) {
var com = coms[i];
var piece = this.state[com.id];
// TODO make sure you own the piece and have enough to add
if (piece.team === team_index) {// && reinforcements_remaining - com.units >= 0) {
//piece.units += com.units;
//reinforcements_remaining -= com.units;
continue;
} else {
all_moves_valid = false;
break;
}
}
// append validated moves
if (all_moves_valid) {
this.all_move_data = this.all_move_data.concat(move_data.commands);
this.removeFromWaitingOn(team_index);
}
console.log(this.all_move_data);
var togo = this.getWaitingOn();
if (togo.length > 0) {
this.io.to(this.id).emit('waiting-on update', togo);
// no one remaining
// only runs once on last player to move
// apply moves, notify, update stage
} else {
console.log('no one is left!!', this.all_move_data);
this.applyReinforcementMoves();
}
},
applyReinforcementMoves : function() {
for (var i = 0; i < this.all_move_data.length; i++) {
var com = this.all_move_data[i];
var pieces = Object.keys(com);
for (var j = 0; j < pieces.length; j++) {
var piece_id = pieces[j];
var units = this.state[piece_id].units + com[piece_id];
this.state[piece_id].units = units;
this.db.query('UPDATE state."' + this.id + '" SET units=' + units + ' WHERE piece_name=\'' + piece_id + '\'', function(err, result) {
if (err) {
console.error('ERROR cannot update state in initReinforcementStage()');
}
});
}
}
this.io.to(this.id).emit('reinforcement update', this.all_move_data);
this.all_move_data = [];
// set to orders stage
this.db.query('UPDATE global.games SET stage = \'orders\'WHERE id = \'' + this.id + '\'', function(err, result) {
if (err) {
console.error('ERROR: cannot switch to orders stage');
}
this.stage = 'orders';
}.bind(this));
this.resetWaitingOn();
this.initStage(this.stages.ORDERS);
},
/**
* Takes a client and their move and handles the backend for the move
*/
handleOrdersMove : function(player, move_data) {
var team_index = move_data.team_index;
// check move is valid
for (var start in move_data.commands) {
var total = 0;
for (var end in move_data.commands[start]) {
total += move_data.commands[start][end];
}
// invalid move
if (total >= this.state[start]) {
console.log('INVALID MOVE, (total sent, total possible)', total, this.state[start]);
// remove all orders from the piece that's cheating
delete move_data.commands[start];
}
}
this.all_move_data[move_data.team_index] = move_data.commands;
console.log('all_move_data updated:', this.all_move_data);
this.removeFromWaitingOn(team_index);
var togo = this.getWaitingOn();
// someone left
if (togo.length > 0) {
this.io.to(this.id).emit('waiting-on update', togo);
// no one left
// only runs on last player to move
} else {
this.applyOrdersMoves();
}
},
applyOrdersMoves : function() {
var results = ConflictHandler.resolveAttacks(this.campus, this.state, this.all_move_data);
this.updatePartialState(results.new_state, false);
// set up for reinforcement stage
this.db.query('UPDATE global.games SET stage = \'reinforcement\' WHERE id = \'' + this.id + '\'', function(err, result) {
if (err) {
console.log('ERROR: cannot switch to reinforcement stage');
}
this.stage = 'reinforcement';
}.bind(this));
this.resetWaitingOn();
this.io.to(this.id).emit('orders update', results);
this.all_move_data = [];
this.initStage(this.stages.REINFORCEMENT);
},
getReinforcementCount : function(team_index) {
var state = this.state;
var reinforcements = {};
// contribution from number of pieces owned
var piece_count = 0;
for (var id in state) {
if (state.hasOwnProperty(id)) {
s = state[id];
if (s.team === team_index) {
piece_count++;
}
}
}
reinforcements.piece_count = {name : "Territory Count Bonus", count: Math.ceil(piece_count / 3)};
// contribution from regions
var regions = this.campus.map.regions;
var keys = Object.keys(regions);
for (var i = 0; i < keys.length; i++) {
var pieces = regions[keys[i]].pieces;
var bonus = regions[keys[i]].value;
for (var j = 0; j < pieces.length; j++) {
if (state[pieces[j]].team !== team_index) {
bonus = 0;
break;
}
}
if (bonus !== 0) {
var region_id = keys[i];
reinforcements[keys[i]] = {name : regions[region_id].name, count: bonus};
}
}
// contribution from contiguous
// contribution from other
var total = 0;
var keys = Object.keys(reinforcements);
for (var i = 0; i < keys.length; i++) {
total += reinforcements[keys[i]].count;
}
// always have at least 1 reinforcement
if (total === 0) {
reinforcements.other = {name : "Other", count : 1};
total++;
}
reinforcements.total = {name : "Total", count : total};
return total;
},
/**
* takes full data and updates everything
*/
updateGameData : function(data) {
if ("map" in data && this.map != data.map) {
console.error("must update game with same map");
console.log(data.map + " " + this.map);
return;
}
if ("id" in data && this.id != data.id) {
console.log('overwriting ' + this.id + " with " + data.id);
}
if("stage" in data) {
this.db.query('UPDATE global.games SET stage = \''+ data.stage +'\'WHERE id = \'' + this.id + '\'', function(err, result) {
if (err) {
console.log(err);
}
this.stage = 'orders';
}.bind(this));
this.resetWaitingOn();
this.stage = data.stage;
}
if("turn" in data) {
this.turn = data.turn;
}
if("team_order" in data) {
this.team_order = data.team_order;
this.waiting_on = new Array(this.team_order.length);
for (var i = 0; i < this.team_order.length; i++) {
this.waiting_on[i] = true;
}
for (var i = 0; i < this.team_order.length; i++) {
var query = "INSERT INTO teams.\"" + this.id + "\" VALUES (" + i + ",'"+ this.team_order[i] + "',1,TRUE,'')";
this.db.query(query, Utils.logIfError);
}
}
if ("state" in data) {
this.updatePartialState(data.state, true);
}
if("eliminated" in data) {
this.eliminated = data.eliminated;
}
if("current_team" in data) {
this.current_team_index = data.current_team;
}
},
randomizeRemaining : function() {
var pieces = Object.keys(this.state);
var new_state = {}
for (var i = 0; i < pieces.length; i++) {
var piece_id = pieces[i];
if (this.state[piece_id].team === -1) {
new_state[piece_id] = this.state[piece_id];
}
}
pieces = Utils.shuffle(Object.keys(new_state));
for (var i = 0; i < pieces.length; i++) {
var piece = new_state[pieces[i]];
piece.team = this.current_team_index;
piece.units = 1;
this.nextTeamIndex();
}
this.updatePartialState(new_state, true);
},
/**
* Takes a set of changes and applies them to the state
* {<piece_id> : { team: <int>, units: <int>}}
*/
updatePartialState : function(updates, update_clients) {
console.log(updates);
var keys = Object.keys(updates);
for(var i = 0; i < keys.length; i++) {
var piece_id = keys[i];
var update = updates[piece_id];
if("team" in update){
this.state[piece_id].team = update.team;
this.db.query('UPDATE state."' + this.id + '" SET team=' + update.team + ' WHERE piece_name=\'' + piece_id + '\'', function(err, result) {
if (err) {
console.error('ERROR cannot update state in initReinforcementStage()');
}
});
}
if("units" in update){
this.state[piece_id].units = update.units;
this.db.query('UPDATE state."' + this.id + '" SET units=' + update.units + ' WHERE piece_name=\'' + piece_id + '\'', function(err, result) {
if (err) {
console.error('ERROR cannot update state in initReinforcementStage()');
}
});
}
}
if (update_clients) {
this.io.to(this.id).emit('update partial state', updates);
}
},
/**
* Returns a list of int ids <int> of players that have yet to move for this turn
*/
getWaitingOn : function() {
ret = [];
for (var i = 0; i < this.waiting_on.length; i++) {
if (this.waiting_on[i]) {
ret.push(i);
}
}
return ret;
// this.db.query('SELECT index FROM teams."' + this.id + '" WHERE waiting_on=TRUE', function(err, result) {
// if (err) {
// console.error('ERROR: query checking teams still waiting on returned error')
// }
// if (result.rows.length > 0) {
// var waiting_on = new Array(result.rows.length);
// for (var i = 0; i < result.rows.length; i++) {
// waiting_on[i] = result.rows[i].index;
// }
// console.log('still waiting on:', waiting_on);
// }.bind(this));
},
/**
* sets every team to waiting on
* used at the start of every move
*/
resetWaitingOn : function() {
for (var key in Object.keys(this.waiting_on)) {
this.waiting_on[key] = true;
}
// this.db.query('UPDATE teams."' + this.id + '" SET waiting_on=TRUE', function(err, result) {
// if (err) {
// console.error('ERROR could not reset waiting_on=TRUE in initOrdersStage')
// }
// });
},
removeFromWaitingOn : function(index) {
//console.log('removing from waiting on', this.waiting_on, index);
this.waiting_on[index] = false;
//console.log('after', this.waiting_on);
// this.db.query('UPDATE teams."' + this.id + '" SET waiting_on=FALSE WHERE id=\'' + move_data.meta.team + '\'', function(err, result) {
// if (err) {
// console.error('ERR GAME.JS UPDATING WAITING_ON');
// }
// }.bind(this));
},
/**
* handles case where player is destroyed
*/
checkForEliminated : function() {
var size = this.team_order.length;
var eliminated = new Array();
while(size--) eliminated.push(false);
var keys = Object.keys(this.state);
for(var i = 0; i< keys.length(); i++){
var piece_id = keys[i];
var piece = this.state[piece_id];
var team = piece.team;
// death condition
var eliminated = piece.units < 1;
eliminated[team] = eliminated[team] || eliminated;
}
for(var j in eliminated){
if(eliminated[j]){
this.eliminateTeam(j);
}
}
},
eliminateTeam: function(team_idx) {
if(this.isEliminated(team_idx)) {
return;
}
this.eliminated.push(team_idx);
var state_changes = {};
var keys = Object.keys(this.state);
for(var i in keys){
var piece_id = keys[i];
var state = this.state[piece_id];
if(state.team == team_idx){
state_changes[piece_id] = {team: -1};
}
}
this.updatePartialState(state_changes);
},
isEliminated: function(team_idx) {
var idx = this.eliminated.indexOf(team_idx);
return idx > -1;
},
endGame: function(victor_idx){
this.io.to(this.id).emit('end game', {victor: victor_idx});
},
/**
* Gets the index of the next player to move
*/
nextTeamIndex : function() {
this.current_team_index = (this.current_team_index + 1) % this.team_order.length;
// TODO fixme
//if (this.current_team_index in this.ai_controlled) {
// this.moveAI(this.current_team_index);
//}
return this.current_team_index;
},
/**
* Master Override
* Resets the turn as if no one moved
*/
forceResetTurn : function() {
this.all_move_data = [];
this.resetWaitingOn();
this.io.to(this.id).emit('server sync');
},
/**
* Master Override
* Continues to next turn even if not all players have moved
*/
forceNextTurn : function() {
switch (this.stage){
case "grab":
this.current_team_index = this.nextTeamIndex();
var query_string = 'UPDATE global.games SET cur_team = ' + this.current_team_index + ' WHERE id = \'' + this.id + '\'';
this.db.query(query_string, Utils.logIfError);
break;
case "reinforcement":
this.applyReinforcementMoves();
break;
case "orders":
this.applyOrdersMoves();
break;
}
this.io.to(this.id).emit('server sync');
},
/**
* Takes a socket and removes it from the list of active players
* @param {Object} socket
*/
removeClientFromActive : function(socket) {
delete this.socket_player_map[socket.id];
},
/**
* Prints data related to this game for debugging
* @param {Object} socket : [OPTIONAL] if included, prints socket info
*/
printDebugInfo : function(socket) {
console.log('================================ Debug Info');
var client_ids = new Array(this.clients.length);
for (var i = 0; i < this.clients.length; i++) {
client_ids[i] = this.clients[i].id;
}
var out = {
stage : this.stage,
all_move_data : this.all_move_data,
clients : client_ids,
waiting_on : this.getWaitingOn(),
}
if (socket) {
out.socket_id = socket.id;
}
console.log(out);
console.log('===========================================');
}
}
// stores info related to a team
function Team(id, name) {
this.id = id;
this.name = name || '';
this.players = [];
}
Team.prototype.setIndex = function(idx) {
this.index = idx;
};
module.exports = Game;