commit b82017701bcc1d7884619aa01dd0272977891075
parent 9573bf8ba659e0a5079281bc2a5cef9b6f4fbe97
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 6 Dec 2023 05:33:43 -0500
prevent data race
Diffstat:
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -14,6 +14,7 @@ import (
"strconv"
"strings"
"sync"
+ "sync/atomic"
"time"
)
@@ -111,8 +112,8 @@ type PokerGame struct {
Players []*PokerStandingPlayer
Ongoing *Ongoing
DealerIdx int
- IsGameDone bool
- IsGameOver bool
+ IsGameDone atomic.Bool
+ IsGameOver atomic.Bool
}
func (g *Ongoing) AddEvent(evts ...PokerEvent) {
@@ -593,12 +594,12 @@ END:
}
}
- g.IsGameDone = true
+ g.IsGameDone.Store(true)
PokerPubSub.Pub(roomTopic, GameIsDoneEvent{DeckStr: strings.Join(g.Ongoing.Deck, ""), Winner: winner.Username, WinnerHand: winnerHand})
// Wait a minimum of X seconds before allowing a new game
time.Sleep(MinTimeAfterGame * time.Second)
- g.IsGameOver = true
+ g.IsGameOver.Store(true)
fmt.Println("GAME IS OVER")
}
@@ -611,11 +612,11 @@ func (g *PokerGame) Deal(roomID string) {
roomTopic := "room_" + roomID
if g.Ongoing != nil {
- if !g.IsGameOver {
+ if !g.IsGameOver.Load() {
fmt.Println("game already ongoing")
return
} else {
- g.IsGameOver = false
+ g.IsGameOver.Store(false)
PokerPubSub.Pub(roomTopic, ResetCardsEvent{})
time.Sleep(time.Second)
}
@@ -1324,7 +1325,7 @@ func PokerHandler(c echo.Context) error {
send(drawCountDownStyle(g.Ongoing.WaitTurnEvent))
g.Ongoing.WaitTurnEventMtx.RUnlock()
send(`<style>#deckHash:before { content: "` + g.Ongoing.GetDeckHash() + `"; }</style>`)
- if g.IsGameDone {
+ if g.IsGameDone.Load() {
send(`<style>#deckStr:before { content: "` + g.Ongoing.GetDeckStr() + `"; }</style>`)
}