-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.lua
More file actions
733 lines (576 loc) · 22.9 KB
/
server.lua
File metadata and controls
733 lines (576 loc) · 22.9 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
local Config = {}
----------------------------------------------------------------------------------------------------------------------
-- Priority list can be any identifier. (hex steamid, steamid32, ip) Integer = power over other priorities
-- A lot of the steamid converting websites are broken rn and give you the wrong steamid. I use https://steamid.xyz/ with no problems.
Config.Priority = {
["steam:11000010add451c"] = 100 -- Bacon_Space
}
Config.RequireSteam = true
Config.PriorityOnly = false -- whitelist only server
-- Needed this because sometimes canceling events don't work... for me at least
-- callback(isbanned, banmsg)
Config.IsBanned = function(src, callback)
callback(false)
--[[
example:
local banned, reason = exports["admin"]:IsPlayerBanned(src)
return banned, (banned and reason or nil)
]]
end
-- easy localization
Config.Language = {
joining = "[BSRP] [Auto Queue] Joining...",
connecting = "[BSRP] [Auto Queue] Connecting...",
idrr = "[BSRP] [Auto Queue] Error: Couldn't retrieve any of your id's, try restarting.",
err = "[BSRP] [Auto Queue] There was an error",
pos = "[Auto Queue] You are %d/%d in queue",
connectingerr = "[BSRP] [Auto Queue] Error adding you to connecting list",
timedout = "[BSRP] [Auto Queue] Timed out?",
wlonly = "[BSRP] [Auto Queue] You must be whitelisted to join this server",
banned = "[BSRP] [Auto Queue] You are banned | reason: %s",
steam = "[BSRP] [Auto Queue] Error: Steam must be running"
}
-----------------------------------------------------------------------------------------------------------------------
local Queue = {}
Queue.QueueList = {}
Queue.PlayerList = {}
Queue.PlayerCount = 0
Queue.Priority = {}
Queue.Connecting = {}
Queue.ThreadCount = 0
local debug = false
local displayQueue = false
local initHostName = false
local maxPlayers = 31
local tostring = tostring
local tonumber = tonumber
local ipairs = ipairs
local pairs = pairs
local print = print
local string_sub = string.sub
local string_format = string.format
local string_lower = string.lower
local math_abs = math.abs
local math_floor = math.floor
local os_time = os.time
local table_insert = table.insert
local table_remove = table.remove
for k,v in pairs(Config.Priority) do
Queue.Priority[string_lower(k)] = v
end
-- converts hex steamid to SteamID 32
function Queue:HexIdToSteamId(hexId)
local cid = math_floor(tonumber(string_sub(hexId, 7), 16))
local steam64 = math_floor(tonumber(string_sub( cid, 2)))
local a = steam64 % 2 == 0 and 0 or 1
local b = math_floor(math_abs(6561197960265728 - steam64 - a) / 2)
local sid = "steam_0:"..a..":"..(a == 1 and b -1 or b)
return sid
end
function Queue:IsSteamRunning(src)
for k,v in ipairs(GetPlayerIdentifiers(src)) do
if string.sub(v, 1, 5) == "steam" then
return true
end
end
return false
end
function Queue:DebugPrint(msg)
if debug then
msg = "QUEUE: " .. tostring(msg)
print(msg)
end
end
function Queue:IsInQueue(ids, rtnTbl, bySource, connecting)
for genericKey1,genericValue1 in ipairs(connecting and self.Connecting or self.QueueList) do
local inQueue = false
if not bySource then
for genericKey2,genericValue2 in ipairs(genericValue1.ids) do
if inQueue then break end
for genericKey3,genericValue3 in ipairs(ids) do
if genericValue3 == genericValue2 then inQueue = true break end
end
end
else
inQueue = ids == genericValue1.source
end
if inQueue then
if rtnTbl then
return genericKey1, connecting and self.Connecting[genericKey1] or self.QueueList[genericKey1]
end
return true
end
end
return false
end
function Queue:IsPriority(ids)
for k,v in ipairs(ids) do
v = string_lower(v)
if string_sub(v, 1, 5) == "steam" and not self.Priority[v] then
local steamid = self:HexIdToSteamId(v)
if self.Priority[steamid] then return self.Priority[steamid] ~= nil and self.Priority[steamid] or false end
end
if self.Priority[v] then return self.Priority[v] ~= nil and self.Priority[v] or false end
end
end
function Queue:AddToQueue(ids, connectTime, name, src, deferrals)
if self:IsInQueue(ids) then return end
local tmp = {
source = src,
ids = ids,
name = name,
firstconnect = connectTime,
priority = self:IsPriority(ids) or (src == "debug" and math.random(0, 15)),
timeout = 0,
deferrals = deferrals
}
local _pos = false
local queueCount = self:GetSize() + 1
for k,v in ipairs(self.QueueList) do
if tmp.priority then
if not v.priority then
_pos = k
else
if tmp.priority > v.priority then
_pos = k
end
end
if _pos then
self:DebugPrint(string_format("%s[%s] was Golden Ticked and placed %d/%d in queue", tmp.name, ids[1], _pos, queueCount))
break
end
end
end
if not _pos then
_pos = self:GetSize() + 1
self:DebugPrint(string_format("%s[%s] was placed %d/%d in queue", tmp.name, ids[1], _pos, queueCount))
end
table_insert(self.QueueList, _pos, tmp)
end
function Queue:RemoveFromQueue(ids, bySource)
if self:IsInQueue(ids, false, bySource) then
local pos, data = self:IsInQueue(ids, true, bySource)
table_remove(self.QueueList, pos)
end
end
function Queue:GetSize()
return #self.QueueList
end
function Queue:ConnectingSize()
return #self.Connecting
end
function Queue:IsInConnecting(ids, bySource, refresh)
local inConnecting, tbl = self:IsInQueue(ids, refresh and true or false, bySource and true or false, true)
if not inConnecting then return false end
if refresh and inConnecting and tbl then
self.Connecting[inConnecting].timeout = 0
end
return true
end
function Queue:RemoveFromConnecting(ids, bySource)
for genericKey1,genericValue1 in ipairs(self.Connecting) do
local inConnecting = false
if not bySource then
for genericKey2,genericValue2 in ipairs(genericValue1.ids) do
if inConnecting then break end
for genericKey3,genericValue3 in ipairs(ids) do
if genericValue3 == genericValue2 then inConnecting = true break end
end
end
else
inConnecting = ids == genericValue1.source
end
if inConnecting then
table_remove(self.Connecting, genericKey1)
return true
end
end
return false
end
function Queue:AddToConnecting(ids, ignorePos, autoRemove, done)
local function removeFromQueue()
if not autoRemove then return end
done(Config.Language.connectingerr)
self:RemoveFromConnecting(ids)
self:RemoveFromQueue(ids)
self:DebugPrint("Player could not be added to the connecting list")
end
if self:ConnectingSize() >= 5 then removeFromQueue() return false end
if ids[1] == "debug" then
table_insert(self.Connecting, {source = ids[1], ids = ids, name = ids[1], firstconnect = ids[1], priority = ids[1], timeout = 0})
return true
end
if self:IsInConnecting(ids) then self:RemoveFromConnecting(ids) end
local pos, data = self:IsInQueue(ids, true)
if not ignorePos and (not pos or pos > 1) then removeFromQueue() return false end
table_insert(self.Connecting, data)
self:RemoveFromQueue(ids)
return true
end
function Queue:GetIds(src)
local ids = GetPlayerIdentifiers(src)
local ip = GetPlayerEndpoint(src)
ids = (ids and ids[1]) and ids or (ip and {"ip:" .. ip} or false)
ids = ids ~= nil and ids or false
if ids and #ids > 1 then
for k,v in ipairs(ids) do
if string.sub(v, 1, 3) == "ip:" then table_remove(ids, k) end
end
end
return ids
end
function Queue:AddPriority(id, power)
if not id then return false end
if type(id) == "table" then
for k, v in pairs(id) do
if k and type(k) == "string" and v and type(v) == "number" then
self.Priority[k] = v
else
self:DebugPrint("Error adding a Golden Ticket id, invalid data passed")
return false
end
end
return true
end
power = (power and type(power) == "number") and power or 10
self.Priority[string_lower(id)] = power
return true
end
function Queue:RemovePriority(id)
if not id then return false end
self.Priority[id] = nil
return true
end
function Queue:UpdatePosData(src, ids, deferrals)
local pos, data = self:IsInQueue(ids, true)
self.QueueList[pos].source = src
self.QueueList[pos].ids = ids
self.QueueList[pos].timeout = 0
self.QueueList[pos].deferrals = deferrals
end
function Queue:NotFull(firstJoin)
local canJoin = self.PlayerCount + self:ConnectingSize() < maxPlayers and self:ConnectingSize() < 5
if firstJoin and canJoin then canJoin = self:GetSize() <= 1 end
return canJoin
end
function Queue:SetPos(ids, newPos)
local pos, data = self:IsInQueue(ids, true)
table_remove(self.QueueList, pos)
table_insert(self.QueueList, newPos, data)
Queue:DebugPrint("Set " .. data.name .. "[" .. data.ids[1] .. "] pos to " .. newPos)
end
-- export
function AddPriority(id, power)
return Queue:AddPriority(id, power)
end
-- export
function RemovePriority(id)
return Queue:RemovePriority(id)
end
Citizen.CreateThread(function()
local function playerConnect(name, setKickReason, deferrals)
maxPlayers = GetConvarInt("sv_maxclients", 30)
debug = GetConvar("sv_debugqueue", "true") == "true" and true or false
displayQueue = GetConvar("sv_displayqueue", "true") == "true" and true or false
initHostName = not initHostName and GetConvar("sv_hostname") or initHostName
local src = source
local ids = Queue:GetIds(src)
local connectTime = os_time()
local connecting = true
deferrals.defer()
Citizen.CreateThread(function()
while connecting do
Citizen.Wait(500)
if not connecting then return end
deferrals.update(Config.Language.connecting)
end
end)
Citizen.Wait(2000)
local function done(msg)
connecting = false
Citizen.CreateThread(function()
if msg then deferrals.update(tostring(msg) and tostring(msg) or "") end
Citizen.Wait(1000)
if not msg then deferrals.done() else deferrals.done(tostring(msg) and tostring(msg) or "") CancelEvent() end
end)
end
local function update(msg)
connecting = false
deferrals.update(tostring(msg) and tostring(msg) or "")
end
if not ids then
-- prevent joining
done(Config.Language.iderr)
CancelEvent()
Queue:DebugPrint("Dropped " .. name .. ", couldn't retrieve any of their id's")
return
end
if Config.RequireSteam and not Queue:IsSteamRunning(src) then
done(Config.Language.steam)
CancelEvent()
return
end
local banned
Config.IsBanned(src, function(_banned, _reason)
banned = _banned
if _banned then
done(string.format(Config.Language.banned, _reason and _reason or "Unknown"))
Queue:RemoveFromQueue(ids)
Queue:RemoveFromConnecting(ids)
end
end)
while banned == nil do Citizen.Wait(0) end
if banned then CancelEvent() return end
local reason = "You were kicked from joining the queue"
local function setReason(msg)
reason = tostring(msg)
end
TriggerEvent("queue:playerJoinQueue", src, setReason)
if WasEventCanceled() then
done(reason)
Queue:RemoveFromQueue(ids)
Queue:RemoveFromConnecting(ids)
CancelEvent()
return
end
if Config.PriorityOnly and not Queue:IsPriority(ids) then done(Config.Language.wlonly) return end
local rejoined = false
if Queue:IsInQueue(ids) then
rejoined = true
Queue:UpdatePosData(src, ids, deferrals)
Queue:DebugPrint(string_format("%s[%s] has rejoined queue after cancelling", name, ids[1]))
else
Queue:AddToQueue(ids, connectTime, name, src, deferrals)
end
if Queue:IsInConnecting(ids, false, true) then
Queue:RemoveFromConnecting(ids)
if Queue:NotFull() then
local added = Queue:AddToConnecting(ids, true, true, done)
if not added then CancelEvent() return end
done()
return
else
Queue:AddToQueue(ids, connectTime, name, src, deferrals)
Queue:SetPos(ids, 1)
end
end
local pos, data = Queue:IsInQueue(ids, true)
if not pos or not data then
done(Config.Language.err .. "[3]")
RemoveFromQueue(ids)
RemoveFromConnecting(ids)
CancelEvent()
return
end
if Queue:NotFull(true) then
-- let them in the server
local added = Queue:AddToConnecting(ids, true, true, done)
if not added then CancelEvent() return end
done()
Queue:DebugPrint(name .. "[" .. ids[1] .. "] is loading into the server")
return
end
update(string_format(Config.Language.pos, pos, Queue:GetSize()))
Citizen.CreateThread(function()
if rejoined then return end
Queue.ThreadCount = Queue.ThreadCount + 1
local dotCount = 0
while true do
Citizen.Wait(1000)
local dots = ""
dotCount = dotCount + 1
if dotCount > 3 then dotCount = 0 end
-- hopefully people will notice this and realize they don't have to keep reconnecting...
for i = 1 , dotCount do dots = dots .. "." end
local pos, data = Queue:IsInQueue(ids, true)
-- will return false if not in queue; timed out?
if not pos or not data then
if data and data.deferrals then data.deferrals.done(Config.Language.timedout) end
CancelEvent()
Queue:RemoveFromQueue(ids)
Queue:RemoveFromConnecting(ids)
Queue.ThreadCount = Queue.ThreadCount - 1
return
end
if pos <= 1 and Queue:NotFull() then
-- let them in the server
local added = Queue:AddToConnecting(ids)
data.deferrals.update(Config.Language.joining)
Citizen.Wait(500)
if not added then
data.deferrals.done(Config.Language.connectingerr)
CancelEvent()
Queue.ThreadCount = Queue.ThreadCount - 1
return
end
data.deferrals.done()
Queue:RemoveFromQueue(ids)
Queue.ThreadCount = Queue.ThreadCount - 1
Queue:DebugPrint(name .. "[" .. ids[1] .. "] is loading into the server")
return
end
-- send status update
local msg = string_format(Config.Language.pos .. "%s", pos, Queue:GetSize(), dots)
data.deferrals.update(msg)
end
end)
end
AddEventHandler("playerConnecting", playerConnect)
local function checkTimeOuts()
local i = 1
while i <= Queue:GetSize() do
local data = Queue.QueueList[i]
local lastMsg = GetPlayerLastMsg(data.source)
if lastMsg == 0 or lastMsg >= 30000 then
data.timeout = data.timeout + 1
else
data.timeout = 0
end
-- check just incase there is invalid data
if not data.ids or not data.name or not data.firstconnect or data.priority == nil or not data.source then
data.deferrals.done(Config.Language.err .. "[1]")
table_remove(Queue.QueueList, i)
Queue:DebugPrint(tostring(data.name) .. "[" .. tostring(data.ids[1]) .. "] was removed from the queue because it had invalid data")
elseif (data.timeout >= 120) and data.source ~= "debug" and os_time() - data.firstconnect > 5 then
-- remove by source incase they rejoined and were duped in the queue somehow
data.deferrals.done(Config.Language.err .. "[2]")
Queue:RemoveFromQueue(data.source, true)
Queue:RemoveFromConnecting(data.source, true)
Queue:DebugPrint(data.name .. "[" .. data.ids[1] .. "] was removed from the queue because they timed out")
else
i = i + 1
end
end
i = 1
while i <= Queue:ConnectingSize() do
local data = Queue.Connecting[i]
local lastMsg = GetPlayerLastMsg(data.source)
data.timeout = data.timeout + 1
if ((data.timeout >= 300 and lastMsg >= 35000) or data.timeout >= 340) and data.source ~= "debug" and os_time() - data.firstconnect > 5 then
Queue:RemoveFromQueue(data.source, true)
Queue:RemoveFromConnecting(data.source, true)
Queue:DebugPrint(data.name .. "[" .. data.ids[1] .. "] was removed from the connecting queue because they timed out")
else
i = i + 1
end
end
local qCount = Queue:GetSize()
-- show queue count in server name
if displayQueue and initHostName then SetConvar("sv_hostname", (qCount > 0 and "[" .. tostring(qCount) .. "] " or "") .. initHostName) end
SetTimeout(1000, checkTimeOuts)
end
checkTimeOuts()
end)
local function playerActivated()
local src = source
local ids = Queue:GetIds(src)
if not Queue.PlayerList[src] then
Queue.PlayerCount = Queue.PlayerCount + 1
Queue.PlayerList[src] = true
Queue:RemoveFromQueue(ids)
Queue:RemoveFromConnecting(ids)
end
end
RegisterServerEvent("Queue:playerActivated")
AddEventHandler("Queue:playerActivated", playerActivated)
local function playerDropped()
local src = source
local ids = Queue:GetIds(src)
if Queue.PlayerList[src] then
Queue.PlayerCount = Queue.PlayerCount - 1
Queue.PlayerList[src] = nil
Queue:RemoveFromQueue(ids)
Queue:RemoveFromConnecting(ids)
end
end
AddEventHandler("playerDropped", playerDropped)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if exports and exports.connectqueue then TriggerEvent("queue:onReady") return end
end
end)
-- debugging / testing commands
local testAdds = 0
AddEventHandler("rconCommand", function(command, args)
-- adds a fake player to the queue for debugging purposes, this will freeze the queue
if command == "addq" then
print("==ADDED FAKE QUEUE==")
Queue:AddToQueue("[BSRP] [Auto Queue] ",{"steam:110000103fd1bb1"..testAdds}, os_time(), "Fake Player", "debug")
testAdds = testAdds + 1
CancelEvent()
-- removes targeted id from the queue
elseif command == "removeq" then
if not args[1] then return end
print("[BSRP] [Auto Queue] REMOVED " .. Queue.QueueList[tonumber(args[1])].name .. " FROM THE QUEUE")
table_remove(Queue.QueueList, args[1])
CancelEvent()
-- print the current queue list
elseif command == "printq" then
print("[BSRP] [Auto Queue] ==CURRENT QUEUE LIST==")
for k,v in ipairs(Queue.QueueList) do
print("[BSRP] [Auto Queue] "..k .. ": [src: " .. v.source .. "] " .. v.name .. "[" .. v.ids[1] .. "] | Priority: " .. (tostring(v.priority and true or false)) .. " | Last Msg: " .. (v.source ~= "debug" and GetPlayerLastMsg(v.source) or "debug") .. " | Timeout: " .. v.timeout)
end
CancelEvent()
-- adds a fake player to the connecting list
elseif command == "addc" then
print("[BSRP] [Auto Queue] ==ADDED FAKE CONNECTING QUEUE==")
Queue:AddToConnecting({"debug"})
CancelEvent()
-- removes a player from the connecting list
elseif command == "removec" then
print("[BSRP] [Auto Queue] ==REMOVED FAKE CONNECTING QUEUE==")
if not args[1] then return end
table_remove(Queue.Connecting, args[1])
CancelEvent()
-- prints a list of players that are connecting
elseif command == "printc" then
print("[BSRP] [Auto Queue] ==CURRENT CONNECTING LIST==")
for k,v in ipairs(Queue.Connecting) do
print(k .. ": [src: " .. v.source .. "] " .. v.name .. "[" .. v.ids[1] .. "] | Priority: " .. (tostring(v.priority and true or false)) .. " | Last Msg: " .. (v.source ~= "debug" and GetPlayerLastMsg(v.source) or "debug") .. " | Timeout: " .. v.timeout)
end
CancelEvent()
-- prints a list of activated players
elseif command == "printl" then
for k,v in pairs(Queue.PlayerList) do
print(k .. ": " .. tostring(v))
end
CancelEvent()
-- prints a list of priority id's
elseif command == "printp" then
print("[BSRP] [Auto Queue] ==CURRENT PRIORITY LIST==")
for k,v in pairs(Queue.Priority) do
print(k .. ": " .. tostring(v))
end
CancelEvent()
-- prints the current player count
elseif command == "printcount" then
print("[BSRP] [Auto Queue] Player Count: " .. Queue.PlayerCount)
CancelEvent()
elseif command == "printt" then
print("[BSRP] [Auto Queue] Thread Count: " .. Queue.ThreadCount)
CancelEvent()
end
end)
-- prevent duplicating queue count in server name
AddEventHandler("onResourceStop", function(resource)
if displayQueue and resource == GetCurrentResourceName() then SetConvar("sv_hostname", initHostName) end
end)
--[[
AddEventHandler("queue:playerJoinQueue", function(src, setKickReason)
setKickReason("No, you can't join")
CancelEvent()
end)
exports.connectqueue:AddPriority("steam:110000#####", 50)
exports.connectqueue:AddPriority("ip:127.0.0.1", 50)
exports.connectqueue:AddPriority("STEAM_0:1:########", 50)
local prioritize = {
["STEAM_0:1:########"] = 10,
["ip:127.0.0.1"] = 20,
["steam:110000#####"] = 100
}
exports.connectqueue:AddPriority(prioritize)
exports.connectqueue:RemovePriority("STEAM_0:1:########")
set sv_debugqueue true
set sv_displayqueue true
]]