Skip to content

Commit

Permalink
fix: turn to next round cause calling player is not right
Browse files Browse the repository at this point in the history
  • Loading branch information
bbb169 committed Oct 22, 2023
1 parent 005b42b commit 829c6ab
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 25 deletions.
11 changes: 7 additions & 4 deletions bin/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import app from '../app';
import debug from 'debug';
import { Server as ServerIO, Socket } from 'socket.io';
import { addPlayerForRoom, createRoom, creatPlayer, deleteRoom, getRoomInfo, updatePlayerActiveTime } from '../database/roomInfo';
import { PlayerInfoType, RoomInfo, VictoryInfo } from '../types/roomInfo';
import { PlayerCallChipsRes, PlayerInfoType, RoomInfo } from '../types/roomInfo';
import { DefaultEventsMap } from 'socket.io/dist/typed-events';
import { socketCallChips, socketDisconnect, socketStartGame, socketTurnToNextGame } from '../routes/wbSocketListeners';

Expand Down Expand Up @@ -198,11 +198,14 @@ websocketIo.on('connection', socket => {
});

socket.on('callChips', (callChips?: number) => {
socketCallChips(roomId, userName, callChips).then((callChipsRes: [PlayerInfoType, VictoryInfo][] | boolean) => {
socketCallChips(roomId, userName, callChips).then(({ victoryPlayers, playersCalledRes }: PlayerCallChipsRes) => {
reportToAllPlayersInRoom(roomId, (socket) => {
// =================== victoryPlayers ==============
if (Array.isArray(callChipsRes)) {
socket.emit('victoryPlayers', callChipsRes);
if (victoryPlayers) {
socket.emit('victoryPlayers', victoryPlayers);
}
if (playersCalledRes) {
socket.emit('playersCalledRes', playersCalledRes);
}
// ================= getGptPredicate ==============
// if (callChipsRes === true && currentPlayer.status.includes('calling')) {
Expand Down
101 changes: 80 additions & 21 deletions database/roomInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HandClassType } from '../types/pokersolver';
import { PlayerInfoType, RoomInfo, VictoryInfo } from '../types/roomInfo';
import { PlayerCallChipsRes, PlayerInfoType, RoomInfo, VictoryInfo } from '../types/roomInfo';
import { isEmpty } from '../utils';
import { distributeCards, translateCardToString, translateStringToCard } from '../utils/cards';
import Hand from '../utils/pokersolver';
Expand Down Expand Up @@ -105,17 +105,17 @@ export const updatePlayerActiveTime = (roomId: string, userName: string) => {
// ====================== game proccesses ====================

export function playerCallChips (roomId: string, userName: string, callChips?: number) {
return new Promise<[PlayerInfoType, VictoryInfo][] | boolean>((resolve) => {
return new Promise<PlayerCallChipsRes>((resolve) => {
const room = getRoomInfo(roomId);
let hasTurnToNextRound = false;
const playersCalledRes: [PlayerInfoType, string][] = [];

if (room) {
const playersQueue = Array.from(room.players.values());

// =============== handle called chips =================
const targetPlayer = playersQueue.find(player => {
if (player.name === userName) {
hanldePlayerCalledChips(roomId, player, callChips);
playersCalledRes.push(hanldePlayerCalledChips(roomId, player, callChips));
return true;
}
return false;
Expand All @@ -126,7 +126,10 @@ export function playerCallChips (roomId: string, userName: string, callChips?: n
}

if (checkRoomValidPlayerNumIsOne(roomId)) {
resolve(determineVictory(roomId));
resolve({
victoryPlayers: determineVictory(roomId),
playersCalledRes,
});
return;
}

Expand All @@ -143,28 +146,34 @@ export function playerCallChips (roomId: string, userName: string, callChips?: n
} else { // pass
// will turn to disconnect player to fold if it can not follow;
if (currentPlayer.status.includes('disconnect')) {
hanldePlayerCalledChips(roomId, currentPlayer);
playersCalledRes.push(hanldePlayerCalledChips(roomId, currentPlayer));
}
currentPosition = (currentPosition + 1) % (playersQueue.length);
}
}

// if didn't has turn to next yet, means it's time to determine victory
if (!hasTurnToNext) {
resolve(determineVictory(roomId));
resolve({
victoryPlayers: determineVictory(roomId),
playersCalledRes,
});
return;
}

if (checkRoomRoundAllCalled(roomId) && checkRoomCallEqual(roomId)) {
if (room.callingSteps === 3) {
resolve(determineVictory(roomId));
resolve({
victoryPlayers: determineVictory(roomId),
playersCalledRes,
});
return;
}
turnToNextRound(roomId);
hasTurnToNextRound = true;
}
}

resolve(hasTurnToNextRound);
resolve({ playersCalledRes });
});
}

Expand Down Expand Up @@ -404,7 +413,9 @@ export function hanldePlayerCalledChips (
player: PlayerInfoType,
/** make player fold */
callChips = -1
) {
): [PlayerInfoType, string] {
let playerCalledRes: string = '';

// probally get null callChips
if (isEmpty(callChips)) {
callChips = -1;
Expand All @@ -417,6 +428,7 @@ export function hanldePlayerCalledChips (
// ================= all in ==============
if (player.holdCent <= callChips) {
finalCallChips = player.holdCent;
playerCalledRes = `全下${finalCallChips}`;
} else if (Math.max(room.currentCallChips, player.blind) > callChips + player.calledChips) {
// ============== fold =================
if (player.calledChips < player.blind) {
Expand All @@ -427,15 +439,26 @@ export function hanldePlayerCalledChips (
} else if (player.status.includes('disconnect')) {
player.status = ['fold', 'disconnect'];
}
// playerFold(roomId, player);

playerCalledRes = '弃牌';
}

// ================= raise ================
if (room.currentCallChips < callChips + player.calledChips) {
clearRoomRoundAllCalled(roomId, player);

if (!playerCalledRes) {
console.log(room.currentCallChips, callChips + player.calledChips);

playerCalledRes = `加注到${callChips + player.calledChips}`;
}
} else if (room.currentCallChips === 0 && !playerCalledRes) { // bet
playerCalledRes = `下注:${callChips}`;
} else if (room.currentCallChips === callChips + player.calledChips && !playerCalledRes) { // call
playerCalledRes = `Check:${callChips}`;
}

// is not all in, just use called chips
// is not all in and fold, just use called chips
if (finalCallChips === -1) {
finalCallChips = callChips;
}
Expand All @@ -450,6 +473,8 @@ export function hanldePlayerCalledChips (

room.currentCallChips = player.calledChips;
}

return [player, playerCalledRes];
}

/** player fold and lose its called chips */
Expand All @@ -465,17 +490,51 @@ export function foldPlayerLoseToRoom (roomId: string, player: PlayerInfoType) {
function turnToNextRound (roomId: string) {
const room = getRoomInfo(roomId);
if (!room) return;
let hasPlayerCalling = true;
const playersQueue = Array.from(room.players.values());
let currentPosition = 0;
let allPlayersClear = false;
let callback = () => {};

while ((currentPosition < playersQueue.length && !allPlayersClear) || !hasPlayerCalling) {
const currentPlayer = playersQueue[currentPosition];
currentPlayer.roundCalled = false;

if (!hasPlayerCalling) {
if (currentPlayer.status.includes('disconnect')) {
callback = () => {
playerCallChips(roomId, currentPlayer.name, 0);
};
} else if (!currentPlayer.status.includes('fold')) {
currentPlayer.status = ['calling'];
hasPlayerCalling = true;
}
} else {
if (currentPlayer.position === room.buttonIndex) {
if (currentPlayer.status.includes('fold')) {
hasPlayerCalling = false;
} else if (currentPlayer.status.includes('disconnect')) {
callback = () => {
playerCallChips(roomId, currentPlayer.name, 0);
};
} else {
currentPlayer.status = ['calling'];
}
} else if (currentPlayer.status.includes('calling')) {
currentPlayer.status = ['waiting'];
}
}

room.players.forEach(player => {
player.roundCalled = false;
if (player.position === room.buttonIndex) {
player.status = ['calling'];
} else if (player.status.includes('calling')) {
player.status = ['waiting'];
if (currentPosition === playersQueue.length - 1) {
allPlayersClear = true;
}
});

currentPosition = (currentPosition + 1) % (playersQueue.length);
}

callback();

// first calling to equal will filp three cards
// first round will filp three cards
if (room.callingSteps === 0) {
if (room.publicCards) {
room.publicCards.forEach((card, index) => {
Expand Down
5 changes: 5 additions & 0 deletions types/roomInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ export interface VictoryInfo {
getChips: number;
cardName?: string;
cards?: CardType[];
}

export interface PlayerCallChipsRes {
victoryPlayers?: [PlayerInfoType, VictoryInfo][],
playersCalledRes: [PlayerInfoType, string][];
}

0 comments on commit 829c6ab

Please sign in to comment.