commit 7f254db17234c333991c6ba3de0f97707f65d96a
parent db2dfeada617be15826c0ce18adbe23363086792
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 17 Dec 2023 04:58:24 -0500
cleanup
Diffstat:
1 file changed, 21 insertions(+), 25 deletions(-)
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -51,6 +51,7 @@ func (p *Poker) GetOrCreateGame(roomID RoomID, pokerTableID int64, pokerTableMin
g, found := p.games[roomID]
if !found {
g = &PokerGame{
+ RoomID: roomID,
PokerTableID: pokerTableID,
PokerTableMinBet: pokerTableMinBet,
PokerTableIsTest: pokerTableIsTest,
@@ -175,6 +176,7 @@ type PlayerCard struct {
type PokerGame struct {
sync.Mutex
+ RoomID RoomID
PokerTableID int64
PokerTableMinBet database.PokerChip
PokerTableIsTest bool
@@ -473,9 +475,9 @@ func NewOngoing(g *PokerGame) *Ongoing {
return &Ongoing{Deck: deck, Players: players, WaitTurnEvent: PokerWaitTurnEvent{Idx: -1}, CreatedAt: time.Now()}
}
-func newLogEvent(g *PokerGame, roomLogsTopic, msg string) {
+func newLogEvent(g *PokerGame, msg string) {
logEvt := LogEvent{Message: msg}
- PokerPubSub.Pub(roomLogsTopic, logEvt)
+ PokerPubSub.Pub(g.RoomID.LogsTopic(), logEvt)
if g.Ongoing != nil {
g.Ongoing.LogEventsMtx.Lock()
g.Ongoing.LogEvents = append(g.Ongoing.LogEvents, logEvt)
@@ -540,7 +542,6 @@ type AutoAction struct {
// Return either or not the game ended because only 1 player left playing (or none)
func execBettingRound(db *database.DkfDB, g *PokerGame, roomID RoomID, skip int, minBet database.PokerChip) bool {
roomTopic := roomID.Topic()
- roomLogsTopic := roomID.LogsTopic()
dealerIdx := g.Ongoing.getPlayerIdxBySeatIdx(int(g.DealerSeatIdx.Load()))
playerToPlayIdx := (dealerIdx + skip) % len(g.Ongoing.Players)
lastRaisePlayerIdx := -1
@@ -594,7 +595,7 @@ RoundIsSettledLoop:
doTimeout := func() int {
if p.GetBet() < minBet {
foldPlayer(p)
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s auto fold", pUsername))
+ newLogEvent(g, fmt.Sprintf("%s auto fold", pUsername))
playerAlive--
if playerAlive == 1 {
@@ -602,7 +603,7 @@ RoundIsSettledLoop:
}
return breakGetPlayerEventLoop
}
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s auto check", pUsername))
+ newLogEvent(g, fmt.Sprintf("%s auto check", pUsername))
return breakGetPlayerEventLoop
}
@@ -617,7 +618,7 @@ RoundIsSettledLoop:
doFold := func() int {
foldPlayer(p)
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s fold", pUsername))
+ newLogEvent(g, fmt.Sprintf("%s fold", pUsername))
playerAlive--
if playerAlive == 1 {
@@ -633,14 +634,14 @@ RoundIsSettledLoop:
PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg})
return continueGetPlayerEventLoop
}
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s check", pUsername))
+ newLogEvent(g, fmt.Sprintf("%s check", pUsername))
return doNothing
}
doCall := func() int {
bet := utils.MinInt(minBet-p.GetBet(), p.GetCash())
if bet == 0 {
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s check", pUsername))
+ newLogEvent(g, fmt.Sprintf("%s check", pUsername))
} else {
p.doBetAndNotif(db, g.PokerTableID, bet, roomTopic)
logMsg := fmt.Sprintf("%s call (%d)", pUsername, bet)
@@ -648,7 +649,7 @@ RoundIsSettledLoop:
logMsg += " (all-in)"
newlyAllInPlayers = append(newlyAllInPlayers, p)
}
- newLogEvent(g, roomLogsTopic, logMsg)
+ newLogEvent(g, logMsg)
}
return doNothing
}
@@ -664,7 +665,7 @@ RoundIsSettledLoop:
if p.isAllIn() {
newlyAllInPlayers = append(newlyAllInPlayers, p)
}
- newLogEvent(g, roomLogsTopic, logMsg)
+ newLogEvent(g, logMsg)
return doNothing
}
@@ -697,7 +698,7 @@ RoundIsSettledLoop:
logMsg += " (all-in)"
newlyAllInPlayers = append(newlyAllInPlayers, p)
}
- newLogEvent(g, roomLogsTopic, logMsg)
+ newLogEvent(g, logMsg)
return doNothing
}
@@ -828,7 +829,7 @@ RoundIsSettled:
PokerPubSub.Pub(roomTopic, AutoActionEvent{Message: ""})
PokerPubSub.Pub(roomTopic, ErrorMsgEvent{Message: ""})
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("--"))
+ newLogEvent(g, fmt.Sprintf("--"))
setWaitTurn(g, roomTopic, -1)
time.Sleep(time.Second)
@@ -942,7 +943,6 @@ func computeAllInMaxGain(g *PokerGame, newlyAllInPlayers []*PokerPlayer, mainPot
func dealerThread(db *database.DkfDB, g *PokerGame, roomID RoomID) {
roomTopic := roomID.Topic()
- roomLogsTopic := roomID.LogsTopic()
bigBlindBet := g.PokerTableMinBet
collectRake := false
nbOngoingPlayers := len(g.Ongoing.Players)
@@ -993,7 +993,7 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID RoomID) {
}
PokerPubSub.Pub(roomTopic, GameStartedEvent{DealerSeatIdx: int(g.DealerSeatIdx.Load())})
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("-- New game --"))
+ newLogEvent(g, fmt.Sprintf("-- New game --"))
applySmallBlindBet(db, g, bigBlindBet, roomID)
time.Sleep(time.Second)
@@ -1067,7 +1067,7 @@ END:
g.Ongoing.setMainPot(0)
PokerPubSub.Pub(roomTopic, GameIsDoneEvent{Winner: winnersStr, WinnerHand: winnerHand})
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("-- Game ended --"))
+ newLogEvent(g, fmt.Sprintf("-- Game ended --"))
// Wait a minimum of X seconds before allowing a new game
time.Sleep(MinTimeAfterGame * time.Second)
@@ -1089,12 +1089,11 @@ func applyBigBlindBet(db *database.DkfDB, g *PokerGame, bigBlindBet database.Pok
func applyBlindBet(db *database.DkfDB, g *PokerGame, playerIdx int, bet database.PokerChip, roomID RoomID, name string) {
p := g.Ongoing.Players[playerIdx]
p.doBetAndNotif(db, g.PokerTableID, bet, roomID.Topic())
- newLogEvent(g, roomID.LogsTopic(), fmt.Sprintf("%s %s %d", p.Username, name, bet))
+ newLogEvent(g, fmt.Sprintf("%s %s %d", p.Username, name, bet))
}
func autoUnsitInactivePlayers(db *database.DkfDB, g *PokerGame, roomID RoomID) {
roomTopic := roomID.Topic()
- roomLogsTopic := roomID.LogsTopic()
g.PlayersMtx.Lock()
defer g.PlayersMtx.Unlock()
for idx, p := range g.Players {
@@ -1112,7 +1111,7 @@ func autoUnsitInactivePlayers(db *database.DkfDB, g *PokerGame, roomID RoomID) {
if playerShallBeBooted {
if err := g.UnSitPlayer1(db, roomID, p, idx); err == nil {
PokerPubSub.Pub(roomTopic, PokerSeatLeftEvent{})
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s auto un-sit", p.Username))
+ newLogEvent(g, fmt.Sprintf("%s auto un-sit", p.Username))
} else {
fmt.Println(err)
}
@@ -1123,7 +1122,6 @@ func autoUnsitInactivePlayers(db *database.DkfDB, g *PokerGame, roomID RoomID) {
func applyGains(db *database.DkfDB, g *PokerGame, playersGain []PlayerGain, mainPot, rake database.PokerChip, roomID RoomID) (winnersStr, winnerHand string) {
nbPlayersGain := len(playersGain)
- roomLogsTopic := roomID.LogsTopic()
tx := db.Begin()
if nbPlayersGain >= 1 {
winnerHand = utils.Ternary(nbPlayersGain == 1, playersGain[0].HandStr, "Split pot")
@@ -1131,10 +1129,10 @@ func applyGains(db *database.DkfDB, g *PokerGame, playersGain []PlayerGain, main
if !g.PokerTableIsTest {
_ = tx.IncrPokerCasinoRake(rake)
}
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("Rake %d (%.2f%%)", rake, (float64(rake)/float64(mainPot))*100))
+ newLogEvent(g, fmt.Sprintf("Rake %d (%.2f%%)", rake, (float64(rake)/float64(mainPot))*100))
for _, el := range playersGain {
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("Winner #%d: %s %s -> %d", el.Group, el.Player.Username, el.HandStr, el.Gain))
+ newLogEvent(g, fmt.Sprintf("Winner #%d: %s %s -> %d", el.Group, el.Player.Username, el.HandStr, el.Gain))
winnersStr += el.Player.Username.String() + " "
el.Player.gain(tx, g.PokerTableID, el.Gain)
}
@@ -1465,7 +1463,6 @@ func PokerUnSitHandler(c echo.Context) error {
roomID := RoomID(c.Param("roomID"))
html := hutils.HtmlCssReset + `<form method="post"><button>UnSit</button></form>`
roomTopic := roomID.Topic()
- roomLogsTopic := roomID.LogsTopic()
g := PokerInstance.GetGame(roomID)
if g == nil {
return c.NoContent(http.StatusNotFound)
@@ -1473,7 +1470,7 @@ func PokerUnSitHandler(c echo.Context) error {
if c.Request().Method == http.MethodPost {
if err := g.UnSitPlayer(db, roomID, authUser); err == nil {
PokerPubSub.Pub(roomTopic, PokerSeatLeftEvent{})
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s un-sit", authUser.Username.String()))
+ newLogEvent(g, fmt.Sprintf("%s un-sit", authUser.Username.String()))
} else {
logrus.Error(err)
}
@@ -1493,7 +1490,6 @@ func PokerSitHandler(c echo.Context) error {
roomID := RoomID(c.Param("roomID"))
roomTopic := roomID.Topic()
roomUserTopic := roomID.UserTopic(authUser.Username)
- roomLogsTopic := roomID.LogsTopic()
g := PokerInstance.GetGame(roomID)
if g == nil {
return c.HTML(http.StatusOK, html)
@@ -1551,7 +1547,7 @@ func PokerSitHandler(c echo.Context) error {
tableAccount.DoSave(tx)
tx.Commit()
PokerPubSub.Pub(roomTopic, PokerSeatTakenEvent{})
- newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s sit", authUser.Username.String()))
+ newLogEvent(g, fmt.Sprintf("%s sit", authUser.Username.String()))
}
return c.HTML(http.StatusOK, html)
}