-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.go
44 lines (37 loc) · 895 Bytes
/
gui.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
package main
import (
"time"
"github.com/gorilla/websocket"
)
// Gui represents a connected browser that shows status information
type Gui struct {
Speed time.Duration
c *websocket.Conn
}
type guiMessage struct {
Speed time.Duration `json:"speed"`
}
func (g *Gui) listenDuration() {
var message guiMessage
for g.c != nil {
err := g.c.ReadJSON(&message)
if err != nil {
g.c = nil
}
g.Speed = message.Speed * time.Millisecond
}
}
// NewGui creates a GUI instance from the specified websocket connection and duration
func NewGui(c *websocket.Conn, duration time.Duration) *Gui {
g := &Gui{duration, c}
go g.listenDuration()
return g
}
// WriteStatus writes the current game status to the GUI, pausing for a specified amount of time
func (g *Gui) WriteStatus(status *GameStatus) {
if g.c != nil {
status.You = 0
g.c.WriteJSON(status)
time.Sleep(g.Speed)
}
}