-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharednet.lua
174 lines (150 loc) · 5.1 KB
/
sharednet.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
-- client.lua
local SharedNet = class("SharedNet")
function SharedNet:initialize()
self.clientIndex = 0 -- how many clients do we have?
self.networkCallbacks = {}
self.isActive = false
self.timeout = 0 --ms
-- we have to specify what we know before we can use them
self:registerKnownCallbacks()
end
function SharedNet:addNetworkCallback(name, func)
self.networkCallbacks[name] = func
end
function SharedNet:onReceive(data, peer)
local ndata = binser.deserialize(data)
if self.networkCallbacks[ndata.command] then
return self.networkCallbacks[ndata.command](self, ndata.args, peer)
end
end
function SharedNet:onConnect(event)
log.trace(self.class.name, "connect", event.peer)
end
function SharedNet:onDisconnect(event)
log.trace(self.class.name, "disconnect", event.peer)
end
--[[
Uses Paramters:
player
x
y
]]
local function ncAt(self, args,clientid)
local x, y = args.x, args.y
assert(x and y)
x, y = tonumber(x), tonumber(y)
-- @TODO: figure out how to get the player ID from here and then adjust their position accordingly
end
local function ncMakePlayer(self, args, clientid)
for _,player in pairs(args.players) do
local x, y = tonumber(player.x), tonumber(player.y)
assert(x and y)
game.world.players[player.id] = Player(player.x, player.y)
end
end
local function ncProbe(self, args,clientid) --client->server
print("[LUBE|server] client ("..clientid..") probing")
print("<debug>"..self.clientIndex)
if game.world.players[self.clientIndex] ~= nil then
print("[LUBE|server] denied probe from client, index occupied ("..clientid..")")
self:send('ProbeDeny', {reason='index occupied'}, clientid)
else
local build = {
yourID = self.clientIndex + 1,
numPlayers = #game.world.players,
}
self:send('ProbeAccept', build, clientid)
print("[LUBE|server] accepted probe from client ("..clientid..") id#"..self.clientIndex)
local x, y = game.spawnpoint.x, game.spawnpoint.y
game.world.players[game.net.clientIndex] = Player(self.clientIndex, x, y)
-- tell him where all the players are
for k, v in pairs(game.world.players) do
if k ~= game.net.clientIndex then
self:send('MakePlayer', {id=k,x=v.x,y=v.y}, clientid)
end
end
self.clientIndex = self.clientIndex + 1
end
end
local function ncProbeAccept(self, args, clientid) --server->client
print("[LUBE|client] probe accepted id#"..args.player)
local x, y = args.x, args.y
assert(x and y)
x, y = tonumber(x), tonumber(y)
for k, v in pairs(args.players) do
if game.world.players[k] ~= nil then
print("<WARNING|extra>[LUBE|client] suppressed overwriting player index "..args.player)
else
print("<debug>[LUBE|client] built fellow client#"..k.." from probe")
game.world.players[k] = Player(k, v.x, v.y)
end
end
if game.world.players[ndata.params.player] ~= nil then
print("<WARNING>[LUBE|client] suppressed overwriting player index "..ndata.params.player)
else
game.world.players[ndata.params.player] = Player(ndata.params.player, x, y)
end
game.net.myID = ndata.params.player
game.isProbeAccepted = true
print("[LUBE|client] connection made")
end
local function ncProbeDeny(self, args, clientid) --server->client
print("[LUBE|client] probe to server denied, unknown error")
-- @TODO: Unhook everything to a fresh pre-connect state.
end
local function ncUpdate(self, args, clientid) --client->server
-- @TODO: Do not accept "update" requests from clients.
for k, v in pairs(game.world.players) do
if k ~= args.player then
self:send('at', {player=k,x=v.x,y=v.y}, clientid)
end
end
end
local function ncMove(self, args, clientid) --client->server
local x, y = args.x, args.y
assert(x and y)
x, y = tonumber(x), tonumber(y)
game.world.players[args.player]:move(x,y)
-- @TODO: announce/relay
end
local function ncPing(self, args, peer)
--log.fatal(self.class.name, "ping", args, peer)
self:send("Ping", args, peer)
end
function SharedNet:registerKnownCallbacks()
--[[
* ncAt is defined at the local scope so it gets included
* the reason that this function exists is so it can get called every time we make
a new client, it can include all these things (at object instantiation level)
and not when the code gets required (important)
]]
self:addNetworkCallback('At', ncAt)
self:addNetworkCallback('MakePlayer', ncMakePlayer)
self:addNetworkCallback('Probe', ncProbe)
self:addNetworkCallback('ProbeAccept', ncProbeAccept)
self:addNetworkCallback('ProbeDeny', ncProbeDeny)
self:addNetworkCallback('Update', ncUpdate)
self:addNetworkCallback('Move', ncMove)
self:addNetworkCallback('Ping', ncPing)
end
--[[
commander.newCommand("notification", {
time = "uint8_t",
color = {
type = "color",
func = function(cdata)
return {cdata.r, cdata.g, cdata.b}
end
},
text = {
type = "unsigned char",
size = 180,
func = ffi.string
}
})
client_commands.notification = function(data)
log("Received notification: %s", data.text)
push_notification(data.text, data.time, data.color)
end
]]
return SharedNet