dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit ffd6c1a470307f72781539e8019557bcb9896b27
parent 45f29329e3c2aba1ff24b9f960fbc4e86458f895
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue, 19 Dec 2023 01:38:48 -0500

cleanup

Diffstat:
Mpkg/web/handlers/poker/poker.go | 182++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mpkg/web/handlers/poker/poker_test.go | 92++++++++++++++++++++++++++++++++++++++++----------------------------------------
2 files changed, 137 insertions(+), 137 deletions(-)

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -106,7 +106,7 @@ var PokerInstance = NewPoker() type Ongoing struct { LogEvents utils.RWMtx[[]LogEvent] deck []string - players []*PokerPlayer + players []*pokerPlayer events utils.RWMtx[[]PokerEvent] waitTurnEvent utils.RWMtx[PokerWaitTurnEvent] autoActionEvent utils.RWMtx[AutoActionEvent] @@ -133,69 +133,69 @@ func (p *seatedPlayer) isEligible(pokerTableMinBet database.PokerChip) bool { return p != nil && p.getCash() >= pokerTableMinBet } -type PokerPlayer struct { +type pokerPlayer struct { *seatedPlayer - Bet utils.RWMtx[database.PokerChip] - Cards utils.RWMtx[[]playerCard] - Folded atomic.Bool - Unsit atomic.Bool - GameBet database.PokerChip - AllInMaxGain database.PokerChip + bet utils.RWMtx[database.PokerChip] + cards utils.RWMtx[[]playerCard] + folded atomic.Bool + unsit atomic.Bool + gameBet database.PokerChip + allInMaxGain database.PokerChip countChancesToAction int } -func (p *PokerPlayer) GetBet() (out database.PokerChip) { - p.Bet.RWith(func(v *database.PokerChip) { out = *v }) +func (p *pokerPlayer) getBet() (out database.PokerChip) { + p.bet.RWith(func(v *database.PokerChip) { out = *v }) return } -func (p *PokerPlayer) canBet() bool { - return !p.Folded.Load() && !p.isAllIn() +func (p *pokerPlayer) canBet() bool { + return !p.folded.Load() && !p.isAllIn() } -func (p *PokerPlayer) isAllIn() bool { +func (p *pokerPlayer) isAllIn() bool { return p.getCash() == 0 } -func (p *PokerPlayer) refundPartialBet(db *database.DkfDB, pokerTableID int64, diff database.PokerChip) { +func (p *pokerPlayer) refundPartialBet(db *database.DkfDB, pokerTableID int64, diff database.PokerChip) { _ = db.PokerTableAccountRefundPartialBet(p.userID, pokerTableID, diff) - p.GameBet -= diff - p.Bet.With(func(v *database.PokerChip) { *v -= diff }) + p.gameBet -= diff + p.bet.With(func(v *database.PokerChip) { *v -= diff }) p.cash.With(func(cash *database.PokerChip) { *cash += diff }) } -func (p *PokerPlayer) doBet(db *database.DkfDB, pokerTableID int64, bet database.PokerChip) { +func (p *pokerPlayer) doBet(db *database.DkfDB, pokerTableID int64, bet database.PokerChip) { _ = db.PokerTableAccountBet(p.userID, pokerTableID, bet) - p.GameBet += bet - p.Bet.With(func(v *database.PokerChip) { *v += bet }) + p.gameBet += bet + p.bet.With(func(v *database.PokerChip) { *v += bet }) p.cash.With(func(cash *database.PokerChip) { *cash -= bet }) } -func (p *PokerPlayer) gain(db *database.DkfDB, pokerTableID int64, gain database.PokerChip) { +func (p *pokerPlayer) gain(db *database.DkfDB, pokerTableID int64, gain database.PokerChip) { _ = db.PokerTableAccountGain(p.userID, pokerTableID, gain) p.cash.With(func(cash *database.PokerChip) { *cash += gain }) - p.Bet.With(func(bet *database.PokerChip) { *bet = 0 }) + p.bet.With(func(bet *database.PokerChip) { *bet = 0 }) } // Reset player's bet to 0 and return the value it had before the reset -func (p *PokerPlayer) resetBet() (old database.PokerChip) { +func (p *pokerPlayer) resetBet() (old database.PokerChip) { // Do not track in database // DB keeps track of what was bet during the whole (1 hand) game - p.Bet.With(func(bet *database.PokerChip) { + p.bet.With(func(bet *database.PokerChip) { old = *bet *bet = 0 }) return } -func (p *PokerPlayer) refundBet(db *database.DkfDB, pokerTableID int64) { - p.gain(db, pokerTableID, p.GetBet()) +func (p *pokerPlayer) refundBet(db *database.DkfDB, pokerTableID int64) { + p.gain(db, pokerTableID, p.getBet()) } -func (p *PokerPlayer) doBetAndNotif(db *database.DkfDB, pokerTableID int64, bet database.PokerChip, roomTopic string) { +func (p *pokerPlayer) doBetAndNotif(db *database.DkfDB, pokerTableID int64, bet database.PokerChip, roomTopic string) { p.doBet(db, pokerTableID, bet) - PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerSeatIdx: p.seatIdx, Player: p.username, Bet: bet, TotalBet: p.GetBet(), Cash: p.getCash()}) + PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerSeatIdx: p.seatIdx, Player: p.username, Bet: bet, TotalBet: p.getBet(), Cash: p.getCash()}) } type playerCard struct { @@ -219,7 +219,7 @@ type PokerGame struct { type gameResult struct { handScore int32 - players []*PokerPlayer + players []*pokerPlayer } func (g *PokerGame) Check(userID database.UserID) { @@ -267,9 +267,9 @@ func (g *Ongoing) setMainPot(v database.PokerChip) { func (g *Ongoing) computeWinners() (winner []gameResult) { countAlive := 0 - var lastAlive *PokerPlayer + var lastAlive *pokerPlayer for _, p := range g.players { - if !p.Folded.Load() { + if !p.folded.Load() { countAlive++ lastAlive = p } @@ -277,17 +277,17 @@ func (g *Ongoing) computeWinners() (winner []gameResult) { if countAlive == 0 { return []gameResult{} } else if countAlive == 1 { - return []gameResult{{-1, []*PokerPlayer{lastAlive}}} + return []gameResult{{-1, []*pokerPlayer{lastAlive}}} } - m := make(map[int32][]*PokerPlayer) + m := make(map[int32][]*pokerPlayer) for _, p := range g.players { - if p.Folded.Load() { + if p.folded.Load() { continue } var playerCard1, playerCard2 string - p.Cards.RWith(func(pCards *[]playerCard) { + p.cards.RWith(func(pCards *[]playerCard) { playerCard1 = (*pCards)[0].name playerCard2 = (*pCards)[1].name }) @@ -307,7 +307,7 @@ func (g *Ongoing) computeWinners() (winner []gameResult) { } handEvaluation := poker.Evaluate(hand) if _, ok := m[handEvaluation]; !ok { - m[handEvaluation] = make([]*PokerPlayer, 0) + m[handEvaluation] = make([]*pokerPlayer, 0) } m[handEvaluation] = append(m[handEvaluation], p) } @@ -326,7 +326,7 @@ func sortGameResults(arr []gameResult) { for idx := range arr { sort.Slice(arr[idx].players, func(i, j int) bool { if arr[idx].players[i].getCash() == arr[idx].players[j].getCash() { - return arr[idx].players[i].GameBet < arr[idx].players[j].GameBet + return arr[idx].players[i].gameBet < arr[idx].players[j].gameBet } return arr[idx].players[i].getCash() < arr[idx].players[j].getCash() }) @@ -351,7 +351,7 @@ func (g *Ongoing) GetDeckHash() string { // Get the player index in Ongoing.Players from a seat index (index in PokerGame.Players) // [nil p1 nil nil p2 nil] -> PokerGame.Players // [p1 p2] -> Ongoing.Players -func (g *Ongoing) getPlayerBySeatIdx(seatIdx int) (*PokerPlayer, int) { +func (g *Ongoing) getPlayerBySeatIdx(seatIdx int) (*pokerPlayer, int) { for idx, p := range g.players { if p.seatIdx == seatIdx { return p, idx @@ -371,14 +371,14 @@ func (g *Ongoing) countCanBetPlayers() (nbCanBet int) { func (g *Ongoing) countAlivePlayers() (playerAlive int) { for _, p := range g.players { - if !p.Folded.Load() { + if !p.folded.Load() { playerAlive++ } } return } -func (g *Ongoing) getPlayer(userID database.UserID) *PokerPlayer { +func (g *Ongoing) getPlayer(userID database.UserID) *pokerPlayer { for _, p := range g.players { if p.userID == userID { return p @@ -411,7 +411,7 @@ func isSeated(players []*seatedPlayer, userID database.UserID) bool { return getPlayer(players, userID) != nil } -func isRoundSettled(players []*PokerPlayer) bool { +func isRoundSettled(players []*pokerPlayer) bool { type Tmp struct { Bet database.PokerChip AllIn bool @@ -419,7 +419,7 @@ func isRoundSettled(players []*PokerPlayer) bool { } arr := make([]Tmp, 0) for _, p := range players { - arr = append(arr, Tmp{Bet: p.GetBet(), AllIn: p.isAllIn(), Folded: p.Folded.Load()}) + arr = append(arr, Tmp{Bet: p.getBet(), AllIn: p.isAllIn(), Folded: p.folded.Load()}) } sort.Slice(arr, func(i, j int) bool { return arr[i].Bet > arr[j].Bet }) b := arr[0].Bet @@ -438,7 +438,7 @@ func (g *PokerGame) incrDealerIdx() (smallBlindIdx, bigBlindIdx int) { ongoing := g.Ongoing nbPlayers := len(ongoing.players) dealerSeatIdx := g.dealerSeatIdx.Load() - var dealerPlayer *PokerPlayer + var dealerPlayer *pokerPlayer var dealerIdx int for { dealerSeatIdx = (dealerSeatIdx + 1) % NbPlayers @@ -493,7 +493,7 @@ func (g *PokerGame) UnSitPlayer(userID database.UserID) { ongoing := g.Ongoing if ongoing != nil { if p := ongoing.getPlayer(userID); p != nil { - p.Unsit.Store(true) + p.unsit.Store(true) } } @@ -513,8 +513,8 @@ func (g *PokerGame) unSitPlayer1(gPlayers *[]*seatedPlayer, seatedPlayer *seated if ongoing != nil { if player := ongoing.getPlayer(seatedPlayer.userID); player != nil { g.sendUnsitPlayerEvent(player.userID) - player.Folded.Store(true) - player.Cards.RWith(func(playerCards *[]playerCard) { + player.folded.Store(true) + player.cards.RWith(func(playerCards *[]playerCard) { for _, card := range *playerCards { evt := PokerEvent{ID: "card" + itoa(card.idx), Name: "", Idx: card.idx, Top: BurnStackY, Left: BurnStackX, Angle: "0deg", Reveal: false} PokerPubSub.Pub(g.roomID.Topic(), evt) @@ -539,11 +539,11 @@ func generateDeck() []string { } func newOngoing(g *PokerGame) *Ongoing { - players := make([]*PokerPlayer, 0) + players := make([]*pokerPlayer, 0) g.Players.RWith(func(gPlayers *[]*seatedPlayer) { for _, p := range *gPlayers { if p.isEligible(g.PokerTableMinBet) { - players = append(players, &PokerPlayer{seatedPlayer: p}) + players = append(players, &pokerPlayer{seatedPlayer: p}) } } }) @@ -565,9 +565,9 @@ func showCards(g *PokerGame, seats []Seat) { ongoing := g.Ongoing roomTopic := g.roomID.Topic() for _, p := range ongoing.players { - if !p.Folded.Load() { + if !p.folded.Load() { var firstCard, secondCard playerCard - p.Cards.RWith(func(pCards *[]playerCard) { + p.cards.RWith(func(pCards *[]playerCard) { firstCard = (*pCards)[0] secondCard = (*pCards)[1] }) @@ -641,11 +641,11 @@ type autoAction struct { evt playerEvent } -func foldPlayer(g *PokerGame, p *PokerPlayer) { +func foldPlayer(g *PokerGame, p *pokerPlayer) { roomTopic := g.roomID.Topic() - p.Folded.Store(true) + p.folded.Store(true) var firstCardIdx, secondCardIdx int - p.Cards.RWith(func(pCards *[]playerCard) { + p.cards.RWith(func(pCards *[]playerCard) { firstCardIdx = (*pCards)[0].idx secondCardIdx = (*pCards)[1].idx }) @@ -656,7 +656,7 @@ func foldPlayer(g *PokerGame, p *PokerPlayer) { g.Ongoing.addEvent(evt1, evt2) } -func doUnsit(g *PokerGame, p *PokerPlayer, playerAlive *int) int { +func doUnsit(g *PokerGame, p *pokerPlayer, playerAlive *int) int { *playerAlive = g.Ongoing.countAlivePlayers() if *playerAlive == 1 { p.countChancesToAction-- @@ -665,9 +665,9 @@ func doUnsit(g *PokerGame, p *PokerPlayer, playerAlive *int) int { return continueGetPlayerEventLoop } -func doTimeout(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, playerAlive *int) int { +func doTimeout(g *PokerGame, p *pokerPlayer, minBet *database.PokerChip, playerAlive *int) int { pUsername := p.username - if p.GetBet() < *minBet { + if p.getBet() < *minBet { foldPlayer(g, p) g.newLogEvent(fmt.Sprintf("%s auto fold", pUsername)) @@ -681,7 +681,7 @@ func doTimeout(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, playerA return breakGetPlayerEventLoop } -func doFold(g *PokerGame, p *PokerPlayer, playerAlive *int) int { +func doFold(g *PokerGame, p *pokerPlayer, playerAlive *int) int { foldPlayer(g, p) g.newLogEvent(fmt.Sprintf("%s fold", p.username)) @@ -693,9 +693,9 @@ func doFold(g *PokerGame, p *PokerPlayer, playerAlive *int) int { return doNothing } -func doCheck(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip) int { - if p.GetBet() < *minBet { - msg := fmt.Sprintf("Need to bet %d", *minBet-p.GetBet()) +func doCheck(g *PokerGame, p *pokerPlayer, minBet *database.PokerChip) int { + if p.getBet() < *minBet { + msg := fmt.Sprintf("Need to bet %d", *minBet-p.getBet()) PokerPubSub.Pub(g.roomID.UserTopic(p.userID), ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } @@ -703,10 +703,10 @@ func doCheck(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip) int { return doNothing } -func doCall(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, - newlyAllInPlayers *[]*PokerPlayer) int { +func doCall(g *PokerGame, p *pokerPlayer, minBet *database.PokerChip, + newlyAllInPlayers *[]*pokerPlayer) int { pUsername := p.username - bet := utils.MinInt(*minBet-p.GetBet(), p.getCash()) + bet := utils.MinInt(*minBet-p.getBet(), p.getCash()) if bet == 0 { g.newLogEvent(fmt.Sprintf("%s check", pUsername)) } else { @@ -721,13 +721,13 @@ func doCall(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, return doNothing } -func doAllIn(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, - newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int) int { +func doAllIn(g *PokerGame, p *pokerPlayer, minBet *database.PokerChip, + newlyAllInPlayers *[]*pokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int) int { bet := p.getCash() - if (p.GetBet() + bet) > *minBet { + if (p.getBet() + bet) > *minBet { *lastRaisePlayerIdx = playerToPlayIdx } - *minBet = utils.MaxInt(p.GetBet()+bet, *minBet) + *minBet = utils.MaxInt(p.getBet()+bet, *minBet) p.doBetAndNotif(g.db, g.pokerTableID, bet, g.roomID.Topic()) logMsg := fmt.Sprintf("%s all-in (%d)", p.username, bet) if p.isAllIn() { @@ -737,20 +737,20 @@ func doAllIn(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, return doNothing } -func doBet(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, - newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int, evt playerEvent) int { +func doBet(g *PokerGame, p *pokerPlayer, minBet *database.PokerChip, + newlyAllInPlayers *[]*pokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int, evt playerEvent) int { roomTopic := g.roomID.Topic() roomUserTopic := g.roomID.UserTopic(p.userID) pokerTableMinBet := g.PokerTableMinBet bet := evt.Bet // Ensure the player cannot bet below the table minimum bet (amount of the big blind) - if p.GetBet()+bet != *minBet && bet < pokerTableMinBet { + if p.getBet()+bet != *minBet && bet < pokerTableMinBet { msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", bet, pokerTableMinBet) PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } - if (p.GetBet() + bet) < *minBet { - msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", bet, *minBet-p.GetBet()) + if (p.getBet() + bet) < *minBet { + msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", bet, *minBet-p.getBet()) PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } @@ -759,10 +759,10 @@ func doBet(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } - if (p.GetBet() + bet) > *minBet { + if (p.getBet() + bet) > *minBet { *lastRaisePlayerIdx = playerToPlayIdx } - *minBet = utils.MaxInt(p.GetBet()+bet, *minBet) + *minBet = utils.MaxInt(p.getBet()+bet, *minBet) p.doBetAndNotif(g.db, g.pokerTableID, bet, roomTopic) logMsg := fmt.Sprintf("%s bet %d", p.username, bet) if p.isAllIn() { @@ -792,8 +792,8 @@ func handleAutoActionReceived(g *PokerGame, autoCache map[database.UserID]autoAc return continueGetPlayerEventLoop } -func applyAutoAction(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, - newlyAllInPlayers *[]*PokerPlayer, +func applyAutoAction(g *PokerGame, p *pokerPlayer, minBet *database.PokerChip, + newlyAllInPlayers *[]*pokerPlayer, lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, autoAction autoAction, autoCache map[database.UserID]autoAction) (actionResult int) { @@ -821,8 +821,8 @@ func applyAutoAction(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, return } -func handlePlayerActionEvent(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, - newlyAllInPlayers *[]*PokerPlayer, +func handlePlayerActionEvent(g *PokerGame, p *pokerPlayer, minBet *database.PokerChip, + newlyAllInPlayers *[]*pokerPlayer, lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, evt playerEvent) (actionResult int) { p.lastActionTS = time.Now() @@ -851,7 +851,7 @@ func execBettingRound(g *PokerGame, skip int, minBet database.PokerChip) bool { _, dealerIdx := ongoing.getPlayerBySeatIdx(int(g.dealerSeatIdx.Load())) playerToPlayIdx := (dealerIdx + skip) % len(ongoing.players) lastRaisePlayerIdx := -1 - newlyAllInPlayers := make([]*PokerPlayer, 0) + newlyAllInPlayers := make([]*pokerPlayer, 0) autoCache := make(map[database.UserID]autoAction) playerAlive := ongoing.countAlivePlayers() @@ -974,12 +974,12 @@ func resetPlayersBet(ongoing *Ongoing) (sum database.PokerChip) { } func refundUncalledBet(db *database.DkfDB, ongoing *Ongoing, pokerTableID int64, roomTopic string) { - newArray := make([]*PokerPlayer, len(ongoing.players)) + newArray := make([]*pokerPlayer, len(ongoing.players)) copy(newArray, ongoing.players) - sort.Slice(newArray, func(i, j int) bool { return newArray[i].GetBet() > newArray[j].GetBet() }) + sort.Slice(newArray, func(i, j int) bool { return newArray[i].getBet() > newArray[j].getBet() }) firstPlayer := newArray[0] secondPlayer := newArray[1] - diff := firstPlayer.GetBet() - secondPlayer.GetBet() + diff := firstPlayer.getBet() - secondPlayer.getBet() if diff > 0 { firstPlayer.refundPartialBet(db, pokerTableID, diff) PokerPubSub.Pub(roomTopic, RedrawSeatsEvent{}) @@ -1078,7 +1078,7 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { PokerPubSub.Pub(roomTopic, evt) PokerPubSub.Pub(roomUserTopic, evt1) - p.Cards.With(func(pCards *[]playerCard) { + p.cards.With(func(pCards *[]playerCard) { *pCards = append(*pCards, playerCard{idx: *idx, name: card}) }) @@ -1087,13 +1087,13 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { } } -func computeAllInMaxGain(ongoing *Ongoing, newlyAllInPlayers []*PokerPlayer, mainPot database.PokerChip) { +func computeAllInMaxGain(ongoing *Ongoing, newlyAllInPlayers []*pokerPlayer, mainPot database.PokerChip) { for _, p := range newlyAllInPlayers { maxGain := mainPot for _, op := range ongoing.players { - maxGain += utils.MinInt(op.GetBet(), p.GetBet()) + maxGain += utils.MinInt(op.getBet(), p.getBet()) } - p.AllInMaxGain = maxGain + p.allInMaxGain = maxGain } } @@ -1295,7 +1295,7 @@ func applyGains(g *PokerGame, playersGain []PlayerGain, mainPot, rake database.P } type PlayerGain struct { - Player *PokerPlayer + Player *pokerPlayer Gain database.PokerChip Group int HandStr string @@ -1327,7 +1327,7 @@ func calculateRake(mainPot, pokerTableMinBet database.PokerChip, nbPlayers int) } func processPot(winners []gameResult, mainPot, pokerTableMinBet database.PokerChip, collectRake bool, nbPlayers int) (res []PlayerGain, rake database.PokerChip) { - newPlayerGain := func(player *PokerPlayer, gain database.PokerChip, groupIdx int, handStr string) PlayerGain { + newPlayerGain := func(player *pokerPlayer, gain database.PokerChip, groupIdx int, handStr string) PlayerGain { return PlayerGain{Player: player, Gain: gain, Group: groupIdx, HandStr: handStr} } @@ -1364,7 +1364,7 @@ func processPot(winners []gameResult, mainPot, pokerTableMinBet database.PokerCh } else if len(group.players) == 1 && group.players[0].isAllIn() { // Only 1 player win but is all-in player := group.players[0] - piece := utils.MinInt(player.AllInMaxGain, mainPot) + piece := utils.MinInt(player.allInMaxGain, mainPot) res = append(res, newPlayerGain(player, piece, groupIdx, handStr)) mainPot -= piece isDone = false @@ -1376,7 +1376,7 @@ func processPot(winners []gameResult, mainPot, pokerTableMinBet database.PokerCh for _, p := range group.players { if p.isAllIn() { allInCount++ - maxGain := p.AllInMaxGain + maxGain := p.allInMaxGain piece := utils.MinInt(maxGain, expectedSplit) piece = utils.MinInt(piece, mainPot) res = append(res, newPlayerGain(p, piece, groupIdx, handStr)) @@ -1799,8 +1799,8 @@ func drawSeatsStyle(authUser *database.User, g *PokerGame) string { html += `#seat` + itoa(i+1) + ` .inner:before { content: "` + pUsername.String() + `"; }` html += `#seat` + itoa(i+1) + `_cash:before { content: "` + itoa2(p.getCash()) + `"; }` if ongoing != nil { - if op := ongoing.getPlayer(pUserID); op != nil && op.GetBet() > 0 { - html += `#seat` + itoa(i+1) + `Pot:before { content: "` + itoa2(op.GetBet()) + `"; }` + if op := ongoing.getPlayer(pUserID); op != nil && op.getBet() > 0 { + html += `#seat` + itoa(i+1) + `Pot:before { content: "` + itoa2(op.getBet()) + `"; }` } } } else { diff --git a/pkg/web/handlers/poker/poker_test.go b/pkg/web/handlers/poker/poker_test.go @@ -12,12 +12,12 @@ func n(v uint64) utils.RWMtx[database.PokerChip] { } func Test_sortGameResults(t *testing.T) { - p1 := &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p1"}, GameBet: 10} - p2 := &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, GameBet: 20} - p3 := &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p3"}, GameBet: 30} - p4 := &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p4"}, GameBet: 100} + p1 := &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p1"}, gameBet: 10} + p2 := &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, gameBet: 20} + p3 := &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p3"}, gameBet: 30} + p4 := &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p4"}, gameBet: 100} arr := []gameResult{ - {1, []*PokerPlayer{p2, p4, p1, p3}}, + {1, []*pokerPlayer{p2, p4, p1, p3}}, } sortGameResults(arr) assert.Equal(t, database.Username("p1"), arr[0].players[0].username) @@ -27,16 +27,16 @@ func Test_sortGameResults(t *testing.T) { } func Test_processPot(t *testing.T) { - var p1, p2, p3, p4 *PokerPlayer + var p1, p2, p3, p4 *pokerPlayer var arr []gameResult var res []PlayerGain - p1 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p1"}, GameBet: 100, AllInMaxGain: 400} - p2 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, GameBet: 200, AllInMaxGain: 700} - p3 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p3"}, GameBet: 300, AllInMaxGain: 900} - p4 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p4"}, GameBet: 400} + p1 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p1"}, gameBet: 100, allInMaxGain: 400} + p2 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, gameBet: 200, allInMaxGain: 700} + p3 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p3"}, gameBet: 300, allInMaxGain: 900} + p4 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p4"}, gameBet: 400} arr = []gameResult{ - {1, []*PokerPlayer{p2, p4, p1, p3}}, + {1, []*pokerPlayer{p2, p4, p1, p3}}, } sortGameResults(arr) res, _ = processPot(arr, 1000, 20, false, 4) @@ -45,12 +45,12 @@ func Test_processPot(t *testing.T) { assert.Equal(t, database.PokerChip(250), res[2].Gain) assert.Equal(t, database.PokerChip(250), res[3].Gain) - p1 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p1"}, GameBet: 10, AllInMaxGain: 40} - p2 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, GameBet: 20, AllInMaxGain: 70} - p3 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p3"}, GameBet: 300, AllInMaxGain: 630} - p4 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p4"}, GameBet: 400} + p1 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p1"}, gameBet: 10, allInMaxGain: 40} + p2 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, gameBet: 20, allInMaxGain: 70} + p3 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p3"}, gameBet: 300, allInMaxGain: 630} + p4 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p4"}, gameBet: 400} arr = []gameResult{ - {1, []*PokerPlayer{p2, p4, p1, p3}}, + {1, []*pokerPlayer{p2, p4, p1, p3}}, } sortGameResults(arr) res, _ = processPot(arr, 1000, 20, false, 4) @@ -59,11 +59,11 @@ func Test_processPot(t *testing.T) { assert.Equal(t, database.PokerChip(445), res[2].Gain) assert.Equal(t, database.PokerChip(445), res[3].Gain) - p1 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p1"}, GameBet: 500} - p2 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, GameBet: 500, AllInMaxGain: 1000} + p1 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p1"}, gameBet: 500} + p2 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, gameBet: 500, allInMaxGain: 1000} arr = []gameResult{ - {1, []*PokerPlayer{p2}}, - {2, []*PokerPlayer{p1}}, + {1, []*pokerPlayer{p2}}, + {2, []*pokerPlayer{p1}}, } sortGameResults(arr) res, _ = processPot(arr, 1000, 20, false, 2) @@ -71,14 +71,14 @@ func Test_processPot(t *testing.T) { assert.Equal(t, database.Username("p2"), res[0].Player.username) assert.Equal(t, database.PokerChip(1000), res[0].Gain) - p1 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p1"}, GameBet: 5} - p2 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p2"}, GameBet: 5} - p3 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p3"}, GameBet: 5} - p4 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p4"}, GameBet: 5} + p1 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p1"}, gameBet: 5} + p2 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p2"}, gameBet: 5} + p3 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p3"}, gameBet: 5} + p4 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(1), username: "p4"}, gameBet: 5} //p5 = &PokerPlayer{Cash: 1, GameBet: 3, Folded: true, Username: "p5"} arr = []gameResult{ - {1, []*PokerPlayer{p1, p2, p3}}, - {2, []*PokerPlayer{p4}}, + {1, []*pokerPlayer{p1, p2, p3}}, + {2, []*pokerPlayer{p4}}, } sortGameResults(arr) res, _ = processPot(arr, 23, 20, false, 4) @@ -87,11 +87,11 @@ func Test_processPot(t *testing.T) { assert.Equal(t, database.PokerChip(8), res[1].Gain) assert.Equal(t, database.PokerChip(7), res[2].Gain) - p1 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p1"}, GameBet: 900, AllInMaxGain: 1560} - p2 = &PokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, GameBet: 640, AllInMaxGain: 1300} + p1 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p1"}, gameBet: 900, allInMaxGain: 1560} + p2 = &pokerPlayer{seatedPlayer: &seatedPlayer{cash: n(0), username: "p2"}, gameBet: 640, allInMaxGain: 1300} arr = []gameResult{ - {1, []*PokerPlayer{p2}}, - {2, []*PokerPlayer{p1}}, + {1, []*pokerPlayer{p2}}, + {2, []*pokerPlayer{p1}}, } sortGameResults(arr) res, _ = processPot(arr, 1560, 20, false, 2) @@ -102,28 +102,28 @@ func Test_processPot(t *testing.T) { func Test_isRoundSettled(t *testing.T) { type args struct { - players []*PokerPlayer + players []*pokerPlayer } tests := []struct { name string args args want bool }{ - {"1", args{players: []*PokerPlayer{ - {Bet: n(10), seatedPlayer: &seatedPlayer{cash: n(0)}}, - {Bet: n(20), seatedPlayer: &seatedPlayer{cash: n(0)}}, - {Bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}, - {Bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}}}, true}, - {"2", args{players: []*PokerPlayer{ - {Bet: n(100), seatedPlayer: &seatedPlayer{cash: n(0)}}, - {Bet: n(20), seatedPlayer: &seatedPlayer{cash: n(0)}}, - {Bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}, - {Bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}}}, false}, - {"3", args{players: []*PokerPlayer{ - {Bet: n(10), seatedPlayer: &seatedPlayer{cash: n(0)}}, - {Bet: n(200), seatedPlayer: &seatedPlayer{cash: n(0)}}, - {Bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}, - {Bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}}}, false}, + {"1", args{players: []*pokerPlayer{ + {bet: n(10), seatedPlayer: &seatedPlayer{cash: n(0)}}, + {bet: n(20), seatedPlayer: &seatedPlayer{cash: n(0)}}, + {bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}, + {bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}}}, true}, + {"2", args{players: []*pokerPlayer{ + {bet: n(100), seatedPlayer: &seatedPlayer{cash: n(0)}}, + {bet: n(20), seatedPlayer: &seatedPlayer{cash: n(0)}}, + {bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}, + {bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}}}, false}, + {"3", args{players: []*pokerPlayer{ + {bet: n(10), seatedPlayer: &seatedPlayer{cash: n(0)}}, + {bet: n(200), seatedPlayer: &seatedPlayer{cash: n(0)}}, + {bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}, + {bet: n(30), seatedPlayer: &seatedPlayer{cash: n(1)}}}}, false}, // TODO: Add test cases. } for _, tt := range tests {