dkforest

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

commit 93fa4cb9f96fa26e297b0ac630f4fa08a06c50b0
parent f36a6b8bf3dd69167c909515b0efbcfd301bcef8
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Fri,  8 Dec 2023 21:00:23 -0500

cleanup

Diffstat:
Mpkg/web/handlers/poker/poker.go | 66++++++++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 46 insertions(+), 20 deletions(-)

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -115,7 +115,7 @@ type PokerPlayer struct { SeatIdx int // 0 indexed Cards []PlayerCard CardsMtx sync.RWMutex - Folded bool + Folded atomic.Bool } func (p *PokerPlayer) isAllIn() bool { @@ -155,7 +155,7 @@ func (g *Ongoing) computeWinners() (winner []GameResult) { countAlive := 0 var lastAlive *PokerPlayer for _, p := range g.Players { - if !p.Folded { + if !p.Folded.Load() { countAlive++ lastAlive = p } @@ -166,7 +166,7 @@ func (g *Ongoing) computeWinners() (winner []GameResult) { m := make(map[int32][]*PokerPlayer) for _, p := range g.Players { - if p.Folded { + if p.Folded.Load() { continue } @@ -253,7 +253,7 @@ func isRoundSettled(players []*PokerPlayer) bool { } arr := make([]Tmp, 0) for _, p := range players { - arr = append(arr, Tmp{Bet: p.Bet, AllIn: p.isAllIn(), Folded: p.Folded}) + arr = append(arr, Tmp{Bet: p.Bet, AllIn: p.isAllIn(), Folded: p.Folded.Load()}) } sort.Slice(arr, func(i, j int) bool { return arr[i].Bet > arr[j].Bet }) b := arr[0].Bet @@ -282,12 +282,31 @@ func incrDealerIdx(dealerIdx int32, nbPlayers int) int32 { return (dealerIdx + 1) % int32(nbPlayers) } -func (g *PokerGame) UnSitPlayer(authUser *database.User) error { +func (g *PokerGame) UnSitPlayer(authUser *database.User, roomID string) error { + roomTopic := "room_" + roomID found := false g.PlayersMtx.Lock() for i, p := range g.Players { if p != nil && p.Username == authUser.Username.String() { g.Players[i] = nil + if g.Ongoing != nil { + if player := g.Ongoing.GetPlayer(p.Username); player != nil { + player.Folded.Store(true) + player.CardsMtx.RLock() + if len(player.Cards) >= 1 { + evt1 := PokerEvent{ID: "card" + itoa(player.Cards[0].Idx), Name: "", Idx: player.Cards[0].Idx, Top: BurnStackY, Left: BurnStackX, Reveal: false} + PokerPubSub.Pub(roomTopic, evt1) + g.Ongoing.AddEvent(evt1) + } + if len(player.Cards) >= 2 { + evt2 := PokerEvent{ID: "card" + itoa(player.Cards[1].Idx), Name: "", Idx: player.Cards[1].Idx, Top: BurnStackY, Left: BurnStackX, Reveal: false} + PokerPubSub.Pub(roomTopic, evt2) + g.Ongoing.AddEvent(evt2) + } + player.CardsMtx.RUnlock() + } + } + found = true break } @@ -347,7 +366,7 @@ func newLogEvent(g *PokerGame, roomLogsTopic, msg string) { func showCards(g *PokerGame, roomTopic string, seats []Seat) { for _, p := range g.Ongoing.Players { - if !p.Folded { + if !p.Folded.Load() { p.CardsMtx.RLock() firstCard := p.Cards[0] secondCard := p.Cards[1] @@ -365,33 +384,35 @@ func showCards(g *PokerGame, roomTopic string, seats []Seat) { // Return either or not the game ended because only 1 player left playing func waitPlayersActionFn(g *PokerGame, roomID string, skip, minBet int) bool { roomTopic := "room_" + roomID + playerAlive := 0 + dealerIdx := int(g.DealerIdx.Load()) + playerToPlayIdx := (dealerIdx + skip) % len(g.Ongoing.Players) + lastRaisePlayerIdx := -1 + newlyAllInPlayers := make([]*PokerPlayer, 0) // Avoid asking for actions if only 1 player can do so (because others are all-in) nbCanVote := 0 for _, p := range g.Ongoing.Players { - if !p.Folded && p.Cash > 0 { + if !p.Folded.Load() && p.Cash > 0 { nbCanVote++ } } - if nbCanVote <= 1 { + if nbCanVote == 0 { + // TODO: Refund bets return false + } else if nbCanVote == 1 { + playerAlive = 1 + goto END1 } - dealerIdx := int(g.DealerIdx.Load()) - playerToPlayIdx := (dealerIdx + skip) % len(g.Ongoing.Players) - lastRaisePlayerIdx := -1 - - playerAlive := 0 for _, p := range g.Ongoing.Players { - if !p.Folded { + if !p.Folded.Load() { playerAlive++ } } // TODO: implement maximum re-raise - newlyAllInPlayers := make([]*PokerPlayer, 0) - OUTER: for { for { @@ -404,7 +425,7 @@ OUTER: lastRaisePlayerIdx = playerToPlayIdx } player := g.Ongoing.GetPlayer(p.Username) - if player.Folded || player.isAllIn() { + if player.Folded.Load() || player.isAllIn() { continue } evt := PokerWaitTurnEvent{Idx: p.SeatIdx} @@ -431,7 +452,7 @@ OUTER: break LOOP } if p.Bet < minBet { - player.Folded = true + player.Folded.Store(true) player.CardsMtx.RLock() evt1 := PokerEvent{ID: "card" + itoa(player.Cards[0].Idx), Name: "", Idx: player.Cards[0].Idx, Top: BurnStackY, Left: BurnStackX, Reveal: false} @@ -458,7 +479,7 @@ OUTER: continue } if evt.Fold { - player.Folded = true + player.Folded.Store(true) evt1 := PokerEvent{ID: "card" + itoa(player.Cards[0].Idx), Name: "", Idx: player.Cards[0].Idx, Top: BurnStackY, Left: BurnStackX, Reveal: false} evt2 := PokerEvent{ID: "card" + itoa(player.Cards[1].Idx), Name: "", Idx: player.Cards[1].Idx, Top: BurnStackY, Left: BurnStackX, Reveal: false} @@ -545,6 +566,8 @@ OUTER: } } +END1: + evt := PokerWaitTurnEvent{Idx: -1} PokerPubSub.Pub(roomTopic, evt) @@ -664,6 +687,9 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { } seatData := seats[p.SeatIdx] time.Sleep(time.Second) + if p.Folded.Load() { + continue + } card = g.Ongoing.Deck[idx] idx++ left := seatData.Left @@ -1049,7 +1075,7 @@ func PokerUnSitHandler(c echo.Context) error { return c.NoContent(http.StatusNotFound) } if c.Request().Method == http.MethodPost { - if err := g.UnSitPlayer(authUser); err == nil { + if err := g.UnSitPlayer(authUser, roomID); err == nil { myTopic := "room_" + roomID PokerPubSub.Pub(myTopic, PokerSeatLeftEvent{}) newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s un-sit", authUser.Username.String()))