[b13f93b] | 1 | import axios from 'axios';
|
---|
| 2 |
|
---|
| 3 | require('dotenv').config();
|
---|
| 4 |
|
---|
| 5 | import { v4 as uuidv4 } from 'uuid';
|
---|
| 6 |
|
---|
| 7 | const sampleTable = {
|
---|
| 8 | id: '',
|
---|
| 9 | name: '',
|
---|
| 10 | status: '_1_just_created',
|
---|
| 11 | creator: '',
|
---|
| 12 | started: false,
|
---|
[3a783f2] | 13 | ended: false,
|
---|
[b13f93b] | 14 | round: 0,
|
---|
[3a783f2] | 15 | turnIdx: -1,
|
---|
| 16 | pot: 0,
|
---|
[b13f93b] | 17 | lastBet: 0,
|
---|
| 18 | turnsSinceLastBet: 0,
|
---|
| 19 | players: [],
|
---|
| 20 | deck: [],
|
---|
| 21 | cardsOnTable: [],
|
---|
[3a783f2] | 22 | winners: [],
|
---|
| 23 | splitWinners: false,
|
---|
| 24 | turnTimeout: null,
|
---|
[b13f93b] | 25 | }
|
---|
| 26 |
|
---|
| 27 | const samplePlayer = {
|
---|
| 28 | id: '',
|
---|
| 29 | table: '',
|
---|
| 30 | status: '_1_just_entered',
|
---|
| 31 | displayName: '',
|
---|
| 32 | cards: [],
|
---|
[3a783f2] | 33 | hand: {
|
---|
| 34 | hand: '',
|
---|
| 35 | highCard: 0,
|
---|
| 36 | },
|
---|
[b13f93b] | 37 | betAmount: 0,
|
---|
| 38 | isSatDown: false,
|
---|
| 39 | isCoordinator: false,
|
---|
| 40 | isFolded: false,
|
---|
[3a783f2] | 41 | isGhost: false,
|
---|
| 42 | credits: 0,
|
---|
[b13f93b] | 43 | }
|
---|
| 44 |
|
---|
| 45 | let tables = []
|
---|
| 46 | // contures -> { status, round, turnIdx, lastBet, turnsSinceLastBet,
|
---|
| 47 | //
|
---|
| 48 | // players -> { id, table, status, displayName, cards,
|
---|
| 49 | // betAmount, isSatDown, isCoordinator },
|
---|
| 50 | //
|
---|
| 51 | // cardsOnTable }
|
---|
| 52 |
|
---|
| 53 | const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
|
---|
| 54 | "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
|
---|
| 55 | "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
|
---|
| 56 | "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK" ];
|
---|
| 57 |
|
---|
[3a783f2] | 58 | const deck = [...singleDeck];
|
---|
| 59 |
|
---|
[b13f93b] | 60 | /* We are using 5 decks */
|
---|
[3a783f2] | 61 | // const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
|
---|
[b13f93b] | 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Replace deck if empty
|
---|
| 65 | */
|
---|
| 66 | function checkDeckSize(tableId) {
|
---|
| 67 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 68 |
|
---|
| 69 | if (tables[tableIdx] !== undefined) {
|
---|
| 70 | if (tables[tableIdx].deck.length === 0) {
|
---|
| 71 | tables[tableIdx].deck = [...deck];
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Draw a SINGLE random card
|
---|
| 78 | */
|
---|
| 79 | function drawASingleCard(tableId) {
|
---|
| 80 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 81 |
|
---|
| 82 | if (tables[tableIdx] !== undefined) {
|
---|
| 83 | checkDeckSize(tableId);
|
---|
| 84 |
|
---|
| 85 | let idx = Math.floor(Math.random() * tables[tableIdx].deck.length);
|
---|
| 86 | let card = tables[tableIdx].deck[idx];
|
---|
| 87 |
|
---|
| 88 | tables[tableIdx].deck.splice(idx, 1);
|
---|
| 89 |
|
---|
| 90 | return card;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return undefined;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[3a783f2] | 96 | function getMaxBet(tableId) {
|
---|
| 97 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 98 |
|
---|
| 99 | if (tables[tableIdx] !== undefined) {
|
---|
| 100 | const table = tables[tableIdx];
|
---|
| 101 |
|
---|
| 102 | let maxBet = 0;
|
---|
| 103 | table.players.forEach(player => {
|
---|
| 104 | if (player.betAmount > maxBet) {
|
---|
| 105 | maxBet = player.betAmount;
|
---|
| 106 | }
|
---|
| 107 | })
|
---|
| 108 |
|
---|
| 109 | return maxBet;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | return 0;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[b13f93b] | 115 | function setNextPlayerIdx(tableId) {
|
---|
| 116 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 117 |
|
---|
| 118 | if (tables[tableIdx] !== undefined) {
|
---|
| 119 | const table = tables[tableIdx];
|
---|
| 120 |
|
---|
[3a783f2] | 121 | if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
|
---|
| 122 |
|
---|
| 123 | let counter = 10;
|
---|
| 124 |
|
---|
[b13f93b] | 125 | while (true) {
|
---|
[3a783f2] | 126 | counter--;
|
---|
| 127 |
|
---|
[b13f93b] | 128 | table.turnIdx++;
|
---|
| 129 | table.turnIdx %= table.players.length;
|
---|
| 130 |
|
---|
| 131 | if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx].isSatDown && !table.players[table.turnIdx].isFolded) {
|
---|
[3a783f2] | 132 | if (table.round >= 2 && table.players[table.turnIdx].credits === 0) continue;
|
---|
| 133 |
|
---|
| 134 | let prevTurnIdx = table.turnIdx;
|
---|
| 135 | table.turnTimeout = setTimeout(() => {
|
---|
| 136 | if (prevTurnIdx === table.turnIdx) {
|
---|
| 137 | if (table.players[table.turnIdx] !== undefined) {
|
---|
| 138 | table.players[table.turnIdx].isFolded = true;
|
---|
| 139 |
|
---|
| 140 | setNextPlayerIdx(table.id);
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | }, 30000);
|
---|
| 144 |
|
---|
| 145 | table.lastBet = getMaxBet(table.id) - table.players[table.turnIdx].betAmount;
|
---|
| 146 | if (table.round === 1 && getMaxBet(table.id) <= 20) table.lastBet = 20;
|
---|
| 147 |
|
---|
[b13f93b] | 148 | return ;
|
---|
| 149 | }
|
---|
[3a783f2] | 150 |
|
---|
| 151 | if (counter <= 0) return ;
|
---|
[b13f93b] | 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | function getCardsOnTable(tableId) {
|
---|
| 157 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 158 |
|
---|
| 159 | if (tables[tableIdx] !== undefined) {
|
---|
| 160 | const table = tables[tableIdx];
|
---|
| 161 |
|
---|
| 162 | if (table.round === 2) {
|
---|
| 163 | for (let i = 0; i < 3; i++) {
|
---|
| 164 | const card = drawASingleCard(table.id);
|
---|
| 165 |
|
---|
| 166 | if (card !== undefined) {
|
---|
| 167 | table.cards.push(card);
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
[3a783f2] | 171 | else if (table.round > 2) {
|
---|
| 172 | const card = drawASingleCard(table.id);
|
---|
| 173 |
|
---|
| 174 | if (card !== undefined) {
|
---|
| 175 | table.cards.push(card);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | function resetGame(tableId) {
|
---|
| 182 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 183 |
|
---|
| 184 | if (tables[tableIdx] !== undefined) {
|
---|
| 185 | const table = tables[tableIdx];
|
---|
| 186 |
|
---|
| 187 | table.started = false;
|
---|
| 188 | table.ended = false;
|
---|
| 189 | table.round = 0;
|
---|
| 190 | table.turnIdx = 0;
|
---|
| 191 | table.turnTimeout = null;
|
---|
| 192 | table.pot = 0;
|
---|
| 193 | table.lastBet = 20;
|
---|
| 194 | table.turnsSinceLastBet = 0;
|
---|
| 195 | table.deck = [...deck];
|
---|
| 196 |
|
---|
| 197 | table.players = table.players.filter(e=>e.isGhost === false);
|
---|
| 198 |
|
---|
| 199 | table.players.forEach(player => {
|
---|
| 200 | player.credits = 0;
|
---|
| 201 | player.cards = [];
|
---|
| 202 | player.isFolded = false;
|
---|
| 203 | player.betAmount = 0;
|
---|
| 204 | player.wonAmount = 0;
|
---|
| 205 | player.hand = {
|
---|
| 206 | hand: '',
|
---|
| 207 | highCard: 0,
|
---|
| 208 | }
|
---|
| 209 | })
|
---|
| 210 | table.winners = [];
|
---|
| 211 | table.splitWinners = false;
|
---|
| 212 | table.cards = [];
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | function giveMoneyToTheWinners(tableId) {
|
---|
| 217 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 218 |
|
---|
| 219 | if (tables[tableIdx] !== undefined) {
|
---|
| 220 | const table = tables[tableIdx];
|
---|
| 221 |
|
---|
| 222 | const satDownPlayers = table.players.filter(e => e.isSatDown === true);
|
---|
| 223 | const satDownCount = satDownPlayers.length;
|
---|
| 224 |
|
---|
| 225 | table.players.forEach(player => {
|
---|
| 226 | let winnings = 0;
|
---|
| 227 | if (table.winners.indexOf(player) !== -1) {
|
---|
| 228 | // winner
|
---|
| 229 | winnings = 0;
|
---|
| 230 | table.players.forEach(tmpPlayer => {
|
---|
| 231 | winnings += Math.min(tmpPlayer.betAmount, player.betAmount);
|
---|
| 232 | })
|
---|
| 233 |
|
---|
| 234 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${player.id}&credits=${winnings}&game=poker&outcome=won`).then(postgreRes => {
|
---|
| 235 | if (postgreRes.data?.success) {
|
---|
| 236 | player.credits = postgreRes.data?.credits;
|
---|
| 237 | }
|
---|
| 238 | });
|
---|
| 239 | }
|
---|
| 240 | else {
|
---|
| 241 | // loser
|
---|
| 242 | winnings = player.betAmount;
|
---|
| 243 | table.players.forEach(tmpPlayer => {
|
---|
| 244 | if (table.winners.indexOf(tmpPlayer) !== -1) {
|
---|
| 245 | winnings -= tmpPlayer.betAmount;
|
---|
| 246 | }
|
---|
| 247 | })
|
---|
| 248 |
|
---|
| 249 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${player.id}&credits=${winnings}&game=poker&outcome=lost`).then(postgreRes => {
|
---|
| 250 | if (postgreRes.data?.success) {
|
---|
| 251 | player.credits = postgreRes.data?.credits;
|
---|
| 252 | }
|
---|
| 253 | });
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | player.wonAmount = winnings;
|
---|
| 257 | })
|
---|
| 258 |
|
---|
| 259 | setTimeout(() => {
|
---|
| 260 | resetGame(table.id);
|
---|
| 261 | }, 15000);
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | function setWinner(tableId) {
|
---|
| 266 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 267 |
|
---|
| 268 | if (tables[tableIdx] !== undefined) {
|
---|
| 269 | const table = tables[tableIdx];
|
---|
| 270 |
|
---|
| 271 | table.turnIdx = -1;
|
---|
| 272 |
|
---|
| 273 | table.players.forEach(player => {
|
---|
| 274 | if (player.isSatDown && !player.isFolded) {
|
---|
| 275 | player.hand = getHandDetails(player.cards.concat(table.cards))
|
---|
| 276 | }
|
---|
| 277 | })
|
---|
| 278 |
|
---|
| 279 | hands.forEach(hand => {
|
---|
| 280 | const playerHands = table.players.filter(e=>e.hand.hand === hand);
|
---|
| 281 |
|
---|
| 282 | if (table.winners.length === 0) {
|
---|
| 283 | if (playerHands.length === 1) {
|
---|
| 284 | table.winners.push(playerHands[0])
|
---|
| 285 | }
|
---|
| 286 | else if (playerHands.length > 1) {
|
---|
| 287 | let tmp = playerHands[0].hand.highCard;
|
---|
| 288 | let tmpWinners = [];
|
---|
| 289 |
|
---|
| 290 | playerHands.forEach(player => {
|
---|
| 291 | if (player.hand.highCard > tmp) {
|
---|
| 292 | tmp = player.hand.highCard;
|
---|
| 293 | }
|
---|
| 294 | })
|
---|
| 295 |
|
---|
| 296 | playerHands.forEach(player => {
|
---|
| 297 | if (player.hand.highCard === tmp) {
|
---|
| 298 | tmpWinners.push(player);
|
---|
| 299 | }
|
---|
| 300 | })
|
---|
| 301 |
|
---|
| 302 | if (tmpWinners.length > 1) table.splitWinners = true;
|
---|
| 303 | table.winners = [...tmpWinners];
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | })
|
---|
| 307 |
|
---|
| 308 | giveMoneyToTheWinners(table.id);
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | function progressRoundIfNeeded(tableId) {
|
---|
| 313 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 314 |
|
---|
| 315 | if (tables[tableIdx] !== undefined) {
|
---|
| 316 | const table = tables[tableIdx];
|
---|
| 317 |
|
---|
| 318 | const satDownPlayers = table.players.filter(e=>e.isSatDown === true);
|
---|
| 319 | const remainingPlayers = satDownPlayers.filter(e=>e.isFolded === false);
|
---|
| 320 |
|
---|
| 321 | if (table.turnsSinceLastBet === remainingPlayers.length) {
|
---|
| 322 | table.round++;
|
---|
| 323 | table.lastBet = 0;
|
---|
| 324 | table.turnsSinceLastBet = 0;
|
---|
| 325 |
|
---|
| 326 | if (table.round <= 4) {
|
---|
| 327 | getCardsOnTable(table.id);
|
---|
| 328 | }
|
---|
| 329 | else {
|
---|
| 330 | table.ended = true;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | if (table.ended && table.winners.length === 0) {
|
---|
| 334 | setWinner(table.id);
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
[b13f93b] | 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | /**
|
---|
| 341 | * ********************* BEGIN OF FUNCTIONS *********************
|
---|
| 342 | */
|
---|
| 343 |
|
---|
| 344 | function createTable(playerId, playerName, tableName) {
|
---|
| 345 | const tableId = uuidv4();
|
---|
| 346 |
|
---|
| 347 | const table = {
|
---|
| 348 | id: tableId,
|
---|
| 349 | name: tableName,
|
---|
| 350 | status: '_1_just_created',
|
---|
| 351 | creator: playerName,
|
---|
| 352 | started: false,
|
---|
[3a783f2] | 353 | ended: false,
|
---|
[b13f93b] | 354 | round: 0,
|
---|
[3a783f2] | 355 | turnIdx: -1,
|
---|
| 356 | pot: 0,
|
---|
[b13f93b] | 357 | lastBet: 20,
|
---|
| 358 | turnsSinceLastBet: 0,
|
---|
| 359 | deck: [...deck],
|
---|
| 360 | players: [{
|
---|
| 361 | id: playerId,
|
---|
| 362 | table: tableId,
|
---|
[3a783f2] | 363 | credits: 0,
|
---|
[b13f93b] | 364 | status: '_1_just_entered',
|
---|
| 365 | displayName: playerName,
|
---|
| 366 | cards: [],
|
---|
| 367 | betAmount: 0,
|
---|
[3a783f2] | 368 | wonAmount: 0,
|
---|
[b13f93b] | 369 | isSatDown: false,
|
---|
| 370 | isCoordinator: true,
|
---|
| 371 | isFolded: false,
|
---|
[3a783f2] | 372 | isGhost: false,
|
---|
| 373 | hand: {
|
---|
| 374 | hand: '',
|
---|
| 375 | highCard: 0,
|
---|
| 376 | },
|
---|
[b13f93b] | 377 | }],
|
---|
[3a783f2] | 378 | winners: [],
|
---|
| 379 | splitWinners: false,
|
---|
[b13f93b] | 380 | cards: [],
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | tables.push(table)
|
---|
| 384 |
|
---|
| 385 | return table;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | function getRestrictedTablesArray() {
|
---|
| 389 | let result = [];
|
---|
| 390 |
|
---|
| 391 | tables.forEach(table => {
|
---|
| 392 | let tmpPlayers = [];
|
---|
| 393 | table.players.forEach(player => {
|
---|
| 394 | tmpPlayers.push({
|
---|
| 395 | ...player,
|
---|
| 396 | id: '',
|
---|
| 397 | table: '',
|
---|
| 398 | cards: '',
|
---|
| 399 | })
|
---|
| 400 | });
|
---|
| 401 |
|
---|
[3a783f2] | 402 | let tmpWinners = [];
|
---|
| 403 | table.winners.forEach(winner => {
|
---|
| 404 | tmpWinners.push({
|
---|
| 405 | ...winner,
|
---|
| 406 | id: '',
|
---|
| 407 | table: '',
|
---|
| 408 | cards: '',
|
---|
| 409 | })
|
---|
| 410 | });
|
---|
| 411 |
|
---|
[b13f93b] | 412 | let tmp = {
|
---|
| 413 | ...table,
|
---|
| 414 | deck: [],
|
---|
| 415 | players: tmpPlayers,
|
---|
[3a783f2] | 416 | winners: tmpWinners,
|
---|
| 417 | turnTimeout: null,
|
---|
[b13f93b] | 418 | }
|
---|
| 419 |
|
---|
| 420 | result.push({...tmp});
|
---|
| 421 | })
|
---|
| 422 |
|
---|
| 423 | return result;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | function getRestrictedTableArray(tableId, session_id) {
|
---|
| 427 | let result = {...sampleTable};
|
---|
| 428 |
|
---|
| 429 | let tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 430 |
|
---|
| 431 | if (tableIdx !== -1) {
|
---|
| 432 | let table = tables[tableIdx];
|
---|
| 433 |
|
---|
| 434 | let tmpPlayers = [];
|
---|
| 435 | table.players.forEach(player => {
|
---|
| 436 | if (player.id === session_id) {
|
---|
| 437 | tmpPlayers.push({
|
---|
| 438 | ...player,
|
---|
| 439 | id: '',
|
---|
| 440 | table: '',
|
---|
| 441 | })
|
---|
| 442 | }
|
---|
| 443 | else {
|
---|
| 444 | tmpPlayers.push({
|
---|
| 445 | ...player,
|
---|
| 446 | id: '',
|
---|
| 447 | table: '',
|
---|
[3a783f2] | 448 | cards: table.ended ? player.cards : player.cards.length > 0 ? ['back', 'back'] : '',
|
---|
[b13f93b] | 449 | })
|
---|
| 450 | }
|
---|
| 451 | });
|
---|
| 452 |
|
---|
[3a783f2] | 453 | let tmpWinners = [];
|
---|
| 454 | table.winners.forEach(winner => {
|
---|
| 455 | if (winner.id === session_id) {
|
---|
| 456 | tmpWinners.push({
|
---|
| 457 | ...winner,
|
---|
| 458 | id: '',
|
---|
| 459 | table: '',
|
---|
| 460 | })
|
---|
| 461 | }
|
---|
| 462 | else {
|
---|
| 463 | tmpWinners.push({
|
---|
| 464 | ...winner,
|
---|
| 465 | id: '',
|
---|
| 466 | table: '',
|
---|
| 467 | cards: table.ended ? winner.cards : winner.cards.length > 0 ? ['back', 'back'] : '',
|
---|
| 468 | })
|
---|
| 469 | }
|
---|
| 470 | });
|
---|
[b13f93b] | 471 | result = {
|
---|
| 472 | ...table,
|
---|
| 473 | players: tmpPlayers,
|
---|
[3a783f2] | 474 | winners: tmpWinners,
|
---|
| 475 | turnTimeout: null,
|
---|
[b13f93b] | 476 | }
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | return result;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | function getTable(tableId) {
|
---|
| 483 | const tableIdx = tables.map(e=>e.id).indexOf(tableId);
|
---|
| 484 |
|
---|
| 485 | if (tableIdx !== -1) {
|
---|
| 486 | return tables[tableIdx];
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | return undefined;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | function getTableAndPlayer(session_id) {
|
---|
| 493 | for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
|
---|
[3a783f2] | 494 | const playerIdx = tables[tableIdx].players.filter(e=>e.isGhost === false).map(e=>e.id).indexOf(session_id);
|
---|
[b13f93b] | 495 |
|
---|
| 496 | if (playerIdx !== -1) {
|
---|
| 497 | return {
|
---|
| 498 | success: true,
|
---|
| 499 | table: tables[tableIdx],
|
---|
| 500 | player: tables[tableIdx].players[playerIdx],
|
---|
| 501 | }
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | return {
|
---|
| 506 | success: false,
|
---|
| 507 | table: sampleTable,
|
---|
| 508 | player: samplePlayer,
|
---|
| 509 | };
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | /**
|
---|
| 513 | * ********************* END OF FUNCTIONS *********************
|
---|
| 514 | */
|
---|
| 515 |
|
---|
| 516 | /**
|
---|
| 517 | * ********************* BEGIN OF REQUEST HANDLER *********************
|
---|
| 518 | */
|
---|
| 519 | export default async function handler(req, res) {
|
---|
| 520 | /**
|
---|
| 521 | * GET method
|
---|
| 522 | */
|
---|
| 523 | if (req.method === 'GET') {
|
---|
| 524 |
|
---|
| 525 | /**
|
---|
| 526 | * /---------------------- GET ----------------------/
|
---|
| 527 | * Creates the table and enters the user inside
|
---|
| 528 | * @action game_action
|
---|
| 529 | * @param session_id
|
---|
| 530 | * @param specificAction
|
---|
| 531 | * @param betAmount
|
---|
| 532 | */
|
---|
| 533 | if (req.query.action === 'game_action' && req.query?.session_id && req.query?.specificAction && req.query?.betAmount) {
|
---|
| 534 | const { success, table, player } = getTableAndPlayer(req.query.session_id)
|
---|
| 535 |
|
---|
[3a783f2] | 536 | if (success && table.started && !table.ended && player.isSatDown && !player.isFolded) {
|
---|
[b13f93b] | 537 | if (table.players.map(e=>e.id).indexOf(req.query.session_id) !== table.turnIdx) {
|
---|
| 538 | res.end();
|
---|
| 539 | return ;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | let okayToGo = false;
|
---|
| 543 |
|
---|
| 544 | if (req.query.specificAction === 'check') {
|
---|
[3a783f2] | 545 | if (table.lastBet === 0) {
|
---|
| 546 | table.turnsSinceLastBet++;
|
---|
| 547 | okayToGo = true;
|
---|
| 548 |
|
---|
| 549 | progressRoundIfNeeded(table.id);
|
---|
| 550 | }
|
---|
[b13f93b] | 551 | }
|
---|
| 552 | else if (req.query.specificAction === 'call') {
|
---|
[3a783f2] | 553 | await axios.get(`${process.env.HOME_URL}/api/postgre/?action=take_credits&session_id=${req.query.session_id}&credits=${table.lastBet}&takeWhatYouCan=true`).then(postgreRes => {
|
---|
| 554 | if (postgreRes.data?.success) {
|
---|
| 555 | player.credits = postgreRes.data?.credits;
|
---|
| 556 |
|
---|
| 557 | if (player.credits >= table.lastBet)
|
---|
| 558 | player.betAmount += table.lastBet;
|
---|
| 559 | else
|
---|
| 560 | player.betAmount += player.credits;
|
---|
| 561 |
|
---|
| 562 | table.pot += table.lastBet;
|
---|
| 563 | table.turnsSinceLastBet++;
|
---|
| 564 | okayToGo = true;
|
---|
| 565 |
|
---|
| 566 | progressRoundIfNeeded(table.id);
|
---|
| 567 | }
|
---|
| 568 | });
|
---|
[b13f93b] | 569 | }
|
---|
| 570 | else if (req.query.specificAction === 'raise') {
|
---|
[3a783f2] | 571 | const betAmount = parseInt(req.query.betAmount);
|
---|
| 572 |
|
---|
| 573 | if (betAmount >= table.lastBet) {
|
---|
| 574 | await axios.get(`${process.env.HOME_URL}/api/postgre/?action=take_credits&session_id=${req.query.session_id}&credits=${betAmount}&takeWhatYouCan=true`).then(postgreRes => {
|
---|
| 575 | if (postgreRes.data?.success) {
|
---|
| 576 | player.credits = postgreRes.data?.credits;
|
---|
| 577 |
|
---|
| 578 | player.betAmount += betAmount;
|
---|
| 579 | table.pot += betAmount;
|
---|
| 580 | table.turnsSinceLastBet = 1;
|
---|
| 581 | okayToGo = true;
|
---|
| 582 |
|
---|
| 583 | progressRoundIfNeeded(table.id);
|
---|
| 584 | }
|
---|
| 585 | });
|
---|
| 586 | }
|
---|
[b13f93b] | 587 | }
|
---|
| 588 | else if (req.query.specificAction === 'fold') {
|
---|
[3a783f2] | 589 | player.isFolded = true;
|
---|
| 590 | okayToGo = true;
|
---|
| 591 |
|
---|
| 592 | progressRoundIfNeeded(table.id);
|
---|
[b13f93b] | 593 | }
|
---|
| 594 |
|
---|
| 595 | if (okayToGo) {
|
---|
| 596 | setNextPlayerIdx(table.id);
|
---|
| 597 | }
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | res.end();
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | /**
|
---|
| 604 | * /---------------------- GET ----------------------/
|
---|
| 605 | * Creates the table and enters the user inside
|
---|
| 606 | * @action start_game
|
---|
| 607 | * @param session_id
|
---|
| 608 | */
|
---|
| 609 | if (req.query.action === 'start_game' && req.query?.session_id) {
|
---|
| 610 | const { success, table } = getTableAndPlayer(req.query.session_id)
|
---|
| 611 |
|
---|
| 612 | if (success && !table.started) {
|
---|
[3a783f2] | 613 | table.players.forEach(player => {
|
---|
| 614 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=check_if_logged_in&session_id=${player.id}`).then(postgreRes => {
|
---|
| 615 | if (postgreRes.data?.success) {
|
---|
| 616 | player.credits = postgreRes.data?.credits;
|
---|
| 617 | }
|
---|
| 618 | });
|
---|
| 619 | })
|
---|
| 620 |
|
---|
[b13f93b] | 621 | table.started = true;
|
---|
| 622 | table.round = 1;
|
---|
| 623 |
|
---|
[3a783f2] | 624 | table.turnIdx = Math.floor(Math.random(0, table.players.length))
|
---|
| 625 | setNextPlayerIdx(table.id);
|
---|
[b13f93b] | 626 |
|
---|
| 627 | table.players.forEach(player => {
|
---|
| 628 | if (player.isSatDown) {
|
---|
| 629 | for (let i = 0; i < 2; i++) {
|
---|
| 630 | const card = drawASingleCard(table.id);
|
---|
| 631 |
|
---|
| 632 | if (card !== undefined) {
|
---|
| 633 | player.cards.push(card);
|
---|
| 634 | }
|
---|
| 635 | }
|
---|
| 636 | }
|
---|
| 637 | })
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | res.end();
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | /**
|
---|
| 644 | * /---------------------- GET ----------------------/
|
---|
| 645 | * Creates the table and enters the user inside
|
---|
| 646 | * @action sit_down
|
---|
| 647 | * @param session_id
|
---|
| 648 | * @param tableId
|
---|
| 649 | */
|
---|
| 650 | if (req.query.action === 'sit_down' && req.query?.session_id && req.query?.tableId) {
|
---|
| 651 | const { success, table, player } = getTableAndPlayer(req.query.session_id)
|
---|
| 652 |
|
---|
| 653 | if (success && !table.started) {
|
---|
| 654 | player.isSatDown = true;
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | res.end();
|
---|
| 658 | }
|
---|
| 659 |
|
---|
[3a783f2] | 660 | /**
|
---|
| 661 | * /---------------------- GET ----------------------/
|
---|
| 662 | * Creates the table and enters the user inside
|
---|
| 663 | * @action leave_table
|
---|
| 664 | * @param session_id
|
---|
| 665 | */
|
---|
| 666 | if (req.query.action === 'leave_table' && req.query?.session_id) {
|
---|
| 667 | const { success, table, player } = getTableAndPlayer(req.query.session_id);
|
---|
| 668 |
|
---|
| 669 | if (success) {
|
---|
| 670 | player.isGhost = true;
|
---|
| 671 | player.isFolded = true;
|
---|
| 672 |
|
---|
| 673 | if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx] === player) {
|
---|
| 674 | setNextPlayerIdx(table.id);
|
---|
| 675 | }
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | res.end();
|
---|
| 679 | }
|
---|
| 680 |
|
---|
[b13f93b] | 681 | /**
|
---|
| 682 | * /---------------------- GET ----------------------/
|
---|
| 683 | * Creates the table and enters the user inside
|
---|
| 684 | * @action join_a_table
|
---|
| 685 | * @param session_id
|
---|
| 686 | * @param tableId
|
---|
| 687 | * @param displayName
|
---|
| 688 | */
|
---|
| 689 | if (req.query.action === 'join_a_table' && req.query?.session_id && req.query?.tableId && req.query?.displayName) {
|
---|
| 690 | if (req.query.tableId.length > 0) {
|
---|
| 691 | const { success } = getTableAndPlayer(req.query.session_id);
|
---|
| 692 |
|
---|
| 693 | if (!success) {
|
---|
| 694 | const table = getTable(req.query.tableId)
|
---|
| 695 |
|
---|
[3a783f2] | 696 | if (table !== undefined && !table.started) {
|
---|
[b13f93b] | 697 | table.players.push({
|
---|
| 698 | ...samplePlayer,
|
---|
| 699 | id: req.query.session_id,
|
---|
| 700 | table: req.query.tableId,
|
---|
| 701 | displayName: req.query.displayName
|
---|
| 702 | })
|
---|
| 703 | }
|
---|
| 704 | }
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | res.end();
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | /**
|
---|
| 711 | * /---------------------- GET ----------------------/
|
---|
| 712 | * Creates the table and enters the user inside
|
---|
| 713 | * @action create_a_table
|
---|
| 714 | * @param session_id
|
---|
| 715 | * @param displayName
|
---|
| 716 | * @param tableName
|
---|
| 717 | */
|
---|
| 718 | if (req.query.action === 'create_a_table' && req.query?.session_id && req.query?.displayName && req.query?.tableName) {
|
---|
| 719 | const { success } = getTableAndPlayer(req.query.session_id);
|
---|
| 720 |
|
---|
| 721 | if (!success) {
|
---|
| 722 | createTable(req.query.session_id, req.query.displayName, req.query.tableName);
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | res.end();
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | /**
|
---|
| 729 | * /---------------------- GET ----------------------/
|
---|
| 730 | * Creates the table and enters the user inside
|
---|
| 731 | * @action update_state
|
---|
| 732 | * @param session_id
|
---|
| 733 | */
|
---|
| 734 | if (req.query.action === 'update_state' && req.query?.session_id) {
|
---|
| 735 | const session_id = req.query.session_id;
|
---|
| 736 |
|
---|
| 737 | const { table, player } = getTableAndPlayer(session_id);
|
---|
| 738 |
|
---|
| 739 | res.json({
|
---|
| 740 | success: true,
|
---|
| 741 | pokerGame: {
|
---|
| 742 | tables: getRestrictedTablesArray(),
|
---|
| 743 | table: getRestrictedTableArray(table.id, req.query.session_id),
|
---|
| 744 | player: player,
|
---|
| 745 | }
|
---|
| 746 | })
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | /**
|
---|
| 750 | * /---------------------- GET ----------------------/
|
---|
| 751 | * If the player is not in an existing room, create a room for them.
|
---|
| 752 | * If they are reconnecting, get the room they were in.
|
---|
| 753 | * @action get_player_info_on_enter
|
---|
| 754 | * @param session_id
|
---|
| 755 | */
|
---|
| 756 | if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
|
---|
| 757 | const session_id = req.query.session_id;
|
---|
| 758 |
|
---|
| 759 | axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
|
---|
| 760 | if (postgreRes.data?.success) {
|
---|
| 761 | res.json({
|
---|
| 762 | success: true,
|
---|
| 763 | displayName: postgreRes.data?.displayName,
|
---|
| 764 | session_id: postgreRes.data?.session_id,
|
---|
| 765 | credits: postgreRes.data?.credits,
|
---|
| 766 | })
|
---|
| 767 | }
|
---|
| 768 | else {
|
---|
| 769 | res.json({
|
---|
| 770 | success: false,
|
---|
| 771 | })
|
---|
| 772 | }
|
---|
| 773 | });
|
---|
| 774 | }
|
---|
| 775 | }
|
---|
| 776 | }
|
---|
| 777 | /**
|
---|
| 778 | * ********************* END OF REQUEST HANDLER *********************
|
---|
| 779 | */
|
---|
[3a783f2] | 780 |
|
---|
| 781 | const hands = [
|
---|
| 782 | 'Royal Flush',
|
---|
| 783 | 'Straight Flush',
|
---|
| 784 | 'Four of a Kind',
|
---|
| 785 | 'Full House',
|
---|
| 786 | 'Flush',
|
---|
| 787 | 'Straight',
|
---|
| 788 | 'Three of a Kind',
|
---|
| 789 | 'Two Pairs',
|
---|
| 790 | 'Pair',
|
---|
| 791 | 'High Card',
|
---|
| 792 | ]
|
---|
| 793 |
|
---|
| 794 | const order = "23456789TJQKA"
|
---|
| 795 | function getHandDetails(hand) {
|
---|
| 796 | const cards = hand
|
---|
| 797 | const faces = cards.map(a => String.fromCharCode([77 - order.indexOf(a[1])])).sort()
|
---|
| 798 | const suits = cards.map(a => a[0]).sort()
|
---|
| 799 | const counts = faces.reduce(count, {})
|
---|
| 800 | const duplicates = Object.values(counts).reduce(count, {})
|
---|
| 801 | const flush = suits[0] === suits[4]
|
---|
| 802 | const first = faces[0].charCodeAt(1)
|
---|
| 803 | const straight = faces.every((f, index) => f.charCodeAt(1) - first === index)
|
---|
| 804 | let rank =
|
---|
| 805 | (flush && straight && 1) ||
|
---|
| 806 | (duplicates[4] && 2) ||
|
---|
| 807 | (duplicates[3] && duplicates[2] && 3) ||
|
---|
| 808 | (flush && 4) ||
|
---|
| 809 | (straight && 5) ||
|
---|
| 810 | (duplicates[3] && 6) ||
|
---|
| 811 | (duplicates[2] > 1 && 7) ||
|
---|
| 812 | (duplicates[2] && 8) ||
|
---|
| 813 | 9;
|
---|
| 814 |
|
---|
| 815 | return { hand: hands[rank], highCard: faces.sort(byCountFirst).join("") }
|
---|
| 816 |
|
---|
| 817 | function byCountFirst(a, b) {
|
---|
| 818 | //Counts are in reverse order - bigger is better
|
---|
| 819 | const countDiff = counts[b] - counts[a]
|
---|
| 820 | if (countDiff) return countDiff // If counts don't match return
|
---|
| 821 | return b > a ? -1 : b === a ? 0 : 1
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | function count(c, a) {
|
---|
| 825 | c[a] = (c[a] || 0) + 1
|
---|
| 826 | return c
|
---|
| 827 | }
|
---|
| 828 | }
|
---|
| 829 |
|
---|
| 830 | function getCardCombinations(playerCards, tableCards) {
|
---|
| 831 | let combinations = [];
|
---|
| 832 |
|
---|
| 833 | combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 834 | combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[2], tableCards[4]])
|
---|
| 835 |
|
---|
| 836 | combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[4], tableCards[3]])
|
---|
| 837 | combinations.push([playerCards[0], tableCards[0], tableCards[4], tableCards[2], tableCards[3]])
|
---|
| 838 | combinations.push([playerCards[0], tableCards[4], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 839 |
|
---|
| 840 |
|
---|
| 841 | combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 842 | combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[2], tableCards[4]])
|
---|
| 843 |
|
---|
| 844 | combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[4], tableCards[3]])
|
---|
| 845 | combinations.push([playerCards[1], tableCards[0], tableCards[4], tableCards[2], tableCards[3]])
|
---|
| 846 | combinations.push([playerCards[1], tableCards[4], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 847 |
|
---|
| 848 |
|
---|
| 849 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[2]])
|
---|
| 850 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[3]])
|
---|
| 851 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[4]])
|
---|
| 852 |
|
---|
| 853 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[2], tableCards[3]])
|
---|
| 854 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[2], tableCards[4]])
|
---|
| 855 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[3], tableCards[4]])
|
---|
| 856 |
|
---|
| 857 | combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 858 | combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[2], tableCards[4]])
|
---|
| 859 | combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[3], tableCards[4]])
|
---|
| 860 |
|
---|
| 861 | combinations.push([playerCards[0], playerCards[1], tableCards[2], tableCards[3], tableCards[4]])
|
---|
| 862 |
|
---|
| 863 |
|
---|
| 864 | return combinations;
|
---|
| 865 | }
|
---|