dkforest

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

commit e56803ef31994bf7966b54324e3217174a904dfa
parent 0329b47367b651edf2b2416ebadba8ccab84af8a
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 11 Dec 2023 20:45:42 -0500

auto unsit inactive players

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

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -20,7 +20,7 @@ import ( ) const NbPlayers = 6 -const MaxUserCountdown = 60 +const MaxUserCountdown = 3 const MinTimeAfterGame = 10 const BackfacingDeg = "-180deg" const BurnStackX = 400 @@ -90,11 +90,13 @@ type Ongoing struct { WaitTurnEvent PokerWaitTurnEvent WaitTurnEventMtx sync.RWMutex MainPot atomic.Int32 + CreatedAt time.Time } type PokerStandingPlayer struct { - Username string - Cash int + Username string + Cash int + LastActionTS time.Time } func (p *PokerStandingPlayer) getDisplayCash(g *PokerGame) int { @@ -298,43 +300,60 @@ func (g *PokerGame) incrDealerIdx() { g.bigBlindIdx = (int(dealerIdx) + 2) % nbPlayers } -func (g *PokerGame) UnSitPlayer(db *database.DkfDB, authUser *database.User, roomID string) error { +func (g *PokerGame) UnSitPlayer(db *database.DkfDB, roomID, playerUsername string) error { + var player *PokerStandingPlayer + g.PlayersMtx.RLock() + for _, p := range g.Players { + if p != nil && p.Username == playerUsername { + player = p + break + } + } + g.PlayersMtx.RUnlock() + if player != nil { + return g.UnSitPlayer1(db, roomID, player) + } + return nil +} + +func (g *PokerGame) UnSitPlayer1(db *database.DkfDB, roomID string, player *PokerStandingPlayer) error { roomTopic := "room_" + roomID - found := false - g.PlayersMtx.Lock() - for i, p := range g.Players { - if p != nil && p.Username == authUser.Username.String() { - authUser.ChipsTest += p.Cash - authUser.DoSave(db) + user, err := db.GetUserByUsername(database.Username(player.Username)) + if err != nil { + return err + } - 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, Angle: "0deg", 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, Angle: "0deg", Reveal: false} - PokerPubSub.Pub(roomTopic, evt2) - g.Ongoing.AddEvent(evt2) - } - player.CardsMtx.RUnlock() - } + user.ChipsTest += player.Cash + user.DoSave(db) + + if g.Ongoing != nil { + if player := g.Ongoing.GetPlayer(player.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, Angle: "0deg", 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, Angle: "0deg", Reveal: false} + PokerPubSub.Pub(roomTopic, evt2) + g.Ongoing.AddEvent(evt2) } + player.CardsMtx.RUnlock() + } + } - found = true + g.PlayersMtx.Lock() + for idx, p := range g.Players { + if p != nil && p.Username == player.Username { + g.Players[idx] = nil break } } g.PlayersMtx.Unlock() - if !found { - return errors.New("player seat not found") - } + return nil } @@ -352,7 +371,7 @@ func (g *PokerGame) SitPlayer(db *database.DkfDB, authUser *database.User, pos i chips := utils.MinInt(authUser.ChipsTest, 2000) authUser.ChipsTest -= chips authUser.DoSave(db) - g.Players[pos] = &PokerStandingPlayer{Username: authUser.Username.String(), Cash: chips} + g.Players[pos] = &PokerStandingPlayer{Username: authUser.Username.String(), Cash: chips, LastActionTS: time.Now()} return nil } @@ -374,7 +393,7 @@ func NewOngoing(g *PokerGame) *Ongoing { } g.PlayersMtx.RUnlock() - return &Ongoing{Deck: deck, Players: players, WaitTurnEvent: PokerWaitTurnEvent{Idx: -1}, MainPot: atomic.Int32{}} + return &Ongoing{Deck: deck, Players: players, WaitTurnEvent: PokerWaitTurnEvent{Idx: -1}, MainPot: atomic.Int32{}, CreatedAt: time.Now()} } func newLogEvent(g *PokerGame, roomLogsTopic, msg string) { @@ -515,6 +534,9 @@ OUTER: if evt.Player != p.Username { continue } + + player.LastActionTS = time.Now() + if evt.Fold { player.Folded.Store(true) @@ -845,6 +867,17 @@ END: // Wait a minimum of X seconds before allowing a new game time.Sleep(MinTimeAfterGame * time.Second) + + // Auto unsit inactive players + for _, p := range g.Players { + if p != nil && p.LastActionTS.Before(g.Ongoing.CreatedAt) { + if err := g.UnSitPlayer1(db, roomID, p); err == nil { + PokerPubSub.Pub(roomTopic, PokerSeatLeftEvent{}) + newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s auto un-sit", p.Username)) + } + } + } + g.IsGameStarted.Store(false) } @@ -1111,15 +1144,15 @@ func PokerUnSitHandler(c echo.Context) error { db := c.Get("database").(*database.DkfDB) authUser := c.Get("authUser").(*database.User) roomID := c.Param("roomID") + roomTopic := "room_" + roomID roomLogsTopic := "room_" + roomID + "_logs" g := PokerInstance.GetGame(roomID) if g == nil { return c.NoContent(http.StatusNotFound) } if c.Request().Method == http.MethodPost { - if err := g.UnSitPlayer(db, authUser, roomID); err == nil { - myTopic := "room_" + roomID - PokerPubSub.Pub(myTopic, PokerSeatLeftEvent{}) + if err := g.UnSitPlayer(db, roomID, authUser.Username.String()); err == nil { + PokerPubSub.Pub(roomTopic, PokerSeatLeftEvent{}) newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s un-sit", authUser.Username.String())) } }