source: pages/api/poker/tableSpecific.js

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

Added authentication with google

  • Property mode set to 100644
File size: 9.9 KB
Line 
1import { saveGameInHistory, tables } from '../postgre/index';
2
3import { deck } from './gameStates'
4
5import { hands, getBestHandDetails } from './handEvaluations';
6
7import axios from 'axios';
8
9require('dotenv').config();
10
11/**
12 * Replace deck if empty
13 */
14export 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 */
27export 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
44export 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
63export 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
110export 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
135export 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
173export 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 table.finished = new Date().toGMTString();
213 saveGameInHistory('poker', table, player.username)
214 })
215
216 setTimeout(() => {
217 resetGame(table.id);
218 }, 15000);
219 }
220}
221
222export function setWinnerDirect(tableId, player) {
223 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
224
225 if (tables[tableIdx] !== undefined) {
226 const table = tables[tableIdx];
227
228 table.turnIdx = -1;
229 table.started = false;
230 table.ended = true;
231 if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
232 table.turnTimeout = null;
233
234 table.onlyOnePlayerLeft = true;
235 table.winners = [player];
236
237 giveMoneyToTheWinners(table.id);
238 }
239}
240
241export function setWinner(tableId) {
242 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
243
244 if (tables[tableIdx] !== undefined) {
245 const table = tables[tableIdx];
246
247 table.turnIdx = -1;
248
249 table.players.forEach(player => {
250 if (player.isSatDown && !player.isFolded) {
251 player.hand = getBestHandDetails(player.cards, table.cards);
252 }
253 })
254
255 hands.forEach(hand => {
256 const playerHands = table.players.filter(e=>e.hand.hand === hand);
257
258 if (table.winners.length === 0) {
259 if (playerHands.length === 1) {
260 table.winners.push(playerHands[0])
261 }
262 else if (playerHands.length > 1) {
263 let tmp = playerHands[0].hand.highCard;
264 let tmpWinners = [];
265
266 playerHands.forEach(player => {
267 if (player.hand.highCard > tmp) {
268 tmp = player.hand.highCard;
269 }
270 })
271
272 playerHands.forEach(player => {
273 if (player.hand.highCard === tmp) {
274 tmpWinners.push(player);
275 }
276 })
277
278 if (tmpWinners.length > 1) table.splitWinners = true;
279 table.winners = [...tmpWinners];
280 }
281 }
282 })
283
284 giveMoneyToTheWinners(table.id);
285 }
286}
287
288export function progressRoundTillTheEnd(tableId) {
289 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
290
291 if (tables[tableIdx] !== undefined) {
292 const table = tables[tableIdx];
293
294 while (table.round < 4) {
295 table.round++;
296 getCardsOnTable(table.id);
297 }
298
299 table.started = false;
300 table.ended = true;
301 if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
302 table.turnTimeout = null;
303 if (table.ended && table.winners.length === 0) {
304 setWinner(table.id);
305 }
306 }
307}
308
309export 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 if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
330 table.turnTimeout = null;
331 }
332
333 if (table.ended && table.winners.length === 0) {
334 setWinner(table.id);
335 }
336 }
337 }
338}
Note: See TracBrowser for help on using the repository browser.