-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
265 lines (225 loc) · 5.79 KB
/
ui.go
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//go:build !tinygo
// +build !tinygo
package exampleclient
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/marianogappa/truco/truco"
"github.com/nsf/termbox-go"
)
type ui struct {
keyCh chan rune
}
func NewUI() *ui {
ui := &ui{}
ui.keyCh = ui.startKeyEventLoop()
err := termbox.Init()
if err != nil {
panic(err)
}
return ui
}
func (u *ui) Close() {
termbox.Close()
}
type renderMode int
const (
PRINT_MODE_NORMAL renderMode = iota
PRINT_MODE_SHOW_ROUND_RESULT
PRINT_MODE_END
)
type renderState struct {
mode renderMode
viewportWidth int
viewportHeight int
gs truco.ClientGameState
possibleActions []truco.Action
}
func calculateRenderState(state truco.ClientGameState) renderState {
var (
viewportWidth, viewportHeight = termbox.Size()
possibleActions = _deserializeActions(state.PossibleActions)
gs = state
mode = PRINT_MODE_NORMAL
)
if state.IsRoundFinished {
mode = PRINT_MODE_SHOW_ROUND_RESULT
}
if state.IsGameEnded {
mode = PRINT_MODE_END
}
return renderState{
mode: mode,
gs: gs,
possibleActions: possibleActions,
viewportWidth: viewportWidth,
viewportHeight: viewportHeight,
}
}
func (u *ui) render(state truco.ClientGameState) error {
if err := termbox.Clear(termbox.ColorWhite, termbox.ColorBlack); err != nil {
return err
}
rs := calculateRenderState(state)
renderScores(rs)
renderTheirUnrevealedCards(rs)
renderTheirRevealedCards(rs)
renderLastAction(rs)
renderEndSummary(rs)
renderYourRevealedCards(rs)
renderYourUnrevealedCards(rs)
renderActions(rs)
termbox.Flush()
// This is an artificial delay to make the game more human-like.
time.Sleep(1 * time.Second)
return nil
}
func renderScores(rs renderState) {
renderUpToAt(rs.viewportWidth-1, 0, fmt.Sprintf("Mano número %d", rs.gs.RoundNumber))
youMano := ""
themMano := ""
if rs.gs.RoundTurnPlayerID == rs.gs.YouPlayerID {
youMano = " (mano)"
} else {
themMano = " (mano)"
}
renderUpToAt(rs.viewportWidth-1, 1, fmt.Sprintf("Vos%v %v", youMano, spanishScore(rs.gs.YourScore)))
renderUpToAt(rs.viewportWidth-1, 2, fmt.Sprintf("Elle%v %v", themMano, spanishScore(rs.gs.TheirScore)))
}
func renderTheirUnrevealedCards(rs renderState) {
displayText := ""
for _, card := range rs.gs.TheirDisplayUnrevealedCards {
if card.IsHole {
displayText += " "
} else {
displayText += "[]"
}
displayText += " "
}
renderAt(0, 0, displayText)
}
func renderTheirRevealedCards(rs renderState) {
renderAt(0, rs.viewportHeight/2-3, getCardsString(rs.gs.TheirRevealedCards))
}
func renderLastAction(rs renderState) {
renderAt(0, rs.viewportHeight/2, getLastActionString(rs))
}
func renderEndSummary(rs renderState) {
var renderText string
switch rs.mode {
case PRINT_MODE_SHOW_ROUND_RESULT:
envidoPart := "el envido no se jugó"
if rs.gs.EnvidoWinnerPlayerID != -1 {
envidoWinner := "vos"
won := "ganaste"
if rs.gs.EnvidoWinnerPlayerID == rs.gs.ThemPlayerID {
envidoWinner = "elle"
won = "ganó"
}
envidoPart = fmt.Sprintf("%v %v %v puntos por el envido", envidoWinner, won, rs.gs.EnvidoPoints)
}
trucoWinner := "vos"
won := "ganaste"
if rs.gs.TrucoWinnerPlayerID == rs.gs.ThemPlayerID {
trucoWinner = "elle"
won = "ganó"
}
renderText = fmt.Sprintf(
"Terminó la mano, %v y %v %v %v puntos por el truco.",
envidoPart,
trucoWinner,
won,
rs.gs.TrucoPoints,
)
case PRINT_MODE_END:
var resultText string
if rs.gs.YouPlayerID == rs.gs.WinnerPlayerID {
resultText = "Ganaste 🥰"
} else {
resultText = "Perdiste 😭"
}
renderText = fmt.Sprintf("%v %v!", getLastActionString(rs), resultText)
}
renderAt(0, rs.viewportHeight/2, renderText)
}
func renderYourRevealedCards(rs renderState) {
renderAt(0, rs.viewportHeight/2+3, getCardsString(rs.gs.YourRevealedCards))
}
func renderYourUnrevealedCards(rs renderState) {
displayText := ""
for _, card := range rs.gs.YourDisplayUnrevealedCards {
if !card.IsHole {
displayText += getDisplayCardString(card)
} else {
displayText += " "
}
displayText += " "
}
renderAt(0, rs.viewportHeight-4, displayText)
}
func renderActions(rs renderState) {
var renderText string
actionsString := ""
for i, action := range rs.possibleActions {
action := spanishAction(action)
actionsString += fmt.Sprintf("%d. %s ", i+1, action)
}
renderText = actionsString
if len(rs.possibleActions) == 0 {
renderText = "Esperando al otro jugador..."
}
if rs.mode == PRINT_MODE_END {
renderText = "Presioná cualquier tecla para continuar..."
}
renderAt(0, rs.viewportHeight-2, renderText)
}
func renderAt(x, y int, s string) {
_s := []rune(s)
for i, r := range _s {
termbox.SetCell(x+i, y, r, termbox.ColorDefault, termbox.ColorDefault)
}
}
// Write so that the output ends at x, y
func renderUpToAt(x, y int, s string) {
_s := []rune(s)
for i, r := range _s {
termbox.SetCell(x-len(_s)+i, y, r, termbox.ColorDefault, termbox.ColorDefault)
}
}
func getCardsString(cards []truco.Card) string {
var cs []string
for _, card := range cards {
cs = append(cs, getCardString(card))
}
return strings.Join(cs, " ")
}
func getCardString(card truco.Card) string {
return fmt.Sprintf("[%v%v ]", card.Number, suitEmoji(card.Suit))
}
func getDisplayCardString(card truco.DisplayCard) string {
return fmt.Sprintf("[%v%v ]", card.Number, suitEmoji(card.Suit))
}
func suitEmoji(suit string) string {
switch suit {
case truco.ESPADA:
return "🔪"
case truco.BASTO:
return "🌿"
case truco.ORO:
return "💰"
case truco.COPA:
return "🍷"
default:
return "❓"
}
}
func _deserializeActions(as []json.RawMessage) []truco.Action {
_as := []truco.Action{}
for _, a := range as {
_a, _ := truco.DeserializeAction(a)
_as = append(_as, _a)
}
return _as
}