1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
package game
import (
"fmt"
"strconv"
"strings"
"thehouseoficarus/internal/casino"
"thehouseoficarus/internal/color"
)
// renderCasinoSettlement is shared by every game. Paint is supplied by the
// recipient's palette; widths are measured before adding ANSI sequences.
// The receipt fits the MUD's minimum 80-column wrap width. Long labels wrap
// rather than losing the hand number or wager identity through truncation.
func renderCasinoSettlement(lines []casino.SettlementLine, unicode bool, paint func(string, string) string) string {
if len(lines) == 0 {
return ""
}
dash := "—"
h, v, tl, tr, bl, br, tj, bj, lj, rj, cross := "─", "│", "╭", "╮", "╰", "╯", "┬", "┴", "├", "┤", "┼"
if !unicode {
dash = "-"
h, v, tl, tr, bl, br, tj, bj, lj, rj, cross = "-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"
}
// The Odds column is only informative when at least one wager quotes
// explicit odds. A board that prices every pocket the same way (Pachinko's
// "Varies") just repeats one word, so drop the column entirely.
showOdds := false
for _, line := range lines {
if line.Odds != "" && line.Odds != "Varies" {
showOdds = true
break
}
}
heading := []string{"Wager", "Staked", "Result", "Net"}
if showOdds {
heading = []string{"Wager", "Staked", "Odds", "Result", "Net"}
}
columns := len(heading)
stakedIdx, oddsIdx, resultIdx, netIdx := 1, -1, columns-2, columns-1
if showOdds {
oddsIdx = 2
}
type row struct {
cells []string
resultTag, netTag string
}
rows := make([]row, 0, len(lines))
widths := make([]int, columns)
for i, head := range heading {
widths[i] = color.VisibleLen(head)
}
var staked, returned, active int64
settled := 0
netTag := func(net int64) string {
if net > 0 {
return "casino_result_win"
}
if net < 0 {
return "casino_result_loss"
}
return "casino_result_push"
}
for _, line := range lines {
label, odds := line.Label, line.Odds
if label == "" {
label = "Wager"
}
if odds == "" {
odds = "Varies"
}
if !unicode {
label = strings.NewReplacer("·", "-", "—", "-").Replace(label)
odds = strings.ReplaceAll(odds, "—", "-")
}
net := int64(line.Returned) - int64(line.Stake)
result, rt := "LOSS", "casino_result_loss"
switch {
case strings.HasPrefix(line.Result, "win"), line.Result == "blackjack":
result, rt = "WIN", "casino_result_win"
case strings.HasPrefix(line.Result, "push"):
result, rt = "PUSH", "casino_result_push"
case line.Result == "insurance":
rt = netTag(net)
if net > 0 {
result = "WIN"
} else if net == 0 {
result = "PUSH"
}
}
nt, amount := netTag(net), fmt.Sprintf("%+d", net)
if net == 0 {
amount = "0"
}
if line.Unresolved {
result, rt, nt, amount = "UNRESOLVED", "warning", "warning", dash
active += int64(line.Stake)
} else {
staked += int64(line.Stake)
returned += int64(line.Returned)
settled++
}
cells := []string{label, strconv.Itoa(line.Stake), result, amount}
if showOdds {
cells = []string{label, strconv.Itoa(line.Stake), odds, result, amount}
}
r := row{cells, rt, nt}
rows = append(rows, r)
for i, s := range r.cells {
widths[i] = max(widths[i], color.VisibleLen(s))
}
}
widths[0] = min(widths[0], 24)
if showOdds {
widths[oddsIdx] = min(widths[oddsIdx], 10)
}
// The frame adds two padding spaces per cell, the internal separators
// and the two outer borders, so it costs 3*columns+1 characters.
others := 0
for i, w := range widths {
if i != 0 {
others += w
}
}
widths[0] = min(widths[0], 79-(3*columns+1)-others)
widths[0] = max(widths[0], 5)
// Grow the label column to reach the 50-wide minimum box (a readable footer
// without forcing tiny one-wager receipts wide).
sum := columns - 1
for _, w := range widths {
sum += w + 2
}
widths[0] += max(0, 50-sum)
inner := max(50, sum)
border := func(left, join, right string) string {
parts := make([]string, columns)
for i, w := range widths {
parts[i] = strings.Repeat(h, w+2)
}
return paint("dim", left+strings.Join(parts, join)+right)
}
pad := func(s string, w int, right bool) string {
spaces := strings.Repeat(" ", max(0, w-color.VisibleLen(s)))
if right {
return spaces + s
}
return s + spaces
}
formatRow := func(cells []string, rt, nt string) string {
parts := make([]string, len(cells))
for i, s := range cells {
tag := "casino_action"
if i == resultIdx {
tag = rt
}
if i == netIdx {
tag = nt
}
parts[i] = " " + paint(tag, pad(s, widths[i], i == stakedIdx || i == oddsIdx || i == netIdx)) + " "
}
return paint("dim", v) + strings.Join(parts, paint("dim", v)) + paint("dim", v)
}
out := []string{border(tl, tj, tr), formatRow(heading, "casino_action", "casino_action"), border(lj, cross, rj)}
for _, r := range rows {
// Wrapping uses runes, matching the existing MUD visible-width contract.
remaining := r.cells
for {
cells := make([]string, len(remaining))
more := false
for i, s := range remaining {
runes := []rune(strings.Join(strings.Fields(s), " "))
n := min(len(runes), widths[i])
cells[i] = string(runes[:n])
remaining[i] = string(runes[n:])
more = more || len(runes) > n
}
out = append(out, formatRow(cells, r.resultTag, r.netTag))
if !more {
break
}
}
}
if settled == 0 {
// Nothing settled yet: the whole totals footer is noise, so end the
// receipt at the wager rows with the flat bottom border.
out = append(out, paint("dim", bl+strings.Repeat(h, inner)+br))
return strings.Join(out, "\n")
}
out = append(out, border(lj, bj, rj))
footer := func(text, tag string) {
out = append(out, paint("dim", v)+" "+paint(tag, pad(text, inner-2, false))+" "+paint("dim", v))
}
summary := fmt.Sprintf("Staked: %d Returned: %d", staked, returned)
if active > 0 {
summary = fmt.Sprintf("Settled: %d Returned: %d Active: %d", staked, returned, active)
}
// Large multi-hand totals can exceed one footer line; never wrap the frame.
for color.VisibleLen(summary) > inner-2 {
cut := strings.LastIndex(summary[:inner-2], " ")
if cut < 0 {
cut = inner - 2
}
footer(summary[:cut], "casino_action")
summary = strings.TrimSpace(summary[cut:])
}
footer(summary, "casino_action")
net := returned - staked
title, amount := "NET EVEN", "0 credits"
if net > 0 {
title, amount = "NET PROFIT", fmt.Sprintf("+%d credits", net)
}
if net < 0 {
title, amount = "NET LOSS", fmt.Sprintf("%d credits", net)
}
tag := netTag(net)
footer(title+strings.Repeat(" ", max(1, inner-2-len(title)-color.VisibleLen(amount)))+amount, tag)
out = append(out, paint("dim", bl+strings.Repeat(h, inner)+br))
return strings.Join(out, "\n")
}
|