commit 92fad7c955333e2ecdf12b8239c77a3563746afa
parent 5395d3e73983afc9e0b01b652d7f202a1407c280
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sat, 16 Dec 2023 15:18:08 -0500
implement buy-in
Diffstat:
3 files changed, 41 insertions(+), 14 deletions(-)
diff --git a/pkg/database/tablePokerTables.go b/pkg/database/tablePokerTables.go
@@ -56,6 +56,8 @@ func (p PokerChip) ToPiconero() Piconero {
func (p PokerChip) String() string { return humanize.Comma(int64(p)) }
+func (p PokerChip) Raw() uint64 { return uint64(p) }
+
type PokerTableAccount struct {
ID int64
UserID UserID
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -1453,6 +1453,7 @@ func PokerSitHandler(c echo.Context) error {
if c.Request().Method == http.MethodPost {
pokerTable, err := db.GetPokerTableBySlug(roomID.String())
tableMinBuyIn := pokerTable.MinBuyIn
+ tableMaxBuyIn := pokerTable.MaxBuyIn
tableID := pokerTable.ID
if err != nil {
return c.HTML(http.StatusOK, html)
@@ -1462,6 +1463,15 @@ func PokerSitHandler(c echo.Context) error {
logrus.Error(err)
return c.HTML(http.StatusOK, html)
}
+ playerBuyIn := database.PokerChip(utils.DoParseUint64(c.QueryParam("buy-in")))
+ if playerBuyIn < tableMinBuyIn {
+ PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: fmt.Sprintf("buy-in too low; min buy in: %d", tableMinBuyIn)})
+ return c.HTML(http.StatusOK, html)
+ }
+ if playerBuyIn > tableMaxBuyIn {
+ PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: fmt.Sprintf("buy-in too high; max buy in: %d", tableMaxBuyIn)})
+ return c.HTML(http.StatusOK, html)
+ }
var userChips database.PokerChip
if pokerTable.IsTest {
userChips = authUser.ChipsTest
@@ -1469,11 +1479,11 @@ func PokerSitHandler(c echo.Context) error {
userChips = authUser.XmrBalance.ToPokerChip()
}
totalChips := userChips + tableAccount.Amount
- if totalChips < tableMinBuyIn {
- PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: fmt.Sprintf("Not enough chips to sit; have: %d; needs: %d", totalChips, tableMinBuyIn)})
+ if totalChips < playerBuyIn {
+ PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: fmt.Sprintf("Not enough chips for the desired buy-in; want: %d; have: %d", playerBuyIn, totalChips)})
return c.HTML(http.StatusOK, html)
}
- needed := tableMinBuyIn - tableAccount.Amount
+ needed := playerBuyIn - tableAccount.Amount
if pokerTable.IsTest {
authUser.ChipsTest -= needed
} else {
@@ -1559,7 +1569,7 @@ func buildPayloadHtml(g *PokerGame, authUser *database.User, payload any) (html
return
}
-func buildBaseHtml(g *PokerGame, authUser *database.User, roomID RoomID) (html string) {
+func buildBaseHtml(g *PokerGame, authUser *database.User, roomID RoomID, playerBuyIn database.PokerChip) (html string) {
html += hutils.HtmlCssReset
html += pokerCss
//html += `<script>document.onclick = function(e) { console.log(e.x, e.y); };</script>` // TODO: dev only
@@ -1568,7 +1578,7 @@ func buildBaseHtml(g *PokerGame, authUser *database.User, roomID RoomID) (html s
//html += buildDevHtml()
html += buildHelpHtml()
html += buildCardsHtml()
- html += buildSeatsHtml(g, authUser, roomID)
+ html += buildSeatsHtml(g, authUser, roomID, playerBuyIn)
html += buildActionsDiv(roomID)
html += buildDealerTokenHtml(g)
html += `<iframe src="/poker/` + roomID.String() + `/logs" id="eventLogs"></iframe>`
@@ -1742,7 +1752,7 @@ func buildActionsDiv(roomID RoomID) (html string) {
return
}
-func buildSeatsHtml(g *PokerGame, authUser *database.User, roomID RoomID) (html string) {
+func buildSeatsHtml(g *PokerGame, authUser *database.User, roomID RoomID, playerBuyIn database.PokerChip) (html string) {
g.PlayersMtx.RLock()
defer g.PlayersMtx.RUnlock()
for i := range g.Players {
@@ -1751,7 +1761,7 @@ func buildSeatsHtml(g *PokerGame, authUser *database.User, roomID RoomID) (html
html += `<div>`
for i := range g.Players {
html += `<div class="seat" id="seat` + itoa(i+1) + `">
-<iframe src="/poker/` + roomID.String() + `/sit/` + itoa(i+1) + `" class="takeSeat takeSeat` + itoa(i+1) + `"></iframe>`
+<iframe src="/poker/` + roomID.String() + `/sit/` + itoa(i+1) + `?buy-in=` + itoa2(playerBuyIn) + `" class="takeSeat takeSeat` + itoa(i+1) + `"></iframe>`
html += ` <div class="inner"></div>`
html += ` <div id="seat` + itoa(i+1) + `_cash" class="cash"></div>`
html += `</div>`
@@ -2243,7 +2253,9 @@ func PokerHandler(c echo.Context) error {
sub := PokerPubSub.Subscribe([]string{roomTopic, roomUserTopic})
defer sub.Close()
- send(buildBaseHtml(g, authUser, roomID))
+ playerBuyIn := database.PokerChip(utils.DoParseUint64(c.QueryParam("buy-in")))
+
+ send(buildBaseHtml(g, authUser, roomID, playerBuyIn))
c.Response().Flush()
Loop:
diff --git a/pkg/web/public/views/pages/poker.gohtml b/pkg/web/public/views/pages/poker.gohtml
@@ -59,17 +59,30 @@
<th>Min/max buy-in (chips)</th>
<th class="text-center">Big Blind</th>
<th class="text-center">Type</th>
+ <th class="text-right">Join</th>
</tr>
{{ range .Data.Tables }}
<tr>
- <td><a href="/poker/{{ .Slug }}">{{ .Name }}</a></td>
- <td class="text-center">{{ .NbSeated }}/6</td>
- <td>{{ .MinBuyIn }} - {{ .MaxBuyIn }}</td>
- <td class="text-center">{{ .MinBet }}</td>
- <td class="text-center">{{ if .IsTest }}FREE{{ else }}XMR{{ end }}</td>
+ <td class="align-middle">
+ {{ .Name }}
+ </td>
+ <td class="text-center align-middle">{{ .NbSeated }}/6</td>
+ <td class="align-middle">{{ .MinBuyIn }} - {{ .MaxBuyIn }}</td>
+ <td class="text-center align-middle">{{ .MinBet }}</td>
+ <td class="text-center align-middle">{{ if .IsTest }}FREE{{ else }}XMR{{ end }}</td>
+ <td class="text-right">
+ <form method="get" action="/poker/{{ .Slug }}" class="d-inline-block">
+ <div class="input-group">
+ <input type="number" min="{{ .MinBuyIn.Raw }}" max="{{ .MaxBuyIn.Raw }}" name="buy-in" value="{{ .MinBuyIn.Raw }}" class="form-control form-control-sm" style="width: 100px;" />
+ <div class="input-group-append">
+ <button class="btn btn-primary btn-sm">Join</button>
+ </div>
+ </div>
+ </form>
+ </td>
</tr>
{{ else }}
- <tr><td colspan="5"><em>No table to show</em></td></tr>
+ <tr><td colspan="6"><em>No table to show</em></td></tr>
{{ end }}
</table>