dkforest

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

commit 66b2ef038ef03aa25bdb4a7f02fe1641c28553ed
parent bd4ef26b9fbf044c1cb593fe52f8a86b3485e9cd
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue,  5 Dec 2023 04:09:42 -0500

update user balance

Diffstat:
Mpkg/web/handlers/poker.go | 64++++++++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 46 insertions(+), 18 deletions(-)

diff --git a/pkg/web/handlers/poker.go b/pkg/web/handlers/poker.go @@ -42,7 +42,7 @@ func (p *Poker) GetOrCreateGame(roomID string) *PokerGame { if !found { g = &PokerGame{ PlayersEventCh: make(chan PlayerEvent), - Players: make([]string, NbPlayers), + Players: make([]PokerStandingPlayer, NbPlayers), } p.Games[roomID] = g } @@ -67,9 +67,15 @@ type Ongoing struct { MainPot int } +type PokerStandingPlayer struct { + Username string + Cash int +} + type PokerPlayer struct { Username string Bet int + Cash int Cards []PlayerCard Folded bool } @@ -82,7 +88,7 @@ type PlayerCard struct { type PokerGame struct { sync.Mutex PlayersEventCh chan PlayerEvent - Players []string + Players []PokerStandingPlayer Ongoing *Ongoing DealerIdx int IsGameDone bool @@ -125,7 +131,7 @@ func (g *PokerGame) Deal(roomID string) { players := make([]*PokerPlayer, NbPlayers) for idx := range g.Players { - players[idx] = &PokerPlayer{Username: g.Players[idx]} + players[idx] = &PokerPlayer{Username: g.Players[idx].Username, Cash: g.Players[idx].Cash} } g.Ongoing = &Ongoing{Deck: deck, Players: players, WaitTurnEvent: PokerWaitTurnEvent{Idx: -1}} @@ -163,6 +169,8 @@ func (g *PokerGame) Deal(roomID string) { } else if evt.Call { } else if evt.Bet > 0 { g.Ongoing.Players[i].Bet += evt.Bet + g.Ongoing.Players[i].Cash -= evt.Bet + PokerPubSub.Pub(myTopic, PlayerBetEvent{PlayerIdx: i, Player: p.Username, Bet: evt.Bet, TotalBet: g.Ongoing.Players[i].Bet, Cash: g.Ongoing.Players[i].Cash}) } break } @@ -314,7 +322,7 @@ func (g *PokerGame) Deal(roomID string) { func (g *PokerGame) CountSeated() (count int) { for _, p := range g.Players { - if p != "" { + if p.Username != "" { count++ } } @@ -325,7 +333,7 @@ func (g *PokerGame) IsSeated(player string) (bool, int) { isSeated := false pos := 0 for idx, p := range g.Players { - if p == player { + if p.Username == player { isSeated = true pos = idx + 1 break @@ -355,6 +363,14 @@ type GameIsDoneEvent struct { type ResetCardsEvent struct { } +type PlayerBetEvent struct { + PlayerIdx int + Player string + Bet int + TotalBet int + Cash int +} + type PlayerFoldEvent struct { Card1Idx, Card2Idx int } @@ -479,8 +495,8 @@ func PokerUnSitHandler(c echo.Context) error { var idx int found := false for i, p := range g.Players { - if p == authUser.Username.String() { - g.Players[i] = "" + if p.Username == authUser.Username.String() { + g.Players[i].Username = "" idx = i found = true break @@ -506,12 +522,13 @@ func PokerSitHandler(c echo.Context) error { return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) } if c.Request().Method == http.MethodPost { - if g.Players[pos] != "" { + if g.Players[pos].Username != "" { fmt.Println("seat already taken") return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) } myTopic := "room_" + roomID - g.Players[pos] = authUser.Username.String() + g.Players[pos].Username = authUser.Username.String() + g.Players[pos].Cash = 1000 PokerPubSub.Pub(myTopic, PokerSeatTakenEvent{}) } return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) @@ -544,7 +561,7 @@ func buildTakeSeatHtml(authUser *database.User, g *PokerGame, roomID string) str seated, _ := g.IsSeated(authUser.Username.String()) for i, p := range g.Players { takeSeatBtns += `<iframe src="/poker/` + roomID + `/sit/` + strconv.Itoa(i+1) + `" class="takeSeat takeSeat` + strconv.Itoa(i+1) + `"></iframe>` - if p != "" || seated { + if p.Username != "" || seated { takeSeatBtns += `<style>.takeSeat` + strconv.Itoa(i+1) + ` { display: none; }</style>` } } @@ -565,11 +582,11 @@ func buildSeatsHtml(g *PokerGame) string { seats := ` <div>` for i, p := range g.Players { - if p != "" { - seats += `<div id="seat` + strconv.Itoa(i+1) + `"></div>` - seats += `<style>#seat` + strconv.Itoa(i+1) + `:before { content: "` + p + `"; }</style>` - } else { - seats += `<div id="seat` + strconv.Itoa(i+1) + `"></div>` + seats += `<div id="seat` + strconv.Itoa(i+1) + `"></div>` + seats += `<div id="seat` + strconv.Itoa(i+1) + `_cash"></div>` + if p.Username != "" { + seats += `<style>#seat` + strconv.Itoa(i+1) + `:before { content: "` + p.Username + `"; }</style>` + seats += `<style>#seat` + strconv.Itoa(i+1) + `_cash:before { content: "` + strconv.Itoa(p.Cash) + `"; }</style>` } } seats += ` @@ -669,6 +686,9 @@ body { #seat1 { position: absolute; top: 80px; left: 700px; } #seat2 { position: absolute; top: 200px; left: 670px; } #seat3 { position: absolute; top: 300px; left: 610px; } +#seat1_cash { position: absolute; top: 100px; left: 700px; } +#seat2_cash { position: absolute; top: 220px; left: 670px; } +#seat3_cash { position: absolute; top: 320px; left: 610px; } #seat1Pot { position: absolute; top: 80px; left: 500px; } .takeSeat1 { position: absolute; top: 80px; left: 700px; } .takeSeat2 { position: absolute; top: 200px; left: 670px; } @@ -702,15 +722,17 @@ body { drawSeats := func() { seated, _ := g.IsSeated(authUser.Username.String()) for i, p := range g.Players { - if p != "" || seated { + if p.Username != "" || seated { send(`<style>.takeSeat` + strconv.Itoa(i+1) + ` { display: none; }</style>`) } else { send(`<style>.takeSeat` + strconv.Itoa(i+1) + ` { display: block; }</style>`) } - if p != "" { - send(`<style>#seat` + strconv.Itoa(i+1) + `:before { content: "` + p + `"; }</style>`) + if p.Username != "" { + send(`<style>#seat` + strconv.Itoa(i+1) + `:before { content: "` + p.Username + `"; }</style>`) + send(`<style>#seat` + strconv.Itoa(i+1) + `_cash:before { content: "` + strconv.Itoa(p.Cash) + `"; }</style>`) } else { send(`<style>#seat` + strconv.Itoa(i+1) + `:before { content: ""; }</style>`) + send(`<style>#seat` + strconv.Itoa(i+1) + `_cash:before { content: ""; }</style>`) } } } @@ -818,6 +840,12 @@ Loop: c.Response().Flush() continue + } else if evt, ok := payload.(PlayerBetEvent); ok { + html := `<style>#seat` + strconv.Itoa(evt.PlayerIdx+1) + `_cash:before { content: "` + strconv.Itoa(evt.Cash) + `"; }</style>` + send(html) + c.Response().Flush() + continue + } else if evt, ok := payload.(PlayerFoldEvent); ok { transition := `transition: 1s ease-in-out; transform: translateX(` + strconv.Itoa(BurnStackX) + `px) translateY(` + strconv.Itoa(BurnStackY) + `px) rotateY(` + BackfacingDeg + `);` html := `<style>#card` + strconv.Itoa(evt.Card1Idx) + ` { ` + transition + ` }