1 | import { saveGameInHistory, 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 | saveGameInHistory('poker', table, player.username)
|
---|
213 | })
|
---|
214 |
|
---|
215 | setTimeout(() => {
|
---|
216 | resetGame(table.id);
|
---|
217 | }, 15000);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | export function setWinnerDirect(tableId, player) {
|
---|
222 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
223 |
|
---|
224 | if (tables[tableIdx] !== undefined) {
|
---|
225 | const table = tables[tableIdx];
|
---|
226 |
|
---|
227 | table.turnIdx = -1;
|
---|
228 | table.started = false;
|
---|
229 | table.ended = true;
|
---|
230 | if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
|
---|
231 | table.turnTimeout = null;
|
---|
232 |
|
---|
233 | table.onlyOnePlayerLeft = true;
|
---|
234 | table.winners = [player];
|
---|
235 |
|
---|
236 | giveMoneyToTheWinners(table.id);
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | export function setWinner(tableId) {
|
---|
241 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
242 |
|
---|
243 | if (tables[tableIdx] !== undefined) {
|
---|
244 | const table = tables[tableIdx];
|
---|
245 |
|
---|
246 | table.turnIdx = -1;
|
---|
247 |
|
---|
248 | table.players.forEach(player => {
|
---|
249 | if (player.isSatDown && !player.isFolded) {
|
---|
250 | player.hand = getBestHandDetails(player.cards, table.cards);
|
---|
251 | }
|
---|
252 | })
|
---|
253 |
|
---|
254 | hands.forEach(hand => {
|
---|
255 | const playerHands = table.players.filter(e=>e.hand.hand === hand);
|
---|
256 |
|
---|
257 | if (table.winners.length === 0) {
|
---|
258 | if (playerHands.length === 1) {
|
---|
259 | table.winners.push(playerHands[0])
|
---|
260 | }
|
---|
261 | else if (playerHands.length > 1) {
|
---|
262 | let tmp = playerHands[0].hand.highCard;
|
---|
263 | let tmpWinners = [];
|
---|
264 |
|
---|
265 | playerHands.forEach(player => {
|
---|
266 | if (player.hand.highCard > tmp) {
|
---|
267 | tmp = player.hand.highCard;
|
---|
268 | }
|
---|
269 | })
|
---|
270 |
|
---|
271 | playerHands.forEach(player => {
|
---|
272 | if (player.hand.highCard === tmp) {
|
---|
273 | tmpWinners.push(player);
|
---|
274 | }
|
---|
275 | })
|
---|
276 |
|
---|
277 | if (tmpWinners.length > 1) table.splitWinners = true;
|
---|
278 | table.winners = [...tmpWinners];
|
---|
279 | }
|
---|
280 | }
|
---|
281 | })
|
---|
282 |
|
---|
283 | giveMoneyToTheWinners(table.id);
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | export function progressRoundTillTheEnd(tableId) {
|
---|
288 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
289 |
|
---|
290 | if (tables[tableIdx] !== undefined) {
|
---|
291 | const table = tables[tableIdx];
|
---|
292 |
|
---|
293 | while (table.round < 4) {
|
---|
294 | table.round++;
|
---|
295 | getCardsOnTable(table.id);
|
---|
296 | }
|
---|
297 |
|
---|
298 | table.started = false;
|
---|
299 | table.ended = true;
|
---|
300 | if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
|
---|
301 | table.turnTimeout = null;
|
---|
302 | if (table.ended && table.winners.length === 0) {
|
---|
303 | setWinner(table.id);
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | export function progressRoundIfNeeded(tableId) {
|
---|
309 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
310 |
|
---|
311 | if (tables[tableIdx] !== undefined) {
|
---|
312 | const table = tables[tableIdx];
|
---|
313 |
|
---|
314 | const satDownPlayers = table.players.filter(e=>e.isSatDown === true);
|
---|
315 | const remainingPlayers = satDownPlayers.filter(e=>e.isFolded === false);
|
---|
316 |
|
---|
317 | if (table.turnsSinceLastBet === remainingPlayers.length) {
|
---|
318 | table.round++;
|
---|
319 | table.lastBet = 0;
|
---|
320 | table.turnsSinceLastBet = 0;
|
---|
321 |
|
---|
322 | if (table.round <= 4) {
|
---|
323 | getCardsOnTable(table.id);
|
---|
324 | }
|
---|
325 | else {
|
---|
326 | table.started = false;
|
---|
327 | table.ended = true;
|
---|
328 | if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
|
---|
329 | table.turnTimeout = null;
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (table.ended && table.winners.length === 0) {
|
---|
333 | setWinner(table.id);
|
---|
334 | }
|
---|
335 | }
|
---|
336 | }
|
---|
337 | } |
---|