-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.lua
190 lines (174 loc) · 6.63 KB
/
server.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
comms = {}
peer = {
peers = {},
getPeers = function()
local data = {}
for k in pairs(peer.peers) do
if peer.peers[k] then
data[k] = peer.peers[k]
end
end
return data
end
}
function listen(n, cb)
comms[n] = cb
end
function tell(s, n, ...)
TriggerClientEvent('peer:client:listen', s, n, ...)
end
listen('peer:server:new', function()
peer.peers[tostring(source)] = {
entity = GetPlayerPed(source),
channels = {
['all'] = true
}
}
for k in pairs (peer.peers) do
if peer.peers[k] then
tell(tonumber(k), 'peer:client:new', source, NetworkGetNetworkIdFromEntity(GetPlayerPed(source)), GetPlayerName(source), GetPlayerIdentifierByType(source, 'license'))
end
end
return { source, GetPlayerName(source), GetPlayerIdentifierByType(source, 'license') }
end)
listen('peer:server:channel', function(t, c)
if not peer.peers[tostring(source)] then print('[browns_peerSync]: attempt to perform a peer action on a non peer, do "peer.new()" to initiate peer first, [ERROR]') return end
if t == 'add' then
peer.peers[tostring(source)].channels[c] = true
end
if t == 'remove' then
if peer.peers[tostring(source)].channels[c] then
peer.peers[tostring(source)].channels[c] = nil
end
end
return { true }
end)
listen('peer:server:trigger', function(s, c, e, ...)
if not peer.peers[tostring(source)] then print('[browns_peerSync]: attempt to perform a peer action on a non peer, do "peer.new()" to initiate peer first, [ERROR]') return end
local channels, channel, continue = {}, false, false
if c and type(c) == 'table' then
for i = 1, #c do
channels[c[i]] = true
channel = true
end
end
if channel then
for check in pairs(peer.peers[tostring(source)].channels) do
print(check)
if channels[check] then
continue = true
break
end
end
else
print('[browns_peerSync]: channel table invalid or not found, [ERROR]') return
end
if not continue then print('[browns_peerSync]: peer attempted to sync to a channel that they are not connected to, [ERROR]') return end
if not s then
for k in pairs (peer.peers) do
if tonumber(k) ~= source then
if peer.peers[k] then
if channel then
for a in pairs(peer.peers[k].channels) do
if peer.peers[k].channels[a] and channels[a] then
tell(tonumber(k), e, table.unpack({...}))
break
end
end
else
tell(tonumber(k), e, table.unpack({...}))
end
end
end
end
return
end
for k in pairs (peer.peers) do
if peer.peers[k] then
if channel then
for a in pairs(peer.peers[k].channels) do
if channels[a] then
tell(tonumber(k), e, table.unpack({...}))
end
end
else
tell(tonumber(k), e, table.unpack({...}))
end
end
end
return { true }
end)
listen('peer:server:drop', function()
if not peer.peers[tostring(source)] then print('[browns_peerSync]: attempt to perform a peer action on a non peer, do "peer.new()" to initiate peer first, [ERROR]') return end
for k in pairs (peer.peers) do
if peer.peers[k] then
tell(tonumber(k), 'peer:client:drop', source)
end
end
peer.peers[tostring(source)] = nil
return { true }
end)
listen('peer:server:setmetadata', function(k, v)
if not peer.peers[tostring(source)] then print('[browns_peerSync]: attempt to perform a peer action on a non peer, do "peer.new()" to initiate peer first, [ERROR]') return end
for i in pairs (peer.peers) do
if peer.peers[i] then
tell(tonumber(i), 'peer:client:setmetadata', source, k, v)
end
end
return { true }
end)
listen('peer:server:setdata', function(k, v)
if not peer.peers[tostring(source)] then print('[browns_peerSync]: attempt to perform a peer action on a non peer, do "peer.new()" to initiate peer first, [ERROR]') return end
for i in pairs (peer.peers) do
if peer.peers[i] then
tell(tonumber(i), 'peer:client:setdata', k, v)
end
end
return { true }
end)
listen('peer:server:net', function(s, n, c, ...)
if not peer.peers[tostring(source)] then print('[browns_peerSync]: attempt to perform a peer action on a non peer, do "peer.new()" to initiate peer first, [ERROR]') return end
if type(c) ~= 'table' then print('[browns_peerSync]: channels data is not a table [ERROR]') return end
local channels = {}
for i = 1, #c do
channels[c[i]] = true
end
if s then
for k in pairs (peer.peers) do
if peer.peers[k] then
for a in pairs(peer.peers[k].channels) do
if channels[a] then
tell(tonumber(k), 'peer:client:on', n, ...)
end
end
end
end
end
if not s then
for k in pairs (peer.peers) do
if k ~= tostring(source) then
if peer.peers[k] then
for a in pairs(peer.peers[k].channels) do
if channels[a] then
tell(tonumber(k), 'peer:client:on', n, ...)
end
end
end
end
end
end
return { true }
end)
listen('peer:server:direct', function(n, i, ...)
if not peer.peers[tostring(source)] then print('[browns_peerSync]: attempt to perform a peer action on a non peer, do "peer.new()" to initiate peer first, [ERROR]') return end
if not peer.peers[tostring(i)] then print('[browns_peerSync]: attempt to send a direct to a non synced peer [ERROR]') return end
tell(i, 'peer:client:on', n, ...)
return { true }
end)
RegisterNetEvent('peer:server:listen', function(n, ...)
if comms[n] then
local data = comms[n](table.unpack({...}))
TriggerClientEvent('peer:client:tell', source, n, data)
end
end)
return peer