-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
1126 lines (1015 loc) · 31.5 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
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var app = angular.module('palace-game', ['ngAnimate']);
app.factory('DATASTORE', function(){
var storage = {};
//shuffle deck
storage.shuffle = function(array) {
/**
* Randomize array element order in-place.
* Using Durstenfeld shuffle algorithm.
*/
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
};
//generate Deck
storage.makeDeck = function(){
var deck = [];
var id = 1;
var suits = ["spades","hearts","clubs","diamonds"];
suits.forEach(function(suit){
for(var i=1;i<14;i++){
deck.push({
id:id,
suit:suit,
value:i,
hidden:false
});
id++;
}
});
return storage.shuffle(deck);//shuffle before return for convenience
};
return storage;
});//end of service
app.controller('MainCtrl', ['$scope', '$q', '$timeout', 'DATASTORE', function($scope, $q, $timeout, DATASTORE){
//ng-style - controls image on JQK
$scope.getFace = function(suit,value){
switch(suit){
case "spades":
case "clubs":
switch(value){
case 11: return { "background-image" : "url(img/jack.png)" };
case 12: return { "background-image" : "url(img/queen.png)" };
case 13: return { "background-image" : "url(img/king.png)" };
}
case "hearts":
case "diamonds":
switch(value){
case 11: return { "background-image" : "url(img/jack2.png)" };
case 12: return { "background-image" : "url(img/queen2.png)" };
case 13: return { "background-image" : "url(img/king2.png)" };
}
}
};
// controls number of stuff in the card center
$scope.isFace = function(value){
return value > 10;
};
//ng-style - controls icon
$scope.getSuit = function(suit){
switch(suit){
case "spades": return { "background-image" : "url(img/Emblem-spade.svg)" };
case "hearts": return { "background-image" : "url(img/Emblem-heart.svg)" };
case "clubs": return { "background-image" : "url(img/Emblem-club.svg)" };
case "diamonds": return { "background-image" : "url(img/Emblem-diamond.svg)" };
}
};
//ng-style - controls colors for numbers
$scope.color = function(suit){
switch(suit){
case "spades":
case "clubs": return { "color" : "#222" };
case "hearts":
case "diamonds": return { "color" : "#d00" };
}
};
// use JQK instead of numbers
$scope.JQK = function(value){
switch(value){
case 11: return "J";
case 12: return "Q";
case 13: return "K";
default: return value;
}
};
// controls number of stuff in the card center
$scope.getNum = function(value){
if(value < 11)
return new Array(value);
else{
return [];
}
};
//ng-style - show title
$scope.showMenu = function(){
if(!$scope.playingGame)
return { "opacity" : 1 , "z-index": 9};
else
return { "opacity" : 0 , "z-index": -9};
};
//ng-style - show deck during play, also controls show whose turn
$scope.showDeck = function(){
if($scope.playingGame)
return { "opacity" : 1 , "z-index": 9};
else
return { "opacity" : 0 , "z-index": -9};
};
//ng-style - show player area animations
$scope.showPlayer = function(player){
if(player.ready)
return { "opacity" : 1 };
else
return { "opacity" : 0 };
};
//ng-style - show current player hand
$scope.showHand = function(){
if($scope.handOn)
return { "opacity" : 1 };
else
return { "opacity" : 0 };
};
//turn on and off pile list
$scope.showPile = function(){
if($scope.pileOn)
$scope.pileOn = false;
else
$scope.pileOn = true;
};
//turn on and off tutorial
$scope.showTut = function(){
if($scope.tutOn)
$scope.tutOn = false;
else
$scope.tutOn = true;
};
//ng-style - glows and slide up selected cards that in cardsToPlay.
$scope.cardAnims = function(card){
//if card is selected
if($scope.getSelected().indexOf(card.id)!==-1){
//selected -> glow & slide up
return { "box-shadow" : "0px 0px 25px rgba(155,255,255,0.8)", "transform":"translateY(-50px)" };
}
//else if card is on hand (does not affect palace cards) but not of same value of as target value
else if ($scope.getCurrentHand().indexOf(card)!== -1 && $scope.cardsToPlay.value!==null){
if(card.value !== $scope.cardsToPlay.value){
return { "opacity": 0.75, "cursor": "default" };
}
}
};
//ng-style - moves deck mock card to simulate deck drawing
$scope.drawAnims = function(){
if($scope.player1.isDrawing){
return { "animation" : "drawToPlayer1 0.75s" };
}
else if($scope.player2.isDrawing){
return { "animation" : "drawToPlayer2 0.75s" };
}
else if($scope.player3.isDrawing){
return { "animation" : "drawToPlayer3 0.75s" };
}
else if($scope.player4.isDrawing){
return { "animation" : "drawToPlayer4 0.75s" };
}
else{
return { "opacity" : 0 };
}
};
//ng-style - moves deck mock card to simulate deck drawing
$scope.forfAnims = function(){
if($scope.player1.forfeiting){
return { "animation" : "drawToPlayer1 0.75s" };
}
else if($scope.player2.forfeiting){
return { "animation" : "drawToPlayer2 0.75s" };
}
else if($scope.player3.forfeiting){
return { "animation" : "drawToPlayer3 0.75s" };
}
else if($scope.player4.forfeiting){
return { "animation" : "drawToPlayer4 0.75s" };
}
else{
return { };
}
};
//ng-style - a 10 is played and the pile blows up
$scope.blowUpAnim = function(){
if($scope.blowUp){
return { "animation" : "pileBlowUp 0.75s" };
}
else{
return { };
}
};
//controls swap button text
$scope.swapPalaceBtn = "Change Upper Palace";
/* ---------- */
/* GAME STATE */
/* ---------- */
//controls sort function
$scope.weakToStrong = [3,4,5,6,9,11,12,13,1,7,8,2,10];
//controls if "PALACE (Play)" is shown or deck
$scope.playingGame = false;
//whose turn, what the game is doing
$scope.gameIsAt = "Setting up Game...";
//game is waiting for player input?
$scope.waitingForInput = true;
//whose turn is it?
$scope.nextPlayer = 0;
//show hand footer
$scope.handOn = false;
//show pile list
$scope.pileOn = false;
//show tutorial
$scope.tutOn = false;
//swap mode for player
$scope.swapMode = false;
//animate pile blowing up
$scope.blowUp = false;
//initiate form for checking cards to be played
//value prevents player from playing cards of different value
//cards keeps track of whole cards. this will be pushed to pile when "played()"
//e.g. cardsToPlay = {value:9,cards:[{}{}]}
$scope.cardsToPlay = {value:null,cards:[]};
//used to loop over players
var players = ['player1','player2','player3','player4'];
//get current player
$scope.getCurrentPlayer = function(){
return $scope[players[$scope.nextPlayer]];
};
//get hand cards
$scope.getCurrentHand = function(){
if(!$scope.handOn){
return [];
}
else{
return $scope.getCurrentPlayer().hand;
}
};
//get currently selected cards
$scope.getSelected = function(){
return $scope.cardsToPlay.cards.map(function(card){
return card.id;
});
};
$scope.isMagicOrAce = function(n){
return n === 1 || n === 2 || n === 7 || n === 8 || n === 10;
};
//clean slate for game
//human -> human player
//ready -> controls show animation
//first -> first turn? controls swapping upper deck with hand
//isDrawing -> controls draw animation
$scope.resetState = function(){
$scope.player1 = {
name: "Player 1",
human: false,
ready: false,
first: false,
isDrawing: false,
forfeiting: false,
sec_palace:[],
upp_palace:[],
hand:[],
};
$scope.player2 = {
name: "Player 2",
human: false,
ready: false,
first: false,
isDrawing: false,
forfeiting: false,
sec_palace:[],
upp_palace:[],
hand:[],
};
$scope.player3 = {
name: "Player 3",
human: false,
ready: false,
first: false,
isDrawing: false,
forfeiting: false,
sec_palace:[],
upp_palace:[],
hand:[],
};
$scope.player4 = {
name: "Player 4",
human: true,
ready: false,
first: false,
isDrawing: false,
forfeiting: false,
sec_palace:[],
upp_palace:[],
hand:[],
};
$scope.playingGame = false;
$scope.gameIsAt = "Setting up Game...";
$scope.waitingForInput = true;
$scope.nextPlayer = 0;
$scope.deck = DATASTORE.makeDeck();
$scope.pile = [];
};
$scope.resetState();
/**/
/* --------- */
/* GAME PLAY */
/* --------- */
$scope.startGame = function(){
//prevent double-tap => only start game once
if(!$scope.playingGame){
//disallow clicks and begin setup
$scope.waitingForInput = false;
$scope.playingGame = true;
$scope.setupGame();
}
};
//used to repeat things x times
$scope.times = function(n, iterator) {
var accum = Array(Math.max(0, n));
for (var i = 0; i < n; i++) accum[i] = iterator.call();
return accum;
};
//set up game. disallow clicks during set up.
$scope.setupGame = function(){
var rdyPlayers = {};
players.forEach(function(player){
rdyPlayers[player] = $q.defer();
});
var all = $q.all([rdyPlayers.player1.promise, rdyPlayers.player2.promise, rdyPlayers.player3.promise, rdyPlayers.player4.promise]);
function allSuccess(){
$scope.runNextTurn();
};
all.then(allSuccess);
//for each player...
players.forEach(function(player){
var deckAnimateTime = 750*(players.indexOf(player));
var playerAppearTime = 750*(players.indexOf(player)+1);
$timeout(function(){
//animate the deck toward the respective player and cancel animation in 500ms
$scope[player].isDrawing = true;
$timeout(function(){
$scope[player].isDrawing = false;
},750);
},deckAnimateTime);
$timeout(function(){
//3 cards each for...
//their secret palace
$scope.times(3,function(times){
//push random card of deck.length
$scope[player].sec_palace.push($scope.deck[0]);
//remove card from deck
$scope.deck.splice(0,1);
});
//their upper palace
$scope.times(3,function(times){
//push random card of deck.length
$scope[player].upp_palace.push($scope.deck[0]);
//remove card from deck
$scope.deck.splice(0,1);
});
//their hand
$scope.times(3,function(times){
//push random card of deck.length
$scope[player].hand.push($scope.deck[0]);
//remove card from deck
$scope.deck.splice(0,1);
});
//animate in. resolve promise
$scope[player].ready = true;
rdyPlayers[player].resolve(player+" is ready")
},playerAppearTime);
});
};
$scope.getPlayables = function(cardToBeat){
if (cardToBeat === 7){
return [3,4,5,6,7,8,2,10];
}
else if (cardToBeat === 8){
return [9,11,12,13,1,7,8,2,10];
}
else if (cardToBeat === 2||cardToBeat === 10){
return [3,4,5,6,9,11,12,13,1,7,8,2,10];
}
else if (cardToBeat !== null){
return $scope.playable.slice($scope.playable.indexOf(cardToBeat));
}
return [3,4,5,6,9,11,12,13,1,7,8,2,10];
}
//runs turn of current player, gets card to beat and calcs rules
$scope.runNextTurn = function(){
/*-----------------------*/
//SET RULES FOR UPCOMING PLAYER
/*-----------------------*/
//get card to beat
var cardToBeat = $scope.cardToBeat();
//get playables (cards that don't trigger forfeit)
//weakest ---> strongest
$scope.playable = $scope.getPlayables(cardToBeat);
//set player and hand, show hand
var player = $scope.getCurrentPlayer();
$scope.gameIsAt = player.name + "'s Turn";
$scope.handOn = true;
//reset form
$scope.cardsToPlay = {value:null,cards:[]};
//WAIT FOR HAND to Fade in
$timeout(function(){
//if human is false, run AI turn sequence
if(!player.human){
$scope.AITurn(player,cardToBeat);
}
//else just allow clicks, set next player on card play
else{
$scope.waitingForInput = true;
}
},500);
};
//FOR AI: general AI Turn sequence
$scope.AITurn = function(player,mustBeat){
var playerAfter = $scope.nextPlayer + 1 >= players.length ? 0 : $scope.nextPlayer + 1;
//swapMode promise
var swapMode = $q.defer();
//Upper Palace Swap Phase if player.first
if(!player.first){
$scope.enterSwapMode(player,swapMode);
}
else{
//immediate resolve swapmode
swapMode.resolve("finished swap mode");
}
//only continue if swapMode is resolved
swapMode.promise.then(function(str){
var handValues = [];
//if player.hand is empty
if(player.hand.length < 1){
//if player.upp-palace is empty
if(player.upp_palace.length < 1){
//do nothing, empty hand values means it's secret palace time
}
else{
//play upper palace
player.upp_palace.forEach(function(card){
handValues.push(card.value);
});
}
}
else{
//else play hand
player.hand.forEach(function(card){
handValues.push(card.value);
});
}
//ADVANCED : forfeit on purpose to take in pile if pile has great value (lots of magics or ace);
//ADVANCED : play hand and cards on upper palace if same value
//if handValues is still empty (Secret Palace Time)
if(handValues.length < 1){
$scope.cardsToPlay.cards.push(player.sec_palace[Math.floor(Math.random()*player.sec_palace.length)])
$timeout(function(){
$scope.playCards(player);
},500);
}
//if hand has one card or all same cards
else if(handValues.length===1||handValues.allValuesSame()){
//if playable, play them all
if($scope.playable.indexOf(handValues[0])!==-1){
$scope.cardsToPlay.value = handValues[0];
//POSSIBLE COMBO:
//search upper_palace for any cards of same value as $scope.cardsToPlay.value
//if there is a card
if(player.upp_palace.getFirstElementThat(function(card){
return card.value === $scope.cardsToPlay.value;
})){
$scope.comboSelect(player);
$timeout(function(){
$scope.playCards(player);
},500);
}
//else, just play what's on hand
else{
$scope.selectCards(player);
$timeout(function(){
$scope.playCards(player);
},500);
}
}
else{
//else, gotta forfeit
$scope.forfeit(player);
}
}
//hand has different cards, more than one card
else{
//sort handValues from weakest to greatest
handValues = handValues.exampleSort($scope.weakToStrong);
//if player after doesn't have a hand,
if ($scope[players[playerAfter]].hand.length<1){
// if player after is on final card
if($scope[players[playerAfter]].sec_palace.length<2){
//drop 10 since blowing up clears way for player after
var preferredValues = handValues.filter(function(number){
return number !== 10;
});
//if you have a K, or 1, drop 7, since 13 or 1 is better
if(preferredValues.indexOf(1)!==-1||preferredValues.indexOf(13)!==-1){
preferredValues = preferredValues.filter(function(number){
return number !== 7;
});
}
//if no 9, J, Q, K, 1 drop 8 to prevent suicide
if(preferredValues.filter(function(number){
return [9,11,12,13,1,7,8,2,10].indexOf(number)!==-1;
}).length<1){
preferredValues = preferredValues.filter(function(number){
return number !== 8;
});
}
//cycle handValues from left to right, find strongest playable value. and set to cardstoplay.value
$scope.cardsToPlay.value = preferredValues.reduce(function(curr,next){
if(preferredValues.indexOf(curr) < preferredValues.indexOf(next) && $scope.playable.indexOf(next)!==-1){
curr = next;
}
return curr;
},preferredValues[preferredValues.length-1]);
}
//else
else{
if($scope[players[playerAfter]].upp_palace.length){
//if player is on upper palace
var enemyHighest = $scope[players[playerAfter]].upp_palace.reduce(function(curr,next){
if(curr.value<next.value){
curr = next;
}
return curr;
},{value:0}).value;
if(!$scope.isMagicOrAce(enemyHighest)&&$scope.playable.indexOf(enemyHighest)){
//if enemy highest is a king or worse, play the weakest card that is playable && beats enemyHighest.
var beatsEnemy = $scope.playable.slice($scope.playable.indexOf(enemyHighest)+1);
$scope.cardsToPlay.value = handValues.reduce(function(curr,next){
if(handValues.indexOf(curr) > handValues.indexOf(next) && beatsEnemy.indexOf(next)!==-1){
curr = next;
}
return curr;
},handValues[handValues.length-1]);
}
else{
//nothing happens, play as usual
}
}
else{
//nothing happens, play as usual
}
}
}
//if current selected value is unplayable, default to regular search
if($scope.playable.indexOf($scope.cardsToPlay.value)===-1){
//cycle handValues from left to right, find weakest playable value. and set to cardstoplay.value
$scope.cardsToPlay.value = handValues.reduce(function(curr,next){
if(handValues.indexOf(curr) > handValues.indexOf(next) && $scope.playable.indexOf(next)!==-1){
curr = next;
}
return curr;
},handValues[handValues.length-1]);
}
//INTEL
//console.log(player.name+", hand: "+handValues);
//console.log("playable: "+$scope.playable);
//FAILSAFE, prevent AI playing last card in hand even if not valid: if $scope.cardsToPlay.value isn't a playable value, forfeit.
if($scope.playable.indexOf($scope.cardsToPlay.value)===-1){
$scope.forfeit(player);
}
else{
//find all cards that have cardstoplay.value and push them to cardstoplay.cards
$scope.selectCards(player);
$timeout(function(){
$scope.playCards(player);
},500);
}
}
});
};
//FOR AI: get the Number value i'd need to beat on the pile
$scope.cardToBeat = function(){
if($scope.pile.length===0){
return null;
}
return $scope.pile[$scope.pile.length-1].value;
};
//FOR AI: Pushes all cards on hand of $scope.cardsToPlay.value to $scope.cardsToPlay.cards
$scope.selectCards = function(player){
//cycle hand or upper palace
// if cardstoplay.value is 1,2,7,8,10, just find one card to play
if($scope.isMagicOrAce($scope.cardsToPlay.value)){
if(player.hand.length < 1){
$scope.cardsToPlay.cards.push(player.upp_palace.getFirstElementThat(function(card){
return card.value === $scope.cardsToPlay.value;
}));
}
else{
//if you only have 1s or 2s or whatever left from victory, play them all
if(player.sec_palace.length<1 && player.upp_palace.length<1){
player.hand.forEach(function(card){
if(card.value===$scope.cardsToPlay.value){
//highlight card, rdy it for play
$scope.cardsToPlay.cards.push(card);
}
});
}
//else
else{
$scope.cardsToPlay.cards.push(player.hand.getFirstElementThat(function(card){
return card.value === $scope.cardsToPlay.value;
}));
}
}
}
else{
if(player.hand.length < 1){
player.upp_palace.forEach(function(card){
if(card.value===$scope.cardsToPlay.value){
//highlight card, rdy it for play
$scope.cardsToPlay.cards.push(card);
}
});
}
else{
player.hand.forEach(function(card){
if(card.value===$scope.cardsToPlay.value){
//highlight card, rdy it for play
$scope.cardsToPlay.cards.push(card);
}
});
}
}
};
//FOR AI: Pushes all cards on hand AND upper palace of $scope.cardsToPlay.value to $scope.cardsToPlay.cards
$scope.comboSelect = function(player){
//cycle hand or upper palace
player.upp_palace.forEach(function(card){
if(card.value===$scope.cardsToPlay.value){
//highlight card, rdy it for play
$scope.cardsToPlay.cards.push(card);
}
});
player.hand.forEach(function(card){
if(card.value===$scope.cardsToPlay.value){
//highlight card, rdy it for play
$scope.cardsToPlay.cards.push(card);
}
});
};
//FOR AI && PLAYER: play card(s) (make all selected cards float up and move to pile, remove from current hand)
$scope.playCards = function(player){
//prevent button mashing
//disallow clicks
if($scope.waitingForInput){
$scope.waitingForInput = false;
}
//if player.first is false, set it true now; they can't swap upper-palace cards now.
if(!player.first){
player.first = true;
}
//push cards to pile
$scope.cardsToPlay.cards.forEach(function(card){
//console.log(player.name+" played " + card.value);
$scope.pile.push(card);
});
//selects rdy to play cards based on id
var ids = $scope.cardsToPlay.cards.map(function(card){
return card.id;
});
//fade out current hand
$scope.handOn = false;
//remove cards
//drop played cards from secret palace
player.sec_palace = player.sec_palace.filter(function(card){
return ids.indexOf(card.id)===-1;
});
//drop played cards from upper palace
player.upp_palace = player.upp_palace.filter(function(card){
return ids.indexOf(card.id)===-1;
});
//drop played cards from hand
player.hand = player.hand.filter(function(card){
return ids.indexOf(card.id)===-1;
});
//reset hand positioning
document.getElementById("player-hand").style.webkitTransform = 'translateX(-50%)';
document.getElementById("player-hand").style.mozTransform = 'translateX(-50%)';
document.getElementById("player-hand").style.transform = 'translateX(-50%)';
//wait for card to appear on pile
$timeout(function(){
// give sometime for hand fadeout and card float animation (500), as well as pile addon animation (1000)
//if the played card is actually an unplayable, forfeit
if($scope.playable.indexOf($scope.pile[$scope.pile.length-1].value)===-1){
$timeout(function(){
$scope.forfeit(player);
},1000);
}
//else if (check player sec_palace, upp_palace, and hand, all [], roll victory)
else if(player.sec_palace.length<1 && player.upp_palace.length<1 && player.hand.length<1){
$scope.cueVictory(player);
}
//if top of pile is 2 or 8, another turn for current player (no draw, since it's not end of turn)
else if($scope.pile[$scope.pile.length-1].value===2||$scope.pile[$scope.pile.length-1].value===8){
$timeout(function(){
$scope.runNextTurn();
},1000);
}
//if top of pile is 10, or top four cards are same value
else if($scope.pile[$scope.pile.length-1].value===10 || topFourSameValue()){
//blow up deck, draw phase, then next player turn
$scope.blowUp = true;
//wait for shake animation to finish
$timeout(function(){
//empty pile, conclude anim and turn
$scope.pile = [];
$scope.drawCards(player);
$scope.blowUp = false;
//wait for pile to clear
$timeout(function(){
$scope.nextPlayer = $scope.nextPlayer + 1 >= players.length ? 0 : $scope.nextPlayer + 1;
$scope.runNextTurn();
},1000);
},1000);
}
else{
$scope.drawCards(player);
$timeout(function(){
//if top of pile is not 2 or 8, draw phase, next player turn
$scope.nextPlayer = $scope.nextPlayer + 1 >= players.length ? 0 : $scope.nextPlayer + 1;
$scope.runNextTurn();
},1000);
}
},500);
};
function topFourSameValue(){
if($scope.pile.length>=4){
return $scope.pile[$scope.pile.length-1].value === $scope.pile[$scope.pile.length-2].value && $scope.pile[$scope.pile.length-2].value === $scope.pile[$scope.pile.length-3].value && $scope.pile[$scope.pile.length-3].value === $scope.pile[$scope.pile.length-4].value;
}
else{
return false;
}
};
//FOR AI && PLAYER: draw cards phase. Only draw if deck.length > 0 and hand.length < 3
$scope.drawCards = function(player){
if(player.hand.length < 3 && $scope.deck.length > 0){
//trigger anim once
player.isDrawing = true;
$timeout(function(){
player.isDrawing = false;
},750);
while(player.hand.length < 3 && $scope.deck.length > 0){
player.hand.push($scope.deck[0]);
$scope.deck.splice(0,1);
}
}
};
//FOR AI && PLAYER: upper palace swap mode.
$scope.enterSwapMode = function(player, promise){
//if game is not in swapmode
if(!$scope.swapMode){
//prevent double tap and enter swapmode if human
if(player.human){
$scope.waitingForInput = false;
$scope.swapMode = true;
$scope.swapPalaceBtn = "Confirm Upper Palace";
}
//put Upper Palace cards on hand
player.upp_palace.forEach(function(card){
player.hand.push(card);
});
player.upp_palace = [];
//wait for hand to update
$timeout(function(){
//if AI
if(!player.human){
//get and use handValues instead so i can sort
var values = [];
player.hand.forEach(function(card){
values.push(card.value);
});
//sort
values = values.exampleSort($scope.weakToStrong);
//for each in handValues top 3, find a card in currhand with that value and push it to upp palace.
values.slice(3).forEach(function(value){
//get card to be put into upp-palace. default first card in hand
var swappedCard = player.hand.getFirstElementThat(function(card){
return card.value === value;
});
//push the card into the palace
player.upp_palace.push(swappedCard);
//remove card from hand
player.hand = player.hand.filter(function(card_in_hand){
return card_in_hand.id !== swappedCard.id;
});
});
//wait for upp_palace to update and hand
$timeout(function(){
//resolve promise
promise.resolve("finished swap mode");
},1000);
}
else{
//else if player, do nothing. reenable inputs
$scope.waitingForInput = true;
}
},1000);
}
//if game is in swapmode
else{
if($scope.cardsToPlay.cards.length === 3){
$scope.cardsToPlay.cards.forEach(function(swappedCard){
//push the card into the palace
player.upp_palace.push(swappedCard);
// remove selected cards from hand
player.hand = player.hand.filter(function(card_in_hand){
return card_in_hand.id !== swappedCard.id;
});
});
$scope.cardsToPlay.cards = [];
$scope.swapMode = false;
$scope.swapPalaceBtn = "Change Upper Palace";
}
else{
$scope.swapPalaceBtn = "You need to select 3 cards";
$timeout(function(){
$scope.swapPalaceBtn = "Confirm Upper Palace";
},1000);
}
}
};
//FOR AI && PLAYER: Forfeit function
$scope.forfeit = function(player){
//prevent button mashing
//disallow clicks
if($scope.waitingForInput){
$scope.waitingForInput = false;
}
//if player.first is false, set it true now; they can't swap upper-palace cards now.
if(!player.first){
player.first = true;
}
//fade out current hand
$scope.handOn = false;
//pile move animation control, start next turn
player.forfeiting = true;
$timeout(function(){
player.forfeiting = false;
$timeout(function(){
$scope.nextPlayer = $scope.nextPlayer + 1 >= players.length ? 0 : $scope.nextPlayer + 1;
$scope.runNextTurn();
},250);
},750);
//for each in pile, push to hand and remove from pile
$scope.pile.forEach(function(card){
player.hand.push(card);
});
$scope.pile = [];
};
//FOR PLAYER: select or deselect a card. depends on swapmode or if card is selected
$scope.selectCard = function(target_card){
//if target_card is not selected (select)
if($scope.getSelected().indexOf(target_card.id)===-1){
//if in swapmode, just add a card to cardsToPlay
if($scope.swapMode){
$scope.cardsToPlay.cards.push(target_card);
}
//else if not in swap mode, add value to limit future clicks
else{
if($scope.cardsToPlay.value === target_card.value || $scope.cardsToPlay.value === null){
//set cardsToPlay.value to selected card
//limit playables to value
$scope.cardsToPlay.cards.push(target_card);
$scope.cardsToPlay.value = target_card.value;
}
}
}
//else (card is selected, deselect it)
else{
$scope.cardsToPlay.cards = $scope.cardsToPlay.cards.filter(function(card){
return card.id !== target_card.id;
});
//remove value if no more cards
if($scope.cardsToPlay.cards.length<1){
$scope.cardsToPlay.value = null;
}
}
};
//FOR PLAYER: disables play button when no selected cards, or in swap mode
$scope.cantPlay = function(){
if($scope.swapMode){
return true;
}
else if($scope.cardsToPlay.cards.length<1){
return true;
}
else{
return false;
}
};