-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_window.go
109 lines (95 loc) · 2.21 KB
/
menu_window.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
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/nsf/termbox-go"
)
var Summary GameSummary
type Log struct {
Time time.Time `json:"time"`
Text string `json:"text"`
}
type Game struct {
Commands string `json:"commands"`
FinishedAt time.Time `json:"finishedAt"`
Game string `json:"game"`
ID string `json:"id"`
Identifier string `json:"identifier"`
IsFinished bool `json:"isFinished"`
Log []Log `json:"log"`
Name string `json:"name"`
PlayerList []string `json:"playerList"`
WhoseTurn []string `json:"whoseTurn"`
Winners []string `json:"winners"`
}
type GameSummary struct {
CurrentTurn []Game `json:"currentTurn"`
OtherActive []Game `json:"otherActive"`
RecentlyFinished []Game `json:"recentlyFinished"`
}
type MenuWindow struct {
WM *WindowManager
Selected int
}
func NewMenuWindow() *MenuWindow {
return &MenuWindow{}
}
func (w *MenuWindow) Init(wm *WindowManager) {
w.WM = wm
w.UpdateGames()
}
func (w *MenuWindow) UpdateGames() {
go func() {
req, err := NewAuthRequest("GET", "/game/summary?renderer=raw", nil)
if err != nil {
log.Printf("Unable to create game summary request, %v", err)
}
resp, err := Client.Do(req)
if err != nil {
log.Printf("Unable to request game summary, %v", err)
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&Summary); err != nil {
log.Printf("Unable to parse summary, %v", err)
}
w.WM.Render()
}()
}
func (w *MenuWindow) Title() string {
return "Menu"
}
func (w *MenuWindow) Render() {
termbox.HideCursor()
y := 2
for _, l := range [][]Game{
Summary.CurrentTurn,
Summary.OtherActive,
Summary.RecentlyFinished,
} {
for _, g := range l {
if y >= w.WM.SizeY-1 {
return
}
w.WM.CursorX = 0
w.WM.CursorY = y
w.WM.Print(
fmt.Sprintf("%s with %s", g.Name, strings.Join(g.PlayerList, ", ")),
termbox.ColorDefault,
termbox.ColorDefault,
)
y++
}
y++
}
}
func (w *MenuWindow) Event(e termbox.Event) {
if e.Type == termbox.EventKey && e.Key == termbox.KeyEnter {
w.WM.AddWindow(NewGameWindow("Roll Through the Ages"))
}
}
func (w *MenuWindow) KeyInfo() []KeyInfo {
return []KeyInfo{}
}