2020-01-22 14:25:58 +00:00
|
|
|
-- Copyright (c) 2020 David Vogel
|
|
|
|
--
|
|
|
|
-- This software is released under the MIT License.
|
|
|
|
-- https://opensource.org/licenses/MIT
|
|
|
|
|
2016-10-05 12:30:35 +00:00
|
|
|
-- Calculate the level from the given XP
|
2018-08-03 09:22:18 +00:00
|
|
|
function D3stats.CalculateLevel(XP)
|
2016-10-05 12:30:35 +00:00
|
|
|
local Level = 1
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
for key, value in pairs(D3stats.Levels) do
|
2016-10-05 12:30:35 +00:00
|
|
|
if value.XP_needed <= XP then
|
|
|
|
Level = key
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Level
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if the level has the given permission
|
2018-08-03 09:22:18 +00:00
|
|
|
function D3stats.LevelCheckPermission(Level, Permission)
|
2016-10-05 12:30:35 +00:00
|
|
|
local Granted = false
|
|
|
|
|
2016-10-09 16:11:56 +00:00
|
|
|
-- If the permission is not on the list, allow it
|
2018-08-03 09:22:18 +00:00
|
|
|
if not D3stats.Permissions[Permission] then
|
2016-10-09 16:11:56 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
for key, value in pairs(D3stats.Levels) do
|
2016-10-05 12:30:35 +00:00
|
|
|
if key <= Level then
|
|
|
|
if value.Permissions and value.Permissions[Permission] then
|
|
|
|
Granted = value.Permissions[Permission]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Granted
|
|
|
|
end
|
|
|
|
|
2016-10-09 16:11:56 +00:00
|
|
|
-- Returns what level is needed for the given permission
|
2018-08-03 09:22:18 +00:00
|
|
|
function D3stats.GetPermissionLevel(Permission)
|
2016-10-09 16:11:56 +00:00
|
|
|
local Level
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
if not D3stats.Permissions[Permission] then
|
2016-10-09 16:11:56 +00:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
for key, value in pairs(D3stats.Levels) do
|
2016-10-09 16:11:56 +00:00
|
|
|
if value.Permissions and value.Permissions[Permission] then
|
|
|
|
return key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2016-10-05 12:30:35 +00:00
|
|
|
-- Count the amount of online players who have the permission
|
|
|
|
if SERVER then
|
2018-08-03 09:22:18 +00:00
|
|
|
function D3stats.CountPermissionPlayers(Permission, Team)
|
2016-10-05 12:30:35 +00:00
|
|
|
local Counter = 0
|
|
|
|
|
|
|
|
local players = player.GetAll()
|
2018-08-03 09:22:18 +00:00
|
|
|
for key, ply in pairs(players) do
|
2016-10-09 16:11:56 +00:00
|
|
|
if Team == nil or ply:Team() == Team then
|
2018-08-03 09:22:18 +00:00
|
|
|
if D3stats.LevelCheckPermission(ply:D3stats_GetLevel(), Permission) == true then
|
2016-10-07 07:31:59 +00:00
|
|
|
Counter = Counter + 1
|
|
|
|
end
|
2016-10-05 12:30:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Counter
|
|
|
|
end
|
|
|
|
end
|