Merge pull request #25 from cassoule/poker

some changes
This commit is contained in:
Milo Gourvest
2025-06-12 19:52:26 +02:00
committed by GitHub
3 changed files with 174 additions and 56 deletions

21
game.js
View File

@@ -184,23 +184,12 @@ export async function eloHandler(p1, p2, p1score, p2score, type) {
return
}
let diff = 5;
const prob1 = 1 / (1 + Math.pow(10, (p2elo.elo - p1elo.elo)/400))
const prob2 = 1 / (1 + Math.pow(10, (p1elo.elo - p2elo.elo)/400))
const p1newElo = Math.max(Math.floor(p1elo.elo + 10 * (p1score - prob1)), 0)
const p2newElo = Math.max(Math.floor(p2elo.elo + 10 * (p2score - prob2)), 0)
if (p1elo.elo > p2elo.elo) {
if (p1score > p2score) {
diff = Math.max(Math.floor(5 * (p2elo.elo/p1elo.elo)), 1)
} else {
diff = Math.max(Math.floor(5 * (1 + (p2elo.elo/p1elo.elo))), 1)
}
} else if (p1elo.elo < p2elo.elo) {
if (p1score < p2score) {
diff = Math.max(Math.floor(5 * (p1elo.elo/p2elo.elo)), 1)
} else {
diff = Math.max(Math.floor(5 * (1 + (p1elo.elo/p2elo.elo))), 1)
}
}
const p1newElo = Math.max(p1elo.elo + (p1score > p2score ? diff : -diff), 0)
const p2newElo = Math.max(p2elo.elo + (p1score > p2score ? -diff : diff), 0)
console.log(`${p1} elo update : ${p1elo.elo} -> ${p1newElo}`)
console.log(`${p2} elo update : ${p2elo.elo} -> ${p2newElo}`)
updateElo.run({ id: p1, elo: p1newElo })

199
index.js
View File

@@ -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();
@@ -525,7 +527,7 @@ client.on('messageCreate', async (message) => {
console.log(activePolls)
}
else if (message.author.id === process.env.DEV_ID) {
const prefix = process.env.DEV_SITE === 'true' ? 'test' : 'flopo'
const prefix = process.env.DEV_SITE === 'true' ? 'dev' : 'flopo'
if (message.content === prefix + ':add-coins-to-users') {
console.log(message.author.id)
try {
@@ -589,6 +591,20 @@ client.on('messageCreate', async (message) => {
else if (message.content.startsWith(prefix + ':poker')) {
io.emit('message', message.content);
}
else if (message.content.startsWith(prefix + ':elo-test')) {
const numbers = message.content.match(/\d+/g);
const score1 = parseInt(numbers[0]);
const score2 = parseInt(numbers[1]);
const prob1 = 1 / (1 + Math.pow(10, (score2 - score1)/400))
const prob2 = 1 / (1 + Math.pow(10, (score1 - score2)/400))
const res1 = Math.floor(score1 + 10 * (1 - prob1))
const res2 = Math.floor(score2 + 10 * (0 - prob2))
console.log(res1, res2)
}
}
}
});
@@ -635,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 ──────────────────────
@@ -965,11 +989,11 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun
const invSkins = getUserInventory.all({user_id: akhy});
const chromaText = (skin) => {
let res = ""
let result = ""
for (let i = 1; i <= skins.find((s) => s.uuid === skin.uuid).chromas.length; i++) {
res += skin.currentChroma === i ? '💠 ' : '◾ '
result += skin.currentChroma === i ? '💠 ' : '◾ '
}
return res
return result
}
const chromaName = (skin) => {
if (skin.currentChroma >= 2) {
@@ -1028,20 +1052,20 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun
const trueSkin = skins.find((s) => s.uuid === invSkins[0].uuid);
const imageUrl = () => {
let res;
let result;
if (invSkins[0].currentLvl === trueSkin.levels.length) {
if (invSkins[0].currentChroma === 1) {
res = trueSkin.chromas[0].displayIcon
result = trueSkin.chromas[0].displayIcon
} else {
res = trueSkin.chromas[invSkins[0].currentChroma-1].fullRender ?? trueSkin.chromas[invSkins[0].currentChroma-1].displayIcon
result = trueSkin.chromas[invSkins[0].currentChroma-1].fullRender ?? trueSkin.chromas[invSkins[0].currentChroma-1].displayIcon
}
} else if (invSkins[0].currentLvl === 1) {
res = trueSkin.levels[0].displayIcon ?? trueSkin.chromas[0].fullRender
result = trueSkin.levels[0].displayIcon ?? trueSkin.chromas[0].fullRender
} else if (invSkins[0].currentLvl === 2 || invSkins[0].currentLvl === 3) {
res = trueSkin.displayIcon
result = trueSkin.displayIcon
}
if (res) return res;
if (result) return result;
return trueSkin.displayIcon
};
@@ -1159,33 +1183,33 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun
// Helper functions (unchanged from your original code)
const videoUrl = () => {
let res;
let result;
if (randomLevel === randomSkin.levels.length) {
if (randomChroma === 1) {
res = randomSkin.levels[randomSkin.levels.length - 1].streamedVideo ?? randomSkin.chromas[0].streamedVideo
result = randomSkin.levels[randomSkin.levels.length - 1].streamedVideo ?? randomSkin.chromas[0].streamedVideo
} else {
res = randomSkin.chromas[randomChroma-1].streamedVideo
result = randomSkin.chromas[randomChroma-1].streamedVideo
}
} else {
res = randomSkin.levels[randomLevel-1].streamedVideo
result = randomSkin.levels[randomLevel-1].streamedVideo
}
return res;
return result;
};
const imageUrl = () => {
let res;
let result;
if (randomLevel === randomSkin.levels.length) {
if (randomChroma === 1) {
res = randomSkin.chromas[0].displayIcon
result = randomSkin.chromas[0].displayIcon
} else {
res = randomSkin.chromas[randomChroma-1].fullRender ?? randomSkin.chromas[randomChroma-1].displayIcon
result = randomSkin.chromas[randomChroma-1].fullRender ?? randomSkin.chromas[randomChroma-1].displayIcon
}
} else if (randomLevel === 1) {
res = randomSkin.levels[0].displayIcon ?? randomSkin.chromas[0].fullRender
result = randomSkin.levels[0].displayIcon ?? randomSkin.chromas[0].fullRender
} else if (randomLevel === 2 || randomLevel === 3) {
res = randomSkin.displayIcon
result = randomSkin.displayIcon
}
if (res) return res;
if (result) return result;
return randomSkin.displayIcon
};
const chromaName = () => {
@@ -1205,41 +1229,41 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun
return ''
};
const lvlText = () => {
let res = ""
let result = ""
if (randomLevel >= 1) {
res += '1⃣ '
result += '1⃣ '
}
if (randomLevel >= 2) {
res += '2⃣ '
result += '2⃣ '
}
if (randomLevel >= 3) {
res += '3⃣ '
result += '3⃣ '
}
if (randomLevel >= 4) {
res += '4⃣ '
result += '4⃣ '
}
if (randomLevel >= 5) {
res += '5⃣ '
result += '5⃣ '
}
for (let i = 0; i < randomSkin.levels.length - randomLevel; i++) {
res += '◾ '
result += '◾ '
}
return res
return result
}
const chromaText = () => {
let res = ""
let result = ""
for (let i = 1; i <= randomSkin.chromas.length; i++) {
res += randomChroma === i ? '💠 ' : '◾ '
result += randomChroma === i ? '💠 ' : '◾ '
}
return res
return result
}
const price = () => {
let res = dbSkins[randomIndex].basePrice;
let result = dbSkins[randomIndex].basePrice;
res *= (1 + (randomLevel / Math.max(randomSkin.levels.length, 2)))
res *= (1 + (randomChroma / 4))
result *= (1 + (randomLevel / Math.max(randomSkin.levels.length, 2)))
result *= (1 + (randomChroma / 4))
return res.toFixed(2);
return result.toFixed(2);
}
// Update the database
@@ -3449,7 +3473,15 @@ app.post('/create-poker-room', async (req, res) => {
name: name,
created_at: Date.now(),
last_move_at: Date.now(),
players: {}
players: {},
pioche: initialShuffledCards(),
tapis: [],
dealer: null,
sb: null,
bb: null,
current_player: null,
playing: false,
fakeMoney: false,
}
io.emit('new-poker-room')
return res.status(200).send({ roomId: id })
@@ -3468,15 +3500,102 @@ app.post('/poker-room/join', async (req, res) => {
const user = await client.users.fetch(userId)
let amount = getUser.get(userId).coins
let fakeMoney = false
if (!amount || amount < 100) {
amount = 100
fakeMoney = true
}
const player = {
id: user.id,
globalName: user.globalName,
hand: [],
bank: amount,
bet: null,
solve: null,
folded: false,
}
try {
pokerRooms[roomId].players[userId] = user
pokerRooms[roomId].players[userId] = player
if (fakeMoney) pokerRooms[roomId].fakeMoney = true
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()
if (userId === pokerRooms[roomId].host_id) {
const newHostId = Object.keys(pokerRooms[roomId].players).find(id => id !== userId)
if (!newHostId) {
delete pokerRooms[roomId]
} else {
pokerRooms[roomId].host_id = newHostId
}
}
} 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()
}
}
}
for (let i = 0; i < 3; i++) {
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].dealer = Object.keys(pokerRooms[roomId].players)[0]
pokerRooms[roomId].sb = Object.keys(pokerRooms[roomId].players)[1]
pokerRooms[roomId].bb = Object.keys(pokerRooms[roomId].players)[2 % Object.keys(pokerRooms[roomId].players).length]
pokerRooms[roomId].players[Object.keys(pokerRooms[roomId].players)[1]].bet = 10 //SB
pokerRooms[roomId].players[Object.keys(pokerRooms[roomId].players)[2 % Object.keys(pokerRooms[roomId].players).length]].bet = 20 //BB
pokerRooms[roomId].current_player = Object.keys(pokerRooms[roomId].players)[3 % Object.keys(pokerRooms[roomId].players).length]
pokerRooms[roomId].playing = true
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);
@@ -3693,7 +3812,7 @@ io.on('connection', (socket) => {
}
if (winner === null) {
await eloHandler(game.p1.id, game.p2.id, 0, 0, 'TICTACTOE')
await eloHandler(game.p1.id, game.p2.id, 0.5, 0.5, 'TICTACTOE')
try {
const guild = await client.guilds.fetch(process.env.GUILD_ID);

View File

@@ -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);
}