dkforest

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

commit 1223243d514a6b07cafd1c7178ac953604d660e8
parent adfd3fcf7c1b91333b925ab2ae641e77f3146c17
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue, 19 Dec 2023 00:02:09 -0500

use userid instead of username

Diffstat:
Mpkg/web/handlers/poker/events.go | 2+-
Mpkg/web/handlers/poker/poker.go | 68++++++++++++++++++++++++++++++++++----------------------------------
2 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/pkg/web/handlers/poker/events.go b/pkg/web/handlers/poker/events.go @@ -13,7 +13,7 @@ type PokerEvent struct { Left int Reveal bool Angle string - Player database.Username + UserID database.UserID } type GameStartedEvent struct { diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -79,7 +79,7 @@ func (p *Poker) GetGame(roomID RoomID) *PokerGame { } type PlayerEvent struct { - Player database.Username + UserID database.UserID Call bool Check bool Fold bool @@ -451,7 +451,7 @@ func (g *PokerGame) UnSitPlayer1(gPlayers *[]*SeatedPlayer, seatedPlayer *Seated if ongoing != nil { if player := ongoing.getPlayer(seatedPlayerUserID); player != nil { select { - case g.PlayersEventCh <- PlayerEvent{Player: player.Username, Unsit: true}: + case g.PlayersEventCh <- PlayerEvent{UserID: player.UserID, Unsit: true}: default: } player.Folded.Store(true) @@ -628,7 +628,7 @@ func doFold(g *PokerGame, p *PokerPlayer, playerAlive *int) int { *playerAlive-- if *playerAlive == 1 { - PokerPubSub.Pub(g.RoomID.UserTopic(p.Username), ErrorMsgEvent{Message: ""}) + PokerPubSub.Pub(g.RoomID.UserTopic(p.UserID), ErrorMsgEvent{Message: ""}) return breakRoundIsSettledLoop } return doNothing @@ -637,7 +637,7 @@ func doFold(g *PokerGame, p *PokerPlayer, playerAlive *int) int { func doCheck(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip) int { if p.GetBet() < *minBet { msg := fmt.Sprintf("Need to bet %d", *minBet-p.GetBet()) - PokerPubSub.Pub(g.RoomID.UserTopic(p.Username), ErrorMsgEvent{Message: msg}) + PokerPubSub.Pub(g.RoomID.UserTopic(p.UserID), ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } g.newLogEvent(fmt.Sprintf("%s check", p.Username)) @@ -681,7 +681,7 @@ func doAllIn(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database. func doBet(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database.PokerChip, newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int, evt PlayerEvent) int { roomTopic := g.RoomID.Topic() - roomUserTopic := g.RoomID.UserTopic(p.Username) + roomUserTopic := g.RoomID.UserTopic(p.UserID) pokerTableMinBet := g.PokerTableMinBet bet := evt.Bet // Ensure the player cannot bet below the table minimum bet (amount of the big blind) @@ -714,20 +714,20 @@ func doBet(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database.Po return doNothing } -func handleAutoActionReceived(g *PokerGame, autoCache map[database.Username]AutoAction, evt PlayerEvent) int { - roomUserTopic := g.RoomID.UserTopic(evt.Player) - autoAction := autoCache[evt.Player] +func handleAutoActionReceived(g *PokerGame, autoCache map[database.UserID]AutoAction, evt PlayerEvent) int { + roomUserTopic := g.RoomID.UserTopic(evt.UserID) + autoAction := autoCache[evt.UserID] if evt.Fold && autoAction.action == FoldAction || evt.Call && autoAction.action == CallAction || evt.Check && autoAction.action == CheckAction { - delete(autoCache, evt.Player) + delete(autoCache, evt.UserID) setAutoAction(g, roomUserTopic, "") return continueGetPlayerEventLoop } action := evt.getAction() if action != NoAction { - autoCache[evt.Player] = AutoAction{action: action, evt: evt} + autoCache[evt.UserID] = AutoAction{action: action, evt: evt} setAutoAction(g, roomUserTopic, "Will auto "+action.String()) } return continueGetPlayerEventLoop @@ -736,10 +736,10 @@ func handleAutoActionReceived(g *PokerGame, autoCache map[database.Username]Auto func applyAutoAction(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database.PokerChip, newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, autoAction AutoAction, - autoCache map[database.Username]AutoAction) (actionResult int) { + autoCache map[database.UserID]AutoAction) (actionResult int) { - pUsername := p.Username - roomUserTopic := g.RoomID.UserTopic(pUsername) + pUserID := p.UserID + roomUserTopic := g.RoomID.UserTopic(pUserID) if autoAction.action > NoAction { time.Sleep(500 * time.Millisecond) p.LastActionTS = time.Now() @@ -757,7 +757,7 @@ func applyAutoAction(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *d actionResult = doBet(g, p, db, minBet, newlyAllInPlayers, lastRaisePlayerIdx, playerToPlayIdx, autoAction.evt) } } - delete(autoCache, pUsername) + delete(autoCache, pUserID) setAutoAction(g, roomUserTopic, "") return } @@ -793,7 +793,7 @@ func execBettingRound(g *PokerGame, skip int, minBet database.PokerChip) bool { playerToPlayIdx := (dealerIdx + skip) % len(ongoing.Players) lastRaisePlayerIdx := -1 newlyAllInPlayers := make([]*PokerPlayer, 0) - autoCache := make(map[database.Username]AutoAction) + autoCache := make(map[database.UserID]AutoAction) playerAlive := ongoing.countAlivePlayers() @@ -812,8 +812,8 @@ RoundIsSettledLoop: playerToPlayIdx = (playerToPlayIdx + 1) % len(ongoing.Players) p := ongoing.Players[playerToPlayIdx] p.countChancesToAction++ - pUsername := p.Username - roomUserTopic := roomID.UserTopic(pUsername) + pUserID := p.UserID + roomUserTopic := roomID.UserTopic(pUserID) if playerToPlayIdx == lastRaisePlayerIdx { break AllPlayersLoop @@ -836,7 +836,7 @@ RoundIsSettledLoop: actionResult := doNothing // Check for pre-selected action - if autoAction, ok := autoCache[pUsername]; ok { + if autoAction, ok := autoCache[pUserID]; ok { actionResult = applyAutoAction(g, p, db, &minBet, &newlyAllInPlayers, &lastRaisePlayerIdx, &playerAlive, playerToPlayIdx, autoAction, autoCache) goto checkActionResult @@ -854,7 +854,7 @@ RoundIsSettledLoop: goto checkActionResult } - if evt.Player != pUsername { + if evt.UserID != pUserID { actionResult = handleAutoActionReceived(g, autoCache, evt) goto checkActionResult } @@ -990,11 +990,11 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { var card string for cardIdx := 1; cardIdx <= NbCardsPerPlayer; cardIdx++ { for _, p := range ongoing.Players { - pUsername := p.Username + pUserID := p.UserID if !p.canBet() { continue } - roomUserTopic := roomID.UserTopic(pUsername) + roomUserTopic := roomID.UserTopic(pUserID) seatData := seats[p.SeatIdx] time.Sleep(time.Second) card = ongoing.Deck[*idx] @@ -1019,7 +1019,7 @@ func dealPlayersCards(g *PokerGame, seats []Seat, idx *int) { } evt := PokerEvent{ID: "card" + itoa(*idx), Name: "", Idx: *idx, Top: top, Left: left, Angle: seatData.Angle} - evt1 := PokerEvent{ID: "card" + itoa(*idx), Name: card, Idx: *idx, Top: seatData1.Top, Left: seatData1.Left, Reveal: true, Player: pUsername} + evt1 := PokerEvent{ID: "card" + itoa(*idx), Name: card, Idx: *idx, Top: seatData1.Top, Left: seatData1.Left, Reveal: true, UserID: pUserID} PokerPubSub.Pub(roomTopic, evt) PokerPubSub.Pub(roomUserTopic, evt1) @@ -1371,7 +1371,7 @@ func cardToPokerCard(name string) string { func (g *PokerGame) Deal(roomID RoomID, authUser *database.User) { roomTopic := roomID.Topic() - roomUserTopic := roomID.UserTopic(authUser.Username) + roomUserTopic := roomID.UserTopic(authUser.ID) if g.countEligibleSeated() < 2 { PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "need at least 2 players"}) return @@ -1423,7 +1423,7 @@ func PokerCheckHandler(c echo.Context) error { } if c.Request().Method == http.MethodPost { select { - case g.PlayersEventCh <- PlayerEvent{Player: authUser.Username, Check: true}: + case g.PlayersEventCh <- PlayerEvent{UserID: authUser.ID, Check: true}: default: } } @@ -1443,7 +1443,7 @@ func PokerBetHandler(c echo.Context) error { betBtn := c.Request().PostFormValue("bet") if betBtn == "betAllIn" { select { - case g.PlayersEventCh <- PlayerEvent{Player: authUser.Username, AllIn: true}: + case g.PlayersEventCh <- PlayerEvent{UserID: authUser.ID, AllIn: true}: default: } } else { @@ -1452,7 +1452,7 @@ func PokerBetHandler(c echo.Context) error { } if bet > 0 { select { - case g.PlayersEventCh <- PlayerEvent{Player: authUser.Username, Bet: bet}: + case g.PlayersEventCh <- PlayerEvent{UserID: authUser.ID, Bet: bet}: default: } } @@ -1488,7 +1488,7 @@ func PokerCallHandler(c echo.Context) error { } if c.Request().Method == http.MethodPost { select { - case g.PlayersEventCh <- PlayerEvent{Player: authUser.Username, Call: true}: + case g.PlayersEventCh <- PlayerEvent{UserID: authUser.ID, Call: true}: default: } } @@ -1504,7 +1504,7 @@ func PokerFoldHandler(c echo.Context) error { } if c.Request().Method == http.MethodPost { select { - case g.PlayersEventCh <- PlayerEvent{Player: authUser.Username, Fold: true}: + case g.PlayersEventCh <- PlayerEvent{UserID: authUser.ID, Fold: true}: default: } } @@ -1525,8 +1525,8 @@ type RoomID string func (r RoomID) String() string { return string(r) } func (r RoomID) Topic() string { return "room_" + string(r) } func (r RoomID) LogsTopic() string { return r.Topic() + "_logs" } -func (r RoomID) UserTopic(username database.Username) string { - return r.Topic() + "_" + username.String() +func (r RoomID) UserTopic(userID database.UserID) string { + return r.Topic() + "_" + userID.String() } func PokerDealHandler(c echo.Context) error { @@ -1573,7 +1573,7 @@ func PokerSitHandler(c echo.Context) error { pos-- roomID := RoomID(c.Param("roomID")) roomTopic := roomID.Topic() - roomUserTopic := roomID.UserTopic(authUser.Username) + roomUserTopic := roomID.UserTopic(authUser.ID) g := PokerInstance.GetGame(roomID) if g == nil { return c.HTML(http.StatusOK, html) @@ -1718,10 +1718,10 @@ func buildBaseHtml(g *PokerGame, authUser *database.User) (html string) { ongoing.Events.RWith(func(v *[]PokerEvent) { for _, evt := range *v { - if evt.Player == "" { + if evt.UserID == 0 { html += getPokerEventHtml(evt, "0s") } - if evt.Player == authUser.Username { + if evt.UserID == authUser.ID { html += getPokerEventHtml(evt, "0s") } } @@ -2375,7 +2375,7 @@ func PokerStreamHandler(c echo.Context) error { } roomTopic := roomID.Topic() - roomUserTopic := roomID.UserTopic(authUser.Username) + roomUserTopic := roomID.UserTopic(authUser.ID) send := func(s string) { _, _ = c.Response().Write([]byte(s)) } g := PokerInstance.GetOrCreateGame(db, roomID, pokerTable.ID, pokerTable.MinBet, pokerTable.IsTest)