-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
59 lines (53 loc) · 1.28 KB
/
server.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
package main
import (
"log"
"time"
"./universe"
)
type server struct {
// set of clients
clients map[*client]struct{}
register chan *client
unregister chan *client
updates chan *universe.Update
gamestate *universe.Gamestate
}
func newServer() *server {
return &server{
clients: make(map[*client]struct{}),
register: make(chan *client),
unregister: make(chan *client),
updates: make(chan *universe.Update),
gamestate: universe.NewGameState(),
}
}
func (s *server) runForever() {
log.Println("server runForever")
ticker := time.NewTicker(universe.GameTick)
for {
select {
case c := <-s.register:
log.Println("server got new client")
s.clients[c] = struct{}{}
case c := <-s.unregister:
log.Println("server unregistered client")
s.gamestate.RemovePlayer(c.id)
delete(s.clients, c)
case u := <-s.updates:
log.Println("server received update from client")
// process updates to gamestate immediately
s.gamestate.Update(u)
case <-ticker.C:
// every gametick, marshal the gamestate to JSON, and send to all clients
json := s.gamestate.Serialize()
log.Println("json: ", string(json))
for c := range s.clients {
select {
case c.send <- json:
default:
log.Println("failed to send json to client")
}
}
}
}
}