commit 4399cf04580b544fd3a77413573ca4d3ad71dac2
parent fd77bdbe8950385fe396fcf7ca335f157452c299
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 21 Dec 2023 13:58:33 -0500
cleanup
Diffstat:
2 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/pkg/utils/rwmtx/rwmtx.go b/pkg/utils/rwmtx/rwmtx.go
@@ -120,3 +120,13 @@ func (s *RWMtxSlice[T]) Clone() (out []T) {
})
return
}
+
+//----------------------
+
+type RWMtxUInt64[T ~uint64] struct {
+ RWMtx[T]
+}
+
+func (s *RWMtxUInt64[T]) Incr(diff T) {
+ s.With(func(v *T) { *v += diff })
+}
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -165,7 +165,7 @@ type seatedPlayer struct {
seatIdx int
userID database.UserID
username database.Username
- cash rwMtxPokerChip
+ cash rwmtx.RWMtxUInt64[database.PokerChip]
lastActionTS time.Time
}
@@ -178,17 +178,9 @@ func (p *seatedPlayer) isEligible(pokerTableMinBet database.PokerChip) bool {
return p != nil && p.getCash() >= pokerTableMinBet
}
-type rwMtxPokerChip struct {
- rwmtx.RWMtx[database.PokerChip]
-}
-
-func (t *rwMtxPokerChip) incr(diff database.PokerChip) {
- t.With(func(v *database.PokerChip) { *v += diff })
-}
-
type pokerPlayer struct {
*seatedPlayer
- bet rwMtxPokerChip
+ bet rwmtx.RWMtxUInt64[database.PokerChip]
cards rwmtx.RWMtx[[]playerCard]
folded atomic.Bool
unsit atomic.Bool
@@ -212,20 +204,20 @@ func (p *pokerPlayer) isAllIn() bool {
func (p *pokerPlayer) refundPartialBet(db *database.DkfDB, pokerTableID int64, diff database.PokerChip) {
_ = db.PokerTableAccountRefundPartialBet(p.userID, pokerTableID, diff)
p.gameBet -= diff
- p.bet.incr(-diff)
- p.cash.incr(diff)
+ p.bet.Incr(-diff)
+ p.cash.Incr(diff)
}
func (p *pokerPlayer) doBet(db *database.DkfDB, pokerTableID int64, bet database.PokerChip) {
_ = db.PokerTableAccountBet(p.userID, pokerTableID, bet)
p.gameBet += bet
- p.bet.incr(bet)
- p.cash.incr(-bet)
+ p.bet.Incr(bet)
+ p.cash.Incr(-bet)
}
func (p *pokerPlayer) gain(db *database.DkfDB, pokerTableID int64, gain database.PokerChip) {
_ = db.PokerTableAccountGain(p.userID, pokerTableID, gain)
- p.cash.incr(gain)
+ p.cash.Incr(gain)
p.bet.Set(0)
}
@@ -505,7 +497,13 @@ func (g *PokerGame) Sit(userID database.UserID, username database.Username, pos
if (*gPlayers)[pos] != nil {
return errors.New("seat already taken")
}
- (*gPlayers)[pos] = &seatedPlayer{seatIdx: pos, userID: userID, username: username, cash: rwMtxPokerChip{rwmtx.New(tableAccount.Amount)}, lastActionTS: time.Now()}
+ (*gPlayers)[pos] = &seatedPlayer{
+ seatIdx: pos,
+ userID: userID,
+ username: username,
+ cash: rwmtx.RWMtxUInt64[database.PokerChip]{rwmtx.New(tableAccount.Amount)},
+ lastActionTS: time.Now(),
+ }
PokerPubSub.Pub(g.roomID.Topic(), PokerSeatTakenEvent{})
g.newLogEvent(fmt.Sprintf("%s sit", username.String()))