-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
233 lines (219 loc) · 5.91 KB
/
main.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
//termsuji is a application to play online-go.com in a terminal.
//It is not complete and can only read board state and play moves, and is
//intended more as a reference than a full-fledged application.
package main
import (
"fmt"
"time"
"github.com/gdamore/tcell/v2"
"github.com/lvank/termsuji/api"
"github.com/lvank/termsuji/config"
"github.com/lvank/termsuji/ui"
"github.com/rivo/tview"
)
var lastRefresh time.Time = time.Now()
var app *tview.Application
var rootPage *tview.Pages
var gameListFrame *tview.Frame
var gameList *tview.List
var frameHint *tview.Frame
var gameBoard *ui.GoBoardUI
var setLoading func(bool)
func main() {
auth := config.InitAuthData()
if auth.Tokens.Refresh != "" {
api.AuthenticateRefreshToken(auth.Tokens.Refresh)
}
cfg, err := config.InitConfig()
if err != nil {
panic(err)
}
cfg.Save() // TODO settings screen or something
app = tview.NewApplication()
rootPage = tview.NewPages()
rootPage.SetBorder(true).SetTitle("termsuji")
gameList = tview.NewList()
gameListFrame = tview.NewFrame(gameList)
gameListFrame.SetBorders(0, 0, 0, 0, 0, 0)
loadingModal := tview.NewModal()
loadingModal.SetText("Loading...")
setLoading = func(loading bool) {
f := rootPage.ShowPage
if !loading {
f = rootPage.HidePage
}
f("loading")
}
gameFrame := tview.NewFlex()
gameHint := tview.NewTextView()
gameHint.SetBorder(true)
gameBoard = ui.NewGoBoard(app, cfg, gameHint)
gameFrame.
AddItem(gameBoard.Box, 20*2+3, 1, true).
AddItem(gameHint, 0, 2, false)
gameBoard.Box.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyRune && event.Rune() == 'q' {
if gameBoard.SelectedTile() != nil {
gameBoard.ResetSelection()
} else {
gameBoard.Close()
async(func() {
refreshGames()
rootPage.SwitchToPage("browser")
})
}
return nil
}
switch event.Key() {
case tcell.KeyUp:
gameBoard.MoveSelection(0, -1)
case tcell.KeyDown:
gameBoard.MoveSelection(0, 1)
case tcell.KeyLeft:
gameBoard.MoveSelection(-1, 0)
case tcell.KeyRight:
gameBoard.MoveSelection(1, 0)
case tcell.KeyEnter:
selTile := gameBoard.SelectedTile()
if selTile == nil {
return nil
}
gameBoard.PlayMove(selTile.X, selTile.Y)
case tcell.KeyRune:
switch event.Rune() {
case 'p':
gameBoard.PlayMove(-1, -1)
case 't':
rootPage.ShowPage("themes")
}
}
return event
})
gameList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyRune:
switch event.Rune() {
case 'q':
app.Stop()
return nil
case 'r':
async(func() {
refreshGames()
})
return nil
case 't':
rootPage.ShowPage("themes")
return nil
}
}
return event
})
loginForm := tview.NewForm()
loginFrame := tview.NewFrame(loginForm)
loginForm.
AddInputField("Username", auth.Username, 32, nil, nil). //if we have a cached username, prefill it
AddPasswordField("Password", "", 32, '*', nil).
AddButton("Submit", func() {
err := api.AuthenticatePassword(
loginForm.GetFormItem(0).(*tview.InputField).GetText(),
loginForm.GetFormItem(1).(*tview.InputField).GetText(),
)
if err != nil {
loginFrame.Clear().AddText(err.Error(), true, tview.AlignLeft, tcell.PaletteColor(1))
return
}
storeAuthData(auth)
refreshGames()
rootPage.SwitchToPage("browser")
})
loginFrame.
SetBorders(0, 0, 0, 0, 1, 0).
AddText("Log in to OGS", true, tview.AlignLeft, tcell.PaletteColor(3))
themeList := tview.NewList()
themeList.SetTitle("Choose a theme")
selectTheme := func(i int, main, secondary string, shortcut rune) {
var theme config.Theme
switch main {
case "default":
theme = config.DefaultTheme
case "vaporwave":
theme = config.VaporwaveTheme
case "unicode":
theme = config.UnicodeTheme
case "catdog":
theme = config.CatdogTheme
case "hongoku":
theme = config.HongokuTheme
default: //No action
}
if main != "quit" {
cfg.Theme = theme
cfg.Save()
gameBoard.SetConfig(cfg)
}
rootPage.HidePage("themes")
}
themeList.
AddItem("default", "The default board theme", '1', nil).
AddItem("vaporwave", "Magenta/cyan board theme", '2', nil).
AddItem("unicode", "Display board with Unicode emoji symbols", '3', nil).
AddItem("catdog", "Board becomes zoo", '4', nil).
AddItem("hongoku", "Board becomes unreadable", '5', nil).
AddItem("quit", "Keep your current settings", 'q', nil).
SetSelectedFunc(selectTheme)
rootPage.AddPage("login", loginFrame, true, true)
rootPage.AddPage("browser", gameListFrame, true, false)
rootPage.AddPage("gameview", gameFrame, true, false)
rootPage.AddPage("themes", themeList, true, false)
rootPage.AddPage("loading", loadingModal, false, false)
if api.AuthData.Authenticated {
storeAuthData(auth)
refreshGames()
rootPage.SwitchToPage("browser")
} else {
rootPage.SwitchToPage("login")
}
if err := app.SetRoot(rootPage, true).Run(); err != nil {
panic(err)
}
}
func refreshGames() {
gameList.Clear()
async(func() {
lastRefresh = time.Now()
gamesArray := api.GetGamesList()
i := 0
for _, game := range gamesArray.Games {
if game.GameOver() {
continue
}
gameID := game.ID
gameList.AddItem(game.Name, game.Description(), rune('1'+i), func() {
async(func() {
gameBoard.Connect(gameID)
rootPage.SwitchToPage("gameview")
})
})
i++
}
gameListHint := fmt.Sprintf("r: refresh, t: themes, q: quit")
gameListFrame.Clear().AddText(gameListHint, false, tview.AlignLeft, tcell.ColorDefault)
})
}
//Helper function to show a loading screen while blocking functions are being called.
func async(f func()) {
go func() {
setLoading(true)
app.Draw()
f()
setLoading(false)
app.Draw()
}()
}
//Stores authentication data from api package after successful authentication.
func storeAuthData(a *config.AuthData) {
a.Username = api.AuthData.Player.Username
a.UserID = api.AuthData.Player.ID
a.Tokens.Refresh = api.AuthData.Oauth.RefreshToken
a.Save()
}