commit 3cc104e0431ef5515067dfc1ba16d2c78f2a1c8e
parent 8a30fe507d6611e08b27e6e782ef24f45f2eaa5b
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Fri, 8 Dec 2023 04:21:17 -0500
cleanup
Diffstat:
1 file changed, 9 insertions(+), 19 deletions(-)
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -88,8 +88,7 @@ type Ongoing struct {
CommunityCards []string
WaitTurnEvent PokerWaitTurnEvent
WaitTurnEventMtx sync.RWMutex
- MainPot int
- MainPotMtx sync.RWMutex
+ MainPot atomic.Int32
}
type PokerStandingPlayer struct {
@@ -304,7 +303,7 @@ func NewOngoing(g *PokerGame) *Ongoing {
}
g.PlayersMtx.RUnlock()
- return &Ongoing{Deck: deck, Players: players, WaitTurnEvent: PokerWaitTurnEvent{Idx: -1}}
+ return &Ongoing{Deck: deck, Players: players, WaitTurnEvent: PokerWaitTurnEvent{Idx: -1}, MainPot: atomic.Int32{}}
}
func newLogEvent(g *PokerGame, roomLogsTopic, msg string) {
@@ -519,8 +518,9 @@ OUTER:
time.Sleep(time.Second)
// Calculate what is the max gain all-in players can make
+ mainPot := int(g.Ongoing.MainPot.Load())
for _, p := range newlyAllInPlayers {
- maxGain := g.Ongoing.MainPot
+ maxGain := mainPot
for _, op := range g.Ongoing.Players {
maxGain += utils.MinInt(op.Bet, p.Bet)
}
@@ -529,16 +529,12 @@ OUTER:
// Transfer players bets into the main pot
for _, p := range g.Ongoing.Players {
- g.Ongoing.MainPotMtx.Lock()
- g.Ongoing.MainPot += p.Bet
- g.Ongoing.MainPotMtx.Unlock()
+ mainPot += p.Bet
p.Bet = 0
}
- g.Ongoing.MainPotMtx.RLock()
- mainPot := g.Ongoing.MainPot
- g.Ongoing.MainPotMtx.RUnlock()
PokerPubSub.Pub(roomTopic, PokerMainPotUpdatedEvent{MainPot: mainPot})
+ g.Ongoing.MainPot.Store(int32(mainPot))
return playerAlive == 1
}
@@ -737,9 +733,7 @@ END:
}
}
- g.Ongoing.MainPotMtx.RLock()
- mainPot := g.Ongoing.MainPot
- g.Ongoing.MainPotMtx.RUnlock()
+ mainPot := int(g.Ongoing.MainPot.Load())
playersGain := processPot(winners, len(g.Ongoing.Players), mainPot)
winnersStr := ""
@@ -757,9 +751,7 @@ END:
el.Player.Cash += el.Gain
}
- g.Ongoing.MainPotMtx.Lock()
- g.Ongoing.MainPot = 0
- g.Ongoing.MainPotMtx.Unlock()
+ g.Ongoing.MainPot.Store(0)
// Sync "ongoing players" with "room players" objects
g.PlayersMtx.RLock()
@@ -1146,9 +1138,7 @@ func buildMainPotHtml(g *PokerGame) string {
html := `<div id="mainPot"></div>`
mainPot := 0
if g.Ongoing != nil {
- g.Ongoing.MainPotMtx.RLock()
- mainPot = g.Ongoing.MainPot
- g.Ongoing.MainPotMtx.RUnlock()
+ mainPot = int(g.Ongoing.MainPot.Load())
}
html += `<style>#mainPot:before { content: "Pot: ` + itoa(mainPot) + `"; }</style>`
return html