aboutsummaryrefslogtreecommitdiff
path: root/internal/game/render_video_poker.go
blob: 045bdc7964ebc89015917f03c54e76fbce525151 (plain)
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
package game

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/cardart"
	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/color"
)

var (
	pokerBlue   = color.NearestXterm256(0, 2, 105)
	pokerRed    = color.NearestXterm256(254, 0, 0)
	pokerYellow = color.NearestXterm256(247, 231, 14)
)

func pokerInk(mode, text string, active bool) string {
	bg := pokerBlue
	if active {
		bg = pokerRed
	}
	return color.Render(mode, color.ColorSpec{Fg: pokerYellow, Bg: bg, Bold: true}, text)
}
func renderPokerPayTable(s *casino.VideoPokerSnapshot, mode string) string {
	rows := s.Config.PayRows(s.Hands)
	if len(rows) == 0 {
		return ""
	}
	nameWidth, coinWidth := len("COINS PER HAND")+3, 6
	for _, row := range rows {
		nameWidth = max(nameWidth, color.VisibleLen(row.Name)+3)
		for _, n := range row.Coins {
			coinWidth = max(coinWidth, len(fmt.Sprint(n))+2)
		}
	}
	hasNext := s.Config.Feature == casino.PokerUltimateX
	widths := []int{nameWidth, coinWidth, coinWidth, coinWidth, coinWidth, coinWidth}
	if hasNext {
		widths = append(widths, 8)
	}
	border := func(left, middle, right string) string {
		parts := make([]string, len(widths))
		for i, w := range widths {
			parts[i] = strings.Repeat("─", w)
		}
		return pokerInk(mode, left+strings.Join(parts, middle)+right, false)
	}
	row := func(cells []string, leaders bool) string {
		var b strings.Builder
		b.WriteString(pokerInk(mode, "│", false))
		for i, text := range cells {
			if i == 0 && leaders {
				text = " " + text + " " + strings.Repeat(".", widths[0]-color.VisibleLen(text)-2)
			} else {
				pad := max(0, widths[i]-color.VisibleLen(text))
				text = strings.Repeat(" ", pad/2) + text + strings.Repeat(" ", pad-pad/2)
			}
			b.WriteString(pokerInk(mode, text, i > 0 && i <= 5 && s.Coins == i))
			b.WriteString(pokerInk(mode, "│", false))
		}
		return b.String()
	}
	headers := []string{"COINS PER HAND", "1", "2", "3", "4", "5"}
	if hasNext {
		headers = append(headers, "NEXT X")
	}
	lines := []string{s.Config.GameLabel() + " — " + s.Config.Payout + " pay", border("┌", "┬", "┐"), row(headers, false), border("├", "┼", "┤")}
	for _, pay := range rows {
		cells := []string{pay.Name}
		for _, n := range pay.Coins {
			cells = append(cells, fmt.Sprint(n))
		}
		if hasNext {
			cells = append(cells, fmt.Sprintf("%dx", pay.NextAward))
		}
		lines = append(lines, row(cells, true))
	}
	lines = append(lines, border("└", "┴", "┘"), fmt.Sprintf("1 coin = %d credits | %d hand(s)", s.Denomination, s.Hands))
	if s.Config.Feature != "" {
		lines = append(lines, fmt.Sprintf("Feature ON adds %d coin(s) per hand; requires five base coins.", s.Config.FeatureFeeCoins()))
	}
	return strings.Join(lines, "\n")
}

func renderPokerCards(cards []casino.CardView, held *[5]bool, mode string, unicode bool, size int, style string, store *cardart.Store) []string {
	if len(cards) == 0 {
		return nil
	}
	var labels []string
	minWidth := 0
	if held != nil {
		labels = make([]string, len(cards))
		minWidth = 6
		for i := range cards {
			text := fmt.Sprint(i + 1)
			if held[i] {
				text = color.Render(mode, color.ColorSpec{Fg: pokerYellow, Bg: -1, Bold: true}, text+" HELD")
			}
			labels[i] = text
		}
	}
	return renderCardRow(cards, labels, minWidth, mode, unicode, size, style, store)
}

func renderVideoPoker(s *casino.VideoPokerSnapshot, mode string, unicode bool, size int, style string, store *cardart.Store) string {
	if s.Hands >= 100 {
		unicode = true
	}
	size = cardRenderSize(unicode, size)
	if s.Hands > 1 {
		size = min(size, 5)
	}
	if s.Hands >= 25 {
		size = 1
	}
	var lines []string
	if s.ShowPayTable {
		lines = append(lines, renderPokerPayTable(s, mode), "")
	}
	feature := ""
	if s.Config.Feature != "" {
		feature = s.Config.FeatureLabel() + " OFF"
		if s.FeatureOn {
			feature = s.Config.FeatureLabel() + " ON"
		}
	}
	badge := ""
	if feature != "" {
		badge = pokerInk(mode, " ["+feature+"] ", false)
	}
	bonus := ""
	mult := s.Multiplier
	if s.Config.Feature == casino.PokerUltimateX {
		for _, n := range s.Current {
			mult = max(mult, n)
		}
	}
	if mult > 1 {
		text := fmt.Sprintf(" %dX! ", mult)
		if s.Config.Feature == casino.PokerUltimateX && s.Hands > 1 {
			text = fmt.Sprintf(" Up to %dX! ", mult)
		}
		bonus = pokerInk(mode, text, true)
	}
	if s.Dice[0] > 0 {
		bonus += fmt.Sprintf(" Dice [%d] + [%d]", s.Dice[0], s.Dice[1])
	}
	decorated := badge != "" || bonus != ""
	if len(s.Cards) > 0 && len(s.Lines) == 0 {
		art := renderPokerCards(s.Cards, &s.Held, mode, unicode, size, style, store)
		if len(art) > 0 && decorated {
			art[0] += "  " + badge + bonus
		}
		lines = append(lines, art...)
	} else if len(s.Lines) == 0 && decorated {
		lines = append(lines, badge+bonus)
	}
	for i, line := range s.Lines {
		art := renderPokerCards(line.Cards, nil, mode, unicode, size, style, store)
		if i == 0 && len(art) > 0 && decorated {
			art[0] += "  " + badge + bonus
		}
		indent := ""
		if len(s.Lines) > 1 {
			indent = "     "
			for row := range art {
				prefix := indent
				if row == 0 {
					prefix = fmt.Sprintf("%3d: ", i+1)
				}
				art[row] = prefix + art[row]
			}
		}
		lines = append(lines, art...)
		text := "No win"
		fg := 15
		if line.Coins > 0 {
			text = line.Category
			fg = pokerYellow
		}
		if line.Multiplier > 1 {
			text += fmt.Sprintf(" (%dx)", line.Multiplier)
		}
		if len(s.Next) > i && s.Next[i] > 1 {
			text += fmt.Sprintf(" | earns %dx", s.Next[i])
		}
		// Results always follow the cards, including compact hundred-play.
		lines = append(lines, indent+color.Render(mode, color.ColorSpec{Fg: fg, Bg: -1, Bold: true}, text))
	}
	if s.Result != "" {
		fg := pokerYellow
		if s.NoWin {
			fg = 15
		}
		lines = append(lines, color.Render(mode, color.ColorSpec{Fg: fg, Bg: -1, Bold: true}, s.Result))
	}
	if !s.Drawing && len(s.Lines) == 0 && s.Config.Feature == casino.PokerUltimateX && len(s.Next) > 0 {
		var parts []string
		for i, n := range s.Next {
			parts = append(parts, fmt.Sprintf("%d:%dx", i+1, n))
		}
		lines = append(lines, "Multipliers on the machine: "+strings.Join(parts, " "))
	}
	return "\n" + strings.Join(lines, "\n") + "\n"
}