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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
package views
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/boozec/rahanna/internal/api/database"
"github.com/boozec/rahanna/internal/network"
"github.com/boozec/rahanna/pkg/ui/multiplayer"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
chessBoard = `
A B C D E F G H
+---------------+
8 |♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜| 8
7 |♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟| 7
6 |. . . . . . . .| 6
5 |. . . . . . . .| 5
4 |. . . . . . . .| 4
3 |. . . . . . . .| 3
2 |♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙| 2
1 |♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖| 1
+---------------+
A B C D E F G H
`
)
type PlayModelPage int
const (
LandingPage PlayModelPage = iota
InsertCodePage
StartGamePage
)
type responseOk struct {
Name string `json:"name"`
IP string `json:"ip"`
Port int `json:"int"`
}
// API response types
type playResponse struct {
Ok responseOk
Error string `json:"error"`
}
// Keyboard controls
type playKeyMap struct {
EnterNewGame key.Binding
StartNewGame key.Binding
GoLogout key.Binding
Quit key.Binding
}
// Default key bindings for the play model
var defaultPlayKeyMap = playKeyMap{
EnterNewGame: key.NewBinding(
key.WithKeys("alt+E", "alt+e"),
key.WithHelp("Alt+E", "Enter a play using code"),
),
StartNewGame: key.NewBinding(
key.WithKeys("alt+s", "alt+S"),
key.WithHelp("Alt+S", "Start a new play"),
),
GoLogout: key.NewBinding(
key.WithKeys("alt+Q", "alt+q"),
key.WithHelp("Alt+Q", "Logout"),
),
Quit: key.NewBinding(
key.WithKeys("Q", "q"),
key.WithHelp(" Q", "Quit"),
),
}
type PlayModel struct {
// UI dimensions
width int
height int
// UI state
err error
keys playKeyMap
namePrompt textinput.Model
page PlayModelPage
isLoading bool
// Game state
playName string
play *database.Game
network *multiplayer.PlayNetwork
}
// NewPlayModel creates a new play model instance
func NewPlayModel(width, height int) PlayModel {
namePrompt := createNamePrompt()
return PlayModel{
width: width,
height: height,
err: nil,
keys: defaultPlayKeyMap,
namePrompt: namePrompt,
page: LandingPage,
isLoading: false,
playName: "",
play: nil,
}
}
// Create and configure the name input prompt
func createNamePrompt() textinput.Model {
namePrompt := textinput.New()
namePrompt.Prompt = " "
namePrompt.TextStyle = inputStyle
namePrompt.Placeholder = "rectangular-lake"
namePrompt.Focus()
namePrompt.CharLimit = 23
namePrompt.Width = 23
return namePrompt
}
func (m PlayModel) Init() tea.Cmd {
ClearScreen()
return textinput.Blink
}
func (m PlayModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if exit := handleExit(msg); exit != nil {
return m, exit
}
switch msg := msg.(type) {
case tea.WindowSizeMsg:
return m.handleWindowSize(msg)
case tea.KeyMsg:
return m.handleKeyPress(msg)
case playResponse:
return m.handlePlayResponse(msg)
case database.Game:
return m.handleGameResponse(msg)
case error:
return m.handleError(msg)
}
// Handle input updates when on the InsertCodePage
if m.page == InsertCodePage {
var cmd tea.Cmd
m.namePrompt, cmd = m.namePrompt.Update(msg)
return m, cmd
}
return m, nil
}
func (m PlayModel) View() string {
formWidth := getFormWidth(m.width)
base := lipgloss.NewStyle().Align(lipgloss.Center).Width(m.width)
content := m.renderPageContent(base)
// Build the main window with error handling
windowContent := m.buildWindowContent(content, formWidth)
// Create navigation buttons
buttons := m.renderNavigationButtons()
centeredContent := lipgloss.JoinVertical(
lipgloss.Center,
getLogo(m.width),
windowContent,
lipgloss.NewStyle().MarginTop(2).Render(buttons),
)
return lipgloss.Place(
m.width,
m.height,
lipgloss.Center,
lipgloss.Center,
centeredContent,
)
}
func (m PlayModel) handleWindowSize(msg tea.WindowSizeMsg) (tea.Model, tea.Cmd) {
m.width = msg.Width
m.height = msg.Height
return m, nil
}
func (m PlayModel) handleKeyPress(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, m.keys.EnterNewGame):
m.page = InsertCodePage
return m, nil
case key.Matches(msg, m.keys.StartNewGame):
m.page = StartGamePage
if !m.isLoading {
m.isLoading = true
return m, m.newGameCallback()
}
case key.Matches(msg, m.keys.GoLogout):
return m, m.logout()
case key.Matches(msg, m.keys.Quit):
return m, tea.Quit
case msg.Type == tea.KeyEnter:
if m.page == InsertCodePage && !m.isLoading {
m.isLoading = true
return m, m.enterGame()
}
}
return m, nil
}
func (m *PlayModel) handlePlayResponse(msg playResponse) (tea.Model, tea.Cmd) {
m.isLoading = false
m.err = nil
if msg.Error != "" {
m.err = fmt.Errorf(msg.Error)
if msg.Error == "unauthorized" {
return m, m.logout()
}
} else {
m.playName = msg.Ok.Name
m.network = multiplayer.NewPlayNetwork("peer-1", msg.Ok.IP, msg.Ok.Port)
}
return m, nil
}
func (m *PlayModel) handleGameResponse(msg database.Game) (tea.Model, tea.Cmd) {
m.isLoading = false
m.play = &msg
m.err = nil
return m, nil
}
func (m *PlayModel) handleError(msg error) (tea.Model, tea.Cmd) {
m.isLoading = false
m.err = msg
return m, nil
}
func (m *PlayModel) renderPageContent(base lipgloss.Style) string {
switch m.page {
case LandingPage:
m.namePrompt.Blur()
return chessBoard
case InsertCodePage:
m.namePrompt.Focus()
return m.renderInsertCodeContent(base)
case StartGamePage:
return m.renderStartGameContent(base)
}
return ""
}
func (m PlayModel) renderInsertCodeContent(base lipgloss.Style) string {
// When loading, show loading status
if m.isLoading {
return base.Render(
lipgloss.NewStyle().
Align(lipgloss.Center).
Bold(true).
Render("Loading..."),
)
}
// When we have a play, show who we're playing against
if m.play != nil {
playerName := lipgloss.NewStyle().
Foreground(lipgloss.Color("#e67e22")).
Render(m.play.Player1.Username)
statusMsg := fmt.Sprintf("You are playing versus %s", playerName)
return base.Render(
lipgloss.NewStyle().
Align(lipgloss.Center).
Width(m.width).
Bold(true).
Render(statusMsg),
)
}
// Default: show input prompt
return base.Render(
lipgloss.JoinVertical(lipgloss.Left,
lipgloss.NewStyle().Width(23).Render("Insert play code:"),
m.namePrompt.View(),
lipgloss.NewStyle().
Align(lipgloss.Center).
PaddingTop(2).
Width(23).
Bold(true).
Render(fmt.Sprintf("Press %s to join",
lipgloss.NewStyle().Italic(true).Render("Enter"))),
),
)
}
func (m PlayModel) renderStartGameContent(base lipgloss.Style) string {
var statusMsg string
if m.isLoading {
statusMsg = "Loading..."
} else if m.playName != "" {
gameCode := lipgloss.NewStyle().
Italic(true).
Foreground(lipgloss.Color("#F39C12")).
Render(m.playName)
statusMsg = fmt.Sprintf("Share `%s` to your friend", gameCode)
}
return base.Render(statusMsg)
}
func (m PlayModel) buildWindowContent(content string, formWidth int) string {
if m.err != nil {
formError := fmt.Sprintf("Error: %v", m.err.Error())
return lipgloss.JoinVertical(
lipgloss.Center,
windowStyle.Width(formWidth).Render(lipgloss.JoinVertical(
lipgloss.Center,
errorStyle.Align(lipgloss.Center).Width(formWidth-4).Render(formError),
content,
)),
)
}
return lipgloss.JoinVertical(
lipgloss.Center,
windowStyle.Width(formWidth).Render(lipgloss.JoinVertical(
lipgloss.Center,
content,
)),
)
}
func (m PlayModel) renderNavigationButtons() string {
logoutKey := fmt.Sprintf("%s %s",
altCodeStyle.Render(m.keys.GoLogout.Help().Key),
m.keys.GoLogout.Help().Desc)
quitKey := fmt.Sprintf("%s %s",
altCodeStyle.Render(m.keys.Quit.Help().Key),
m.keys.Quit.Help().Desc)
if m.page == LandingPage {
enterKey := fmt.Sprintf("%s %s",
altCodeStyle.Render(m.keys.EnterNewGame.Help().Key),
m.keys.EnterNewGame.Help().Desc)
startKey := fmt.Sprintf("%s %s",
altCodeStyle.Render(m.keys.StartNewGame.Help().Key),
m.keys.StartNewGame.Help().Desc)
return lipgloss.JoinVertical(
lipgloss.Left,
enterKey,
startKey,
logoutKey,
quitKey,
)
}
return lipgloss.JoinVertical(
lipgloss.Left,
logoutKey,
quitKey,
)
}
func (m *PlayModel) newGameCallback() tea.Cmd {
return func() tea.Msg {
// Get authorization token
authorization, err := getAuthorizationToken()
if err != nil {
return playResponse{Error: err.Error()}
}
// Set up network connection
port, err := network.GetRandomAvailablePort()
if err != nil {
return playResponse{Error: err.Error()}
}
ip := network.GetOutboundIP().String()
// FIXME: ip
ip = "0.0.0.0"
// Prepare request payload
payload, err := json.Marshal(map[string]string{
"ip": fmt.Sprintf("%s:%d", ip, port),
})
if err != nil {
return playResponse{Error: err.Error()}
}
// Send API request
url := os.Getenv("API_BASE") + "/play"
resp, err := sendAPIRequest("POST", url, payload, authorization)
if err != nil {
return playResponse{Error: err.Error()}
}
defer resp.Body.Close()
// Handle response
if resp.StatusCode != http.StatusOK {
var response playResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return playResponse{Error: fmt.Sprintf("HTTP error: %d, unable to decode body", resp.StatusCode)}
}
return playResponse{Error: response.Error}
}
// Decode successful response
var response struct {
Name string `json:"name"`
Error string `json:"error"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return playResponse{Error: fmt.Sprintf("Error decoding JSON: %v", err)}
}
return playResponse{Ok: responseOk{Name: response.Name, IP: ip, Port: port}}
}
}
func (m PlayModel) enterGame() tea.Cmd {
return func() tea.Msg {
// Get authorization token
authorization, err := getAuthorizationToken()
if err != nil {
return playResponse{Error: err.Error()}
}
// Set up network connection
port, err := network.GetRandomAvailablePort()
if err != nil {
return playResponse{Error: err.Error()}
}
ip := network.GetOutboundIP().String()
// FIXME: ip
ip = "0.0.0.0"
// Prepare request payload
payload, err := json.Marshal(map[string]string{
"ip": fmt.Sprintf("%s:%d", ip, port),
"name": m.namePrompt.Value(),
})
if err != nil {
return playResponse{Error: err.Error()}
}
// Send API request
url := os.Getenv("API_BASE") + "/enter-game"
resp, err := sendAPIRequest("POST", url, payload, authorization)
if err != nil {
return playResponse{Error: err.Error()}
}
defer resp.Body.Close()
// Handle response
if resp.StatusCode != http.StatusOK {
var response playResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return playResponse{Error: fmt.Sprintf("HTTP error: %d, unable to decode body", resp.StatusCode)}
}
return playResponse{Error: response.Error}
}
// Decode successful response
var response database.Game
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return playResponse{Error: fmt.Sprintf("Error decoding JSON: %v", err)}
}
return response
}
}
func (m PlayModel) logout() tea.Cmd {
if err := os.Remove(".rahannarc"); err != nil {
return nil
}
return SwitchModelCmd(NewAuthModel(m.width, m.height+1))
}
// getAuthorizationToken reads the authentication token from the .rahannarc file
func getAuthorizationToken() (string, error) {
f, err := os.Open(".rahannarc")
if err != nil {
return "", err
}
defer f.Close()
scanner := bufio.NewScanner(f)
var authorization string
for scanner.Scan() {
authorization = scanner.Text()
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("error reading auth token: %v", err)
}
return authorization, nil
}
// sendAPIRequest sends an HTTP request to the API with the given parameters
func sendAPIRequest(method, url string, payload []byte, authorization string) (*http.Response, error) {
req, err := http.NewRequest(method, url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", authorization))
client := &http.Client{}
return client.Do(req)
}
|