dkforest

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

commit 91a98c2aa197f9c3387ee7c604f0277127bb5747
parent c6279b39532f043f06170b253a4b0e80875a202b
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 21 Dec 2023 13:25:19 -0500

cleanup

Diffstat:
Mpkg/utils/rwmtx/rwmtx.go | 8++++++++
Mpkg/web/handlers/poker/poker.go | 30+++++++++++++++++-------------
2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/pkg/utils/rwmtx/rwmtx.go b/pkg/utils/rwmtx/rwmtx.go @@ -69,6 +69,14 @@ func (m *RWMtx[T]) Set(v T) { m.v = v } +func (m *RWMtx[T]) Replace(newVal T) (old T) { + m.With(func(v *T) { + old = *v + *v = newVal + }) + return +} + func (m *RWMtx[T]) RWith(clb func(v T)) { _ = m.RWithE(func(tx T) error { clb(tx) 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 rwmtx.RWMtx[database.PokerChip] + cash rwMtxPokerChip lastActionTS time.Time } @@ -178,9 +178,17 @@ 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 rwmtx.RWMtx[database.PokerChip] + bet rwMtxPokerChip cards rwmtx.RWMtx[[]playerCard] folded atomic.Bool unsit atomic.Bool @@ -205,20 +213,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.With(func(v *database.PokerChip) { *v -= diff }) - p.cash.With(func(cash *database.PokerChip) { *cash += 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.With(func(v *database.PokerChip) { *v += bet }) - p.cash.With(func(cash *database.PokerChip) { *cash -= 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.With(func(cash *database.PokerChip) { *cash += gain }) + p.cash.incr(gain) p.bet.Set(0) } @@ -226,11 +234,7 @@ func (p *pokerPlayer) gain(db *database.DkfDB, pokerTableID int64, gain database 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) { - old = *bet - *bet = 0 - }) - return + return p.bet.Replace(0) } func (p *pokerPlayer) refundBet(db *database.DkfDB, pokerTableID int64) { @@ -511,7 +515,7 @@ 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: rwmtx.New(tableAccount.Amount), lastActionTS: time.Now()} + (*gPlayers)[pos] = &seatedPlayer{seatIdx: pos, userID: userID, username: username, cash: rwMtxPokerChip{rwmtx.New(tableAccount.Amount)}, lastActionTS: time.Now()} PokerPubSub.Pub(g.roomID.Topic(), PokerSeatTakenEvent{}) g.newLogEvent(fmt.Sprintf("%s sit", username.String()))