-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.lua
289 lines (244 loc) · 8.7 KB
/
client.lua
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
local lume = require("lib.lume")
local sock = require("lib.sock")
local lfg = require("lfg")
local GamePlayer = require("player")
local Projectile = require("projectile")
local Client = {}
Client.__index = Client
local function init(_Client, host, port)
assert(host, "Host is present")
assert(port, "Port is present")
log("STARTING CLIENT CONNECTION.")
local client = sock.newClient(host, port)
local players = {}
local projectiles = {}
local projectile_ams = {}
local reqs = {}
local pjt_col = assert(lfg.get_effect("Blast"))
local pjt_col_type = "fire"
local pjt_col_am = assert(pjt_col.ams[pjt_col_type]["power"])
local self = setmetatable({
client = client,
players = players,
projectiles = projectiles,
projectile_ams = projectile_ams,
pjt_col = pjt_col,
pjt_col_type = pjt_col_type,
pjt_col_am = pjt_col_am,
reqs = reqs,
last_tick = 0,
curr_updates = {},
user = nil,
}, Client)
client:on("connect", function(data)
log("Successfully connected to server: (%s)", data)
end)
client:on("welcome", function(data)
log("Uh oh... got eerie hello from server: %s", data.msg)
client.clid = data.uuid
self.uuid = data.uuid
end)
client:on("disconnect", function(data)
log("[ERROR] DISCONNECTED: %s", data)
end)
client:on("announce_player", function(data)
log("GOT PLAYER ANNOUNCE: %s", ppsl(data))
local player = GamePlayer(data)
-- TODO: remove lfg here and migrate insert elsewhere
local layer = lfg.map.layers["KorePlayers"]
players[data.clid] = player
table.insert(layer.players, player)
end)
client:on("player_update", function(data)
local player = self.players[data.clid]
-- TODO: better handle case of local player
-- issue being the local player isn't in players
if (player) then
player.x = data.x
player.y = data.y
end
end)
client:on("created_projectile", function(data)
assert(data.uuid)
local pjt = Projectile(data)
local layer = lfg.map.layers["KoreProjectiles"]
self.projectiles[data.uuid] = pjt
layer.projectiles[data.uuid] = pjt
end)
client:on("create_player_nack", function(data)
error(data.error)
end)
client:on("create_player_ack", function(data)
log("GOT CREATE_PLAYER_ACK: %s -- %s", ppsl(data), ppsl(data.player))
local req = reqs[data.req_id]
if req then
-- TODO: dedupe this logic with annouce_player
local player = GamePlayer(data.player)
req.user:bootstrap_player(player)
-- TODO: remove lfg here and migrate insert elsewhere
local layer = lfg.map.layers["KorePlayers"]
self.players[self.uuid] = player
self.players[player.uuid] = player
table.insert(layer.players, player)
else
log("ERROR[client_player_ack]: UNKNOWN REQ: %s", ppsl(data))
end
end)
client:on("server_tick", function(data)
--log("GOT SERVER_TICK[%i]: %s", data.tick, ppsl(data))
self.last_tick = data.tick
self.curr_updates = data.updates
-- TODO: better handle bootsrapping. We can see a tick before player announce
-- which can result in none of the players existing
for puid, update in pairs(data.updates) do
if self.players[puid] then
local player = assert(self.players[puid])
player:update_player(update, data.tick)
end
end
for uuid, spjt in pairs(data.pjt_data.projectiles) do
local pjt = self.projectiles[uuid]
if pjt then
pjt:update_projectile(spjt)
else
pjt = self:created_projectile(spjt)
end
end
local layer = lfg.map.layers["KoreProjectiles"]
for uuid, _spjt in pairs(data.pjt_data.expired) do
local pjt = self.projectiles[uuid]
if pjt.collision == "projectile" then
-- TODO: use animation's duration
local am_uuid = lume.uuid()
local payload = {
am = self.pjt_col_am:clone(),
am_uuid = am_uuid,
duration = 0.6,
x = pjt:screen_x(),
y = pjt:screen_y(),
}
self.projectile_ams[am_uuid] = payload
end
self.projectiles[uuid] = nil
layer.projectiles[uuid] = nil
end
for puid, hit in pairs(data.hits) do
if self.players[puid] then
local player = assert(self.players[puid])
player:get_hit(hit)
end
end
for puid, _tval in pairs(data.disconnects) do
if self.players[puid] then
local layer = lfg.map.layers["KorePlayers"]
-- TODO: switch player layer.players to use puid as key
local index
for i, player in ipairs(layer.players) do
if player.uuid == puid then index = i end
end
assert(index)
table.remove(layer.players, index)
self.players[puid] = nil
-- else: disconnected player in last frame, never announced
end
end
if self.user then
local scores = {}
for puid, score in pairs(data.scores) do
local player = self.players[puid]
if player then
table.insert(scores,
{puid=puid, score=score, name=player.name})
end
end
table.sort(scores, function(a, b) return a.score > b.score end)
self.user:update_scores(scores, data.tick)
for _i, msg_data in pairs(data.messages) do
self.user:print(msg_data.msg)
end
end
end)
client:on("announce_players", function(data)
for puid, pobj in pairs(data.players) do
local player = self.players[puid]
if not player then
player = GamePlayer(pobj)
self.players[puid] = player
-- TODO: remove lfg here and migrate insert elsewhere
local layer = lfg.map.layers["KorePlayers"]
table.insert(layer.players, player)
end
player:update_player(pobj, data.tick)
end
end)
log("CONNECTING TO SERVER")
client:connect()
log("FINISHED CONNECTING TO SERVER")
return self
end
setmetatable(Client, {__call = init})
function Client:update(dt)
self.client:update(dt)
local to_remove = {}
for uuid, pjt_am in pairs(self.projectile_ams) do
pjt_am.duration = pjt_am.duration - dt
if pjt_am.duration < 0.0 then
table.insert(to_remove, uuid)
end
pjt_am.am:update(dt)
end
for _i, uuid in ipairs(to_remove) do
self.projectile_ams[uuid] = nil
end
end
-- TODO: client draw hack for projectile collision effects
-- need a better place for the temporary animation objects to be stored
function Client:draw()
for uuid, pjt_am in pairs(self.projectile_ams) do
pjt_am.am:draw(self.pjt_col.sprite, pjt_am.x, pjt_am.y, 0, 0.5, 0.5, 0, 0)
end
end
function Client:create_player(user, payload)
-- TODO: find a better place to set this
self.user = user
--log("CREATING PLAYER: %s", ppsl(payload))
local req_id = lume.uuid()
local req = {
action = "create_player",
req_id = req_id,
user_id = user.uuid,
payload = payload,
}
self.client:send(req.action, req)
self.reqs[req_id] = {user=user, req=req}
end
function Client:send_player_update(_user, updates)
self.client:send("player_update", updates)
end
function Client:send_player_respawn(user)
local payload = {
user_id = user.uuid,
puid = user:puid(),
action = "respawn",
}
self.client:send("player_respawn_request", payload)
end
function Client:create_projectile(m_info)
self.client:send("create_projectile", m_info)
end
function Client:send_msg(msg)
local payload = {msg=msg, clid=self.client.clid}
self.client:send("send_msg", payload)
end
function Client:created_projectile(data, skip_world)
assert(data.uuid)
local pjt = Projectile(data)
if not skip_world then
local layer = lfg.map.layers["KoreProjectiles"]
self.projectiles[data.uuid] = pjt
layer.projectiles[data.uuid] = pjt
self.projectiles[pjt.uuid] = pjt
end
return pjt
end
return Client