source: pages/api/poker/tableSpecific.js@ 189cd8f

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

Code cleanings

  • Property mode set to 100644
File size: 8.4 KB
Line 
1import { tables, deck } from './gameStates'
2
3import { hands, getBestHandDetails } from './handEvaluations';
4
5import axios from 'axios';
6
7require('dotenv').config();
8
9/**
10 * Replace deck if empty
11 */
12export 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 */
25export 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
42export 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
61export function setNextPlayerIdx(tableId) {
62 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
63
64 if (tables[tableIdx] !== undefined) {
65 const table = tables[tableIdx];
66
67 if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
68
69 let counter = 10;
70
71 while (true) {
72 counter--;
73
74 table.turnIdx++;
75 table.turnIdx %= table.players.length;
76
77 if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx].isSatDown && !table.players[table.turnIdx].isFolded) {
78 if (table.round >= 2 && table.players[table.turnIdx].credits === 0) continue;
79
80 let prevTurnIdx = table.turnIdx;
81 table.turnTimeout = setTimeout(() => {
82 if (prevTurnIdx === table.turnIdx) {
83 if (table.players[table.turnIdx] !== undefined) {
84 table.players[table.turnIdx].isFolded = true;
85
86 setNextPlayerIdx(table.id);
87 }
88 }
89 }, 30000);
90
91 table.lastBet = getMaxBet(table.id) - table.players[table.turnIdx].betAmount;
92 if (table.round === 1 && getMaxBet(table.id) <= 20) table.lastBet = 20;
93
94 return ;
95 }
96
97 if (counter <= 0) {
98 return ;
99 }
100 }
101 }
102}
103
104export function getCardsOnTable(tableId) {
105 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
106
107 if (tables[tableIdx] !== undefined) {
108 const table = tables[tableIdx];
109
110 if (table.round === 2) {
111 for (let i = 0; i < 3; i++) {
112 const card = drawASingleCard(table.id);
113
114 if (card !== undefined) {
115 table.cards.push(card);
116 }
117 }
118 }
119 else if (table.round > 2) {
120 const card = drawASingleCard(table.id);
121
122 if (card !== undefined) {
123 table.cards.push(card);
124 }
125 }
126 }
127}
128
129export function resetGame(tableId) {
130 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
131
132 if (tables[tableIdx] !== undefined) {
133 const table = tables[tableIdx];
134
135 table.started = false;
136 table.ended = false;
137 table.round = 0;
138 table.turnIdx = 0;
139 table.turnTimeout = null;
140 table.pot = 0;
141 table.lastBet = 20;
142 table.turnsSinceLastBet = 0;
143 table.deck = [...deck];
144
145 table.players = table.players.filter(e=>e.isGhost === false);
146 table.players = table.players.filter(e=>e.credits >= 20);
147
148 table.players.forEach(player => {
149 player.credits = 0;
150 player.cards = [];
151 player.isFolded = false;
152 player.betAmount = 0;
153 player.wonAmount = 0;
154 player.hand = {
155 hand: '',
156 highCard: 0,
157 }
158 })
159 table.winners = [];
160 table.splitWinners = false;
161 table.cards = [];
162 }
163}
164
165export function giveMoneyToTheWinners(tableId) {
166 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
167
168 if (tables[tableIdx] !== undefined) {
169 const table = tables[tableIdx];
170
171 const satDownPlayers = table.players.filter(e => e.isSatDown === true);
172 const satDownCount = satDownPlayers.length;
173
174 table.players.forEach(player => {
175 let winnings = 0;
176 if (table.winners.indexOf(player) !== -1) {
177 // winner
178 winnings = 0;
179 table.players.forEach(tmpPlayer => {
180 winnings += Math.min(tmpPlayer.betAmount, player.betAmount);
181 })
182
183 axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${player.id}&credits=${winnings}&game=poker&outcome=won`).then(postgreRes => {
184 if (postgreRes.data?.success) {
185 player.credits = postgreRes.data?.credits;
186 }
187 });
188 }
189 else {
190 // loser
191 winnings = player.betAmount;
192 table.players.forEach(tmpPlayer => {
193 if (table.winners.indexOf(tmpPlayer) !== -1) {
194 winnings -= tmpPlayer.betAmount;
195 }
196 })
197
198 axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${player.id}&credits=${winnings}&game=poker&outcome=lost`).then(postgreRes => {
199 if (postgreRes.data?.success) {
200 player.credits = postgreRes.data?.credits;
201 }
202 });
203 }
204
205 player.wonAmount = winnings;
206 })
207
208 setTimeout(() => {
209 resetGame(table.id);
210 }, 15000);
211 }
212}
213
214export function setWinner(tableId) {
215 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
216
217 if (tables[tableIdx] !== undefined) {
218 const table = tables[tableIdx];
219
220 table.turnIdx = -1;
221
222 table.players.forEach(player => {
223 if (player.isSatDown && !player.isFolded) {
224 player.hand = getBestHandDetails(player.cards, table.cards);
225 }
226 })
227
228 hands.forEach(hand => {
229 const playerHands = table.players.filter(e=>e.hand.hand === hand);
230
231 if (table.winners.length === 0) {
232 if (playerHands.length === 1) {
233 table.winners.push(playerHands[0])
234 }
235 else if (playerHands.length > 1) {
236 let tmp = playerHands[0].hand.highCard;
237 let tmpWinners = [];
238
239 playerHands.forEach(player => {
240 if (player.hand.highCard > tmp) {
241 tmp = player.hand.highCard;
242 }
243 })
244
245 playerHands.forEach(player => {
246 if (player.hand.highCard === tmp) {
247 tmpWinners.push(player);
248 }
249 })
250
251 if (tmpWinners.length > 1) table.splitWinners = true;
252 table.winners = [...tmpWinners];
253 }
254 }
255 })
256
257 giveMoneyToTheWinners(table.id);
258 }
259}
260
261export function progressRoundIfNeeded(tableId) {
262 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
263
264 if (tables[tableIdx] !== undefined) {
265 const table = tables[tableIdx];
266
267 const satDownPlayers = table.players.filter(e=>e.isSatDown === true);
268 const remainingPlayers = satDownPlayers.filter(e=>e.isFolded === false);
269
270 if (table.turnsSinceLastBet === remainingPlayers.length) {
271 table.round++;
272 table.lastBet = 0;
273 table.turnsSinceLastBet = 0;
274
275 if (table.round <= 4) {
276 getCardsOnTable(table.id);
277 }
278 else {
279 table.ended = true;
280 }
281
282 if (table.ended && table.winners.length === 0) {
283 setWinner(table.id);
284 }
285 }
286 }
287}
Note: See TracBrowser for help on using the repository browser.