commit 483a2d951394813b1ede9a3a88a635bc92f6446c
parent 6c5b00a84e11d1614daf01967978b91f1ac0c2e2
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Mon, 4 Dec 2023 14:38:14 -0500
poker
Diffstat:
3 files changed, 51 insertions(+), 2 deletions(-)
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -32,6 +32,7 @@ import (
"regexp"
"strconv"
"strings"
+ "sync"
"time"
)
@@ -755,6 +756,36 @@ func BHCHandler(c echo.Context) error {
return c.Render(http.StatusOK, "bhc", data)
}
+type Poker struct {
+ sync.Mutex
+ Games map[string]*PokerGame
+}
+
+func NewPoker() *Poker {
+ p := &Poker{}
+ p.Games = make(map[string]*PokerGame)
+ return p
+}
+
+func (p *Poker) GetOrCreateGame(roomID string) *PokerGame {
+ p.Lock()
+ defer p.Unlock()
+ g, found := p.Games[roomID]
+ if !found {
+ g = &PokerGame{
+ Players: [10]string{},
+ }
+ p.Games[roomID] = g
+ }
+ return g
+}
+
+var PokerInstance *Poker = NewPoker()
+
+type PokerGame struct {
+ Players [10]string
+}
+
type PokerEvent struct {
ID string
Idx int
@@ -772,8 +803,21 @@ var PokerPubSub = pubsub.NewPubSub[PokerEvent]()
func PokerSitHandler(c echo.Context) error {
authUser := c.Get("authUser").(*database.User)
+ pos, _ := strconv.Atoi(c.Param("pos"))
+ roomID := c.Param("roomID")
+ PokerInstance.Lock()
+ g, found := PokerInstance.Games[roomID]
+ PokerInstance.Unlock()
+ if !found {
+ return c.NoContent(http.StatusNotFound)
+ }
if c.Request().Method == http.MethodPost {
+ if g.Players[pos] != "" {
+ fmt.Println("seat already taken")
+ return c.NoContent(http.StatusNotFound)
+ }
myTopic := "room_" + authUser.ID.String() + "_seats"
+ g.Players[pos] = authUser.Username.String()
PokerPubSub.Pub(myTopic, PokerEvent{})
return c.HTML(http.StatusOK, ``)
}
@@ -787,6 +831,9 @@ func PokerHandler(c echo.Context) error {
// hash the deck and show hash
//return c.Render(http.StatusOK, "poker", nil)
+ roomID := c.Param("roomID")
+ g := PokerInstance.GetOrCreateGame(roomID)
+
authUser := c.Get("authUser").(*database.User)
send := func(s string) {
@@ -1090,6 +1137,8 @@ Loop:
if topic == myTopic1 {
send(`<style>.takeSeat { display: none; }</style>`)
+ c.Response().Flush()
+ fmt.Println(g.Players)
continue
}
diff --git a/pkg/web/middlewares/middlewares.go b/pkg/web/middlewares/middlewares.go
@@ -33,7 +33,7 @@ var GzipMiddleware = middleware.GzipWithConfig(
c.Path() == "/vip/challenges/re-1/:filename" ||
c.Path() == "/chess/:key" ||
c.Path() == "/chess/:key/analyze" ||
- c.Path() == "/poker" ||
+ c.Path() == "/poker/:roomID" ||
c.Path() == "/api/v1/chat/messages/:roomName/stream" ||
c.Path() == "/uploads/:filename" ||
c.Path() == "/" {
diff --git a/pkg/web/web.go b/pkg/web/web.go
@@ -97,7 +97,7 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te
authGroup.POST("/captcha", handlers.CaptchaHandler, middlewares.AuthRateLimitMiddleware(time.Second, 1))
authGroup.GET("/donate", handlers.DonateHandler)
authGroup.GET("/shop", handlers.ShopHandler)
- authGroup.GET("/poker", handlers.PokerHandler)
+ authGroup.GET("/poker/:roomID", handlers.PokerHandler)
authGroup.GET("/poker/:roomID/sit/:pos", handlers.PokerSitHandler)
authGroup.POST("/poker/:roomID/sit/:pos", handlers.PokerSitHandler)
authGroup.GET("/chess", handlers.ChessHandler)