render.gno
10.54 Kb · 353 lines
1package poker
2
3import (
4 "chain/runtime"
5 "strconv"
6 "strings"
7
8 "gno.land/p/g1lnkytfqcjwllws63gvf0mv9yt04aswy4y9amhm/poker/cards"
9 "gno.land/p/g1lnkytfqcjwllws63gvf0mv9yt04aswy4y9amhm/poker/holdem"
10)
11
12func i64s(v int64) string { return "\"" + strconv.FormatInt(v, 10) + "\"" }
13func itoa(v int) string { return strconv.Itoa(v) }
14func qs(s string) string { return "\"" + s + "\"" }
15
16func bools(b bool) string {
17 if b {
18 return "true"
19 }
20 return "false"
21}
22
23func intList(xs []int) string {
24 var b strings.Builder
25 b.WriteByte('[')
26 for i, x := range xs {
27 if i > 0 {
28 b.WriteByte(',')
29 }
30 b.WriteString(itoa(x))
31 }
32 b.WriteByte(']')
33 return b.String()
34}
35
36func (t *Table) summaryJSON(b *strings.Builder) {
37 b.WriteString("{\"id\":" + strconv.FormatUint(t.ID, 10))
38 b.WriteString(",\"smallBlind\":" + i64s(t.SmallBlind))
39 b.WriteString(",\"bigBlind\":" + i64s(t.BigBlind))
40 b.WriteString(",\"minBuyIn\":" + i64s(t.MinBuyIn))
41 b.WriteString(",\"maxBuyIn\":" + i64s(t.MaxBuyIn))
42 b.WriteString(",\"maxPlayers\":" + itoa(t.MaxPlayers))
43 b.WriteString(",\"actionTimeoutBlocks\":" + strconv.FormatInt(t.ActionTimeout, 10))
44 b.WriteString(",\"challengeWindowBlocks\":" + strconv.FormatInt(t.ChallengeWindow, 10))
45 b.WriteString(",\"challengeBond\":" + i64s(t.ChallengeBond))
46 b.WriteString(",\"button\":" + itoa(t.Button))
47 b.WriteString(",\"handNo\":" + strconv.FormatUint(t.HandNo, 10))
48 seated := 0
49 for _, s := range t.Seats {
50 if s != nil {
51 seated++
52 }
53 }
54 b.WriteString(",\"seated\":" + itoa(seated))
55 phase := ""
56 if t.Hand != nil {
57 phase = phaseNames[t.Hand.Phase]
58 }
59 b.WriteString(",\"phase\":" + qs(phase) + "}")
60}
61
62// ListTables returns a JSON array of table summaries.
63func ListTables() string {
64 var b strings.Builder
65 b.WriteString("[")
66 for i, t := range tables {
67 if i > 0 {
68 b.WriteString(",")
69 }
70 t.summaryJSON(&b)
71 }
72 b.WriteString("]")
73 return b.String()
74}
75
76// GetTable returns the full table and hand state as JSON.
77func GetTable(tableID uint64) string {
78 t := getTable(tableID)
79 var b strings.Builder
80 b.WriteString("{\"id\":" + strconv.FormatUint(t.ID, 10))
81 b.WriteString(",\"smallBlind\":" + i64s(t.SmallBlind))
82 b.WriteString(",\"bigBlind\":" + i64s(t.BigBlind))
83 b.WriteString(",\"minBuyIn\":" + i64s(t.MinBuyIn))
84 b.WriteString(",\"maxBuyIn\":" + i64s(t.MaxBuyIn))
85 b.WriteString(",\"maxPlayers\":" + itoa(t.MaxPlayers))
86 b.WriteString(",\"actionTimeoutBlocks\":" + strconv.FormatInt(t.ActionTimeout, 10))
87 b.WriteString(",\"challengeWindowBlocks\":" + strconv.FormatInt(t.ChallengeWindow, 10))
88 b.WriteString(",\"challengeBond\":" + i64s(t.ChallengeBond))
89 b.WriteString(",\"height\":" + strconv.FormatInt(runtime.ChainHeight(), 10))
90 b.WriteString(",\"button\":" + itoa(t.Button))
91 b.WriteString(",\"handNo\":" + strconv.FormatUint(t.HandNo, 10))
92 b.WriteString(",\"seats\":[")
93 for i, s := range t.Seats {
94 if i > 0 {
95 b.WriteString(",")
96 }
97 if s == nil {
98 b.WriteString("null")
99 continue
100 }
101 b.WriteString("{\"seat\":" + itoa(i))
102 b.WriteString(",\"owner\":" + qs(s.Owner.String()))
103 b.WriteString(",\"session\":" + qs(s.Session.String()))
104 b.WriteString(",\"stack\":" + i64s(s.Stack))
105 b.WriteString(",\"sittingOut\":" + bools(s.SittingOut))
106 b.WriteString(",\"leaving\":" + bools(s.Leaving))
107 b.WriteString(",\"waiting\":" + bools(s.Waiting) + "}")
108 }
109 b.WriteString("],\"hand\":")
110 if t.Hand == nil {
111 b.WriteString("null}")
112 return b.String()
113 }
114 t.Hand.writeJSON(&b)
115 b.WriteString("}")
116 return b.String()
117}
118
119func (h *Hand) writeJSON(b *strings.Builder) {
120 n := h.n()
121 b.WriteString("{\"handNo\":" + strconv.FormatUint(h.No, 10))
122 b.WriteString(",\"phase\":" + qs(phaseNames[h.Phase]))
123 b.WriteString(",\"players\":[")
124 for j, p := range h.Players {
125 bp := h.Bet.Players[j]
126 if j > 0 {
127 b.WriteString(",")
128 }
129 b.WriteString("{\"seat\":" + itoa(p.Seat))
130 b.WriteString(",\"owner\":" + qs(p.Owner.String()))
131 b.WriteString(",\"commit\":" + qs(p.Commit))
132 b.WriteString(",\"contributed\":" + i64s(bp.Contributed))
133 b.WriteString(",\"streetBet\":" + i64s(bp.StreetBet))
134 b.WriteString(",\"folded\":" + bools(bp.Folded))
135 b.WriteString(",\"allIn\":" + bools(bp.AllIn))
136 b.WriteString(",\"acted\":" + bools(bp.Acted))
137 canRaise := isBettingPhase(h.Phase) && !bp.Folded && !bp.AllIn && h.Bet.CanRaise(j)
138 b.WriteString(",\"canRaise\":" + bools(canRaise))
139 b.WriteString(",\"revealed\":" + bools(p.Revealed))
140 b.WriteString(",\"accepted\":" + bools(p.Accepted))
141 b.WriteString(",\"seed\":" + qs(p.Seed))
142 b.WriteString(",\"keys\":{")
143 first := true
144 for pos := 0; pos < cards.DeckSize; pos++ {
145 k, ok := p.Keys[pos]
146 if !ok {
147 continue
148 }
149 if !first {
150 b.WriteString(",")
151 }
152 first = false
153 b.WriteString(qs(itoa(pos)) + ":" + qs(k))
154 }
155 b.WriteString("}}")
156 }
157 b.WriteString("]")
158 b.WriteString(",\"turn\":" + itoa(h.turnIndex()))
159 owing := []int{}
160 if isDealPhase(h.Phase) {
161 for j, o := range h.Owing {
162 if o {
163 owing = append(owing, j)
164 }
165 }
166 } else if h.Phase == phReveal {
167 for j, p := range h.Players {
168 if !p.Revealed {
169 owing = append(owing, j)
170 }
171 }
172 }
173 b.WriteString(",\"owing\":" + intList(owing))
174 b.WriteString(",\"deadline\":" + strconv.FormatInt(h.Deadline, 10))
175 b.WriteString(",\"pot\":" + i64s(h.Bet.Pot()))
176 b.WriteString(",\"currentBet\":" + i64s(h.Bet.CurrentBet))
177 b.WriteString(",\"minRaise\":" + i64s(h.Bet.MinRaise))
178 b.WriteString(",\"deckSeq\":" + itoa(len(h.Roots)))
179 b.WriteString(",\"roots\":[")
180 for i, r := range h.Roots {
181 if i > 0 {
182 b.WriteString(",")
183 }
184 b.WriteString(qs(r))
185 }
186 b.WriteString("]")
187 if h.Cards != nil {
188 b.WriteString(",\"board\":" + intList(h.Cards[2*n:2*n+5]))
189 b.WriteString(",\"cards\":{")
190 for i, c := range h.Cards {
191 if i > 0 {
192 b.WriteString(",")
193 }
194 b.WriteString(qs(itoa(i)) + ":" + itoa(c))
195 }
196 b.WriteString("}")
197 } else {
198 b.WriteString(",\"board\":[],\"cards\":{}")
199 }
200 b.WriteString(",\"result\":")
201 if h.Result == nil {
202 b.WriteString("null}")
203 return
204 }
205 r := h.Result
206 b.WriteString("{\"aborted\":" + bools(r.Aborted))
207 b.WriteString(",\"convicted\":" + intList(r.Convicted))
208 b.WriteString(",\"stallers\":" + intList(r.Stallers))
209 b.WriteString(",\"payouts\":{")
210 for j, c := range r.Credits {
211 if j > 0 {
212 b.WriteString(",")
213 }
214 b.WriteString(qs(itoa(j)) + ":" + i64s(c))
215 }
216 b.WriteString("},\"hands\":{")
217 first := true
218 for j, s := range r.Scores {
219 if s < 0 {
220 continue
221 }
222 if !first {
223 b.WriteString(",")
224 }
225 first = false
226 b.WriteString(qs(itoa(j)) + ":" + qs(holdem.CategoryName(s)))
227 }
228 b.WriteString("},\"challenges\":[")
229 for i, c := range r.Challenges {
230 if i > 0 {
231 b.WriteString(",")
232 }
233 b.WriteString("{\"challenger\":" + itoa(c.Challenger) + ",\"accused\":" + itoa(c.Accused) +
234 ",\"kind\":" + qs(c.Kind) + ",\"index\":" + itoa(c.Index) + ",\"guilty\":" + bools(c.Guilty) + "}")
235 }
236 b.WriteString("]}}")
237}
238
239// GetDeck returns the latest deck pass of the current hand as standard base64,
240// or "" before the first pass.
241func GetDeck(tableID uint64) string {
242 t := getTable(tableID)
243 if t.Hand == nil || t.DeckHand != t.HandNo || len(t.Hand.Roots) == 0 {
244 return ""
245 }
246 return cards.EncodeDeck([]byte(t.Deck))
247}
248
249// renderMaxTables is the most tables the list page shows.
250const renderMaxTables = 50
251
252// Render shows the newest renderMaxTables tables, or one table at path "<id>".
253func Render(path string) string {
254 if path == "" {
255 var b strings.Builder
256 b.WriteString("# Poker tables\n\n")
257 if len(tables) == 0 {
258 b.WriteString("No tables yet.\n")
259 return b.String()
260 }
261 b.WriteString("| Table | Blinds | Buy-in | Seated | Hand | Phase |\n|---|---|---|---|---|---|\n")
262 first := 0
263 if len(tables) > renderMaxTables {
264 first = len(tables) - renderMaxTables
265 }
266 for k := len(tables) - 1; k >= first; k-- {
267 t := tables[k]
268 seated := 0
269 for i := 0; i < len(t.Seats) && i < maxSeats; i++ {
270 if t.Seats[i] != nil {
271 seated++
272 }
273 }
274 phase := "-"
275 if t.Hand != nil {
276 phase = phaseNames[t.Hand.Phase]
277 }
278 id := strconv.FormatUint(t.ID, 10)
279 b.WriteString("| [" + id + "](:" + id + ") | " + strconv.FormatInt(t.SmallBlind, 10) + "/" + strconv.FormatInt(t.BigBlind, 10) +
280 " | " + strconv.FormatInt(t.MinBuyIn, 10) + "-" + strconv.FormatInt(t.MaxBuyIn, 10) +
281 " | " + itoa(seated) + "/" + itoa(t.MaxPlayers) + " | " + strconv.FormatUint(t.HandNo, 10) + " | " + phase + " |\n")
282 }
283 if first > 0 {
284 b.WriteString("\nShowing the newest " + itoa(renderMaxTables) + " of " + itoa(len(tables)) + " tables. Older tables are at `:<id>`.\n")
285 }
286 return b.String()
287 }
288 id, err := strconv.ParseUint(path, 10, 64)
289 if err != nil || id == 0 || id > uint64(len(tables)) {
290 return "# Not found\n"
291 }
292 t := tables[id-1]
293 var b strings.Builder
294 b.WriteString("# Table " + path + "\n\n")
295 b.WriteString("Blinds " + strconv.FormatInt(t.SmallBlind, 10) + "/" + strconv.FormatInt(t.BigBlind, 10) + " ugnot, hand " + strconv.FormatUint(t.HandNo, 10) + "\n\n")
296 b.WriteString("| Seat | Player | Stack | Status |\n|---|---|---|---|\n")
297 for i := 0; i < len(t.Seats) && i < maxSeats; i++ {
298 s := t.Seats[i]
299 if s == nil {
300 b.WriteString("| " + itoa(i) + " | (empty) | | |\n")
301 continue
302 }
303 status := []string{}
304 if i == t.Button {
305 status = append(status, "button")
306 }
307 if s.SittingOut {
308 status = append(status, "sitting out")
309 }
310 if s.Leaving {
311 status = append(status, "leaving")
312 }
313 if s.Waiting {
314 status = append(status, "waiting")
315 }
316 b.WriteString("| " + itoa(i) + " | " + s.Owner.String() + " | " + strconv.FormatInt(s.Stack, 10) + " | " + strings.Join(status, ", ") + " |\n")
317 }
318 h := t.Hand
319 if h == nil {
320 return b.String()
321 }
322 n := h.n()
323 b.WriteString("\n## Hand " + strconv.FormatUint(h.No, 10) + ": " + phaseNames[h.Phase] + "\n\n")
324 b.WriteString("Pot " + strconv.FormatInt(h.Bet.Pot(), 10) + " ugnot, deadline block " + strconv.FormatInt(h.Deadline, 10) + "\n\n")
325 if h.Cards != nil {
326 board := []string{}
327 for k := 0; k < 5; k++ {
328 board = append(board, cards.CardString(h.Cards[2*n+k]))
329 }
330 b.WriteString("Board: " + strings.Join(board, " ") + "\n\n")
331 }
332 b.WriteString("| # | Seat | Bet | Status | Cards | Result |\n|---|---|---|---|---|---|\n")
333 for j := 0; j < n && j < maxSeats; j++ {
334 p := h.Players[j]
335 bp := h.Bet.Players[j]
336 status := ""
337 if bp.Folded {
338 status = "folded"
339 } else if bp.AllIn {
340 status = "all-in"
341 }
342 hole := ""
343 if h.Cards != nil {
344 hole = cards.CardString(h.Cards[2*j]) + " " + cards.CardString(h.Cards[2*j+1])
345 }
346 res := ""
347 if h.Result != nil && j < len(h.Result.Credits) {
348 res = "+" + strconv.FormatInt(h.Result.Credits[j], 10)
349 }
350 b.WriteString("| " + itoa(j) + " | " + itoa(p.Seat) + " | " + strconv.FormatInt(bp.Contributed, 10) + " | " + status + " | " + hole + " | " + res + " |\n")
351 }
352 return b.String()
353}