diff --git a/lib/holdem_game.js b/lib/holdem_game.js index a046517..a040715 100644 --- a/lib/holdem_game.js +++ b/lib/holdem_game.js @@ -101,26 +101,10 @@ HoldemGame.prototype.details = function() { HoldemGame.prototype.tick = function() { Room.prototype.tick.call(this); - + var room = this; if(room.is_ingame) { var gamer = room.in_gamers[0]; - if(room.turn_countdown > 0) { - room.notifyAll('countdown', { - seat: gamer.seat, - sec: room.turn_countdown - }); - room.turn_countdown --; - - } else if(room.turn_countdown === 0) { - // TODO: for test only - room.gamerMoveTurn(true); - //room.gamerGiveUp( gamer ); - - } else { - // not started, just wait - } - } else { if(room.ready_countdown > 0) { room.notifyAll('countdown', { @@ -166,6 +150,7 @@ HoldemGame.prototype.gameStart = function() { gamer = gamers[ uid ]; if(gamer.is_ready) { in_gamers.push( gamer ); + room.chips[gamer.seat] = 0; } } } @@ -306,7 +291,7 @@ HoldemGame.prototype.gameOver = function() { scorelist.push(item); } - room.notifyAll('gameover', scorelist); + room.notifyAll('gameover', {scores: scorelist, showdown: room.ingamers_count > 0}); room.notifyAll('prompt', { fold: null, @@ -323,8 +308,18 @@ HoldemGame.prototype.gameOver = function() { room.ingamers_count = 0; // for next round, move deal seat to next - room.dealer_seat = room.deal_order[0].seat; - + for (i=0; i= room.ingamers_count) { // >= for all in case room.state ++; switch(room.state) { @@ -556,10 +553,10 @@ HoldemGame.prototype.gamerShowDown = function() { var i, gamer, maxFive, someone_allin = false; for(i=0; iA>B>D>E>C,但F已盖牌不能分配任何彩池,则此局主池即为A于此局赢得的筹码($300), - * B可赢得第一个边池($1000),D参与至最后一个边池,且牌面胜过参与第二、第三及第四边池的所有牌手, - * 因此可赢得剩下所有的边池(400+450+600=$1450)。 - */ gamers_bychips.sort( function(a,b) { return a.chips - b.chips; } ); + num_players = gamers_bychips.length; + var not_in_game_prize = room.pot; + for (i=0; i < num_players; i++) { + gamers_bychips[i].chips_tmp = gamers_bychips[i].chips; + not_in_game_prize -= gamers_bychips[i].chips; + } + while (gamers_bychips[num_players - 1].chips_tmp > 0) { + var price_step = 0; + var current_prize = 0; + var start_index = -1; + for (i=0; i < num_players; i++) { + if (gamers_bychips[i].chips_tmp === 0) { + continue; + } + if (gamers_bychips[i].chips_tmp > 0 && price_step === 0) { + price_step = gamers_bychips[i].chips_tmp; + start_index = i; + } + gamers_bychips[i].chips_tmp -= price_step; + current_prize += price_step; + } + + // we must add the chips of players that are not left in the game, but + // only for the first pot evaluation. + current_prize += not_in_game_prize; + not_in_game_prize = 0; + // we must only consider those players with the highest rank + // for winning the (side) pot + var pot_winners = gamers_bychips.slice(start_index); + pot_winners.sort( function(a,b){ return b.maxFiveRank - a.maxFiveRank; } ); + for (i=0; i finals[i+1].maxFiveRank) { + // if rank at i+1 is strictly lower than rank of i, all gamers + // behind i are deleted. var losers = finals.splice(i+1, Number.MAX_VALUE); while(losers.length > 0) { var loser = losers.shift(); @@ -615,15 +625,29 @@ HoldemGame.prototype.gamerShowDown = function() { break; } } - - var prize = room.pot; + room.evaluate_pot(finals, room.pot); + } + + room.gameOver(); +}; + +HoldemGame.prototype.evaluate_pot = function(finals, prize) { + /* + * Compute how much each gamer in finals shall receive + * of the prize. Prize value is adapted in finals. Prize + * is added up as to support several pots with the same + * gamer objects. + */ var n = finals.length; - if(n > 1) { + + if(n > 1) { // pot is splitted due to tie var average = Math.floor( prize / n ); for(i=0; i 0) { // find the nearest winner after dealer seat @@ -642,13 +666,11 @@ HoldemGame.prototype.gamerShowDown = function() { finals[0].prize += odd; } } else { - finals[0].prize = prize; + finals[0].prize += prize; } - } - - room.gameOver(); }; + HoldemGame.prototype.onGamer_ready = function(req, reply) { var room = this; var uid = req.uid; diff --git a/www/main.js b/www/main.js index b67b86e..6755200 100644 --- a/www/main.js +++ b/www/main.js @@ -163,12 +163,14 @@ $(document).ready(function(){ client.on('countdown', function(ret){ addMsg(_T('count down:') + ret.seat + ', ' + ret.sec); }); + client.on('fold', function(ret){ addMsg( ret.uid + _T_('at seat') + ret.seat + _T_('fold')); }); client.on('call', function(ret){ + var seat = parseInt(ret.seat); addMsg( ret.uid + _T_('at seat') + seat + _T_('call') + ret.call); @@ -207,6 +209,24 @@ $(document).ready(function(){ showRoom(client.room); }); + client.on('all_in', function(ret){ + + var seat = parseInt(ret.seat); + var gamers = client.room.gamers; + raise_sum = gamers[ret.uid].coins + client.room.pot += raise_sum + gamers[ret.uid].coins = 0 + addMsg(ret.uid + _T_('at seat') + seat + ' all in with ' + raise_sum ); + gamers[ret.uid].is_allin = true; + + var chips = client.room.chips; + if(chips) { + chips[ seat ] += raise_sum; + } + + showRoom(client.room); + }); + client.on('pk', function(ret){ addMsg( ret.uid + _T_('at seat') + ret.seat + _T('pk') + ret.pk_uid + _T_('at seat') + ret.pk_seat + ', ' + _T('result') + ': ' + (ret.win?_T('win'):_T('fail'))); @@ -235,29 +255,40 @@ $(document).ready(function(){ } }); - client.on('gameover', function(ret){ + client.on('gameover', function(args){ addMsg( _T('game over!')); - + // if only one player remained, we should not show the cards + var showdown = args.showdown; + var ret = args.scores; + if (!showdown) { + addMsg('Only one player remained.'); + } var shared_cards = client.room.shared_cards; var gamers = client.room.gamers; var cards = client.room.cards; var chips = client.room.chips; while(ret.length > 0) { + var gamer = ret.shift(); var uid = gamer.uid; var n = (gamer.prize - gamer.chips); if(n > 0) n = '+' + n; - + var mycards = gamer.cards; var pattern = ''; if(mycards.length === 3) { pattern = Jinhua.patternString(mycards); addMsg( '#' + gamer.seat + ', ' + uid + ': ' + n + ', ' + _T_(pattern) ); } else { - var maxFive = Holdem.sort( Holdem.maxFive(mycards, shared_cards) ); - pattern = Holdem.patternString( maxFive ); - addMsg( '#' + gamer.seat + ', ' + uid + ': ' + n + ', ' + _T_(pattern) + ' (' + Poker.visualize(maxFive) + ')' ); + if (showdown) { + var maxFive = Holdem.sort( Holdem.maxFive(mycards, shared_cards) ); + pattern = Holdem.patternString( maxFive ); + + addMsg( '#' + gamer.seat + ', ' + uid + ': ' + n + ', ' + _T_(pattern) + ' (' + Poker.visualize(maxFive) + ')' );} else { + addMsg( '#' + gamer.seat + ', ' + uid + ': ' + n); + } + } cards[ gamer.seat ] = gamer.cards; @@ -598,7 +629,6 @@ function showRoom(room) { if(g) { str += g.uid + ' (' + g.name + ') [' + g.coins + ', ' + g.score + ', ' + g.exp + ', ' + g.level + ']'; if(cards && cards[i]) { - str += _T_('private cards') + '[ ' + Poker.visualize( cards[i] ) + ' ]'; if(g.uid === client.uid) { $('#mycards').html( client.uid + ', ' + _T('my cards') + ':
' + Poker.toHTML(cards[i]) );