From 023011b3a6308303c27dc50ef36c6a9b3ba12d75 Mon Sep 17 00:00:00 2001 From: milo Date: Tue, 10 Jun 2025 18:31:39 +0200 Subject: [PATCH] poker start game --- index.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- utils.js | 10 ++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 244890b..ceac6b9 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ import { gork, getRandomHydrateText, getAPOUsers, - postAPOBuy + postAPOBuy, initialShuffledCards } from './utils.js'; import {channelPointsHandler, eloHandler, pokerTest, slowmodesHandler} from './game.js'; import { Client, GatewayIntentBits, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js'; @@ -48,6 +48,8 @@ import { getValorantSkins, getSkinTiers } from './valo.js'; import {sleep} from "openai/core"; import { v4 as uuidv4 } from 'uuid'; import { uniqueNamesGenerator, adjectives, languages, animals } from 'unique-names-generator'; +import pkg from 'pokersolver'; +const { Hand } = pkg; // Create an express app const app = express(); @@ -649,6 +651,14 @@ client.once('ready', async () => { } } } + for (const roomId in Object.keys(pokerRooms)) { + const room = pokerRooms[roomId]; + if (Object.keys(room.players)?.length === 0) { + delete pokerRooms[roomId]; + console.log(`Removing empty poker room : ${roomId}`); + io.emit('new-poker-room') + } + } }); // ─── 💀 Midnight Chaos Timer ────────────────────── @@ -3463,7 +3473,9 @@ app.post('/create-poker-room', async (req, res) => { name: name, created_at: Date.now(), last_move_at: Date.now(), - players: {} + players: {}, + pioche: initialShuffledCards(), + tapis: [], } io.emit('new-poker-room') return res.status(200).send({ roomId: id }) @@ -3482,15 +3494,74 @@ app.post('/poker-room/join', async (req, res) => { const user = await client.users.fetch(userId) + const player = { + id: user.id, + globalName: user.globalName, + hand: [], + bank: 100, + bet: null, + solve: null, + } + try { - pokerRooms[roomId].players[userId] = user + pokerRooms[roomId].players[userId] = player + pokerRooms[roomId].last_move_at = Date.now() } catch (e) { // } io.emit('player-joined') + io.emit('new-poker-room') + return res.status(200) }); +app.post('/poker-room/leave', async (req, res) => { + const { userId, roomId } = req.body + + try { + delete pokerRooms[roomId].players[userId] + pokerRooms[roomId].last_move_at = Date.now() + } catch (e) { + // + } + + io.emit('player-joined') + io.emit('new-poker-room') + return res.status(200) +}); + +app.post('/poker-room/start', async (req, res) => { + const { roomId } = req.body + + try { + for (const playerId in pokerRooms[roomId].players) { + const player = pokerRooms[roomId].players[playerId] + for (let i = 0; i < 2; i++) { + if (pokerRooms[roomId].pioche.length > 0) { + player.hand.push(pokerRooms[roomId].pioche[0]) + pokerRooms[roomId].pioche.shift() + } + } + } + if (pokerRooms[roomId].pioche.length > 0) { + pokerRooms[roomId].tapis.push(pokerRooms[roomId].pioche[0]) + pokerRooms[roomId].pioche.shift() + } + for (const playerId in pokerRooms[roomId].players) { + const player = pokerRooms[roomId].players[playerId] + let fullHand = pokerRooms[roomId].tapis + player.solve = Hand.solve(fullHand.concat(player.hand), 'standard', false).descr + } + } catch (e) { + console.log(e) + } + + pokerRooms[roomId].last_move_at = Date.now() + io.emit('poker-room-started') + io.emit('new-poker-room') + return res.status(200) +}) + import http from 'http'; import { Server } from 'socket.io'; const server = http.createServer(app); diff --git a/utils.js b/utils.js index aac527a..56e41b9 100644 --- a/utils.js +++ b/utils.js @@ -95,6 +95,16 @@ export function getRandomHydrateText() { return texts[Math.floor(Math.random() * texts.length)]; } +export const initialCards = [ + 'Ad', '2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', 'Td', 'Jd', 'Qd', 'Kd', + 'As', '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', 'Ts', 'Js', 'Qs', 'Ks', + 'Ac', '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', 'Tc', 'Jc', 'Qc', 'Kc', + 'Ah', '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th', 'Jh', 'Qh', 'Kh', +] + +export function initialShuffledCards() { + return initialCards.sort((a, b) => 0.5 - Math.random()); +} export function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); }