dkforest

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

commit 8fb77693c13c433159c6b6612803f8980e1e6a18
parent b3c176a338e03c2200d563810f56a4a42d90a6b4
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue, 19 Dec 2023 01:31:51 -0500

cleanup

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

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -104,13 +104,13 @@ func (e PlayerEvent) getAction() PlayerAction { var PokerInstance = NewPoker() type Ongoing struct { - Deck []string - Players []*PokerPlayer - Events utils.RWMtx[[]PokerEvent] + deck []string + players []*PokerPlayer + events utils.RWMtx[[]PokerEvent] LogEvents utils.RWMtx[[]LogEvent] - WaitTurnEvent utils.RWMtx[PokerWaitTurnEvent] - AutoActionEvent utils.RWMtx[AutoActionEvent] - CreatedAt time.Time + waitTurnEvent utils.RWMtx[PokerWaitTurnEvent] + autoActionEvent utils.RWMtx[AutoActionEvent] + createdAt time.Time mainPot atomic.Uint64 communityCards []string } @@ -254,7 +254,7 @@ func (g *PokerGame) sendPlayerEvent(evt PlayerEvent) { } func (g *Ongoing) isHeadsUpGame() bool { - return len(g.Players) == 2 // https://en.wikipedia.org/wiki/Heads-up_poker + return len(g.players) == 2 // https://en.wikipedia.org/wiki/Heads-up_poker } func (g *Ongoing) getMainPot() (out database.PokerChip) { @@ -268,7 +268,7 @@ func (g *Ongoing) setMainPot(v database.PokerChip) { func (g *Ongoing) computeWinners() (winner []gameResult) { countAlive := 0 var lastAlive *PokerPlayer - for _, p := range g.Players { + for _, p := range g.players { if !p.Folded.Load() { countAlive++ lastAlive = p @@ -281,7 +281,7 @@ func (g *Ongoing) computeWinners() (winner []gameResult) { } m := make(map[int32][]*PokerPlayer) - for _, p := range g.Players { + for _, p := range g.players { if p.Folded.Load() { continue } @@ -335,13 +335,13 @@ func sortGameResults(arr []gameResult) { } func (g *Ongoing) addEvent(evts ...PokerEvent) { - g.Events.With(func(events *[]PokerEvent) { + g.events.With(func(events *[]PokerEvent) { *events = append(*events, evts...) }) } func (g *Ongoing) getDeckStr() string { - return strings.Join(g.Deck, "") + return strings.Join(g.deck, "") } func (g *Ongoing) GetDeckHash() string { @@ -352,7 +352,7 @@ func (g *Ongoing) GetDeckHash() string { // [nil p1 nil nil p2 nil] -> PokerGame.Players // [p1 p2] -> Ongoing.Players func (g *Ongoing) getPlayerBySeatIdx(seatIdx int) (*PokerPlayer, int) { - for idx, p := range g.Players { + for idx, p := range g.players { if p.seatIdx == seatIdx { return p, idx } @@ -361,7 +361,7 @@ func (g *Ongoing) getPlayerBySeatIdx(seatIdx int) (*PokerPlayer, int) { } func (g *Ongoing) countCanBetPlayers() (nbCanBet int) { - for _, p := range g.Players { + for _, p := range g.players { if p.canBet() { nbCanBet++ } @@ -370,7 +370,7 @@ func (g *Ongoing) countCanBetPlayers() (nbCanBet int) { } func (g *Ongoing) countAlivePlayers() (playerAlive int) { - for _, p := range g.Players { + for _, p := range g.players { if !p.Folded.Load() { playerAlive++ } @@ -379,7 +379,7 @@ func (g *Ongoing) countAlivePlayers() (playerAlive int) { } func (g *Ongoing) getPlayer(userID database.UserID) *PokerPlayer { - for _, p := range g.Players { + for _, p := range g.players { if p.userID == userID { return p } @@ -436,7 +436,7 @@ func isRoundSettled(players []*PokerPlayer) bool { func (g *PokerGame) incrDealerIdx() (smallBlindIdx, bigBlindIdx int) { ongoing := g.Ongoing - nbPlayers := len(ongoing.Players) + nbPlayers := len(ongoing.players) dealerSeatIdx := g.dealerSeatIdx.Load() var dealerPlayer *PokerPlayer var dealerIdx int @@ -547,7 +547,7 @@ func newOngoing(g *PokerGame) *Ongoing { } } }) - return &Ongoing{Deck: generateDeck(), Players: players, WaitTurnEvent: utils.NewRWMtx(PokerWaitTurnEvent{Idx: -1}), CreatedAt: time.Now()} + return &Ongoing{deck: generateDeck(), players: players, waitTurnEvent: utils.NewRWMtx(PokerWaitTurnEvent{Idx: -1}), createdAt: time.Now()} } func (g *PokerGame) newLogEvent(msg string) { @@ -564,7 +564,7 @@ func (g *PokerGame) newLogEvent(msg string) { func showCards(g *PokerGame, seats []Seat) { ongoing := g.Ongoing roomTopic := g.roomID.Topic() - for _, p := range ongoing.Players { + for _, p := range ongoing.players { if !p.Folded.Load() { var firstCard, secondCard playerCard p.Cards.RWith(func(pCards *[]playerCard) { @@ -591,13 +591,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.With(func(v *PokerWaitTurnEvent) { *v = 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.With(func(v *AutoActionEvent) { *v = evt }) } type PlayerAction int @@ -849,7 +849,7 @@ func execBettingRound(g *PokerGame, skip int, minBet database.PokerChip) bool { roomID := g.roomID roomTopic := roomID.Topic() _, dealerIdx := ongoing.getPlayerBySeatIdx(int(g.dealerSeatIdx.Load())) - playerToPlayIdx := (dealerIdx + skip) % len(ongoing.Players) + playerToPlayIdx := (dealerIdx + skip) % len(ongoing.players) lastRaisePlayerIdx := -1 newlyAllInPlayers := make([]*PokerPlayer, 0) autoCache := make(map[database.UserID]AutoAction) @@ -868,8 +868,8 @@ RoundIsSettledLoop: for { // Repeat until the round is settled (all players have equals bet or fold or all-in) AllPlayersLoop: for { // Repeat until all players have played - playerToPlayIdx = (playerToPlayIdx + 1) % len(ongoing.Players) - p := ongoing.Players[playerToPlayIdx] + playerToPlayIdx = (playerToPlayIdx + 1) % len(ongoing.players) + p := ongoing.players[playerToPlayIdx] p.countChancesToAction++ pUserID := p.userID roomUserTopic := roomID.UserTopic(pUserID) @@ -932,7 +932,7 @@ RoundIsSettledLoop: } // End of repeat until we get an event from the player we're interested in } // End of repeat until all players have played // All settle when all players have the same bet amount - if isRoundSettled(ongoing.Players) { + if isRoundSettled(ongoing.players) { break RoundIsSettledLoop } } // End of repeat until the round is settled (all players have equals bet or fold or all-in) @@ -967,15 +967,15 @@ RoundIsSettled: // Reset all players bets, and return the sum of it func resetPlayersBet(ongoing *Ongoing) (sum database.PokerChip) { - for _, p := range ongoing.Players { + for _, p := range ongoing.players { sum += p.resetBet() } return } func refundUncalledBet(db *database.DkfDB, ongoing *Ongoing, pokerTableID int64, roomTopic string) { - newArray := make([]*PokerPlayer, len(ongoing.Players)) - copy(newArray, ongoing.Players) + newArray := make([]*PokerPlayer, len(ongoing.players)) + copy(newArray, ongoing.players) sort.Slice(newArray, func(i, j int) bool { return newArray[i].GetBet() > newArray[j].GetBet() }) firstPlayer := newArray[0] secondPlayer := newArray[1] @@ -1022,7 +1022,7 @@ func burnCard(g *PokerGame, idx, burnIdx *int) { func dealCard(g *PokerGame, idx *int, dealCardIdx int) { ongoing := g.Ongoing - card := ongoing.Deck[*idx] + card := ongoing.deck[*idx] *idx++ evt := PokerEvent{ ID: "card" + itoa(*idx), @@ -1043,7 +1043,7 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { roomTopic := roomID.Topic() var card string for cardIdx := 1; cardIdx <= NbCardsPerPlayer; cardIdx++ { - for _, p := range ongoing.Players { + for _, p := range ongoing.players { pUserID := p.userID if !p.canBet() { continue @@ -1051,7 +1051,7 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { roomUserTopic := roomID.UserTopic(pUserID) seatData := seats[p.seatIdx] time.Sleep(time.Second) - card = ongoing.Deck[*idx] + card = ongoing.deck[*idx] *idx++ left := seatData.Left top := seatData.Top @@ -1090,7 +1090,7 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { func computeAllInMaxGain(ongoing *Ongoing, newlyAllInPlayers []*PokerPlayer, mainPot database.PokerChip) { for _, p := range newlyAllInPlayers { maxGain := mainPot - for _, op := range ongoing.Players { + for _, op := range ongoing.players { maxGain += utils.MinInt(op.GetBet(), p.GetBet()) } p.AllInMaxGain = maxGain @@ -1190,7 +1190,7 @@ END: winners := ongoing.computeWinners() mainPot := ongoing.getMainPot() - playersGain, rake := processPot(winners, mainPot, bigBlindBet, collectRake, len(ongoing.Players)) + playersGain, rake := processPot(winners, mainPot, bigBlindBet, collectRake, len(ongoing.players)) winnersStr, winnerHand := applyGains(g, playersGain, mainPot, rake) ongoing.setMainPot(0) @@ -1216,7 +1216,7 @@ func applyBigBlindBet(g *PokerGame, bigBlindBet database.PokerChip, bbIdx int) { } func applyBlindBet(g *PokerGame, playerIdx int, bet database.PokerChip, name string) { - p := g.Ongoing.Players[playerIdx] + p := g.Ongoing.players[playerIdx] p.doBetAndNotif(g.db, g.pokerTableID, bet, g.roomID.Topic()) g.newLogEvent(fmt.Sprintf("%s %s %d", p.username, name, bet)) } @@ -1245,7 +1245,7 @@ func playerShouldBeBooted(p *seatedPlayer, ongoing *Ongoing, pokerTableMinBet da if !pIsEligible { return true } - if p.lastActionTS.Before(ongoing.CreatedAt) { + if p.lastActionTS.Before(ongoing.createdAt) { // If the player was playing the game, must be booted if he had the chance to make actions and did not. // If the player was not playing the game, must be booted if he's not eligible to play the next one. op := ongoing.getPlayer(p.userID) @@ -1280,13 +1280,13 @@ func applyGains(g *PokerGame, playersGain []PlayerGain, mainPot, rake database.P winnersStr += el.Player.username.String() + " " el.Player.gain(tx, pokerTableID, el.Gain) } - for _, op := range ongoing.Players { + for _, op := range ongoing.players { op.gain(tx, pokerTableID, 0) } } else if nbPlayersGain == 0 { // No winners, refund bets - for _, op := range ongoing.Players { + for _, op := range ongoing.players { op.refundBet(tx, pokerTableID) } } @@ -1588,14 +1588,14 @@ func BuildBaseHtml(g *PokerGame, authUser *database.User) (html string) { if ongoing != nil { - ongoing.WaitTurnEvent.RWith(func(v *PokerWaitTurnEvent) { + ongoing.waitTurnEvent.RWith(func(v *PokerWaitTurnEvent) { html += drawCountDownStyle(*v) }) - ongoing.AutoActionEvent.RWith(func(v *AutoActionEvent) { + ongoing.autoActionEvent.RWith(func(v *AutoActionEvent) { html += drawAutoActionMsgEvent(*v) }) - ongoing.Events.RWith(func(v *[]PokerEvent) { + ongoing.events.RWith(func(v *[]PokerEvent) { for _, evt := range *v { if evt.UserID == 0 { html += getPokerEventHtml(evt, "0s")