-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
153 lines (137 loc) · 3.62 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
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
package main
import (
"log"
"math/rand"
"time"
"github.com/gogo/protobuf/proto"
"github.com/silbinarywolf/networkplatformer-go/gameserver"
"github.com/silbinarywolf/networkplatformer-go/netmsg"
)
var (
server *Server
)
type Server struct {
*gameserver.Server
}
func NewServer() *Server {
server := &Server{
Server: gameserver.NewServer("/abgame"),
}
return server
}
func (s *Server) Update() {
RecvMsgLoop:
for {
select {
case client := <-s.ChRegister():
clientSlot := int32(client.ClientSlot())
// Create player instance
char := &Char{
X: float64(rand.Int63n(90) + 130),
Y: float64(380),
}
// Create client
s.RegisterClient(client, char)
// Add client to simulation
chars = append(chars, char)
// Send connection response
{
sendMsg := netmsg.ConnectResponse{
ClientSlot: clientSlot,
X: char.X,
Y: char.Y,
}
data, err := proto.Marshal(&sendMsg)
if err != nil {
log.Fatal("client connect: marshaling error: ", err)
}
// Send to connecting player their information
packetData := make([]byte, 1, len(data)+1)
packetData[0] = netmsg.MsgConnectResponse
packetData = append(packetData, data...)
client.SendMessage(packetData)
}
case client := <-s.ChUnregister():
if s.RemoveClient(client) {
char := client.Data().(*Char)
char.RemoveFromSimulation()
log.Printf("client #%d disconnected", client.ClientSlot())
// Tell clients player disconnected
sendMsg := netmsg.DisconnectPlayer{
ClientSlot: int32(client.ClientSlot()),
}
data, err := proto.Marshal(&sendMsg)
if err != nil {
log.Fatal("client connect: marshaling error: ", err)
}
// Send disconnected player
packetData := make([]byte, 1, len(data)+1)
packetData[0] = netmsg.MsgDisconnectPlayer
packetData = append(packetData, data...)
for otherClient := range s.GetClients() {
if otherClient == client {
continue
}
otherClient.SendMessage(packetData)
}
}
case message := <-s.ChBroadcast():
var (
client = message.Client()
buf = message.Data()
)
kind := netmsg.Kind(buf[0])
buf = buf[1:]
switch kind {
case netmsg.MsgUpdatePlayer:
// Receive update
recvMsg := &netmsg.UpdatePlayer{}
err := recvMsg.Unmarshal(buf)
if err != nil {
log.Fatal("unmarshaling error: ", err)
break
}
char := client.Data().(*Char)
char.X = recvMsg.X
char.Y = recvMsg.Y
char.isKeyLeftPressed = recvMsg.IsKeyLeftPressed
char.isKeyRightPressed = recvMsg.IsKeyRightPressed
default:
log.Printf("Unhandled netmsg kind: %s, with data: %v\n", kind.String(), buf)
}
default:
// no-op
break RecvMsgLoop
}
}
// Send updates to clients
for client := range s.GetClients() {
char := client.Data().(*Char)
elapsed := time.Since(char.lastUpdatedTimer)
if elapsed > 15*time.Millisecond {
// Reset countdown till next update
char.lastUpdatedTimer = time.Now()
// Send update to other players
sendMsg := netmsg.UpdatePlayer{
ClientSlot: int32(client.ClientSlot()),
X: char.X,
Y: char.Y,
IsKeyLeftPressed: char.isKeyLeftPressed,
IsKeyRightPressed: char.isKeyRightPressed,
}
data, err := proto.Marshal(&sendMsg)
if err != nil {
log.Fatal("client update: marshaling error: ", err)
}
packetData := make([]byte, 1, len(data)+1)
packetData[0] = netmsg.MsgUpdatePlayer
packetData = append(packetData, data...)
for otherClient := range s.GetClients() {
if otherClient == client {
continue
}
otherClient.SendMessage(packetData)
}
}
}
}