summaryrefslogtreecommitdiff
path: root/ui/views/play.go
blob: b9d923052f28e55eadd4b3cdf77a04f0f01e709f (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
package views

import (
	"errors"
	"fmt"
	"os"

	"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 {
	EnterNewPlay key.Binding
	StartNewPlay key.Binding
	GoLogout     key.Binding
	Quit         key.Binding
}

var defaultPlayKeyMap = playKeyMap{
	EnterNewPlay: key.NewBinding(
		key.WithKeys("alt+E", "alt+e"),
		key.WithHelp("Alt+E", "Enter a play using code"),
	),
	StartNewPlay: 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
	StartPlayPage
)

type PlayModel struct {
	width      int
	height     int
	err        error
	keys       playKeyMap
	namePrompt textinput.Model
	page       PlayModelPage
	isLoading  bool
}

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:       defaultPlayKeyMap,
		namePrompt: namePrompt,
		page:       LandingPage,
		isLoading:  false,
	}
}

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.EnterNewPlay):
			m.page = InsertCodePage
			return m, nil
		case key.Matches(msg, m.keys.StartNewPlay):
			// TODO: handle new play
			return m, nil
		case key.Matches(msg, m.keys.GoLogout):
			if err := os.Remove(".rahannarc"); err != nil {
				m.err = err
				return m, nil
			}
			return m, SwitchModelCmd(NewAuthModel(m.width, m.height+1))
		case key.Matches(msg, m.keys.Quit):
			return m, tea.Quit
		case msg.Type == tea.KeyEnter:
			if m.page == InsertCodePage {
				m.err = errors.New("Can't join for now...")
			}
		}
	}

	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)

	// Error message
	formError := ""
	if m.err != nil {
		formError = fmt.Sprintf("Error: %v", m.err.Error())
	}

	// Status message
	statusMsg := fmt.Sprintf("Press %s to join", lipgloss.NewStyle().Italic(true).Render("Enter"))
	if m.isLoading {
		statusMsg = "Creating account..."
	}

	var content string

	switch m.page {
	case LandingPage:
		content = chess
		m.namePrompt.Blur()
	case InsertCodePage:
		m.namePrompt.Focus()
		content = m.namePrompt.View()

		content = lipgloss.NewStyle().
			Align(lipgloss.Center).
			Width(m.width).
			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),
				),
			)
	}

	windowContent := lipgloss.JoinVertical(
		lipgloss.Center,
		windowStyle.
			Width(formWidth).
			Render(lipgloss.JoinVertical(
				lipgloss.Center,
				errorStyle.Align(lipgloss.Center).Width(formWidth-4).Render(formError),
				content,
			)),
	)

	enterKey := fmt.Sprintf("%s %s", altCodeStyle.Render(m.keys.EnterNewPlay.Help().Key), m.keys.EnterNewPlay.Help().Desc)
	startKey := fmt.Sprintf("%s %s", altCodeStyle.Render(m.keys.StartNewPlay.Help().Key), m.keys.StartNewPlay.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,
	)
}