commit 07f98d1a2316cae0f1854b0a7773b59d2fcf18b4
parent 5e6cdda72bd09a8c4baa80f91aac34c6e5f0c3a0
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 21 Dec 2023 11:52:43 -0500
cleanup
Diffstat:
2 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/pkg/utils/rwmtx/rwmtx.go b/pkg/utils/rwmtx/rwmtx.go
@@ -13,6 +13,10 @@ func NewMtx[T any](v T) Mtx[T] {
return Mtx[T]{v: v}
}
+func (m *Mtx[T]) Val() *T {
+ return &m.v
+}
+
func (m *Mtx[T]) Get() T {
m.Lock()
defer m.Unlock()
@@ -38,10 +42,6 @@ func (m *Mtx[T]) WithE(clb func(v *T) error) error {
return clb(&m.v)
}
-func (m *Mtx[T]) Val() *T {
- return &m.v
-}
-
//----------------------
type RWMtx[T any] struct {
@@ -53,12 +53,22 @@ func New[T any](v T) RWMtx[T] {
return RWMtx[T]{v: v}
}
+func (m *RWMtx[T]) Val() *T {
+ return &m.v
+}
+
func (m *RWMtx[T]) Get() T {
m.RLock()
defer m.RUnlock()
return m.v
}
+func (m *RWMtx[T]) Set(v T) {
+ m.Lock()
+ defer m.Unlock()
+ m.v = v
+}
+
func (m *RWMtx[T]) RWith(clb func(v *T)) {
_ = m.RWithE(func(tx *T) error {
clb(tx)
@@ -72,12 +82,6 @@ func (m *RWMtx[T]) RWithE(clb func(v *T) error) error {
return clb(&m.v)
}
-func (m *RWMtx[T]) Set(v T) {
- m.Lock()
- defer m.Unlock()
- m.v = v
-}
-
func (m *RWMtx[T]) With(clb func(v *T)) {
_ = m.WithE(func(tx *T) error {
clb(tx)
@@ -90,7 +94,3 @@ func (m *RWMtx[T]) WithE(clb func(v *T) error) error {
defer m.Unlock()
return clb(&m.v)
}
-
-func (m *RWMtx[T]) Val() *T {
- return &m.v
-}
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -148,8 +148,7 @@ type seatedPlayer struct {
}
func (p *seatedPlayer) getCash() (out database.PokerChip) {
- p.cash.RWith(func(v *database.PokerChip) { out = *v })
- return
+ return p.cash.Get()
}
// Return either or not a player is eligible to play a game
@@ -169,8 +168,7 @@ type pokerPlayer struct {
}
func (p *pokerPlayer) getBet() (out database.PokerChip) {
- p.bet.RWith(func(v *database.PokerChip) { out = *v })
- return
+ return p.bet.Get()
}
func (p *pokerPlayer) canBet() bool {
@@ -199,7 +197,7 @@ func (p *pokerPlayer) doBet(db *database.DkfDB, pokerTableID int64, bet database
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.Set(0)
}
// Reset player's bet to 0 and return the value it had before the reset
@@ -620,13 +618,13 @@ func showCards(g *PokerGame, seats []Seat) {
func setWaitTurn(g *PokerGame, seatIdx int) {
evt := PokerWaitTurnEvent{Idx: seatIdx, CreatedAt: time.Now()}
PokerPubSub.Pub(g.roomID.Topic(), evt)
- g.ongoing.waitTurnEvent.With(func(v *PokerWaitTurnEvent) { *v = evt })
+ g.ongoing.waitTurnEvent.Set(evt)
}
func setAutoAction(g *PokerGame, roomUserTopic, msg string) {
evt := AutoActionEvent{Message: msg}
PokerPubSub.Pub(roomUserTopic, evt)
- g.ongoing.autoActionEvent.With(func(v *AutoActionEvent) { *v = evt })
+ g.ongoing.autoActionEvent.Set(evt)
}
type PlayerAction int