1 | import { tables, deck } from './gameStates'
|
---|
2 |
|
---|
3 | import { hands, getBestHandDetails } from './handEvaluations';
|
---|
4 |
|
---|
5 | import axios from 'axios';
|
---|
6 |
|
---|
7 | require('dotenv').config();
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Replace deck if empty
|
---|
11 | */
|
---|
12 | export function checkDeckSize(tableId) {
|
---|
13 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
14 |
|
---|
15 | if (tables[tableIdx] !== undefined) {
|
---|
16 | if (tables[tableIdx].deck.length === 0) {
|
---|
17 | tables[tableIdx].deck = [...deck];
|
---|
18 | }
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Draw a SINGLE random card
|
---|
24 | */
|
---|
25 | export function drawASingleCard(tableId) {
|
---|
26 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
27 |
|
---|
28 | if (tables[tableIdx] !== undefined) {
|
---|
29 | checkDeckSize(tableId);
|
---|
30 |
|
---|
31 | let idx = Math.floor(Math.random() * tables[tableIdx].deck.length);
|
---|
32 | let card = tables[tableIdx].deck[idx];
|
---|
33 |
|
---|
34 | tables[tableIdx].deck.splice(idx, 1);
|
---|
35 |
|
---|
36 | return card;
|
---|
37 | }
|
---|
38 |
|
---|
39 | return undefined;
|
---|
40 | }
|
---|
41 |
|
---|
42 | export function getMaxBet(tableId) {
|
---|
43 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
44 |
|
---|
45 | if (tables[tableIdx] !== undefined) {
|
---|
46 | const table = tables[tableIdx];
|
---|
47 |
|
---|
48 | let maxBet = 0;
|
---|
49 | table.players.forEach(player => {
|
---|
50 | if (player.betAmount > maxBet) {
|
---|
51 | maxBet = player.betAmount;
|
---|
52 | }
|
---|
53 | })
|
---|
54 |
|
---|
55 | return maxBet;
|
---|
56 | }
|
---|
57 |
|
---|
58 | return 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | export function setNextPlayerIdx(tableId) {
|
---|
62 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
63 |
|
---|
64 | if (tables[tableIdx] !== undefined && !tables[tableIdx].ended) {
|
---|
65 | const table = tables[tableIdx];
|
---|
66 |
|
---|
67 | const remainingPlayers = table.players.filter(e=>e.isSatDown===true).filter(e=>e.isGhost===false);
|
---|
68 | if (remainingPlayers.length === 1) {
|
---|
69 | setWinnerDirect(table.id, remainingPlayers[0]);
|
---|
70 |
|
---|
71 | return ;
|
---|
72 | }
|
---|
73 |
|
---|
74 | const remainingPlayersWithCredits = table.players.filter(e=>e.isSatDown===true).filter(e=>e.isGhost===false).filter(e=>e.isFolded===false).filter(e=>e.credits > 0);
|
---|
75 | if (remainingPlayersWithCredits.length === 1) {
|
---|
76 | progressRoundTillTheEnd(table.id);
|
---|
77 |
|
---|
78 | return ;
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
|
---|
82 |
|
---|
83 | let counter = 10;
|
---|
84 |
|
---|
85 | while (true) {
|
---|
86 | counter--;
|
---|
87 |
|
---|
88 | table.turnIdx++;
|
---|
89 | table.turnIdx %= table.players.length;
|
---|
90 |
|
---|
91 | if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx].isSatDown && !table.players[table.turnIdx].isFolded) {
|
---|
92 | if (table.round >= 2 && table.players[table.turnIdx].credits === 0) continue;
|
---|
93 |
|
---|
94 | let prevTurnIdx = table.turnIdx;
|
---|
95 | table.turnTimeout = setTimeout(() => {
|
---|
96 | if (prevTurnIdx === table.turnIdx) {
|
---|
97 | if (table.players[table.turnIdx] !== undefined) {
|
---|
98 | table.players[table.turnIdx].isFolded = true;
|
---|
99 |
|
---|
100 | setNextPlayerIdx(table.id);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }, 30000);
|
---|
104 |
|
---|
105 | table.lastBet = getMaxBet(table.id) - table.players[table.turnIdx].betAmount;
|
---|
106 | if (table.round === 1 && getMaxBet(table.id) <= 20) table.lastBet = 20;
|
---|
107 |
|
---|
108 | return ;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (counter <= 0) {
|
---|
112 | return ;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | export function getCardsOnTable(tableId) {
|
---|
119 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
120 |
|
---|
121 | if (tables[tableIdx] !== undefined) {
|
---|
122 | const table = tables[tableIdx];
|
---|
123 |
|
---|
124 | if (table.round === 2) {
|
---|
125 | for (let i = 0; i < 3; i++) {
|
---|
126 | const card = drawASingleCard(table.id);
|
---|
127 |
|
---|
128 | if (card !== undefined) {
|
---|
129 | table.cards.push(card);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | else if (table.round > 2) {
|
---|
134 | const card = drawASingleCard(table.id);
|
---|
135 |
|
---|
136 | if (card !== undefined) {
|
---|
137 | table.cards.push(card);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | export function resetGame(tableId) {
|
---|
144 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
145 |
|
---|
146 | if (tables[tableIdx] !== undefined) {
|
---|
147 | const table = tables[tableIdx];
|
---|
148 |
|
---|
149 | table.started = false;
|
---|
150 | table.ended = false;
|
---|
151 | table.round = 0;
|
---|
152 | table.turnIdx = -1;
|
---|
153 | table.turnTimeout = null;
|
---|
154 | table.pot = 0;
|
---|
155 | table.lastBet = 20;
|
---|
156 | table.turnsSinceLastBet = 0;
|
---|
157 | table.deck = [...deck];
|
---|
158 |
|
---|
159 | table.players = table.players.filter(e=>e.isGhost === false).filter(e=>e.credits >= 20);
|
---|
160 |
|
---|
161 | table.players.forEach(player => {
|
---|
162 | player.credits = 0;
|
---|
163 | player.cards = [];
|
---|
164 | player.isFolded = false;
|
---|
165 | player.betAmount = 0;
|
---|
166 | player.wonAmount = 0;
|
---|
167 | player.hand = {
|
---|
168 | hand: '',
|
---|
169 | highCard: 0,
|
---|
170 | }
|
---|
171 | })
|
---|
172 |
|
---|
173 | table.onlyOnePlayerLeft = false;
|
---|
174 | table.winners = [];
|
---|
175 | table.splitWinners = false;
|
---|
176 | table.cards = [];
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | export function giveMoneyToTheWinners(tableId) {
|
---|
181 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
182 |
|
---|
183 | if (tables[tableIdx] !== undefined) {
|
---|
184 | const table = tables[tableIdx];
|
---|
185 |
|
---|
186 | table.players.forEach(player => {
|
---|
187 | let winnings = 0;
|
---|
188 | if (table.winners.indexOf(player) !== -1) {
|
---|
189 | // winner
|
---|
190 | winnings = 0;
|
---|
191 | table.players.forEach(tmpPlayer => {
|
---|
192 | winnings += Math.min(tmpPlayer.betAmount, player.betAmount);
|
---|
193 | })
|
---|
194 |
|
---|
195 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${player.id}&credits=${winnings}&game=poker&outcome=won`).then(postgreRes => {
|
---|
196 | if (postgreRes.data?.success) {
|
---|
197 | player.credits = postgreRes.data?.credits;
|
---|
198 | }
|
---|
199 | });
|
---|
200 | }
|
---|
201 | else {
|
---|
202 | // loser
|
---|
203 | winnings = player.betAmount;
|
---|
204 | table.players.forEach(tmpPlayer => {
|
---|
205 | if (table.winners.indexOf(tmpPlayer) !== -1) {
|
---|
206 | winnings -= tmpPlayer.betAmount;
|
---|
207 | }
|
---|
208 | })
|
---|
209 |
|
---|
210 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${player.id}&credits=${winnings}&game=poker&outcome=lost`).then(postgreRes => {
|
---|
211 | if (postgreRes.data?.success) {
|
---|
212 | player.credits = postgreRes.data?.credits;
|
---|
213 | }
|
---|
214 | });
|
---|
215 | }
|
---|
216 |
|
---|
217 | player.wonAmount = winnings;
|
---|
218 | })
|
---|
219 |
|
---|
220 | setTimeout(() => {
|
---|
221 | resetGame(table.id);
|
---|
222 | }, 15000);
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | export function setWinnerDirect(tableId, player) {
|
---|
227 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
228 |
|
---|
229 | if (tables[tableIdx] !== undefined) {
|
---|
230 | const table = tables[tableIdx];
|
---|
231 |
|
---|
232 | table.turnIdx = -1;
|
---|
233 | table.started = false;
|
---|
234 | table.ended = true;
|
---|
235 |
|
---|
236 | table.onlyOnePlayerLeft = true;
|
---|
237 | table.winners = [player];
|
---|
238 |
|
---|
239 | giveMoneyToTheWinners(table.id);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | export function setWinner(tableId) {
|
---|
244 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
245 |
|
---|
246 | if (tables[tableIdx] !== undefined) {
|
---|
247 | const table = tables[tableIdx];
|
---|
248 |
|
---|
249 | table.turnIdx = -1;
|
---|
250 |
|
---|
251 | table.players.forEach(player => {
|
---|
252 | if (player.isSatDown && !player.isFolded) {
|
---|
253 | player.hand = getBestHandDetails(player.cards, table.cards);
|
---|
254 | }
|
---|
255 | })
|
---|
256 |
|
---|
257 | hands.forEach(hand => {
|
---|
258 | const playerHands = table.players.filter(e=>e.hand.hand === hand);
|
---|
259 |
|
---|
260 | if (table.winners.length === 0) {
|
---|
261 | if (playerHands.length === 1) {
|
---|
262 | table.winners.push(playerHands[0])
|
---|
263 | }
|
---|
264 | else if (playerHands.length > 1) {
|
---|
265 | let tmp = playerHands[0].hand.highCard;
|
---|
266 | let tmpWinners = [];
|
---|
267 |
|
---|
268 | playerHands.forEach(player => {
|
---|
269 | if (player.hand.highCard > tmp) {
|
---|
270 | tmp = player.hand.highCard;
|
---|
271 | }
|
---|
272 | })
|
---|
273 |
|
---|
274 | playerHands.forEach(player => {
|
---|
275 | if (player.hand.highCard === tmp) {
|
---|
276 | tmpWinners.push(player);
|
---|
277 | }
|
---|
278 | })
|
---|
279 |
|
---|
280 | if (tmpWinners.length > 1) table.splitWinners = true;
|
---|
281 | table.winners = [...tmpWinners];
|
---|
282 | }
|
---|
283 | }
|
---|
284 | })
|
---|
285 |
|
---|
286 | giveMoneyToTheWinners(table.id);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | export function progressRoundTillTheEnd(tableId) {
|
---|
291 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
292 |
|
---|
293 | if (tables[tableIdx] !== undefined) {
|
---|
294 | const table = tables[tableIdx];
|
---|
295 |
|
---|
296 | while (table.round < 4) {
|
---|
297 | table.round++;
|
---|
298 | getCardsOnTable(table.id);
|
---|
299 | }
|
---|
300 |
|
---|
301 | table.started = false;
|
---|
302 | table.ended = true;
|
---|
303 | if (table.ended && table.winners.length === 0) {
|
---|
304 | setWinner(table.id);
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | export function progressRoundIfNeeded(tableId) {
|
---|
310 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
311 |
|
---|
312 | if (tables[tableIdx] !== undefined) {
|
---|
313 | const table = tables[tableIdx];
|
---|
314 |
|
---|
315 | const satDownPlayers = table.players.filter(e=>e.isSatDown === true);
|
---|
316 | const remainingPlayers = satDownPlayers.filter(e=>e.isFolded === false);
|
---|
317 |
|
---|
318 | if (table.turnsSinceLastBet === remainingPlayers.length) {
|
---|
319 | table.round++;
|
---|
320 | table.lastBet = 0;
|
---|
321 | table.turnsSinceLastBet = 0;
|
---|
322 |
|
---|
323 | if (table.round <= 4) {
|
---|
324 | getCardsOnTable(table.id);
|
---|
325 | }
|
---|
326 | else {
|
---|
327 | table.started = false;
|
---|
328 | table.ended = true;
|
---|
329 | }
|
---|
330 |
|
---|
331 | if (table.ended && table.winners.length === 0) {
|
---|
332 | setWinner(table.id);
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 | } |
---|