dkforest

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

commit 9c28668769daf2f1b2c2633696d57425e17ac08f
parent c6417d13cad00e56b9aabdcc08999ba86bf76df6
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon,  4 Dec 2023 16:28:41 -0500

garbage progress

Diffstat:
Mpkg/web/handlers/handlers.go | 260++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mpkg/web/middlewares/middlewares.go | 6++++--
Mpkg/web/web.go | 2++
3 files changed, 150 insertions(+), 118 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -780,84 +780,28 @@ func (p *Poker) GetOrCreateGame(roomID string) *PokerGame { return g } -var PokerInstance *Poker = NewPoker() +var PokerInstance = NewPoker() -type PokerGame struct { +type Ongoing struct { + Deck []string Players []string } -func (g PokerGame) IsSeated(player string) (bool, int) { - isSeated := false - pos := 0 - for idx, p := range g.Players { - if p == player { - isSeated = true - pos = idx + 1 - break - } - } - return isSeated, pos -} - -type PokerEvent struct { - ID string - Idx int - Name string - Top int - Left int - Burn bool - Angle string -} - -type PokerSeatTakenEvent struct { -} - -type PokerSeatLeftEvent struct { - Player string +type PokerGame struct { + sync.Mutex + Players []string + Ongoing *Ongoing } -var PokerPubSub = pubsub.NewPubSub[any]() - -func PokerSitHandler(c echo.Context) error { - authUser := c.Get("authUser").(*database.User) - pos, _ := strconv.Atoi(c.Param("pos")) - pos-- - roomID := c.Param("roomID") - PokerInstance.Lock() - g, found := PokerInstance.Games[roomID] - PokerInstance.Unlock() - if !found { - return c.NoContent(http.StatusNotFound) +func (g *PokerGame) Deal(roomID string) { + if g.Ongoing != nil { + fmt.Println("game already ongoing") + return } - if c.Request().Method == http.MethodPost { - if g.Players[pos] != "" { - fmt.Println("seat already taken") - return c.NoContent(http.StatusNotFound) - } - myTopic := "room_" + roomID - g.Players[pos] = authUser.Username.String() - PokerPubSub.Pub(myTopic, PokerSeatTakenEvent{}) - return c.HTML(http.StatusOK, ``) - } - return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) -} - -func PokerHandler(c echo.Context) error { - // players 2-10 - // $5 -> 1000 chips - // generate deck - // 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) { - _, _ = c.Response().Write([]byte(s)) + if g.CountSeated() < 2 { + fmt.Println("need at least 2 players") + return } - deck := []string{ "A♠", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "Q♠", "K♠", "A♥", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "J♥", "Q♥", "K♥", @@ -865,13 +809,16 @@ func PokerHandler(c echo.Context) error { "A♦", "2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♦", } utils.Shuffle(deck) - deckStr := strings.Join(deck, "") - deckSha256 := utils.Sha256([]byte(deckStr)) - quit := hutils.CloseSignalChan(c) - myTopic := "room_" + roomID + players := make([]string, 10) + for idx := range g.Players { + players[idx] = g.Players[idx] + } + + g.Ongoing = &Ongoing{Deck: deck, Players: players} go func() { + myTopic := "room_" + roomID type Seat struct { Top int Left int @@ -910,13 +857,9 @@ func PokerHandler(c echo.Context) error { idx := 0 for _, d := range data { - select { - case <-time.After(time.Second): - case <-quit: - return - } + time.Sleep(time.Second) idx++ - card, deck = deck[0], deck[1:] + card, g.Ongoing.Deck = g.Ongoing.Deck[0], g.Ongoing.Deck[1:] name := "" if d.Reveal { name = card @@ -933,13 +876,9 @@ func PokerHandler(c echo.Context) error { } // Burn - select { - case <-time.After(time.Second): - case <-quit: - return - } + time.Sleep(time.Second) idx++ - card, deck = deck[0], deck[1:] + card, g.Ongoing.Deck = g.Ongoing.Deck[0], g.Ongoing.Deck[1:] PokerPubSub.Pub(myTopic, PokerEvent{ ID: "card" + strconv.Itoa(idx), Name: "", @@ -951,13 +890,9 @@ func PokerHandler(c echo.Context) error { // Flop (3 first cards) for i := 1; i <= 3; i++ { - select { - case <-time.After(time.Second): - case <-quit: - return - } + time.Sleep(time.Second) idx++ - card, deck = deck[0], deck[1:] + card, g.Ongoing.Deck = g.Ongoing.Deck[0], g.Ongoing.Deck[1:] PokerPubSub.Pub(myTopic, PokerEvent{ ID: "card" + strconv.Itoa(idx), Name: card, @@ -968,13 +903,9 @@ func PokerHandler(c echo.Context) error { } // Burn - select { - case <-time.After(time.Second): - case <-quit: - return - } + time.Sleep(time.Second) idx++ - card, deck = deck[0], deck[1:] + card, g.Ongoing.Deck = g.Ongoing.Deck[0], g.Ongoing.Deck[1:] PokerPubSub.Pub(myTopic, PokerEvent{ ID: "card" + strconv.Itoa(idx), Name: "", @@ -985,13 +916,9 @@ func PokerHandler(c echo.Context) error { }) // Turn (4th card) - select { - case <-time.After(time.Second): - case <-quit: - return - } + time.Sleep(time.Second) idx++ - card, deck = deck[0], deck[1:] + card, g.Ongoing.Deck = g.Ongoing.Deck[0], g.Ongoing.Deck[1:] PokerPubSub.Pub(myTopic, PokerEvent{ ID: "card" + strconv.Itoa(idx), Name: card, Idx: idx, @@ -1000,13 +927,9 @@ func PokerHandler(c echo.Context) error { }) // Burn - select { - case <-time.After(time.Second): - case <-quit: - return - } + time.Sleep(time.Second) idx++ - card, deck = deck[0], deck[1:] + card, g.Ongoing.Deck = g.Ongoing.Deck[0], g.Ongoing.Deck[1:] PokerPubSub.Pub(myTopic, PokerEvent{ ID: "card" + strconv.Itoa(idx), Name: "", @@ -1017,13 +940,9 @@ func PokerHandler(c echo.Context) error { }) // River (5th card) - select { - case <-time.After(time.Second): - case <-quit: - return - } + time.Sleep(time.Second) idx++ - card, deck = deck[0], deck[1:] + card, g.Ongoing.Deck = g.Ongoing.Deck[0], g.Ongoing.Deck[1:] PokerPubSub.Pub(myTopic, PokerEvent{ ID: "card" + strconv.Itoa(idx), Name: card, Idx: idx, @@ -1031,6 +950,112 @@ func PokerHandler(c echo.Context) error { Left: 100 + (5 * 55), }) }() +} + +func (g *PokerGame) CountSeated() (count int) { + for _, p := range g.Players { + if p != "" { + count++ + } + } + return +} + +func (g *PokerGame) IsSeated(player string) (bool, int) { + isSeated := false + pos := 0 + for idx, p := range g.Players { + if p == player { + isSeated = true + pos = idx + 1 + break + } + } + return isSeated, pos +} + +type PokerEvent struct { + ID string + Idx int + Name string + Top int + Left int + Burn bool + Angle string +} + +type PokerSeatTakenEvent struct { +} + +type PokerSeatLeftEvent struct { + Player string +} + +var PokerPubSub = pubsub.NewPubSub[any]() + +func PokerDealHandler(c echo.Context) error { + 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 { + g.Deal(roomID) + return c.HTML(http.StatusOK, ``) + } + return c.HTML(http.StatusOK, `<form method="post"><button>Deal</button></form>`) +} + +func PokerSitHandler(c echo.Context) error { + authUser := c.Get("authUser").(*database.User) + pos, _ := strconv.Atoi(c.Param("pos")) + 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_" + roomID + g.Players[pos] = authUser.Username.String() + PokerPubSub.Pub(myTopic, PokerSeatTakenEvent{}) + return c.HTML(http.StatusOK, ``) + } + return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) +} + +func PokerHandler(c echo.Context) error { + // players 2-10 + // $5 -> 1000 chips + // generate deck + // 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) { + _, _ = c.Response().Write([]byte(s)) + } + + deckStr := "" + if g.Ongoing != nil { + deckStr = strings.Join(g.Ongoing.Deck, "") + } + deckSha256 := utils.Sha256([]byte(deckStr)) + + quit := hutils.CloseSignalChan(c) + myTopic := "room_" + roomID sub := PokerPubSub.Subscribe([]string{myTopic}) defer sub.Close() @@ -1107,6 +1132,7 @@ body { .takeSeat1 { position: absolute; top: 80px; left: 700px; } .takeSeat2 { position: absolute; top: 200px; left: 670px; } .takeSeat3 { position: absolute; top: 300px; left: 610px; } +#dealBtn { position: absolute; top: 400px; left: 50px; } </style>`) cardsHtml := "" for i := 52; i >= 1; i-- { @@ -1142,6 +1168,8 @@ body { <form><button>fold</button></form> </div>` send(turnAction) + actions := `<iframe src="/poker/123/deal" id="dealBtn"></iframe>` + send(actions) deckHash := deckSha256 send(`<div>` + deckStr + `</div>`) send(`<div>` + deckHash + `</div>`) diff --git a/pkg/web/middlewares/middlewares.go b/pkg/web/middlewares/middlewares.go @@ -181,7 +181,8 @@ func CSRFMiddleware() echo.MiddlewareFunc { c.Path() == "/api/v1/battleship" || c.Path() == "/api/v1/werewolf" || c.Path() == "/chess/:key" || - c.Path() == "/poker/:roomID/sit/:pos" + c.Path() == "/poker/:roomID/sit/:pos" || + c.Path() == "/poker/:roomID/deal" }, } return CSRFWithConfig(csrfConfig) @@ -291,7 +292,8 @@ func IsAuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc { !strings.Contains(c.Path(), "/api/v1/chat/messages/:roomName/stream") && !strings.Contains(c.Path(), "/api/v1/chat/top-bar") && !strings.Contains(c.Path(), "/api/v1/chat/controls") && - !strings.Contains(c.Path(), "/poker/:roomID/sit/:pos") { + !strings.Contains(c.Path(), "/poker/:roomID/sit/:pos") && + !strings.Contains(c.Path(), "/poker/:roomID/deal") { c.Response().Header().Set("X-Frame-Options", "DENY") } diff --git a/pkg/web/web.go b/pkg/web/web.go @@ -98,6 +98,8 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te authGroup.GET("/donate", handlers.DonateHandler) authGroup.GET("/shop", handlers.ShopHandler) authGroup.GET("/poker/:roomID", handlers.PokerHandler) + authGroup.GET("/poker/:roomID/deal", handlers.PokerDealHandler) + authGroup.POST("/poker/:roomID/deal", handlers.PokerDealHandler) authGroup.GET("/poker/:roomID/sit/:pos", handlers.PokerSitHandler) authGroup.POST("/poker/:roomID/sit/:pos", handlers.PokerSitHandler) authGroup.GET("/chess", handlers.ChessHandler)