dkforest

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

commit fb5187b60a77a69804df3cdde60a72ab919cc678
parent 83f14b3f45c636dbf633867ceadc874ba5a9ec04
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sun, 17 Dec 2023 06:35:20 -0500

cleanup

Diffstat:
Mpkg/web/handlers/poker/poker.go | 137++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
1 file changed, 76 insertions(+), 61 deletions(-)

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -366,25 +366,27 @@ func isRoundSettled(players []*PokerPlayer) bool { } func (g *PokerGame) incrDealerIdx() { - nbPlayers := len(g.Ongoing.Players) + ongoing := g.Ongoing + nbPlayers := len(ongoing.Players) dealerSeatIdx := g.DealerSeatIdx.Load() var dealerPlayer *PokerPlayer var dealerIdx int for { dealerSeatIdx = (dealerSeatIdx + 1) % NbPlayers - if dealerPlayer, dealerIdx = g.Ongoing.getPlayerBySeatIdx(int(dealerSeatIdx)); dealerPlayer != nil { + if dealerPlayer, dealerIdx = ongoing.getPlayerBySeatIdx(int(dealerSeatIdx)); dealerPlayer != nil { break } } g.DealerSeatIdx.Store(dealerSeatIdx) - startIDx := utils.Ternary(g.Ongoing.isHeadsUpGame(), 0, 1) + startIDx := utils.Ternary(ongoing.isHeadsUpGame(), 0, 1) g.smallBlindIdx = (dealerIdx + startIDx) % nbPlayers g.bigBlindIdx = (dealerIdx + startIDx + 1) % nbPlayers } func (g *PokerGame) UnSitPlayer(username database.Username) error { - if g.Ongoing != nil { - if p := g.Ongoing.GetPlayer(username); p != nil { + ongoing := g.Ongoing + if ongoing != nil { + if p := ongoing.GetPlayer(username); p != nil { p.Unsit.Store(true) } } @@ -399,6 +401,7 @@ func (g *PokerGame) UnSitPlayer(username database.Username) error { func (g *PokerGame) UnSitPlayer1(seatedPlayer *SeatedPlayer) error { db := g.DB + ongoing := g.Ongoing pokerTableID := g.PokerTableID roomTopic := g.RoomID.Topic() seatedPlayerUsername := seatedPlayer.Username @@ -418,8 +421,8 @@ func (g *PokerGame) UnSitPlayer1(seatedPlayer *SeatedPlayer) error { user.DoSave(tx) tx.Commit() - if g.Ongoing != nil { - if player := g.Ongoing.GetPlayer(seatedPlayerUsername); player != nil { + if ongoing != nil { + if player := ongoing.GetPlayer(seatedPlayerUsername); player != nil { select { case g.PlayersEventCh <- PlayerEvent{Player: player.Username, Unsit: true}: default: @@ -429,7 +432,7 @@ func (g *PokerGame) UnSitPlayer1(seatedPlayer *SeatedPlayer) error { for _, card := range player.Cards { evt := PokerEvent{ID: "card" + itoa(card.Idx), Name: "", Idx: card.Idx, Top: BurnStackY, Left: BurnStackX, Angle: "0deg", Reveal: false} PokerPubSub.Pub(roomTopic, evt) - g.Ongoing.AddEvent(evt) + ongoing.AddEvent(evt) } player.CardsMtx.RUnlock() } @@ -476,18 +479,20 @@ func NewOngoing(g *PokerGame) *Ongoing { } func (g *PokerGame) newLogEvent(msg string) { + ongoing := g.Ongoing logEvt := LogEvent{Message: msg} PokerPubSub.Pub(g.RoomID.LogsTopic(), logEvt) - if g.Ongoing != nil { - g.Ongoing.LogEventsMtx.Lock() - g.Ongoing.LogEvents = append(g.Ongoing.LogEvents, logEvt) - g.Ongoing.LogEventsMtx.Unlock() + if ongoing != nil { + ongoing.LogEventsMtx.Lock() + ongoing.LogEvents = append(ongoing.LogEvents, logEvt) + ongoing.LogEventsMtx.Unlock() } } func showCards(g *PokerGame, seats []Seat) { + ongoing := g.Ongoing roomTopic := g.RoomID.Topic() - for _, p := range g.Ongoing.Players { + for _, p := range ongoing.Players { if !p.Folded.Load() { p.CardsMtx.RLock() firstCard := p.Cards[0] @@ -505,19 +510,20 @@ func showCards(g *PokerGame, seats []Seat) { evt2 := PokerEvent{ID: "card" + itoa(secondCard.Idx), Name: secondCard.Name, Idx: secondCard.Idx, Top: seatData.Top, Left: seatData.Left + 53, Reveal: true} PokerPubSub.Pub(roomTopic, evt1) PokerPubSub.Pub(roomTopic, evt2) - g.Ongoing.AddEvent(evt1, evt2) + ongoing.AddEvent(evt1, evt2) } } } func setWaitTurn(g *PokerGame, seatIdx int) { + ongoing := g.Ongoing roomTopic := g.RoomID.Topic() evt := PokerWaitTurnEvent{Idx: seatIdx, CreatedAt: time.Now()} PokerPubSub.Pub(roomTopic, evt) - g.Ongoing.WaitTurnEventMtx.Lock() - g.Ongoing.WaitTurnEvent = evt - g.Ongoing.WaitTurnEventMtx.Unlock() + ongoing.WaitTurnEventMtx.Lock() + ongoing.WaitTurnEvent = evt + ongoing.WaitTurnEventMtx.Unlock() } const ( @@ -544,11 +550,12 @@ type AutoAction struct { // Return either or not the game ended because only 1 player left playing (or none) func execBettingRound(g *PokerGame, skip int, minBet database.PokerChip) bool { db := g.DB + ongoing := g.Ongoing pokerTableID := g.PokerTableID roomID := g.RoomID roomTopic := roomID.Topic() - _, dealerIdx := g.Ongoing.getPlayerBySeatIdx(int(g.DealerSeatIdx.Load())) - playerToPlayIdx := (dealerIdx + skip) % len(g.Ongoing.Players) + _, dealerIdx := ongoing.getPlayerBySeatIdx(int(g.DealerSeatIdx.Load())) + playerToPlayIdx := (dealerIdx + skip) % len(ongoing.Players) lastRaisePlayerIdx := -1 newlyAllInPlayers := make([]*PokerPlayer, 0) autoCache := make(map[database.Username]AutoAction) @@ -561,13 +568,13 @@ func execBettingRound(g *PokerGame, skip int, minBet database.PokerChip) bool { p.CardsMtx.RUnlock() PokerPubSub.Pub(roomTopic, evt1) PokerPubSub.Pub(roomTopic, evt2) - g.Ongoing.AddEvent(evt1, evt2) + ongoing.AddEvent(evt1, evt2) } - playerAlive := g.Ongoing.CountAlivePlayers() + playerAlive := ongoing.CountAlivePlayers() // Avoid asking for actions if only 1 player can do so (because others are all-in) - nbCanVote := g.Ongoing.CountCanVotePlayers() + nbCanVote := ongoing.CountCanVotePlayers() if nbCanVote == 0 || nbCanVote == 1 { goto RoundIsSettled } @@ -578,8 +585,8 @@ RoundIsSettledLoop: for { // Repeat until the round is settled (all players have equals bet or fold or all-in) AllPlayersLoop: for { // Repeat until all players have played - playerToPlayIdx = (playerToPlayIdx + 1) % len(g.Ongoing.Players) - p := g.Ongoing.Players[playerToPlayIdx] + playerToPlayIdx = (playerToPlayIdx + 1) % len(ongoing.Players) + p := ongoing.Players[playerToPlayIdx] p.countChancesToAction++ pUsername := p.Username roomUserTopic := roomID.UserTopic(pUsername) @@ -613,7 +620,7 @@ RoundIsSettledLoop: } doUnsit := func() int { - playerAlive = g.Ongoing.CountAlivePlayers() + playerAlive = ongoing.CountAlivePlayers() if playerAlive == 1 { p.countChancesToAction-- return breakRoundIsSettledLoop @@ -825,7 +832,7 @@ RoundIsSettledLoop: } // End of repeat until we get an event from the player we're interested in } // End of repeat until all players have played // All settle when all players have the same bet amount - if isRoundSettled(g.Ongoing.Players) { + if isRoundSettled(ongoing.Players) { break RoundIsSettledLoop } } // End of repeat until the round is settled (all players have equals bet or fold or all-in) @@ -839,7 +846,7 @@ RoundIsSettled: time.Sleep(time.Second) - mainPot := g.Ongoing.getMainPot() + mainPot := ongoing.getMainPot() // Calculate what is the max gain all-in players can make computeAllInMaxGain(g, newlyAllInPlayers, mainPot) @@ -847,8 +854,8 @@ RoundIsSettled: // When only one player remain alive (everyone else fold) // We refund the "uncalled bet" so that it does not go in the main pot and does not get raked. if playerAlive == 1 { - newArray := make([]*PokerPlayer, len(g.Ongoing.Players)) - copy(newArray, g.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() }) firstPlayer := newArray[0] secondPlayer := newArray[1] @@ -857,13 +864,13 @@ RoundIsSettled: } // Transfer players bets into the main pot - for _, p := range g.Ongoing.Players { + for _, p := range ongoing.Players { mainPot += p.GetBet() p.resetBet() } PokerPubSub.Pub(roomTopic, PokerMainPotUpdatedEvent{MainPot: mainPot}) - g.Ongoing.setMainPot(mainPot) + ongoing.setMainPot(mainPot) return playerAlive <= 1 } @@ -887,10 +894,11 @@ var dealerTokenPos = [][]int{ } func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { + ongoing := g.Ongoing roomTopic := g.RoomID.Topic() var card string for cardIdx := 1; cardIdx <= NbCardsPerPlayer; cardIdx++ { - for _, p := range g.Ongoing.Players { + for _, p := range ongoing.Players { pUsername := p.Username if p.isAllIn() { continue @@ -901,7 +909,7 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { if p.Folded.Load() { continue } - card = g.Ongoing.Deck[*idx] + card = ongoing.Deck[*idx] *idx++ left := seatData.Left top := seatData.Top @@ -932,7 +940,7 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { p.Cards = append(p.Cards, PlayerCard{Idx: *idx, Name: card}) p.CardsMtx.Unlock() - g.Ongoing.AddEvent(evt, evt1) + ongoing.AddEvent(evt, evt1) } } } @@ -952,7 +960,8 @@ func dealerThread(g *PokerGame) { roomTopic := roomID.Topic() bigBlindBet := g.PokerTableMinBet collectRake := false - isHeadsUpGame := g.Ongoing.isHeadsUpGame() + ongoing := g.Ongoing + isHeadsUpGame := ongoing.isHeadsUpGame() g.incrDealerIdx() @@ -978,12 +987,12 @@ func dealerThread(g *PokerGame) { Left: BurnStackX + (burnIdx * 4), } PokerPubSub.Pub(roomTopic, evt) - g.Ongoing.AddEvent(evt) + ongoing.AddEvent(evt) burnIdx++ } dealCard := func(dealCardIdx int) { - card := g.Ongoing.Deck[idx] + card := ongoing.Deck[idx] idx++ evt := PokerEvent{ ID: "card" + itoa(idx), @@ -994,8 +1003,8 @@ func dealerThread(g *PokerGame) { Reveal: true, } PokerPubSub.Pub(roomTopic, evt) - g.Ongoing.AddEvent(evt) - g.Ongoing.CommunityCards = append(g.Ongoing.CommunityCards, card) + ongoing.AddEvent(evt) + ongoing.CommunityCards = append(ongoing.CommunityCards, card) } PokerPubSub.Pub(roomTopic, GameStartedEvent{DealerSeatIdx: int(g.DealerSeatIdx.Load())}) @@ -1065,12 +1074,12 @@ func dealerThread(g *PokerGame) { END: - winners := g.Ongoing.computeWinners() - mainPot := g.Ongoing.getMainPot() + winners := ongoing.computeWinners() + mainPot := ongoing.getMainPot() playersGain, rake := processPot(winners, mainPot, bigBlindBet, collectRake) winnersStr, winnerHand := applyGains(g, playersGain, mainPot, rake) - g.Ongoing.setMainPot(0) + ongoing.setMainPot(0) PokerPubSub.Pub(roomTopic, GameIsDoneEvent{Winner: winnersStr, WinnerHand: winnerHand}) g.newLogEvent(fmt.Sprintf("-- Game ended --")) @@ -1099,6 +1108,7 @@ func applyBlindBet(g *PokerGame, playerIdx int, bet database.PokerChip, name str } func autoUnsitInactivePlayers(g *PokerGame) { + ongoing := g.Ongoing pokerTableMinBet := g.PokerTableMinBet roomTopic := g.RoomID.Topic() g.PlayersMtx.Lock() @@ -1108,10 +1118,10 @@ func autoUnsitInactivePlayers(g *PokerGame) { playerShallBeBooted := false if !p.isEligible(pokerTableMinBet) { playerShallBeBooted = true - } else if p.LastActionTS.Before(g.Ongoing.CreatedAt) { + } else if p.LastActionTS.Before(ongoing.CreatedAt) { // If the player was playing the game, must be booted if he had the chance to make actions and did not. // If the player was not playing the game, must be booted if he's not eligible to play the next one. - op := g.Ongoing.GetPlayer(p.Username) + op := ongoing.GetPlayer(p.Username) playerShallBeBooted = (op != nil && op.countChancesToAction > 0) || (op == nil && !p.isEligible(pokerTableMinBet)) } @@ -1128,6 +1138,7 @@ func autoUnsitInactivePlayers(g *PokerGame) { } func applyGains(g *PokerGame, playersGain []PlayerGain, mainPot, rake database.PokerChip) (winnersStr, winnerHand string) { + ongoing := g.Ongoing pokerTableID := g.PokerTableID nbPlayersGain := len(playersGain) tx := g.DB.Begin() @@ -1144,13 +1155,13 @@ func applyGains(g *PokerGame, playersGain []PlayerGain, mainPot, rake database.P winnersStr += el.Player.Username.String() + " " el.Player.gain(tx, pokerTableID, el.Gain) } - for _, op := range g.Ongoing.Players { + for _, op := range ongoing.Players { op.resetBet() } } else if nbPlayersGain == 0 { // No winners, refund bets - for _, op := range g.Ongoing.Players { + for _, op := range ongoing.Players { op.refundBet(tx, pokerTableID) } } @@ -1631,6 +1642,7 @@ func buildPayloadHtml(g *PokerGame, authUser *database.User, payload any) (html } func buildBaseHtml(g *PokerGame, authUser *database.User, playerBuyIn database.PokerChip) (html string) { + ongoing := g.Ongoing roomID := g.RoomID html += hutils.HtmlCssReset html += pokerCss @@ -1648,13 +1660,13 @@ func buildBaseHtml(g *PokerGame, authUser *database.User, playerBuyIn database.P html += buildWinnerHtml() html += buildCountdownsHtml() - if g.Ongoing != nil { - g.Ongoing.WaitTurnEventMtx.RLock() - html += drawCountDownStyle(g.Ongoing.WaitTurnEvent) - g.Ongoing.WaitTurnEventMtx.RUnlock() + if ongoing != nil { + ongoing.WaitTurnEventMtx.RLock() + html += drawCountDownStyle(ongoing.WaitTurnEvent) + ongoing.WaitTurnEventMtx.RUnlock() - g.Ongoing.EventsMtx.RLock() - for _, evt := range g.Ongoing.Events { + ongoing.EventsMtx.RLock() + for _, evt := range ongoing.Events { if evt.Player == "" { html += getPokerEventHtml(evt, "0s") } @@ -1662,7 +1674,7 @@ func buildBaseHtml(g *PokerGame, authUser *database.User, playerBuyIn database.P html += getPokerEventHtml(evt, "0s") } } - g.Ongoing.EventsMtx.RUnlock() + ongoing.EventsMtx.RUnlock() } return } @@ -1768,10 +1780,11 @@ func buildCardsHtml() (html string) { } func buildMainPotHtml(g *PokerGame) string { + ongoing := g.Ongoing html := `<div id="mainPot"></div>` mainPot := uint64(0) - if g.Ongoing != nil { - mainPot = uint64(g.Ongoing.getMainPot()) + if ongoing != nil { + mainPot = uint64(ongoing.getMainPot()) } html += `<style>#mainPot:before { content: "Pot: ` + itoa1(mainPot) + `"; }</style>` return html @@ -1838,6 +1851,7 @@ func buildSeatsHtml(g *PokerGame, authUser *database.User, playerBuyIn database. } func drawSeatsStyle(authUser *database.User, g *PokerGame) string { + ongoing := g.Ongoing html := "<style>" seated, _ := g.IsSeated(authUser.Username) g.PlayersMtx.RLock() @@ -1855,8 +1869,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 g.Ongoing != nil { - if op := g.Ongoing.GetPlayer(pUsername); op != nil && op.GetBet() > 0 { + if ongoing != nil { + if op := ongoing.GetPlayer(pUsername); op != nil && op.GetBet() > 0 { html += `#seat` + itoa(i+1) + `Pot:before { content: "` + itoa2(op.GetBet()) + `"; }` } } @@ -2243,6 +2257,7 @@ func PokerLogsHandler(c echo.Context) error { if g == nil { return c.Redirect(http.StatusFound, "/") } + ongoing := g.Ongoing roomLogsTopic := roomID.LogsTopic() sub := PokerPubSub.Subscribe([]string{roomLogsTopic}) defer sub.Close() @@ -2257,12 +2272,12 @@ func PokerLogsHandler(c echo.Context) error { send(hutils.HtmlCssReset) send(`<style>body { background-color: #ccc; }</style><div style="display:flex;flex-direction:column-reverse;">`) - if g.Ongoing != nil { - g.Ongoing.LogEventsMtx.RLock() - for _, evt := range g.Ongoing.LogEvents { + if ongoing != nil { + ongoing.LogEventsMtx.RLock() + for _, evt := range ongoing.LogEvents { send(fmt.Sprintf(`<div>%s</div>`, evt.Message)) } - g.Ongoing.LogEventsMtx.RUnlock() + ongoing.LogEventsMtx.RUnlock() } c.Response().Flush()