commit 1f4a2a15357ddc6fab1db061ee30834868842b68
parent 85e5572c5a3ea1657ef16e6e9a36429ef692327f
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Fri, 15 Dec 2023 05:24:15 -0500
cleanup
Diffstat:
2 files changed, 59 insertions(+), 50 deletions(-)
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -785,7 +785,7 @@ func PokerHomeHandler(c echo.Context) error {
pokerTables, _ := db.GetPokerTables()
for _, t := range pokerTables {
var nbSeated int
- if g := poker.PokerInstance.GetGame(t.Slug); g != nil {
+ if g := poker.PokerInstance.GetGame(poker.RoomID(t.Slug)); g != nil {
nbSeated = g.CountSeated()
}
data.Tables = append(data.Tables, TmpTable{PokerTable: t, NbSeated: nbSeated})
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -36,16 +36,16 @@ const NbCardsPerPlayer = 2
type Poker struct {
sync.Mutex
- games map[string]*PokerGame
+ games map[RoomID]*PokerGame
}
func NewPoker() *Poker {
p := &Poker{}
- p.games = make(map[string]*PokerGame)
+ p.games = make(map[RoomID]*PokerGame)
return p
}
-func (p *Poker) GetOrCreateGame(roomID string, pokerTableID int64, pokerTableMinBet database.PokerChip, pokerTableIsTest bool) *PokerGame {
+func (p *Poker) GetOrCreateGame(roomID RoomID, pokerTableID int64, pokerTableMinBet database.PokerChip, pokerTableIsTest bool) *PokerGame {
p.Lock()
defer p.Unlock()
g, found := p.games[roomID]
@@ -64,7 +64,7 @@ func (p *Poker) GetOrCreateGame(roomID string, pokerTableID int64, pokerTableMin
return g
}
-func (p *Poker) GetGame(roomID string) *PokerGame {
+func (p *Poker) GetGame(roomID RoomID) *PokerGame {
p.Lock()
defer p.Unlock()
g, found := PokerInstance.games[roomID]
@@ -338,7 +338,7 @@ func (g *PokerGame) incrDealerIdx() {
g.bigBlindIdx = (dealerIdx + startIDx + 1) % nbPlayers
}
-func (g *PokerGame) UnSitPlayer(db *database.DkfDB, roomID string, authUser *database.User) error {
+func (g *PokerGame) UnSitPlayer(db *database.DkfDB, roomID RoomID, authUser *database.User) error {
if g.Ongoing != nil {
if p := g.Ongoing.GetPlayer(authUser.Username); p != nil {
p.Unsit.Store(true)
@@ -355,8 +355,8 @@ func (g *PokerGame) UnSitPlayer(db *database.DkfDB, roomID string, authUser *dat
return errors.New("player is not sit")
}
-func (g *PokerGame) UnSitPlayer1(db *database.DkfDB, roomID string, standingPlayer *PokerStandingPlayer, idx int) error {
- roomTopic := "room_" + roomID
+func (g *PokerGame) UnSitPlayer1(db *database.DkfDB, roomID RoomID, standingPlayer *PokerStandingPlayer, idx int) error {
+ roomTopic := roomID.Topic()
standingPlayerUsername := standingPlayer.Username
user, err := db.GetUserByUsername(standingPlayerUsername)
@@ -479,9 +479,9 @@ func setWaitTurn(g *PokerGame, roomTopic string, seatIdx int) {
}
// Return either or not the game ended because only 1 player left playing (or none)
-func waitPlayersActionFn(db *database.DkfDB, g *PokerGame, roomID string, skip int, minBet database.PokerChip) bool {
- roomTopic := "room_" + roomID
- roomLogsTopic := "room_" + roomID + "_logs"
+func waitPlayersActionFn(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
@@ -505,7 +505,7 @@ OUTER:
playerToPlayIdx = (playerToPlayIdx + 1) % len(g.Ongoing.Players)
p := g.Ongoing.Players[playerToPlayIdx]
pUsername := p.Username
- roomUserTopic := "room_" + roomID + "_" + pUsername.String()
+ roomUserTopic := roomID.UserTopic(pUsername)
if playerToPlayIdx == lastRaisePlayerIdx {
break
@@ -784,9 +784,9 @@ func dealPlayersCards(g *PokerGame, roomTopic string, seats []Seat, idx *int) {
}
}
-func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) {
- roomTopic := "room_" + roomID
- roomLogsTopic := "room_" + roomID + "_logs"
+func dealerThread(db *database.DkfDB, g *PokerGame, roomID RoomID) {
+ roomTopic := roomID.Topic()
+ roomLogsTopic := roomID.LogsTopic()
bigBlindBet := g.PokerTableMinBet
collectRake := false
@@ -1085,15 +1085,15 @@ func cardToPokerCard(name string) string {
return r.Replace(name)
}
-func (g *PokerGame) Deal(db *database.DkfDB, roomID string, authUser *database.User) {
- roomTopic := "room_" + roomID
- roomUserTopic := "room_" + roomID + "_" + authUser.Username.String()
+func (g *PokerGame) Deal(db *database.DkfDB, roomID RoomID, authUser *database.User) {
+ roomTopic := roomID.Topic()
+ roomUserTopic := roomID.UserTopic(authUser.Username)
if g.CountEligibleSeated() < 2 {
- PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "need at least 2 players"})
+ PokerPubSub.Pub(roomTopic, ErrorMsgEvent{Message: "need at least 2 players"})
return
}
if !g.IsGameStarted.CompareAndSwap(false, true) {
- PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "game already ongoing"})
+ PokerPubSub.Pub(roomTopic, ErrorMsgEvent{Message: "game already ongoing"})
return
}
@@ -1147,7 +1147,7 @@ var PokerPubSub = pubsub.NewPubSub[any]()
func PokerCheckHandler(c echo.Context) error {
authUser := c.Get("authUser").(*database.User)
- roomID := c.Param("roomID")
+ roomID := RoomID(c.Param("roomID"))
g := PokerInstance.GetGame(roomID)
if g == nil {
return c.NoContent(http.StatusNotFound)
@@ -1163,7 +1163,7 @@ func PokerCheckHandler(c echo.Context) error {
func PokerBetHandler(c echo.Context) error {
authUser := c.Get("authUser").(*database.User)
- roomID := c.Param("roomID")
+ roomID := RoomID(c.Param("roomID"))
g := PokerInstance.GetGame(roomID)
if g == nil {
return c.NoContent(http.StatusNotFound)
@@ -1204,7 +1204,7 @@ func PokerBetHandler(c echo.Context) error {
func PokerCallHandler(c echo.Context) error {
authUser := c.Get("authUser").(*database.User)
- roomID := c.Param("roomID")
+ roomID := RoomID(c.Param("roomID"))
g := PokerInstance.GetGame(roomID)
if g == nil {
return c.NoContent(http.StatusNotFound)
@@ -1220,7 +1220,7 @@ func PokerCallHandler(c echo.Context) error {
func PokerFoldHandler(c echo.Context) error {
authUser := c.Get("authUser").(*database.User)
- roomID := c.Param("roomID")
+ roomID := RoomID(c.Param("roomID"))
g := PokerInstance.GetGame(roomID)
if g == nil {
return c.NoContent(http.StatusNotFound)
@@ -1253,8 +1253,17 @@ func Refund(db *database.DkfDB) {
tx.Commit()
}
+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 "room_" + string(r) + "_logs" }
+func (r RoomID) UserTopic(username database.Username) string {
+ return "room_" + string(r) + "_" + username.String()
+}
+
func PokerDealHandler(c echo.Context) error {
- roomID := c.Param("roomID")
+ roomID := RoomID(c.Param("roomID"))
authUser := c.Get("authUser").(*database.User)
db := c.Get("database").(*database.DkfDB)
g := PokerInstance.GetGame(roomID)
@@ -1272,10 +1281,10 @@ func PokerDealHandler(c echo.Context) error {
func PokerUnSitHandler(c echo.Context) error {
db := c.Get("database").(*database.DkfDB)
authUser := c.Get("authUser").(*database.User)
- roomID := c.Param("roomID")
+ roomID := RoomID(c.Param("roomID"))
html := hutils.HtmlCssReset + `<form method="post"><button>UnSit</button></form>`
- roomTopic := "room_" + roomID
- roomLogsTopic := "room_" + roomID + "_logs"
+ roomTopic := roomID.Topic()
+ roomLogsTopic := roomID.LogsTopic()
g := PokerInstance.GetGame(roomID)
if g == nil {
return c.NoContent(http.StatusNotFound)
@@ -1298,17 +1307,17 @@ func PokerSitHandler(c echo.Context) error {
return c.HTML(http.StatusOK, html)
}
pos--
- roomID := c.Param("roomID")
- roomTopic := "room_" + roomID
- roomUserTopic := "room_" + roomID + "_" + authUser.Username.String()
- roomLogsTopic := "room_" + roomID + "_logs"
+ 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)
}
if c.Request().Method == http.MethodPost {
- pokerTable, err := db.GetPokerTableBySlug(roomID)
+ pokerTable, err := db.GetPokerTableBySlug(roomID.String())
tableMinBuyIn := pokerTable.MinBuyIn
tableID := pokerTable.ID
if err != nil {
@@ -1414,7 +1423,7 @@ func buildPayloadHtml(g *PokerGame, authUser *database.User, payload any) (html
return
}
-func buildBaseHtml(g *PokerGame, authUser *database.User, roomID string) (html string) {
+func buildBaseHtml(g *PokerGame, authUser *database.User, roomID RoomID) (html string) {
html += hutils.HtmlCssReset
html += pokerCss
//html += `<script>document.onclick = function(e) { console.log(e.x, e.y); };</script>` // TODO: dev only
@@ -1426,7 +1435,7 @@ func buildBaseHtml(g *PokerGame, authUser *database.User, roomID string) (html s
html += buildSeatsHtml(g, authUser, roomID)
html += buildActionsDiv(roomID)
html += buildDealerTokenHtml(g)
- html += `<iframe src="/poker/` + roomID + `/logs" id="eventLogs"></iframe>`
+ html += `<iframe src="/poker/` + roomID.String() + `/logs" id="eventLogs"></iframe>`
html += `<div id="errorMsg"></div>`
html += buildMainPotHtml(g)
html += buildWinnerHtml()
@@ -1570,19 +1579,19 @@ func buildCountdownsHtml() (html string) {
return
}
-func buildActionsDiv(roomID string) (html string) {
+func buildActionsDiv(roomID RoomID) (html string) {
html += `<div id="actionsDiv">`
- html += `<iframe src="/poker/` + roomID + `/deal" id="dealBtn"></iframe>`
- html += `<iframe src="/poker/` + roomID + `/unsit" id="unSitBtn"></iframe>`
- html += `<iframe src="/poker/` + roomID + `/bet" id="betBtn"></iframe>`
- html += `<iframe src="/poker/` + roomID + `/call" id="callBtn"></iframe>`
- html += `<iframe src="/poker/` + roomID + `/check" id="checkBtn"></iframe>`
- html += `<iframe src="/poker/` + roomID + `/fold" id="foldBtn"></iframe>`
+ html += `<iframe src="/poker/` + roomID.String() + `/deal" id="dealBtn"></iframe>`
+ html += `<iframe src="/poker/` + roomID.String() + `/unsit" id="unSitBtn"></iframe>`
+ html += `<iframe src="/poker/` + roomID.String() + `/bet" id="betBtn"></iframe>`
+ html += `<iframe src="/poker/` + roomID.String() + `/call" id="callBtn"></iframe>`
+ html += `<iframe src="/poker/` + roomID.String() + `/check" id="checkBtn"></iframe>`
+ html += `<iframe src="/poker/` + roomID.String() + `/fold" id="foldBtn"></iframe>`
html += `</div>`
return
}
-func buildSeatsHtml(g *PokerGame, authUser *database.User, roomID string) (html string) {
+func buildSeatsHtml(g *PokerGame, authUser *database.User, roomID RoomID) (html string) {
g.PlayersMtx.RLock()
defer g.PlayersMtx.RUnlock()
for i := range g.Players {
@@ -1591,7 +1600,7 @@ func buildSeatsHtml(g *PokerGame, authUser *database.User, roomID string) (html
html += `<div>`
for i := range g.Players {
html += `<div class="seat" id="seat` + itoa(i+1) + `">
-<iframe src="/poker/` + roomID + `/sit/` + itoa(i+1) + `" class="takeSeat takeSeat` + itoa(i+1) + `"></iframe>`
+<iframe src="/poker/` + roomID.String() + `/sit/` + itoa(i+1) + `" class="takeSeat takeSeat` + itoa(i+1) + `"></iframe>`
html += ` <div class="inner"></div>`
html += ` <div id="seat` + itoa(i+1) + `_cash" class="cash"></div>`
html += `</div>`
@@ -1980,14 +1989,14 @@ body {
</style>`
func PokerLogsHandler(c echo.Context) error {
- roomID := c.Param("roomID")
+ roomID := RoomID(c.Param("roomID"))
authUser := c.Get("authUser").(*database.User)
send := func(s string) { _, _ = c.Response().Write([]byte(s)) }
g := PokerInstance.GetGame(roomID)
if g == nil {
return c.Redirect(http.StatusFound, "/")
}
- roomLogsTopic := "room_" + roomID + "_logs"
+ roomLogsTopic := roomID.LogsTopic()
sub := PokerPubSub.Subscribe([]string{roomLogsTopic})
defer sub.Close()
quit := hutils.CloseSignalChan(c)
@@ -2036,17 +2045,17 @@ Loop:
}
func PokerHandler(c echo.Context) error {
- roomID := c.Param("roomID")
+ roomID := RoomID(c.Param("roomID"))
authUser := c.Get("authUser").(*database.User)
db := c.Get("database").(*database.DkfDB)
- pokerTable, err := db.GetPokerTableBySlug(roomID)
+ pokerTable, err := db.GetPokerTableBySlug(roomID.String())
if err != nil {
return c.Redirect(http.StatusFound, "/")
}
- roomTopic := "room_" + roomID
- roomUserTopic := "room_" + roomID + "_" + authUser.Username.String()
+ roomTopic := roomID.Topic()
+ roomUserTopic := roomID.UserTopic(authUser.Username)
send := func(s string) { _, _ = c.Response().Write([]byte(s)) }
g := PokerInstance.GetOrCreateGame(roomID, pokerTable.ID, pokerTable.MinBet, pokerTable.IsTest)