-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
331 lines (284 loc) · 7.1 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"fmt"
"log"
"math"
"math/rand"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/ysoding/multiplayer-game-prototype/message"
)
type Direction uint8
const (
Left Direction = iota
Right
Up
Down
)
const (
directionCount = 4
worldWidth = 800
worldHeight = 600
playerSpeed = 500
playerSize = 30
serverFPS = 60
sleepTimeInterval = time.Second / serverFPS
)
var players map[uint32]*PlayerOnServer
var idCounter uint32 = 0
var joinedIDs map[uint32]struct{}
var leftIDs map[uint32]struct{}
var pingIDs map[uint32]uint32
var mu sync.RWMutex
var directions [][]int // Left: {x:-1, y: 0}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func init() {
players = map[uint32]*PlayerOnServer{}
joinedIDs = map[uint32]struct{}{}
leftIDs = map[uint32]struct{}{}
pingIDs = map[uint32]uint32{}
directions = make([][]int, directionCount)
directions[Left] = []int{-1, 0}
directions[Right] = []int{1, 0}
directions[Up] = []int{0, -1}
directions[Down] = []int{0, 1}
}
func main() {
http.HandleFunc("/", handler)
go tick()
log.Println("Listening to ws://0.0.0.0:6970")
if err := http.ListenAndServe(":6970", nil); err != nil {
panic(err)
}
}
func tick() {
previousTime := time.Now()
for {
startTime := time.Now()
deltaTime := startTime.Sub(previousTime)
previousTime = startTime
mu.RLock()
if len(joinedIDs) > 0 {
// initialize joined player
{
tmpPlayers := make([]message.Player, 0, len(players))
for _, player := range players {
tmpPlayers = append(tmpPlayers, player.Player)
}
playersJoinedMsg := message.NewPlayersJoinedMsgStruct(tmpPlayers)
for id := range joinedIDs {
joinedPlayer, ok := players[id]
if !ok {
continue
}
helloMsg := message.NewHelloMsgStruct(joinedPlayer.ID, joinedPlayer.X, joinedPlayer.Y, joinedPlayer.Hue)
joinedPlayer.sendMsg(helloMsg)
// Reconstructing the state of the other players
joinedPlayer.sendMsg(playersJoinedMsg)
}
}
// notifying old player about who joined
{
tmpPlayers := make([]message.Player, 0, len(joinedIDs))
for id := range joinedIDs {
playerJoined, ok := players[id]
if !ok {
continue
}
tmpPlayers = append(tmpPlayers, playerJoined.Player)
}
playersJoinedMsg := message.NewPlayersJoinedMsgStruct(tmpPlayers)
for id, player := range players {
if _, ok := joinedIDs[id]; ok { // skip self
continue
}
player.sendMsg(playersJoinedMsg)
}
}
}
// notifying about whom left
{
if len(leftIDs) > 0 {
tmpIDs := make([]uint32, 0, len(players))
for id := range leftIDs {
tmpIDs = append(tmpIDs, id)
}
msg := message.NewPlayersLeftMsgStruct(tmpIDs)
for _, player := range players {
player.sendMsg(msg)
}
}
}
// notifying about moving player
{
cnt := 0
for _, player := range players {
if player.newMoving != player.Moving {
cnt++
}
}
if cnt > 0 {
tmpPlayers := make([]message.Player, 0)
for _, player := range players {
if player.newMoving != player.Moving {
player.Moving = player.newMoving
tmpPlayers = append(tmpPlayers, player.Player)
}
}
msg := message.NewPlayersMovingMsgStruct(tmpPlayers)
for _, player := range players {
player.sendMsg(msg)
}
}
}
// update player state
for _, player := range players {
player.update(float64(deltaTime.Milliseconds()) / 1000)
}
// send ping
for id, ping := range pingIDs {
if player, ok := players[id]; ok {
msg := message.NewPongMsgStruct(ping)
player.sendMsg(msg)
}
}
mu.RUnlock()
mu.Lock()
joinedIDs = map[uint32]struct{}{}
leftIDs = map[uint32]struct{}{}
pingIDs = map[uint32]uint32{}
mu.Unlock()
tickTime := time.Since(startTime)
sleepTime := sleepTimeInterval - tickTime
if sleepTime > 0 {
time.Sleep(sleepTime)
}
}
}
func handler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
id := getNewID()
remoteAddr := conn.UnderlyingConn().RemoteAddr().String()
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
x := rnd.Float32() * (worldWidth - playerSize)
y := rnd.Float32() * (worldHeight - playerSize)
hue := uint8((math.Floor(rnd.Float64()*360) / 360) * 256)
player := NewPlayer(conn, remoteAddr, id, x, y, hue)
mu.Lock()
players[id] = player
joinedIDs[id] = struct{}{}
mu.Unlock()
log.Printf("Player%d connected\n", id)
for {
messageType, p, err := conn.ReadMessage()
if err != nil {
log.Printf("Player%d ReadMessage %d error:%v\n", id, messageType, err)
onConnectionClose(id)
break
}
go player.handleMsg(messageType, p)
}
}
func getNewID() uint32 {
id := idCounter
idCounter += 1
return id
}
type PlayerOnServer struct {
message.Player
conn *websocket.Conn
remoteAddress string
newMoving uint8
}
func NewPlayer(conn *websocket.Conn, remoteAddr string, id uint32, x, y float32, hue uint8) *PlayerOnServer {
return &PlayerOnServer{
Player: message.Player{
ID: id,
X: x,
Y: y,
Hue: hue,
Moving: 0,
},
conn: conn,
remoteAddress: remoteAddr,
newMoving: 0,
}
}
func (p *PlayerOnServer) sendMsgWithData(data []byte) {
err := p.conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseMessage) {
onConnectionClose(p.ID)
}
log.Printf("SendMsgWithData error:%v\n", err)
return
}
}
func (p *PlayerOnServer) sendMsg(msg message.Msg) {
bytes, err := msg.Encode()
if err != nil {
log.Printf("SendMsg error: %v\n", err)
}
p.sendMsgWithData(bytes)
}
func (p *PlayerOnServer) update(deltaTime float64) {
dx := 0.0
dy := 0.0
for dir := 0; dir < directionCount; dir++ {
if ((p.Moving >> dir) & 1) != 0 {
dx += float64(directions[dir][0])
dy += float64(directions[dir][1])
}
}
l := dx*dx + dy*dy
if l != 0 {
length := math.Sqrt(l)
dx /= length
dy /= length
}
p.X = float32(properMod(float64(p.X)+dx*playerSpeed*deltaTime, worldWidth))
p.Y = float32(properMod(float64(p.Y)+dy*playerSpeed*deltaTime, worldHeight))
}
func properMod(a float64, b float64) float64 {
return math.Mod(math.Mod(a, b)+b, b)
}
func (p *PlayerOnServer) handleMsg(messageType int, data []byte) {
if messageType != websocket.BinaryMessage {
log.Println("received not BinaryMessage")
return
}
ammaMovingMsg := message.AmmaMovingMsgStruct{}
pingMsg := message.PingMsgStruct{}
if err := ammaMovingMsg.Decode(data); err == nil {
// log.Printf("processing AmmaMovingMsg: %v\n", msg)
if ammaMovingMsg.Start == 1 {
p.newMoving |= (1 << ammaMovingMsg.Direction)
} else {
p.newMoving &= ^(1 << ammaMovingMsg.Direction)
}
} else if err := pingMsg.Decode(data); err == nil {
pingIDs[p.ID] = pingMsg.Timestamp
} else {
fmt.Printf("received bogus-amogus message from player %d\n", p.ID)
p.conn.Close()
return
}
}
func onConnectionClose(id uint32) {
mu.Lock()
defer mu.Unlock()
delete(players, id)
delete(joinedIDs, id)
leftIDs[id] = struct{}{}
}