-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
187 lines (166 loc) · 4.57 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
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"gopkg.in/olahol/melody.v1"
)
func serverSetupRoutes() *melody.Melody {
m := melody.New()
m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
m.HandleMessageBinary(func(s *melody.Session, msgb []byte) {
fmt.Println("->", string(msgb))
msg := strings.Split(string(msgb), ":")
switch msg[0] {
case "login": // -> login:user,pass
serverLogin(s, msg[1]) // <- player:{player}
case "create_mesh":
serverCreateMesh(m, s, msg[1])
case "world_around":
serverSendWorldAround(s)
case "walk_to":
serverWalkTo(s, msg[1])
case "notify_movement":
serverNotifyMovement(s, msg[1])
case "delete_mesh":
serverDeleteMesh(m, s, msg[1])
default:
fmt.Println("Unknown command", msg)
}
})
m.HandleConnect(func(s *melody.Session) {
fmt.Println("New connection")
})
return m
}
/* -------- */
/* COMMANDS */
/* -------- */
/*
Available client commands:
- login:ok
- player:<x>,<z>
- newmesh:<type>,<x>,<z>,<rotation>
*/
func serverLogin(s *melody.Session, data string) {
login := strings.Split(data, ",")
p := &tPlayer{}
if login[0] == "jairo" && login[1] == "new" { // TODO proper errors
p = &tPlayer{
Name: "Jairo",
Position: tPos{X: 0, Z: 0},
}
db.newPlayer(p)
s.Set("posx", 0)
s.Set("posz", 0)
send(s, "login:ok")
serverSendWorldAround(s)
} else if login[1] == "get" {
p = db.getPlayer("1")
x := strconv.FormatInt(p.Position.X, 10)
z := strconv.FormatInt(p.Position.Z, 10)
s.Set("posx", p.Position.X)
s.Set("posz", p.Position.Z)
send(s, "player:"+x+","+z)
serverSendWorldAround(s)
}
s.Set("player", p.ID)
}
func serverCreateMesh(m *melody.Melody, s *melody.Session, d string) {
data := strings.Split(d, ",")
t, _ := strconv.ParseInt(data[0], 10, 64)
x, _ := strconv.ParseInt(data[1], 10, 64)
z, _ := strconv.ParseInt(data[2], 10, 64)
y, _ := strconv.ParseInt(data[3], 10, 64)
mesh := &tMesh{
Type: t,
Position: tPos{
X: x,
Z: z,
},
VerticalLevel: y,
Walkable: true,
WalkingCost: 1,
Rotation: data[4],
}
db.newMesh(mesh)
broadcast(m, s, "newmesh:"+data[0]+","+data[1]+","+data[2]+","+data[3]+","+data[4])
}
func serverSendWorldAround(s *melody.Session) {
var x, z int64
ix, ok := s.Get("posx")
if !ok {
x = int64(0)
} else {
x = ix.(int64)
}
iz, ok := s.Get("posz")
if !ok {
z = int64(0)
} else {
z = iz.(int64)
}
meshes := []string{}
db.getNearbyMeshes(x, z, 5, &meshes, nil) // TODO calculate proper screen distance
m := &tMesh{}
for _, v := range meshes {
db.getMesh(m, v)
t := strconv.FormatInt(m.Type, 10)
x := strconv.FormatInt(m.Position.X, 10)
z := strconv.FormatInt(m.Position.Z, 10)
y := strconv.FormatInt(m.VerticalLevel, 10)
send(s, "newmesh:"+t+","+x+","+z+","+y+","+m.Rotation)
}
}
func serverWalkTo(s *melody.Session, d string) {
data := strings.Split(d, ",")
x, _ := strconv.ParseInt(data[0], 10, 64)
z, _ := strconv.ParseInt(data[1], 10, 64)
pIDi, _ := s.Get("player")
pID := pIDi.(string)
player := db.getPlayer(pID)
origin := db.getMeshByPos(player.Position.X, player.Position.Z, 0)
destination := db.getMeshByPos(x, z, 0)
// do nothing if origin or destination don't exist or it's the same position
if origin == nil || destination == nil || (origin.Position.X == destination.Position.X && origin.Position.Z == destination.Position.Z) {
return
}
steps := movementCalculatePath(destination, origin)
steps = steps[1:] // remove first step (it is player's current position)
for _, step := range steps {
send(s, "move:"+strconv.FormatInt(step.Position.X, 10)+","+strconv.FormatInt(step.Position.Z, 10))
}
s.Set("posx", x)
s.Set("posz", z)
serverSendWorldAround(s)
}
func serverDeleteMesh(m *melody.Melody, s *melody.Session, d string) {
data := strings.Split(d, ",")
x, _ := strconv.ParseInt(data[0], 10, 64)
z, _ := strconv.ParseInt(data[1], 10, 64)
y, _ := strconv.ParseInt(data[2], 10, 64)
db.deleteMeshByPos(x, z, y)
broadcast(m, s, "deletemesh:"+data[0]+","+data[1]+","+data[2])
}
func serverNotifyMovement(s *melody.Session, d string) {
// TODO check movement validity
// TODO update player's world on movement
data := strings.Split(d, ",")
x, _ := strconv.ParseInt(data[0], 10, 64)
z, _ := strconv.ParseInt(data[1], 10, 64)
pIDi, _ := s.Get("player")
pID := pIDi.(string)
player := db.getPlayer(pID)
player.Position.X = x
player.Position.Z = z
db.setPlayerPos(player)
}
func broadcast(m *melody.Melody, s *melody.Session, d string) {
fmt.Println("<--", d)
m.Broadcast([]byte(d))
}
func send(s *melody.Session, d string) {
fmt.Println("<-", d)
s.Write([]byte(d))
}