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