/
game_hub.go
106 lines (94 loc) · 3.02 KB
/
game_hub.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
package pkg
import (
"log"
"time"
"github.com/calamity-of-subterfuge/cos/pkg/srvpkts"
"github.com/gorilla/websocket"
)
// GameHub manages a single server websocket connection in order to run a
// single game, notifying a particular channel upon completion and allowing
// cancellation
type GameHub struct {
UID string
game Game
conn *Conn
recvQueue chan ReceivedMessage
connClosed chan string
finishNotifyQueue chan string
cancelChan chan struct{}
}
// NewGameHub takes over management of the given game server websocket to
// run a game initialized using the given GameConstructor. This does not
// start managing the game; that should be done in a dedicated goroutine
// by calling the long-running function Manage()
func NewGameHub(conn *websocket.Conn, uid string, finishNotifyQueue chan string, gameConstructor GameConstructor) *GameHub {
recvQueue := make(chan ReceivedMessage, 1024)
closedQueue := make(chan string, 1)
wrappedConn := NewConn(conn, uid, recvQueue, closedQueue)
game := gameConstructor(wrappedConn.SendQueue)
return &GameHub{
UID: uid,
conn: wrappedConn,
game: game,
recvQueue: recvQueue,
connClosed: closedQueue,
finishNotifyQueue: finishNotifyQueue,
cancelChan: make(chan struct{}, 1),
}
}
// Closes this game hub if it is being managed right now. GameHubs cannot be
// reused.
func (h *GameHub) Close() {
select {
case h.cancelChan <- struct{}{}:
default:
}
}
// Manage this game hub. Typically run on a dedicated goroutine, this will
// monitor the channels for this game hub in order to execute the game.
func (h *GameHub) Manage() {
ticker := time.NewTicker(time.Second / 60)
lastTick := time.Now()
lastWarnBehindAt := time.Now()
manageLoop:
for {
select {
case msg := <-h.recvQueue:
srvPacket, err := srvpkts.ParseSinglePacket(msg.Message)
if err != nil {
log.Printf("ignoring bad packet from server: %v (%v)", msg.Message, err)
break
}
h.game.OnReceiveMessage(srvPacket)
case curTick := <-ticker.C:
if time.Since(curTick) > time.Second/5 {
// Eat the tick to avoid falling so far behind; we'll tick again
// later with a big time.Duration
if time.Since(lastWarnBehindAt) > 5*time.Minute {
log.Printf(
"WARN: eating ticks because they are too old (%v) - "+
"this warning happens only once per 5 minutes "+
"and means that the AI is overloaded. This results "+
"in ticks with large Duration's, which can lead to"+
"instability",
time.Since(curTick),
)
lastWarnBehindAt = time.Now()
}
break
}
h.game.Tick(curTick.Sub(lastTick))
lastTick = curTick
case <-h.connClosed:
break manageLoop
case <-h.cancelChan:
break manageLoop
}
}
log.Printf("GameHub %s shutting down", h.UID)
h.game.OnDisconnected()
h.Close()
h.conn.Close()
ticker.Stop()
h.finishNotifyQueue <- h.UID
}