dkforest

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

commit d0e4e40fb2bfdb986324cabc67af452840c1ea41
parent 3d9ad948af16ca1c4ecd4cc20d1b46c2f6e40fe1
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed,  6 Dec 2023 23:25:12 -0500

cleanup

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

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -27,6 +27,8 @@ const BurnStackY = 30 const DealerStackX = 250 const DealerStackY = 30 const NbCardsPerPlayer = 2 +const SmallBlindBet = 10 +const BigBlindBet = 20 type Poker struct { sync.Mutex @@ -97,7 +99,7 @@ type PokerPlayer struct { Username string Bet int Cash int - SeatIdx int + SeatIdx int // 0 indexed Cards []PlayerCard CardsMtx sync.RWMutex Folded bool @@ -137,9 +139,14 @@ func (g *Ongoing) GetDeckHash() string { return utils.MD5([]byte(g.getDeckStr())) } +func (g *Ongoing) GetNextPlayer() *PokerPlayer { + + return nil +} + func (g *Ongoing) GetPlayer(player string) *PokerPlayer { for _, p := range g.Players { - if p != nil && p.Username == player { + if p.Username == player { return p } } @@ -150,20 +157,18 @@ func isRoundSettled(players []*PokerPlayer) bool { allSettled := true b := -1 for _, p := range players { - if p != nil { - if p.Folded { - continue - } - if p.Cash == 0 { // all in - continue - } - if b == -1 { - b = p.Bet - } else { - if p.Bet != b { - allSettled = false - break - } + if p.Folded { + continue + } + if p.Cash == 0 { // all in + continue + } + if b == -1 { + b = p.Bet + } else { + if p.Bet != b { + allSettled = false + break } } } @@ -175,8 +180,8 @@ func (g *PokerGame) incrDealerIdx() { dealerIdx := g.DealerIdx g.DealerIdxMtx.RUnlock() var found bool - for i := dealerIdx + 1; i < len(g.Players); i++ { - if g.Players[i] != nil { + for i := dealerIdx + 1; i < len(g.Ongoing.Players); i++ { + if g.Ongoing.Players[i] != nil { dealerIdx = i found = true break @@ -184,7 +189,7 @@ func (g *PokerGame) incrDealerIdx() { } if !found { for i := 0; i < dealerIdx; i++ { - if g.Players[i] != nil { + if g.Ongoing.Players[i] != nil { dealerIdx = i found = true break @@ -213,13 +218,11 @@ func NewOngoing(g *PokerGame) *Ongoing { } utils.Shuffle(deck) - players := make([]*PokerPlayer, NbPlayers) + players := make([]*PokerPlayer, 0) for idx, p := range g.Players { - var player *PokerPlayer if p != nil && p.Cash > 0 { - player = &PokerPlayer{Username: p.Username, Cash: p.Cash, SeatIdx: idx} + players = append(players, &PokerPlayer{Username: p.Username, Cash: p.Cash, SeatIdx: idx}) } - players[idx] = player } return &Ongoing{Deck: deck, Players: players, WaitTurnEvent: PokerWaitTurnEvent{Idx: -1}} @@ -257,7 +260,7 @@ func waitPlayersActionFn(g *PokerGame, roomID string) bool { playerAlive := 0 for _, p := range g.Ongoing.Players { - if p != nil && !p.Folded { + if !p.Folded { playerAlive++ } } @@ -267,9 +270,6 @@ OUTER: for { // Loop until the round is settled players := reorderPlayers(g.Ongoing.Players, dealerIdx) for _, p := range players { - if p == nil { - continue - } if p.SeatIdx == lastRisePlayerIdx { break OUTER } @@ -408,13 +408,11 @@ OUTER: time.Sleep(time.Second) // Transfer players bets into the main pot - for i := range g.Ongoing.Players { - if g.Ongoing.Players[i] != nil { - g.Ongoing.MainPotMtx.Lock() - g.Ongoing.MainPot += g.Ongoing.Players[i].Bet - g.Ongoing.MainPotMtx.Unlock() - g.Ongoing.Players[i].Bet = 0 - } + for _, p := range g.Ongoing.Players { + g.Ongoing.MainPotMtx.Lock() + g.Ongoing.MainPot += p.Bet + g.Ongoing.MainPotMtx.Unlock() + p.Bet = 0 } g.Ongoing.MainPotMtx.RLock() @@ -490,14 +488,18 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { PokerPubSub.Pub(roomTopic, GameStartedEvent{DeckHash: deckHash}) // TODO: implement small/big blinds + //p := g.Ongoing.GetNextPlayer() + // Small blind + // Big blind + // Next player starts // Deal cards for cardIdx := 1; cardIdx <= NbCardsPerPlayer; cardIdx++ { - for i, p := range g.Ongoing.Players { - if p == nil || p.Cash == 0 { + for _, p := range g.Ongoing.Players { + if p.Cash == 0 { continue } - seatData := seats[i] + seatData := seats[p.SeatIdx] time.Sleep(time.Second) card = g.Ongoing.Deck[idx] idx++ @@ -521,9 +523,9 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { PokerPubSub.Pub(roomTopic, evt) PokerPubSub.Pub(roomTopic+"_"+p.Username, YourCardEvent{Idx: cardIdx, Name: card}) - g.Ongoing.Players[i].CardsMtx.Lock() - g.Ongoing.Players[i].Cards = append(g.Ongoing.Players[i].Cards, PlayerCard{Idx: idx, Name: card}) - g.Ongoing.Players[i].CardsMtx.Unlock() + p.CardsMtx.Lock() + p.Cards = append(p.Cards, PlayerCard{Idx: idx, Name: card}) + p.CardsMtx.Unlock() g.Ongoing.AddEvent(evt) } @@ -580,11 +582,11 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { } // Show cards - for idx, p := range g.Ongoing.Players { + for _, p := range g.Ongoing.Players { if p != nil && !p.Folded { p.CardsMtx.RLock() - evt1 := PokerEvent{ID: "card" + itoa(p.Cards[0].Idx), Name: p.Cards[0].Name, Idx: p.Cards[0].Idx, Top: seats[idx].Top, Left: seats[idx].Left, Reveal: true} - evt2 := PokerEvent{ID: "card" + itoa(p.Cards[1].Idx), Name: p.Cards[1].Name, Idx: p.Cards[1].Idx, Top: seats[idx].Top, Left: seats[idx].Left + 53, Reveal: true} + evt1 := PokerEvent{ID: "card" + itoa(p.Cards[0].Idx), Name: p.Cards[0].Name, Idx: p.Cards[0].Idx, Top: seats[p.SeatIdx].Top, Left: seats[p.SeatIdx].Left, Reveal: true} + evt2 := PokerEvent{ID: "card" + itoa(p.Cards[1].Idx), Name: p.Cards[1].Name, Idx: p.Cards[1].Idx, Top: seats[p.SeatIdx].Top, Left: seats[p.SeatIdx].Left + 53, Reveal: true} p.CardsMtx.RUnlock() PokerPubSub.Pub(roomTopic, evt1) PokerPubSub.Pub(roomTopic, evt2) @@ -644,10 +646,11 @@ END: // Sync "ongoing players" with "room players" objects for idx := range g.Players { - if g.Ongoing.Players[idx] != nil && g.Players[idx] != nil { - g.Players[idx].Cash = g.Ongoing.Players[idx].Cash + if g.Players[idx] != nil { + op := g.Ongoing.GetPlayer(g.Players[idx].Username) + g.Players[idx].Cash = op.Cash // TODO: need to figure out how to prevent double spend - if user, err := db.GetUserByUsername(database.Username(g.Ongoing.Players[idx].Username)); err == nil { + if user, err := db.GetUserByUsername(database.Username(op.Username)); err == nil { user.ChipsTest = g.Players[idx].Cash user.DoSave(db) }