source: components/admin/Calculations.jsx@ faff334

main
Last change on this file since faff334 was faff334, checked in by anastasovv <simon@…>, 2 years ago

Ability for admin to watch live games and for user to see games history

  • Property mode set to 100644
File size: 5.4 KB
Line 
1import React from 'react'
2
3
4function calculateHandValue(cards) {
5 let value = 0;
6 let aces = 0;
7 for (let i = 0; i < cards.length; i++) {
8 let card = cards[i];
9 if (card.substring(1) === 'A') {
10 value += 11;
11 aces++;
12 } else if (card.substring(1) === 'X' || card.substring(1) === 'J' || card.substring(1) === 'Q' || card.substring(1) === 'K') {
13 value += 10;
14 } else {
15 value += parseInt(card.substring(1));
16 }
17 }
18 while (value > 21 && aces > 0) {
19 value -= 10;
20 aces--;
21 }
22 return value;
23}
24
25const hands = [
26 'Royal Flush',
27 'Straight Flush',
28 'Four of a Kind',
29 'Full House',
30 'Flush',
31 'Straight',
32 'Three of a Kind',
33 'Two Pairs',
34 'Pair',
35 'High Card',
36]
37
38const order = "23456789XJQKA"
39function getHandDetails(hand) {
40 const cards = hand
41 const faces = cards.map(a => String.fromCharCode([77 - order.indexOf(a[1])])).sort()
42 const suits = cards.map(a => a[0]).sort()
43 const counts = faces.reduce(count, {})
44 const duplicates = Object.values(counts).reduce(count, {})
45 const flush = suits[0] === suits[4]
46 const first = faces[0].charCodeAt(1)
47 const straight = faces.every((f, index) => f.charCodeAt(1) - first === index)
48 let rank =
49 (flush && straight && 1) ||
50 (duplicates[4] && 2) ||
51 (duplicates[3] && duplicates[2] && 3) ||
52 (flush && 4) ||
53 (straight && 5) ||
54 (duplicates[3] && 6) ||
55 (duplicates[2] > 1 && 7) ||
56 (duplicates[2] && 8) ||
57 9;
58
59 return { hand: hands[rank], highCard: faces.sort(byCountFirst).join("") }
60
61 function byCountFirst(a, b) {
62 //Counts are in reverse order - bigger is better
63 const countDiff = counts[b] - counts[a]
64 if (countDiff) return countDiff // If counts don't match return
65 return b > a ? -1 : b === a ? 0 : 1
66 }
67
68 function count(c, a) {
69 c[a] = (c[a] || 0) + 1
70 return c
71 }
72}
73
74function compareHands(h1, h2) {
75 let d1 = getHandDetails(h1)
76 let d2 = getHandDetails(h2)
77 if (hands.indexOf(d1.hand) === hands.indexOf(d2.hand)) {
78 if (d1.highCard < d2.highCard) {
79 return d2;
80 } else if (d1.highCard > d2.highCard) {
81 return d1;
82 } else {
83 return d1;
84 }
85 }
86 return hands.indexOf(d1.hand) < hands.indexOf(d2.hand) ? d2 : d1;
87}
88
89function getBestHandDetails(playerCards, tableCards) {
90 let combinations = getCardCombinations(playerCards, tableCards);
91
92 let h1 = combinations[0];
93 let bestHand = h1;
94 combinations.forEach(combination => {
95 bestHand = compareHands(h1, combination);
96 h1 = combination;
97 })
98
99 return bestHand;
100}
101
102function getCardCombinations(playerCards, tableCards) {
103 let combinations = [];
104
105 if (tableCards[3] !== undefined) {
106 combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[2], tableCards[3]])
107 combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[2], tableCards[3]])
108 combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[3]])
109 combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[2], tableCards[3]])
110 combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[2], tableCards[3]])
111 }
112
113 if (tableCards[4] !== undefined) {
114 combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[2], tableCards[4]])
115 combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[4], tableCards[3]])
116 combinations.push([playerCards[0], tableCards[0], tableCards[4], tableCards[2], tableCards[3]])
117 combinations.push([playerCards[0], tableCards[4], tableCards[1], tableCards[2], tableCards[3]])
118
119 combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[2], tableCards[4]])
120 combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[4], tableCards[3]])
121 combinations.push([playerCards[1], tableCards[0], tableCards[4], tableCards[2], tableCards[3]])
122 combinations.push([playerCards[1], tableCards[4], tableCards[1], tableCards[2], tableCards[3]])
123 combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[4]])
124
125 combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[2], tableCards[4]])
126 combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[3], tableCards[4]])
127 combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[2], tableCards[4]])
128 combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[3], tableCards[4]])
129
130 combinations.push([playerCards[0], playerCards[1], tableCards[2], tableCards[3], tableCards[4]])
131 }
132
133 combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[2]])
134
135 return combinations;
136}
137
138const Calculations = ({action, cards, cards2}) => {
139 if (action === 'calculateHandValue') {
140 return (
141 <>
142 {calculateHandValue(cards)}
143 </>
144 )
145 }
146 else {
147 return (
148 <>
149 { cards.length > 0 && cards2.length > 0 && getBestHandDetails(cards, cards2).hand}
150 { (cards.length === 0 || cards2.length === 0) && <span>-</span>}
151 </>
152 )
153 }
154}
155
156export default Calculations
Note: See TracBrowser for help on using the repository browser.