commit 81a8173045b5c4d563d4f815bca7b523874afeb8
parent 40fb3c7680fb3670230906d4343209675a9e70c6
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Mon, 4 Dec 2023 22:15:32 -0500
countdown
Diffstat:
1 file changed, 40 insertions(+), 20 deletions(-)
diff --git a/pkg/web/handlers/poker.go b/pkg/web/handlers/poker.go
@@ -15,7 +15,7 @@ import (
"time"
)
-const NbPlayers = 10
+const NbPlayers = 6
type Poker struct {
sync.Mutex
@@ -54,10 +54,15 @@ var PokerInstance = NewPoker()
type Ongoing struct {
Deck []string
- Players []string
+ Players []PokerPlayer
Events []PokerEvent
}
+type PokerPlayer struct {
+ Username string
+ Bet int
+}
+
type PokerGame struct {
sync.Mutex
PlayersEventCh chan PlayerEvent
@@ -83,18 +88,20 @@ func (g *PokerGame) Deal(roomID string) {
}
utils.Shuffle(deck)
- players := make([]string, NbPlayers)
+ players := make([]PokerPlayer, NbPlayers)
for idx := range g.Players {
- players[idx] = g.Players[idx]
+ players[idx] = PokerPlayer{Username: g.Players[idx]}
}
g.Ongoing = &Ongoing{Deck: deck, Players: players}
go func() {
+ myTopic := "room_" + roomID
waitPlayersActionFn := func() {
- for _, p := range g.Ongoing.Players {
- if p != "" {
+ for i, p := range g.Ongoing.Players {
+ if p.Username != "" {
+ PokerPubSub.Pub(myTopic, PokerWaitTurnEvent{Idx: i})
fmt.Println("WAIT FOR ACTION", p)
waitCh := time.After(10 * time.Second)
LOOP:
@@ -106,17 +113,18 @@ func (g *PokerGame) Deal(roomID string) {
fmt.Println("WAIT TOO LONG, either auto check, or fold")
break LOOP
}
- if evt.Player != p {
+ if evt.Player != p.Username {
continue
}
fmt.Println("GOT FROM", evt.Player, evt)
+ g.Ongoing.Players[i].Bet += evt.Bet
break
}
}
}
+ PokerPubSub.Pub(myTopic, PokerWaitTurnEvent{Idx: -1})
}
- myTopic := "room_" + roomID
type Seat struct {
Top int
Left int
@@ -128,13 +136,6 @@ func (g *PokerGame) Deal(roomID string) {
{Top: 50, Left: 600, Top2: 50 + 5, Left2: 600 + 5, Angle: "-90deg"},
{Top: 150, Left: 574, Top2: 150 + 5, Left2: 574 + 3, Angle: "-80deg"},
{Top: 250, Left: 530, Top2: 250 + 5, Left2: 530 + 1, Angle: "-70deg"},
- //{Top: 150, Left: 550},
- //{Top: 150, Left: 550},
- //{Top: 150, Left: 550},
- //{Top: 150, Left: 550},
- //{Top: 150, Left: 550},
- //{Top: 150, Left: 550},
- //{Top: 150, Left: 550},
}
var card string
@@ -172,7 +173,7 @@ func (g *PokerGame) Deal(roomID string) {
// Deal cards
for j := 1; j <= 2; j++ {
for i, p := range g.Ongoing.Players {
- if p == "" {
+ if p.Username == "" {
continue
}
d := seats[i]
@@ -201,6 +202,7 @@ func (g *PokerGame) Deal(roomID string) {
}
// Wait for players to bet/call/check/fold...
+ time.Sleep(time.Second)
waitPlayersActionFn()
// Burn
@@ -214,6 +216,7 @@ func (g *PokerGame) Deal(roomID string) {
}
// Wait for players to bet/call/check/fold...
+ time.Sleep(time.Second)
waitPlayersActionFn()
// Burn
@@ -225,6 +228,7 @@ func (g *PokerGame) Deal(roomID string) {
dealCard(4)
// Wait for players to bet/call/check/fold...
+ time.Sleep(time.Second)
waitPlayersActionFn()
// Burn
@@ -236,6 +240,7 @@ func (g *PokerGame) Deal(roomID string) {
dealCard(5)
// Wait for players to bet/call/check/fold...
+ time.Sleep(time.Second)
waitPlayersActionFn()
// Wait a minimum of X seconds before allowing a new game
@@ -275,6 +280,10 @@ type PokerEvent struct {
Angle string
}
+type PokerWaitTurnEvent struct {
+ Idx int
+}
+
type PokerSeatTakenEvent struct {
}
@@ -549,16 +558,13 @@ body {
.takeSeat4 { position: absolute; top: 300px; left: 550px; }
.takeSeat5 { position: absolute; top: 300px; left: 500px; }
.takeSeat6 { position: absolute; top: 300px; left: 450px; }
-.takeSeat7 { position: absolute; top: 300px; left: 400px; }
-.takeSeat8 { position: absolute; top: 300px; left: 350px; }
-.takeSeat9 { position: absolute; top: 300px; left: 300px; }
-.takeSeat10 { position: absolute; top: 300px; left: 250px; }
#dealBtn { position: absolute; top: 400px; left: 50px; width: 80px; height: 30px; }
#unSitBtn { position: absolute; top: 430px; left: 50px; width: 80px; height: 30px; }
#checkBtn { width: 60px; height: 30px; }
#foldBtn { width: 50px; height: 30px; }
#callBtn { width: 50px; height: 30px; }
#betBtn { width: 145px; height: 30px; }
+#countdown { position: absolute; display: none; z-index: 100; }
</style>`)
drawSeats := func() {
@@ -622,6 +628,7 @@ body {
actions += `<iframe src="/poker/` + roomID + `/unsit" id="unSitBtn"></iframe>`
send(actions)
deckHash := deckSha256
+ send(`<div id="countdown">CD</div>`)
send(`<div>` + deckStr + `</div>`)
send(`<div>` + deckHash + `</div>`)
if g.Ongoing != nil {
@@ -656,6 +663,19 @@ Loop:
c.Response().Flush()
continue
+ } else if evt, ok := payload.(PokerWaitTurnEvent); ok {
+ if evt.Idx == -1 {
+ send(`<style>#countdown { display: none; }</style>`)
+ } else if evt.Idx == 0 {
+ send(`<style>#countdown { top: 50px; left: 600px; display: block; }</style>`)
+ } else if evt.Idx == 1 {
+ send(`<style>#countdown { top: 150px; left: 574px; display: block; }</style>`)
+ } else if evt.Idx == 2 {
+ send(`<style>#countdown { top: 250px; left: 530px; display: block; }</style>`)
+ }
+ c.Response().Flush()
+ continue
+
} else if payload, ok := payload.(PokerEvent); ok {
send(getPokerEventHtml(payload, "1s"))
c.Response().Flush()