-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconflictHandler.js
800 lines (669 loc) · 22.8 KB
/
conflictHandler.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
/**
* ConflictHandler.js
* Handles conflict results for a turn
* Only public function is resolveAttacks()
* returns instance of ConflictSetResult
*
*/
module.exports = {
resolveAttacks : function(campus_data, state, move_data) {
var ch = new ConflictHandler(campus_data, state, move_data);
return ch.getAllAttackResults();
},
}
ConflictHandler = function(campus_data, state, move_data) {
this.campus = campus_data;
this.state = state;
this.new_state = {};
this.set_result = new ConflictSetResult();
this.set_result.setCommands(move_data);
// inverted so it lists piece being attacked, and then list of pieces attacking (end : start)
this.inverse_orders = {};
for (var i = 0; i < move_data.length; i++) {
for (var start in move_data[i]) {
for (var end in move_data[i][start]) {
if (!this.inverse_orders[end]) {
this.inverse_orders[end] = {};
}
var units = move_data[i][start][end];
// over extending units - invalid move
if (this.state[start] < units) {
break;
}
this.state[start].units -= units;
this.inverse_orders[end][start] = new Attacker(i, units, start, end);
}
}
}
};
ConflictHandler.prototype = {
getUpdatedState : function() {
var ret = {};
ret.commands = this.move_data;
ret.new_state = {};
ret.conflicts = [];
var keys;
for (var i = 0; i < this.move_data.length; i++) {
var com = move_data[i];
// error handling
if (com == undefined) {
com = {};
}
keys = Object.keys(com);
if (keys.length === 0) {
continue;
}
start = keys[0];
var ends = com[start];
var total_attacking = 0;
keys = Object.keys(ends);
for (var j = 0; j < keys.length; j++) {
var end = keys[j];
total_attacking += ends[end];
}
this.new_state[start] = {};
this.new_state[start].units = this.state[start].units - total_attacking;
}
for (var atk_team = 0; atk_team < this.move_data.length; atk_team++) {
var move_set = this.move_data[atk_team];
for (var start in move_set) {
if (!move_set.hasOwnProperty(start)) {
continue;
}
//var ends = move_set[start];
// var end_keys = Object.keys(ends);
for (var end in move_set[start]) {
if (!move_set[start].hasOwnProperty(end)) {
continue;
}
// add endpoints to modified state
if (!new_state[end]) {
this.new_state[end] = {};
this.new_state[end].units = this.state[end].units;
}
var victor = -1;
var units = -1;
var teams = [];
var pieces = [end, start];
var playout = [];
var atk_units = move_set[start][end];
var dfd_team = this.state[end].team;
var dfd_units = new_state[end].units;
// index 0 is always defender
// allows for multiple attackers
teams.push(dfd_team);
teams.push(atk_team);
//if(atk_team === dfd_team) {
//}
//var liklihood = cdf(defending, attacking+defending, attacking/(attacking+defending));
while (atk_units > 0 && dfd_units > 0) {
var r = Math.random();
if (r > (atk_units / (atk_units + dfd_units))) {
dfd_units--;
// defender is always index 0
playout.push(0);
} else {
atk_units--;
playout.push(1);
}
}
if (atk_units > 0) {
this.new_state[end].units = atk_units;
this.new_state[end].team = this.state[start].team;
} else {
this.new_state[end].units = dfd_units;
}
var state_change = {};
state_change[start] = this.new_state[start];
state_change[end] = this.new_state[end];
ret.conflicts.push(new ConflictResult(pieces, playout, state_change));
}
}
}
ret.new_state = this.new_state;
return ret;
},
getAllAttackResults : function() {
/*
* ==== Attack Flow ====
*
* Attacks result in one of their children
* Continue through the tree until resolved
*
*
* bidirectional
* / \
* single multi
* \ / \
* \ / FFA
* \ / /
* ----RESOLVED----
*
*/
var live_teams_orig = this.getLivingTeams();
this.processBidirectionalAttacks();
this.processMultiAttacks();
this.processFFAAttacks();
this.processSingleAttacks();
this.set_result.setNewState(this.state);
this.addEliminatedToResults(live_teams_orig, this.set_result.attacks);
return this.set_result;
},
processBidirectionalAttacks : function() {
var defender_ids = Object.keys(this.inverse_orders);
for (var i = 0; i < defender_ids.length; i++) {
var defender_id = defender_ids[i];
// may have been handled and removed
if (!defender_id)
continue;
var attacker_ids = Object.keys(this.inverse_orders[defender_id]);
for (var j = 0; j < attacker_ids.length; j++) {
var attacker_id = attacker_ids[j];
// may have been handled and removed
if (!attacker_id)
continue;
// test if bidirectional
if (this.inverse_orders[attacker_id] && this.inverse_orders[attacker_id][defender_id]) {
var attacker1 = this.inverse_orders[defender_id][attacker_id];
var attacker2 = this.inverse_orders[attacker_id][defender_id];
var bidir_atk = new BidirectionalAttack(attacker1, attacker2);
var result_data = this.getBidirectionalAttackResult(bidir_atk);
this.set_result.attacks.push(result_data);
// remove the direction that was handled already
delete this.inverse_orders[defender_id][attacker_id];
delete this.inverse_orders[attacker_id][defender_id];
// piece_ids of pieces that won and lost
var winner = result_data.new_state[attacker_id] ? attacker_id : defender_id;
var loser = result_data.new_state[attacker_id] ? defender_id : attacker_id;
// add new command for the army to continue to attack the piece
this.inverse_orders[loser][winner] = new Attacker(result_data.pieces.indexOf(winner), result_data.new_state[winner].units, winner, loser);
}
}
}
},
processMultiAttacks : function() {
var defender_ids = Object.keys(this.inverse_orders);
for (var i = 0; i < defender_ids.length; i++) {
// piece id
var defender_id = defender_ids[i];
// may have been handled and removed
if (!defender_id)
continue;
// <string> piece_id[]
var attacker_ids = Object.keys(this.inverse_orders[defender_id]);
// Attack[]
var attackers = [];
for (var j = 0; j < attacker_ids.length; j++) {
var attacker_id = attacker_ids[j];
// push
var attacker = this.inverse_orders[defender_id][attacker_id];
attackers.push(new Attacker(attacker.team, attacker.units, attacker_id, defender_id));
}
if(!this.state[defender_id]){
}
var defender = new Defender(this.state[defender_id].team, this.state[defender_id].units, defender_id);
// multi attack
if (attackers.length > 1 && defender.units > 0) {
// XXX case where many different teams attack a building
var multi_attack = new MultiAttack(attackers, defender);
var results = this.getMultiAttackResult(multi_attack);
this.set_result.attacks.push(results);
var attackers = Object.keys(this.inverse_orders[defender.piece]);
for (var k = 0; k < attackers.length; k++) {
delete this.inverse_orders[defender.piece][attackers[k]];
}
var new_attacker_ids = Object.keys(results.new_state);
// defender lost, add ffa data into inverse orders
if (!(results.pieces[0] in results.new_state)) {
for (var k = 0; k < new_attacker_ids.length; k++) {
var new_attacker = new_attacker_ids[k];
var result = results.new_state[new_attacker];
this.inverse_orders[defender.piece][new_attacker] = new Attacker(result.team, result.units, new_attacker, defender.piece);
}
}
}
}
},
processFFAAttacks : function() {
var defender_ids = Object.keys(this.inverse_orders);
for (var i = 0; i < defender_ids.length; i++) {
// piece id
var defender_id = defender_ids[i];
// may have been handled and removed
if (!defender_id)
continue;
// <string> piece_id[]
var attacker_ids = Object.keys(this.inverse_orders[defender_id]);
// Attack[]
var attackers = [];
for (var j = 0; j < attacker_ids.length; j++) {
var attacker_id = attacker_ids[j];
// push
var attacker = this.inverse_orders[defender_id][attacker_id];
attackers.push(new Attacker(attacker.team, attacker.units, attacker_id, defender_id));
}
var defender = new Defender(this.state[defender_id].team, this.state[defender_id].units, defender_id);
// ffa attack
if (attackers.length > 1 && defender.units == 0) {
// XXX case where many different teams attack a building
var ffa_attack = new FFAAttack(attackers, defender);
var results = this.getFFAAttackResult(ffa_attack);
this.set_result.attacks.push(results);
}
}
},
processSingleAttacks : function() {
var defender_ids = Object.keys(this.inverse_orders);
for (var i = 0; i < defender_ids.length; i++) {
// piece id
var defender_id = defender_ids[i];
var attacker_ids = Object.keys(this.inverse_orders[defender_id]);
if (attacker_ids.length == 1) {
var attacker_id = attacker_ids[0];
var attacker = this.inverse_orders[defender_id][attacker_id];
var defender = new Defender(this.state[defender_id].team, this.state[defender_id].units, defender_id);
var single_attack = new SingleAttack(attacker, defender);
var result = this.getSingleAttackResult(single_attack);
this.set_result.attacks.push(result);
}
}
// get results
// push results
},
/**
* adds the victor arrays (battle results) to move data
* index of move_data is that teams moves
*/
getBidirectionalAttackResult : function(bidirectional_attack) {
var attacker1 = bidirectional_attack.attacker1;
var attacker2 = bidirectional_attack.attacker2;
var ret = []
var playout = [];
var pieces = [attacker1.piece, attacker2.piece];
var teams = [attacker1.team, attacker2.team];
// true if start wins
var winner = true;
while (attacker1.units > 0 && attacker2.units > 0) {
// random unit lost
attacker2_lost_unit = Math.random() > attacker2.units / (attacker1.units + attacker2.units);
if (attacker2_lost_unit) {
attacker2.units--;
} else {
attacker1.units--;
}
// push index of piece that lost unit
playout.push(+attacker2_lost_unit);
}
var new_state = {};
if (attacker1.units > 0) {
new_state[attacker1.piece] = {};
new_state[attacker1.piece].units = attacker1.units;
} else {
new_state[attacker2.piece] = {};
new_state[attacker2.piece].units = attacker2.units;
}
// no need to update state
return new BidirectionalAttackResult(pieces, teams, playout, new_state);
},
// {team, start_id, target_id, units}
// attack_list - list of pieces, with units and teams, attacking
// defender - team data of defender, with units and team info
getMultiAttackResult : function(multi_attack) {
var attackers = multi_attack.attackers;
var defender = multi_attack.defender;
var total_units = defender.units;
var pieces = new Array(attackers.length + 1);
var teams = new Array(attackers.length + 1);
pieces[0] = defender.piece;
teams[0] = defender.team;
for (var i = 0; i < attackers.length; i++) {
total_units += attackers[i].units;
pieces[i + 1] = attackers[i].piece;
teams[i + 1] = attackers[i].team;
}
var playout = [];
while (defender.units > 0 && total_units > defender.units) {
// someone loses a unit
total_units--;
var destroyed = Math.ceil(Math.random() * total_units);
// defender loses
if (destroyed <= defender.units) {
defender.units--;
playout.push(0);
// attacker loses
} else {
destroyed -= defender.units;
for (var i = 0; i < attackers.length; i++) {
destroyed -= attackers[i].units;
if (destroyed <= 0) {
attackers[i].units--;
playout.push(i + 1);
break;
}
}
}
}
var new_state = {};
// defender wins
if (defender.units > 0) {
new_state[defender.piece] = {};
new_state[defender.piece].units = defender.units;
// delete inverse orders
var attackers = Object.keys(this.state[defender.piece]);
for (var i = 0; i < attackers.length; i++) {
delete this.inverse_orders[defender.piece][attackers[i]];
}
this.updateState(new_state);
return new MultiAttackResult(pieces, teams, playout, new_state, true);
// defender loses
} else {
this.state[defender.piece].team = -1;
this.state[defender.piece].units = 0;
var team = attackers[0].team;
var all_same_team = true;
var unit_sum = 0;
for (var i = 1; i < attackers.length; i++) {
delete this.inverse_orders[defender.piece][attackers[i].piece];
unit_sum += attackers[i].units;
if (attackers[i].team != team) {
all_same_team = false;
break;
}
}
if (all_same_team) {
new_state[defender.piece] = {};
new_state[defender.piece].team = team;
new_state[defender.piece].units = unit_sum;
this.updateState(new_state);
return new MultiAttackResult(pieces, teams, playout, new_state, true);
} else {
for (var i = 0; i < attackers.length; i++) {
new_state[attackers[i].piece] = {};
new_state[attackers[i].piece].units = attackers[i].units;
}
var result = new MultiAttackResult(pieces, teams, playout, new_state, false);
// add new attacks
var attackers = Object.keys(result.new_state);
for (var i = 0; i < attackers.length; i++) {
var change = result.new_state[attackers[i]];
this.inverse_orders[defender.piece][attackers[i]] = new Attacker(change.team, change.units, attackers[i], defender.piece);
}
// NOT RESOLVED - don't update state
return result;
}
}
},
// for a Free for All trying to claim a piece with no defender
getFFAAttackResult : function(ffa_attack) {
var attackers = ffa_attack.attackers;
var defender = ffa_attack.defender;
// returns id of winning team, -1 if no winner
function lastTeamLeft(attackers) {
var winner = -1;
for (var i = 0; i < attackers.length; i++) {
var attacker = attackers[i];
if (attacker.units > 0) {
// init
if (winner === -1) {
winner = attacker.team;
// two teams left
} else if (winner !== attacker.team) {
return -1;
}
}
}
return winner;
}
var total_units = 0;
var pieces = [defender.piece];
var teams = [defender.team];
for (var i = 0; i < attackers.length; i++) {
total_units += attackers[i].units;
pieces.push(attackers[i].piece);
teams.push(attackers[i].team);
}
var winner = lastTeamLeft(attackers);
var playout = [];
while (winner == -1) {
// someone loses a unit
var destroyed = Math.ceil(Math.random() * total_units);
for (var i = 0; i < attackers.length; i++) {
destroyed -= attackers[i].units;
if (destroyed <= 0) {
attackers[i].units--;
total_units--;
playout.push(i);
break;
}
}
winner = lastTeamLeft(attackers);
}
var new_state = {};
new_state[defender.piece] = {
units : total_units,
team : winner
};
this.updateState(new_state);
return new FFAAttackResult(pieces, teams, playout, new_state);
},
getSingleAttackResult : function(single_attack) {
var attacker = single_attack.attacker;
var defender = single_attack.defender;
// index 0 is always defender
var pieces = [defender.piece, attacker.piece];
var teams = [defender.team, attacker.team];
var atk_team = attacker.team;
var atk_units = attacker.units;
var dfd_team = defender.team;
var dfd_units = defender.units;
var state_change = {};
var playout = [];
//var liklihood = cdf(defending, attacking+defending, attacking/(attacking+defending));
var range_multiplier = this.campus.getWeightBetween(attacker.piece, defender.piece);
while (atk_units > 0 && dfd_units > 0) {
var r = Math.random();
if (r < (atk_units * range_multiplier / (atk_units + dfd_units))) {
dfd_units--;
// defender is always index 0
playout.push(0);
} else {
atk_units--;
playout.push(1);
}
}
// attacker won!
if (atk_units > 0) {
state_change[defender.piece] = {};
state_change[defender.piece].units = atk_units;
state_change[defender.piece].team = atk_team;
// defender won
} else {
state_change[defender.piece] = {};
state_change[defender.piece].units = dfd_units;
}
this.updateState(state_change);
return new SingleAttackResult(pieces, teams, playout, state_change);
},
getLivingTeams : function() {
// hashset of living teams
// use array to int keys instead of string keys
var set = [];
var pieces = Object.keys(this.state);
for (var i = 0; i < pieces.length; i++) {
var team = this.state[pieces[i]].team;
set[team] = true;
}
// cast string keys to int before returning
ret = [];
var str_teams = Object.keys(set).sort();
for (var i = 0; i < str_teams.length; i++) {
ret.push(parseInt(str_teams[i]));
}
return ret;
},
/**
* Adds field "eliminated" : <int[]> to the last AttackResult
* involving a team if they no pieces at the end of the turn
*/
addEliminatedToResults : function(live_teams_orig, result_list) {
var live_teams_now = this.getLivingTeams();
// add final victor
if (live_teams_now.length === 1) {
result_list[result_list.length - 1].victor = live_teams_now[0];
}
var eliminated = [];
for (var i = 0; i < live_teams_orig.length; i++) {
if (live_teams_now.indexOf(live_teams_orig[i]) === -1) {
eliminated.push(live_teams_orig[i]);
}
}
for (var i = 0; i < eliminated.length; i++) {
debugger;
var team = eliminated[i];
for (var j = result_list.length - 1; j >= 0; j--) {
var result = result_list[j];
if (result.teams.indexOf(team) !== -1) {
if (!result.eliminated) {
result.eliminated = [];
}
result.eliminated.push(team);
}
}
}
},
updateState : function(updates) {
var keys = Object.keys(updates);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var update = updates[key];
if ('team' in update) {
this.state[key].team = update.team;
}
if ('units' in update) {
this.state[key].units = update.units;
}
}
},
}
Attacker = function(team, units, origin, target) {
this.team = team;
this.units = units;
this.piece = origin;
this.target = target;
}
Defender = function(team, units, piece) {
this.team = team;
this.units = units;
this.piece = piece;
}
SingleAttack = function(attacker, defender) {
if (attacker.target != defender.piece) {
throw "Invalid SingleAttack";
}
this.attacker = attacker;
this.defender = defender;
}
BidirectionalAttack = function(attacker1, attacker2) {
if (attacker1.target != attacker2.piece || attacker2.target != attacker1.piece) {
throw "Invalid BidirectionalAttack";
}
this.attacker1 = attacker1;
this.attacker2 = attacker2;
}
MultiAttack = function(attackers, defender) {
for (var i = 0; i < attackers.length; i++) {
if (attackers[i].target != defender.piece) {
throw "Invalid MultiAttack";
}
}
this.attackers = attackers;
this.defender = defender;
}
FFAAttack = function(attackers, defender) {
if (defender.team != -1) {
throw "Invalid FFAAttack";
}
this.attackers = attackers;
this.defender = defender;
}
/**
* <int> victor : team_index of the winner
* <int> units : how many units survived
* <int[]> teams : list of team_indexes involved
* <string[]> pieces : piece_ids involved. match indices to teams that own them
* <int[]> playout : index of pieces[] that loses in each kerfuffle.
*/
SingleAttackResult = function(pieces, teams, playout, new_state) {
this.pieces = pieces;
this.teams = teams;
this.playout = playout;
this.new_state = new_state;
this.type = "Single";
this.resolved = true;
};
BidirectionalAttackResult = function(pieces, teams, playout, new_state) {
this.pieces = pieces;
this.teams = teams;
this.playout = playout;
this.new_state = new_state;
this.type = "Bidirectional";
this.resolved = false;
}
// similiar to ConflictResult
// victors is a list
// units is a list corresponding to victors
MultiAttackResult = function(pieces, teams, playout, new_state, resolved) {
this.pieces = pieces;
this.teams = teams;
this.playout = playout;
this.new_state = new_state;
this.type = "Multi";
this.resolved = resolved || false;
};
MultiAttackResult.prototype = {
// must have either a resulting attack or state
setResultFFAAttack : function(atk) {
this.resolved = false;
this.result_ffa_attack = atk;
},
setNewState : function(new_state) {
this.new_state = new_state;
},
};
FFAAttackResult = function(pieces, teams, playout, new_state) {
if (teams[0] != -1) {
throw "Invalid FFAAttackResult";
}
this.pieces = pieces;
this.teams = teams;
this.playout = playout;
this.new_state = new_state;
this.type = "FFA";
this.resolved = true;
}
/**
* Results for a full set of conflicts in a turn
* commands: {<string> start : {<string> end : <int> units}}
* the moves sent by players
* conflicts: list of conflictResults
* includes playouts to view
* new_state: {<string> start : {<string> end : {<int> units, <int> team}}
* only includes changes made
* checks to make sure end state matches server
*/
ConflictSetResult = function() {
this.commands = {};
this.attacks = [];
this.new_state = {};
};
ConflictSetResult.prototype = {
setCommands : function(coms) {
this.commands = coms;
},
setAttacks : function(atks) {
this.attacks = atks;
},
setNewState : function(state) {
this.new_state = state;
},
};