dkforest

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

commit 34cd39b25133e1620d7a501fa7b3a00a60bd0d82
parent 8f05989d0c4442f140e9c1cc48ba3bf022df8f51
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed,  6 Dec 2023 04:36:25 -0500

improve logs

Diffstat:
Mpkg/web/handlers/poker/poker.go | 41+++++++++++++++++++++++++++++++++++------
1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -76,6 +76,7 @@ type Ongoing struct { Deck []string Players []*PokerPlayer Events []PokerEvent + LogEvents []LogEvent CommunityCards []string WaitTurnEvent PokerWaitTurnEvent MainPot int @@ -240,14 +241,20 @@ OUTER: g.Ongoing.Events = append(g.Ongoing.Events, evt1, evt2) //PokerPubSub.Pub(roomTopic, PlayerFoldEvent{Card1Idx: player.Cards[0].Idx, Card2Idx: player.Cards[1].Idx}) - PokerPubSub.Pub(roomLogsTopic, LogEvent{Message: fmt.Sprintf("%s auto fold", p.Username)}) + + logEvt := LogEvent{Message: fmt.Sprintf("%s auto fold", p.Username)} + PokerPubSub.Pub(roomLogsTopic, logEvt) + g.Ongoing.LogEvents = append(g.Ongoing.LogEvents, logEvt) + playerAlive-- if playerAlive == 1 { break OUTER } break LOOP } - PokerPubSub.Pub(roomLogsTopic, LogEvent{Message: fmt.Sprintf("%s auto check", p.Username)}) + logEvt := LogEvent{Message: fmt.Sprintf("%s auto check", p.Username)} + PokerPubSub.Pub(roomLogsTopic, logEvt) + g.Ongoing.LogEvents = append(g.Ongoing.LogEvents, logEvt) break LOOP } @@ -264,7 +271,11 @@ OUTER: g.Ongoing.Events = append(g.Ongoing.Events, evt1, evt2) //PokerPubSub.Pub(roomTopic, PlayerFoldEvent{Card1Idx: player.Cards[0].Idx, Card2Idx: player.Cards[1].Idx}) - PokerPubSub.Pub(roomLogsTopic, LogEvent{Message: fmt.Sprintf("%s fold", p.Username)}) + + logEvt := LogEvent{Message: fmt.Sprintf("%s fold", p.Username)} + PokerPubSub.Pub(roomLogsTopic, logEvt) + g.Ongoing.LogEvents = append(g.Ongoing.LogEvents, logEvt) + playerAlive-- if playerAlive == 1 { break OUTER @@ -275,7 +286,11 @@ OUTER: PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) continue } - PokerPubSub.Pub(roomLogsTopic, LogEvent{Message: fmt.Sprintf("%s check", p.Username)}) + + logEvt := LogEvent{Message: fmt.Sprintf("%s check", p.Username)} + PokerPubSub.Pub(roomLogsTopic, logEvt) + g.Ongoing.LogEvents = append(g.Ongoing.LogEvents, logEvt) + } else if evt.Call { bet := minBet - p.Bet if p.Cash < bet { @@ -288,7 +303,11 @@ OUTER: p.Cash -= bet } PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerIdx: i, Player: p.Username, Bet: bet, TotalBet: p.Bet, Cash: p.Cash}) - PokerPubSub.Pub(roomLogsTopic, LogEvent{Message: fmt.Sprintf("%s call", p.Username)}) + + logEvt := LogEvent{Message: fmt.Sprintf("%s call", p.Username)} + PokerPubSub.Pub(roomLogsTopic, logEvt) + g.Ongoing.LogEvents = append(g.Ongoing.LogEvents, logEvt) + } else if evt.Bet > 0 { if (p.Bet + evt.Bet) < minBet { msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", evt.Bet, minBet-p.Bet) @@ -302,7 +321,11 @@ OUTER: p.Bet += evt.Bet p.Cash -= evt.Bet PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerIdx: i, Player: p.Username, Bet: evt.Bet, TotalBet: p.Bet, Cash: p.Cash}) - PokerPubSub.Pub(roomLogsTopic, LogEvent{Message: fmt.Sprintf("%s bet %d", p.Username, evt.Bet)}) + + logEvt := LogEvent{Message: fmt.Sprintf("%s bet %d", p.Username, evt.Bet)} + PokerPubSub.Pub(roomLogsTopic, logEvt) + g.Ongoing.LogEvents = append(g.Ongoing.LogEvents, logEvt) + } else { continue } @@ -1167,6 +1190,7 @@ func PokerLogsHandler(c echo.Context) error { send := func(s string) { _, _ = c.Response().Write([]byte(s)) } + g := PokerInstance.GetOrCreateGame(roomID) roomLogsTopic := "room_" + roomID + "_logs" sub := PokerPubSub.Subscribe([]string{roomLogsTopic}) defer sub.Close() @@ -1178,6 +1202,11 @@ func PokerLogsHandler(c echo.Context) error { send(cssReset) send(`<style>body { background-color: #ccc; }</style><div style="display:flex;flex-direction:column-reverse;">`) + if g.Ongoing != nil { + for _, evt := range g.Ongoing.LogEvents { + send(fmt.Sprintf(`<div>%s</div>`, evt.Message)) + } + } c.Response().Flush() Loop: