summaryrefslogtreecommitdiff
path: root/pkg/ui/views/play.go
blob: 991849ea176a63d0eb9fda7363fecf10e7b6f528 (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
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
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/charmbracelet/bubbles/key"
	"github.com/charmbracelet/bubbles/textinput"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

var chess string = `
 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 playKeyMap struct {
	EnterNewGame key.Binding
	StartNewGame key.Binding
	GoLogout     key.Binding
	Quit         key.Binding
}

type playResponse struct {
	Name  string `json:"name"`
	Error string `json:"error"`
}

var defaultGameKeyMap = 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 PlayModelPage int

const (
	LandingPage PlayModelPage = iota
	InsertCodePage
	StartGamePage
)

type PlayModel struct {
	width      int
	height     int
	err        error
	keys       playKeyMap
	namePrompt textinput.Model
	page       PlayModelPage
	isLoading  bool
	playName   string
	play       *database.Game
}

func NewPlayModel(width, height int) PlayModel {
	namePrompt := textinput.New()
	namePrompt.Prompt = " "
	namePrompt.TextStyle = inputStyle
	namePrompt.Placeholder = "rectangular-lake"
	namePrompt.Focus()
	namePrompt.CharLimit = 23
	namePrompt.Width = 23

	return PlayModel{
		width:      width,
		height:     height,
		err:        nil,
		keys:       defaultGameKeyMap,
		namePrompt: namePrompt,
		page:       LandingPage,
		isLoading:  false,
		playName:   "",
		play:       nil,
	}
}

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:
		m.width = msg.Width
		m.height = msg.Height
		return m, nil

	case tea.KeyMsg:
		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 {
				if !m.isLoading {
					m.isLoading = true
					return m, m.enterGame()
				}
			}
		}
	case playResponse:
		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.Name
		}
		return m, nil
	case database.Game:
		m.isLoading = false
		m.play = &msg
		m.err = nil
		return m, nil
	case error:
		m.isLoading = false
		m.err = msg
	}

	var cmd tea.Cmd = nil

	if m.page == InsertCodePage {
		m.namePrompt, cmd = m.namePrompt.Update(msg)
	}

	return m, tea.Batch(cmd)
}

func (m PlayModel) View() string {
	formWidth := getFormWidth(m.width)

	var content string
	base := lipgloss.NewStyle().Align(lipgloss.Center).Width(m.width)

	switch m.page {
	case LandingPage:
		content = chess
		m.namePrompt.Blur()
	case InsertCodePage:
		m.namePrompt.Focus()
		var statusMsg string
		if m.isLoading {
			statusMsg = "Loading..."
			content = base.
				Render(
					lipgloss.NewStyle().
						Align(lipgloss.Center).
						Bold(true).
						Render(statusMsg),
				)
		} else if m.play != nil {
			statusMsg = fmt.Sprintf("You are playing versus %s", lipgloss.NewStyle().Foreground(lipgloss.Color("#e67e22")).Render(m.play.Player1.Username))
			content = base.
				Render(
					lipgloss.NewStyle().
						Align(lipgloss.Center).
						Width(m.width).
						Bold(true).
						Render(statusMsg),
				)
		} else {
			statusMsg = fmt.Sprintf("Press %s to join", lipgloss.NewStyle().Italic(true).Render("Enter"))
			content = 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(statusMsg),
					),
				)
		}

	case StartGamePage:
		var statusMsg string
		if m.isLoading {
			statusMsg = "Loading..."
		} else if m.playName != "" {
			statusMsg = fmt.Sprintf("Share `%s` to your friend", lipgloss.NewStyle().Italic(true).Foreground(lipgloss.Color("#F39C12")).Render(m.playName))
		}

		content = base.
			Render(statusMsg)
	}

	var windowContent string
	if m.err != nil {
		formError := fmt.Sprintf("Error: %v", m.err.Error())
		windowContent = lipgloss.JoinVertical(
			lipgloss.Center,
			windowStyle.Width(formWidth).Render(lipgloss.JoinVertical(
				lipgloss.Center,
				errorStyle.Align(lipgloss.Center).Width(formWidth-4).Render(formError),
				content,
			)),
		)
	} else {
		windowContent = lipgloss.JoinVertical(
			lipgloss.Center,
			windowStyle.Width(formWidth).Render(lipgloss.JoinVertical(
				lipgloss.Center,
				content,
			)),
		)
	}

	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)
	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)

	// Vertically align the buttons
	buttons := lipgloss.JoinVertical(
		lipgloss.Left,
		enterKey,
		startKey,
		logoutKey,
		quitKey,
	)

	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) newGameCallback() tea.Cmd {
	return func() tea.Msg {
		f, err := os.Open(".rahannarc")
		if err != nil {
			return playResponse{Error: err.Error()}
		}
		defer f.Close()

		scanner := bufio.NewScanner(f)
		var authorization string
		for scanner.Scan() {
			authorization = scanner.Text()
		}

		if err := scanner.Err(); err != nil {
			fmt.Println("Error during scanning:", err)
		}

		url := os.Getenv("API_BASE") + "/play"

		port, err := network.GetRandomAvailablePort()
		if err != nil {
			return playResponse{Error: err.Error()}
		}

		payload, err := json.Marshal(map[string]string{
			"ip": fmt.Sprintf("%s:%d", network.GetOutboundIP().String(), port),
		})

		if err != nil {
			return playResponse{Error: err.Error()}
		}

		req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
		if err != nil {
			return playResponse{Error: err.Error()}
		}

		req.Header.Set("Content-Type", "application/json")
		req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", authorization))

		client := &http.Client{}

		resp, err := client.Do(req)

		defer resp.Body.Close()

		if resp.StatusCode != http.StatusOK {
			var response playResponse
			err = json.NewDecoder(resp.Body).Decode(&response)
			if err != nil {
				return playResponse{Error: fmt.Sprintf("HTTP error: %d, unable to decode body", resp.StatusCode)}
			}
			return playResponse{Error: response.Error}
		}

		var response playResponse
		err = json.NewDecoder(resp.Body).Decode(&response)
		if err != nil {
			return playResponse{Error: fmt.Sprintf("Error decoding JSON: %v", err)}
		}

		return response
	}
}

func (m PlayModel) enterGame() tea.Cmd {
	return func() tea.Msg {
		f, err := os.Open(".rahannarc")
		if err != nil {
			return playResponse{Error: err.Error()}
		}
		defer f.Close()

		scanner := bufio.NewScanner(f)
		var authorization string
		for scanner.Scan() {
			authorization = scanner.Text()
		}

		if err := scanner.Err(); err != nil {
			fmt.Println("Error during scanning:", err)
		}

		url := os.Getenv("API_BASE") + "/enter-game"

		port, err := network.GetRandomAvailablePort()
		if err != nil {
			return playResponse{Error: err.Error()}
		}

		payload, err := json.Marshal(map[string]string{
			"ip":   fmt.Sprintf("%s:%d", network.GetOutboundIP().String(), port),
			"name": m.namePrompt.Value(),
		})

		if err != nil {
			return playResponse{Error: err.Error()}
		}

		req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
		if err != nil {
			return playResponse{Error: err.Error()}
		}

		req.Header.Set("Content-Type", "application/json")
		req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", authorization))

		client := &http.Client{}

		resp, err := client.Do(req)

		defer resp.Body.Close()

		if resp.StatusCode != http.StatusOK {
			var response playResponse
			err = json.NewDecoder(resp.Body).Decode(&response)
			if err != nil {
				return playResponse{Error: fmt.Sprintf("HTTP error: %d, unable to decode body", resp.StatusCode)}
			}
			return playResponse{Error: response.Error}
		}

		var response database.Game
		err = json.NewDecoder(resp.Body).Decode(&response)
		if 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))
}