-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackJack.js
980 lines (805 loc) · 28.8 KB
/
BlackJack.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
//declaration of deck structure and content
const cards = {
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
ten: 10,
jack: 10,
queen: 10,
king: 10,
ace: 'ace'
}
const baseDeck = { }
const cardNames = Object.keys(cards)
const suitNames = ['clubs','hearts','spades','diamonds']
cardNames.forEach(cardName => {
baseDeck[cardName] = {}
baseDeck[cardName]['totalAmount'] = 4
baseDeck[cardName]['cardValue'] = cards[cardName]
baseDeck[cardName]['suits'] = {}
suitNames.forEach(suitName => {
baseDeck[cardName].suits[suitName] = 1
})
})
//const blackjackVariants = []
const splitRules = {
standard: {
maxAllowedSplits: 2,
canResplitAces: false,
onlySplitIdenticalValueTenCards: false,
multipleHitsOnSplitAce: false,
canHitAndDoubleOnSplits: true
},
variations: { //contains an array for each option that inc,udes all valid variations
maxAllowedSplits: [2,3,-1], //"-1" stands for unlimited splits
canResplitAces: [false, true],
onlySplitIdenticalValueTenCards: [false, true],
multipleHitsOnSplitAce: [false, true],
canHitAndDoubleOnSplits: [false, true],
}
}
let sideBetsReal = {
available: {
perfectPairMixed: {
includesSubOptions: true,
entered: false,
betAmount: 0,
dealerInvolved: false,
mixed: {
fulfilled: (hand) => {
if(hand[0].cardValue == hand[1].cardValue){
return true
}
return false
},
payout: '5:1',
},
colored:{
fulfilled: (hand) => {
if(hand[0].cardValue == hand[1].cardValue && (hand[0].suitName === 'hearts' || hand[1].suitName === 'diamonds') && (hand[0].suitName === 'hearts' || hand[1].suitName === 'diamonds')){
return true
}
return false
},
payout: '12:1'
},
perfect:{
fulfilled: (hand) => {
if(hand[0].cardValue == hand[1].cardValue && hasSameSuit(hand,2)){
return true
}
return false
},
payout: '30:1'
}
},
twentyOnePlusThree: {
includesSubOptions: true,
entered: false,
betAmount: 0,
flush: {
fulfilled: (hand) => {
if(hasSameSuit(hand,3)) return true
return false
},
payout: '5:1'
},
straight: {
fulfilled: (hand) => {
if(isStraight(hand,3)) return true
return false
},
payout: '10:1'
},
threeOfAKind: {
fulfilled: (hand) => {
if(hasSameValue(hand,3)) return true
return false
},
payout: '30:1'
},
straightFlush: {
fulfilled: (hand) => {
if(isStraight(hand,3) && hasSameSuit(hand,3)) return true
return false
},
payout: '40:1'
},
suitedTrips: {
fulfilled: (hand) => {
if(hasSameValue(hand,3) && hasSameSuit(hand,3)) return true
},
payout: '100:1'
}
},
royalMatch: {
includesSubOptions: true,
entered: false,
betAmount: 0,
easy: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2)) return true
return false
},
payout: '5:2'
},
easyBlackjack: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && isBlackjack(hand)) return true
return false
},
payout: '5:1'
},
royal: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && ((hand[0].cardName === 'queen' && hand[1].cardName === 'king') || (hand[0].cardName === 'king' && hand[1].cardName === 'queen'))) return true
return false
},
payout: '25:1'
},
},
overUnderThirteen: {
includesSubOptions: false,
entered: false,
betAmount: 0,
fulfilled: (hand) => {
let handValueArray = []
for (let i = 0; i < 2; i++){ //first two cards
if(hand[i].cardValue === 'ace') handValueArray.push(1) //ace counted as one
else handValueArray.push(hand[i].cardValue)
}
let totalValue = 0
handValueArray.forEach(value => {
totalValue += value
})
if(totalValue > 13 || totalValue < 13) return true
return false
},
payout: '1:1'
},
superSevens: {
includesSubOptions: true,
entered: false,
betAmount: 0,
single: {
fulfilled: (hand) => {
if(hand[0].cardName === 'seven') return true
return false
},
payout: '3:1'
},
pair: {
fulfilled: (hand) => {
if(hasSameValue(hand,2) && hand[0].cardName === 'seven') return true
return false
},
payout: '50:1'
},
suitedPair: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && hasSameValue(hand,2) && hand[0].cardName === 'seven') return true
return false
},
payout: '100:1'
},
set: {
fulfilled: (hand) => {
if(hasSameValue(hand,3) && hand[0].cardName === 'seven') return true
return false
},
payout: '500:1'
},
suitedSet: {
fulfilled: (hand) => {
if(hasSameSuit(hand,3) && hasSameValue(hand,3) && hand[0].cardName === 'seven') return true
return false
},
payout: '5000:1'
}
},
luckyLadies: {
includesSubOptions: true,
entered: false,
betAmount: 0,
any: {
fulfilled: (hand) => {
if(hand[0].cardValue + hand[1].cardValue == 20) return true
return false
},
payout: '4:1'
},
suited: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && hand[0].cardValue + hand[1].cardValue == 20) return true
return false
},
payout: '10:1'
},
matching: {
fulfilled: (hand) => {
if(hand[0].cardName == hand[1].cardName && hasSameSuit(hand,2) && hand[0].cardValue == 10 && hand[1].cardValue == 10) return true
return false
},
payout: '25:1'
},
queensOfHeart: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && hand[0].cardName === 'queen' && hand[1].cardName === 'queen') return true
return false
},
payout: '200:1'
},
queensOfHeartDealerBlackjack: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && hand[0].cardName === 'queen' && hand[1].cardName === 'queen' && isBlackjack(dealerHand)) return true
return false
},
payout: '1000:1'
}
}
}
}
let sideBets = {
available: {
perfectPairMixed: {
includesSubOptions: true,
entered: true,
betAmount: 100,
dealerInvolved: false,
mixed: {
fulfilled: (hand) => {
return true
},
payout: '5:1',
},
colored:{
fulfilled: (hand) => {
if(hand[0].cardValue == hand[1].cardValue && (hand[0].suitName === 'hearts' || hand[1].suitName === 'diamonds') && (hand[0].suitName === 'hearts' || hand[1].suitName === 'diamonds')){
return true
}
return false
},
payout: '12:1'
},
perfect:{
fulfilled: (hand) => {
return true
},
payout: '30:1'
}
},
twentyOnePlusThree: {
includesSubOptions: true,
entered: false,
betAmount: 0,
flush: {
fulfilled: (hand) => {
if(hasSameSuit(hand,3)) return true
return false
},
payout: '5:1'
},
straight: {
fulfilled: (hand) => {
if(isStraight(hand,3)) return true
return false
},
payout: '10:1'
},
threeOfAKind: {
fulfilled: (hand) => {
if(hasSameValue(hand,3)) return true
return false
},
payout: '30:1'
},
straightFlush: {
fulfilled: (hand) => {
if(isStraight(hand,3) && hasSameSuit(hand,3)) return true
return false
},
payout: '40:1'
},
suitedTrips: {
fulfilled: (hand) => {
if(hasSameValue(hand,3) && hasSameSuit(hand,3)) return true
},
payout: '100:1'
}
},
royalMatch: {
includesSubOptions: true,
entered: true,
betAmount: 10,
easy: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2)) return true
return false
},
payout: '5:2'
},
easyBlackjack: {
fulfilled: (hand) => {
return true
},
payout: '5:1'
},
royal: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && ((hand[0].cardName === 'queen' && hand[1].cardName === 'king') || (hand[0].cardName === 'king' && hand[1].cardName === 'queen'))) return true
return false
},
payout: '25:1'
},
},
overUnderThirteen: {
includesSubOptions: false,
entered: false,
betAmount: 0,
fulfilled: (hand) => {
let handValueArray = []
for (let i = 0; i < 2; i++){ //first two cards
if(hand[i].cardValue === 'ace') handValueArray.push(1) //ace counted as one
else handValueArray.push(hand[i].cardValue)
}
let totalValue = 0
handValueArray.forEach(value => {
totalValue += value
})
if(totalValue > 13 || totalValue < 13) return true
return false
},
payout: '1:1'
},
superSevens: {
includesSubOptions: true,
entered: false,
betAmount: 0,
single: {
fulfilled: (hand) => {
if(hand[0].cardName === 'seven') return true
return false
},
payout: '3:1'
},
pair: {
fulfilled: (hand) => {
if(hasSameValue(hand,2) && hand[0].cardName === 'seven') return true
return false
},
payout: '50:1'
},
suitedPair: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && hasSameValue(hand,2) && hand[0].cardName === 'seven') return true
return false
},
payout: '100:1'
},
set: {
fulfilled: (hand) => {
if(hasSameValue(hand,3) && hand[0].cardName === 'seven') return true
return false
},
payout: '500:1'
},
suitedSet: {
fulfilled: (hand) => {
if(hasSameSuit(hand,3) && hasSameValue(hand,3) && hand[0].cardName === 'seven') return true
return false
},
payout: '5000:1'
}
},
luckyLadies: {
includesSubOptions: true,
entered: true,
betAmount: 25,
any: {
fulfilled: (hand) => {
if(hand[0].cardValue + hand[1].cardValue == 20) return true
return false
},
payout: '4:1'
},
suited: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && hand[0].cardValue + hand[1].cardValue == 20) return true
return false
},
payout: '10:1'
},
matching: {
fulfilled: (hand) => {
if(hand[0].cardName == hand[1].cardName && hasSameSuit(hand,2) && hand[0].cardValue == 10 && hand[1].cardValue == 10) return true
return false
},
payout: '25:1'
},
queensOfHeart: {
fulfilled: (hand) => {
if(hasSameSuit(hand,2) && hand[0].cardName === 'queen' && hand[1].cardName === 'queen') return true
return false
},
payout: '200:1'
},
queensOfHeartDealerBlackjack: {
fulfilled: (hand,dealerHand) => {
console.log('dealer hand:',dealerHand)
return true
},
payout: '1000:1'
}
}
}
}
const numberOfDecks = 1
let currentStack = createStackFromDeck(baseDeck,numberOfDecks)
let playerObject = {
playerName:'RainbowPepe',
initialBalance: '100',
playerBet: '10',
currentBalance: '100',
hands: {},
hasInsurance: false
}
////////////////////////////////////////////////////////////////////////////////////////////////
let keepPlaying = true
while (keepPlaying) {
//place bets and sidebets
currentStack = createStackFromDeck(baseDeck,numberOfDecks) //restocks deck
playerObject.playerBet = 10
playerObject.hasInsurance = false //reset insurance-status before new round
playerObject.hands = {}
//start of game
let dealerHand = []
//setup player hand
playerObject = createHandInPlayerObject(playerObject)
playerObject.hands['0'].push(hit(currentStack))
playerObject.hands['0'].push(hit(currentStack))
//console.log("deckafterhit: ", currentStack)
if(isBlackjack(playerObject.hands['0'])) console.log('blackjack!')
else console.log('The value of your hand is: ' ,calculateHandValue(playerObject.hands['0']))
dealerHand.push(hit(currentStack))
dealerHand.push(hit(currentStack))
for(let i = 0; i < 10; i++){
console.log(generalSideBetCalculation(sideBets, playerObject.hands['0'],dealerHand))
}
return
//console.log(dealerHand[0]) //shows first card of dealer
if(insuranceAvailable(dealerHand)){
console.log("Dealer has an ace. Do you want to buy insurance?")
if("<Player buys insurance>"){
playerObject = setInsurance(playerObject)
}
}
Object.keys(playerObject.hands).forEach(handIndex => {
let hand = playerObject.hands[handIndex]
if(!canDraw(hand)){
console.log('You cannot draw with this hand! Hand-value is ' + calculateHandValue(hand))
return
}
//let interactableEmbed = ""
//if(isSplitable(hand, splitRules)){ interactableEmbed.add(splitOptionButton) } //pseudo-code
})
keepGoing = false
console.log('====================')
//console.log('<ALL EARNINGS/GAMESTATS HERE>')
//console.log('Play again?')
} // end of playing-loop
//functions
function randomDeckCard(deck){
const cardAmount = Object.keys(deck).length - 1
let pickedCardIndex = randomInRange(0,cardAmount)
while(deck[Object.keys(deck)[pickedCardIndex]].totalAmount < 1){
pickedCardIndex = randomInRange(0,cardAmount)
}//picks new card until picked card is still present in deck
const pickedCardName = Object.keys(deck)[pickedCardIndex]
let pickedCardObject = deck[pickedCardName]
//console.log(pickedCardName,Object.keys(pickedCardObject.suits).length)
const suitAmount = Object.keys(pickedCardObject.suits).length - 1
//console.log(suitAmount)
//return
let pickedSuitIndex = randomInRange(0,suitAmount)
while(pickedCardObject.suits[Object.keys(pickedCardObject.suits)[pickedSuitIndex]] < 1){
pickedCardIndex = randomInRange(0,cardAmount)
}//picks new suit until picked suit of card is still present in deck
const pickedSuitName = Object.keys(pickedCardObject.suits)[pickedSuitIndex]
pickedCardObject['suitName'] = pickedSuitName
pickedCardObject['cardName'] = pickedCardName
const tempReturnObjectUntilFailureFound = {
cardValue: '',
cardName: '',
suitName: '',
}
tempReturnObjectUntilFailureFound.cardName = pickedCardObject.cardName
tempReturnObjectUntilFailureFound.suitName = pickedCardObject.suitName
tempReturnObjectUntilFailureFound.cardValue = pickedCardObject.cardValue
//delete pickedCardObject['suits']
//delete pickedCardObject['totalAmount']
//console.log(tempReturnObjectUntilFailureFound)
return tempReturnObjectUntilFailureFound
}
function randomInRange(min, max){
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function takeCard(card,deck){
deck[card.cardName].totalAmount--
deck[card.cardName].suits[card.suitName]--
return deck
}
function calculateHandValue(hand){
let includesAce = false
hand.forEach(card => {
if(card.cardName === 'ace') includesAce = true
})
if(!includesAce){
let sumAmount = 0
hand.forEach(card => {
sumAmount += card.cardValue
})
return sumAmount
}
else{
return calculateAce(hand)
}
}
function calculateAce(hand){
let aceArray = []
hand.forEach(card => {
if(card.cardName === 'ace') aceArray.push(card)
})
restArray = hand.filter(card => card.cardName !== 'ace')
let handWithoutAce = 0
restArray.forEach(card => {
handWithoutAce += card.cardValue
})
let addAceToValue = handWithoutAce
aceArray.forEach(card => {
if(addAceToValue + 11 > 21){
//1
addAceToValue += 1
card.cardValue = 1
}
else if(addAceToValue + 11 <= 21){
//11
addAceToValue += 11
card.cardValue = 11
}
else console.log('else')
})
return addAceToValue
}
function isSplitable(hand, splitRules) {
if(hand.length != 2) return false //cant split 1 or 3 cards
if(hand[0].cardValue != hand[1].cardValue) return false //cant split non-equal cards
return true
}
function doubleDown(hand){
}
function hit(stack){
let randomCard = randomDeckCard(stack)
takeCard(randomCard, stack)
return randomCard
}
function stand(hand){
}
function createStackFromDeck(baseDeck,numberOfDecks){
returnStack = baseDeck
Object.keys(returnStack).forEach(cardName => {
returnStack[cardName].totalAmount *= numberOfDecks
//console.log(cardName)
Object.keys(returnStack[cardName].suits).forEach(suitName => {
returnStack[cardName].suits[suitName] *= numberOfDecks
})
})
return returnStack
//console.log(returnStack)
}
function isBlackjack(hand){
if(hand.length != 2) return false
if(calculateHandValue(hand) != 21) return false
return true
}
function splitHand(hand){
if(!isSplitable(hand, splitRules)) return
return [hand[0], hand[1]]
}
function createHandInPlayerObject(playerObject){
if(!Object.keys(playerObject).includes('hands')){
console.log('Object doesnt have "hands"-property!')
return
}
const arrayOfHandNames = Object.keys(playerObject.hands)
if(arrayOfHandNames.length === 0){
playerObject.hands['0'] = []
return playerObject
}
console.log("getObj:",typeof parseInt(arrayOfHandNames[arrayOfHandNames.length-1]))
if(isNaN(parseInt(arrayOfHandNames[arrayOfHandNames.length-1]))){
console.log("Error, couldnt recognize index of object: ",arrayOfHandNames[arrayOfHandNames.length-1])
return
}
let newObjectIndex = parseInt(arrayOfHandNames[arrayOfHandNames.length-1]) + 1
newObjectIndex = String(newObjectIndex)
playerObject.hands[newObjectIndex] = []
return playerObject
}
function setInsurance(playerObject){
if(!hasNeccessaryKeys(playerObject)) throw new Error("Player-object doesnt have neccessary keys!")
playerObject.hasInsurance = true
playerObject.playerBet = playerObject.playerBet * 1.5
return playerObject
}
function hasNeccessaryKeys(playerObject){
const requiredKeys = ['currentBalance','initialBalance','hands','playerName','playerBet','hasInsurance']
const presentKeys = Object.keys(playerObject)
requiredKeys.forEach(key => {
if(!presentKeys.includes(key)) return false
})
return true
}
function surrender(playerObject,dealerHand){
}
function isBust(hand){
if(calculateHandValue(hand) <= 21) return false
return true
}
function insuranceAvailable(dealerHand){
if(dealerHand[0].cardName === 'ace') return true //if first card of dealer is ace
return false
}
function canDraw(hand){
if(isBust(hand)) return false
if(isBlackjack(hand)) return false
if(calculateHandValue(hand) >= 21) return false
//if(!isSplitable(hand, splitRules)) return false
return true
}
function isStraight(hand,firstXCards){
let cardValueArray = []
if(insufficientCardsInHand(hand,firstXCards)) return false
for(let i = 0; i < firstXCards; i++) {
//console.log(i)
cardValueArray.push(hand[i].cardValue)
}
/*hand.forEach(card => {
cardValueArray.push(card.cardValue)
})*/
cardValueArray.sort()
for(let i = 1; i < firstXCards; i++) {
//console.log(cardValueArray[i-1], cardValueArray[i])
if(cardValueArray[i-1] + 1 != cardValueArray[i]) return false
}
return true
}
function hasSameSuit(hand,firstXCards){
if(insufficientCardsInHand(hand,firstXCards)) return false
for(let i = 1; i < firstXCards; i++) {
if(hand[i-1].suitName != hand[i].suitName) return false
}
return true
}
function hasSameValue(hand,firstXCards){
if(insufficientCardsInHand(hand,firstXCards)) return false
for(let i = 1; i < firstXCards; i++) {
if(hand[i-1].cardValue != hand[i].cardValue) return false
}
return true
}
function insufficientCardsInHand(hand,expectedCardsAmount){
if(Object.keys(hand).length != expectedCardsAmount) return true
return false
}
//===========Sidebet functions================
function calculateSideBetEarnings(fulfilledBets){
let wonBetObject = {
wonBetNames: [],
totalPayout: 0
}
//console.log(fulfilledBets)
fulfilledBets.forEach(bet => {
let payoutRatio = getNetPayoutFromRatio(bet.payout)
const finalPayout = bet.betAmount * payoutRatio
wonBetObject.wonBetNames.push(bet.betName)
wonBetObject.totalPayout += finalPayout
})
return wonBetObject
}
function filterEqualCategoryBets(fulfilledBets){
let dublicateCategories = []
let categoryNamesObject = {}
let filteredObjects = []
//console.log(fulfilledBets)
fulfilledBets.forEach(bet => {
if(!Object.keys(categoryNamesObject).includes(bet.betName)){
categoryNamesObject[bet.betName] = []
}
categoryNamesObject[bet.betName].push(bet)
})
//console.log(categoryNamesObject)
Object.keys(categoryNamesObject).forEach(categoryName => {
if(Object.keys(categoryName).length > 1) dublicateCategories.push(categoryName)
})
//console.log(dublicateCategories)
//console.log(categoryNamesObject)
dublicateCategories.forEach(category => {
let finalCategoryElement = {payout: '1:1'}
categoryNamesObject[category].forEach(betObject => {
//console.log(categoryNamesObject[category])
let payoutRatio = getNetPayoutFromRatio(betObject.payout)
if(payoutRatio > getNetPayoutFromRatio(finalCategoryElement.payout)) finalCategoryElement = betObject
//console.log(finalCategoryElement)
})
filteredObjects.push(finalCategoryElement)
})
//console.log(filteredObjects)
return filteredObjects
}
function getNetPayoutFromRatio(payoutRatio) {
let payoutArray = payoutRatio.split(':')
return (parseInt(payoutArray[0]) / parseInt(payoutArray[1]))
}
function getFulfilledBets(sideBets, hand, dealerHand){
let fulfilledBets = []
//let sideBetsKeys = Object.keys(sideBets)
sideBets.forEach(sideBet => {
if(Object.keys(sideBet).includes('fulfilled')){
//console.log(hand)^
if(sideBet.entered === true){
if(sideBet.fulfilled(hand,dealerHand) === true){
delete sideBet['entered']
sideBet.fulfilled = true
fulfilledBets.push(sideBet)
}
}
}
})
return fulfilledBets
}
function createSideBetList(data){
if(!Object.keys(data)) return false
let isObjectCandidates = []
if(!Object.keys(data).includes('available')) return false
Object.keys(data.available).forEach(key => {
//console.log(typeof data.available[key])
if(Object.keys(data.available[key]).includes('includesSubOptions')) {
if(data.available[key].includesSubOptions === false){
let appendPropObject = data.available[key]
delete appendPropObject['includesSubOptions']
appendPropObject['betName'] = key
isObjectCandidates.push(appendPropObject)
}
}
if(Object.keys(data.available[key]).includes('includesSubOptions')){
if(data.available[key].includesSubOptions === true){
//console.log(Object.keys(data.available[key]))
Object.keys(data.available[key]).forEach(subOption => {
//console.log(subOption)
let appendPropObject = {}
appendPropObject['betName'] = key
appendPropObject['entered'] = data.available[key].entered
appendPropObject['betAmount'] = data.available[key].betAmount
appendPropObject['fulfilled'] = data.available[key][subOption].fulfilled
appendPropObject['payout'] = data.available[key][subOption].payout
if(typeof data.available[key][subOption] === 'object') isObjectCandidates.push(appendPropObject)
})
}
}
})
return isObjectCandidates
}
function generalSideBetCalculation(sideBets, playerHand, dealerHand){
let formattedSideBetList = createSideBetList(sideBets)
let fulfilledBets = getFulfilledBets(formattedSideBetList,playerHand, dealerHand)
let filteredFulfilledList = filterEqualCategoryBets(fulfilledBets)
let totalPayout = calculateSideBetEarnings(filteredFulfilledList)
return [filteredFulfilledList, totalPayout]
}