2014-12-19 21:08:12 +08:00
|
|
|
local SUPPORTER_MESSAGE = "JetBoom says: thank you for supporting my gamemodes!"
|
2014-11-07 13:03:40 +08:00
|
|
|
|
|
|
|
local CACHE = {
|
|
|
|
MaxSize = 128,
|
2014-12-19 21:08:12 +08:00
|
|
|
BufferTime = 6,
|
2014-11-07 13:03:40 +08:00
|
|
|
Cache = {}
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:08:12 +08:00
|
|
|
local Buffer = {}
|
|
|
|
|
2014-11-07 13:03:40 +08:00
|
|
|
function CACHE:Set(steamid, memberlevel, nolookup)
|
|
|
|
if nolookup then
|
|
|
|
table.insert(self.Cache, {steamid, memberlevel})
|
|
|
|
else
|
|
|
|
for i, tab in pairs(self.Cache) do
|
|
|
|
if tab[1] == steamid then
|
|
|
|
tab[2] = memberlevel
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(self.Cache, {steamid, memberlevel})
|
|
|
|
end
|
|
|
|
|
|
|
|
if #self.Cache > self.MaxSize then
|
|
|
|
table.remove(self.Cache, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CACHE:Get(steamid)
|
|
|
|
for i, tab in pairs(self.Cache) do
|
|
|
|
if tab[1] == steamid then
|
|
|
|
return tab[2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CACHE:Remove(steamid)
|
|
|
|
for i, tab in pairs(self.Cache) do
|
|
|
|
if tab[1] == steamid then
|
|
|
|
table.remove(self.Cache, i)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CACHE:Save()
|
|
|
|
local tosave = {}
|
|
|
|
|
2014-12-24 00:26:08 +08:00
|
|
|
for _, cached in pairs(self.Cache) do
|
|
|
|
table.insert(tosave, cached[1].."="..cached[2])
|
2014-11-07 13:03:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
file.Write("noxapi_cache.txt", table.concat(tosave, "\n"))
|
|
|
|
end
|
|
|
|
|
|
|
|
function CACHE:Load()
|
|
|
|
if file.Exists("noxapi_cache.txt", "DATA") then
|
|
|
|
self.Cache = {}
|
|
|
|
|
|
|
|
for i, line in pairs(string.Explode("\n", file.Read("noxapi_cache.txt", "DATA"))) do
|
|
|
|
local cont = string.Explode("=", line)
|
|
|
|
local steamid, memberlevel = cont[1], tonumber(cont[2]) or 0
|
|
|
|
|
|
|
|
self:Set(steamid, memberlevel, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-19 21:08:12 +08:00
|
|
|
function CACHE:BufferRequest()
|
|
|
|
local IDS = {}
|
2014-12-24 00:26:08 +08:00
|
|
|
local steamid_to_player = {}
|
|
|
|
local steamids = {}
|
2014-12-19 21:08:12 +08:00
|
|
|
for i=1, math.min(10, #Buffer) do
|
|
|
|
IDS[#IDS + 1] = {Buffer[1][1], Buffer[1][2]}
|
2014-12-24 00:26:08 +08:00
|
|
|
steamids[#steamids + 1] = Buffer[1][1]
|
2014-12-19 21:08:12 +08:00
|
|
|
table.remove(Buffer, 1)
|
|
|
|
end
|
2014-12-24 00:26:08 +08:00
|
|
|
for _, pl in pairs(player.GetAll()) do
|
|
|
|
steamid_to_player[pl:SteamID()] = pl
|
2014-12-19 21:08:12 +08:00
|
|
|
end
|
|
|
|
|
2014-12-24 00:26:08 +08:00
|
|
|
http.Fetch("http://www.noxiousnet.com/api/player/memberlevel?steamids="..table.concat(steamids, ","), function(body, len, headers, code)
|
2014-12-19 21:08:12 +08:00
|
|
|
local levels = string.Explode(",", body)
|
2014-12-24 00:26:08 +08:00
|
|
|
if #levels == #IDS then
|
|
|
|
local allplayers = player.GetAll()
|
2014-12-19 21:08:12 +08:00
|
|
|
|
2014-12-24 00:26:08 +08:00
|
|
|
for k, v in pairs(levels) do
|
|
|
|
local kv = string.Explode("=", v)
|
|
|
|
if #kv == 2 then
|
|
|
|
local steamid = kv[1]
|
|
|
|
local level = tonumber(kv[2]) or 0
|
|
|
|
local pl = steamid_to_player[steamid]
|
|
|
|
|
|
|
|
if (level == 1 or level == 2) and pl and pl:IsValid() then
|
|
|
|
pl:SetDTBool(15, true)
|
|
|
|
pl:PrintMessage(HUD_PRINTTALK, SUPPORTER_MESSAGE)
|
|
|
|
end
|
|
|
|
|
|
|
|
CACHE:Set(steamid, level)
|
2014-12-19 21:08:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
if #Buffer > 0 then
|
|
|
|
CACHE:WaitForBuffer()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CACHE:WaitForBuffer()
|
|
|
|
if not timer.Exists("noxapibuffer") then
|
|
|
|
timer.Create("noxapibuffer", CACHE.BufferTime, 1, CACHE.BufferRequest)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
hook.Add("PlayerInitialSpawn", "noxapi", function(pl)
|
2018-05-02 06:38:08 +08:00
|
|
|
--[[if NDB or pl:IsBot() then return end
|
2014-11-07 13:03:40 +08:00
|
|
|
|
|
|
|
local steamid = pl:SteamID()
|
|
|
|
local memberlevel = CACHE:Get(steamid)
|
|
|
|
if memberlevel then
|
2014-12-24 00:26:08 +08:00
|
|
|
if memberlevel == 1 or memberlevel == 2 then
|
2014-12-19 21:08:12 +08:00
|
|
|
pl:SetDTBool(15, true)
|
|
|
|
pl:PrintMessage(HUD_PRINTTALK, SUPPORTER_MESSAGE)
|
2014-10-02 08:49:54 +08:00
|
|
|
end
|
2014-11-07 13:03:40 +08:00
|
|
|
else
|
2014-12-19 21:08:12 +08:00
|
|
|
table.insert(Buffer, {steamid, pl})
|
|
|
|
CACHE:WaitForBuffer()
|
2018-05-02 06:38:08 +08:00
|
|
|
end]]
|
2014-11-07 13:03:40 +08:00
|
|
|
end)
|
2014-10-02 08:49:54 +08:00
|
|
|
|
|
|
|
hook.Add("Initialize", "noxapi", function()
|
2018-05-02 06:38:08 +08:00
|
|
|
--resource.AddFile("materials/noxiousnet/noxicon.png")
|
2014-11-07 13:03:40 +08:00
|
|
|
|
|
|
|
if not NDB then
|
|
|
|
CACHE:Load()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
hook.Add("ShutDown", "noxapi", function()
|
|
|
|
if not NDB then
|
|
|
|
CACHE:Save()
|
|
|
|
end
|
2014-10-02 08:49:54 +08:00
|
|
|
end)
|
2014-11-07 13:03:40 +08:00
|
|
|
|
|
|
|
concommand.Add("noxapi_forcerefresh", function(sender, command, arguments)
|
2018-05-02 06:38:08 +08:00
|
|
|
--[[if sender._ForcedNoxAPILookup or sender:IsNoxSupporter() or NDB then return end
|
2014-12-19 21:08:12 +08:00
|
|
|
|
2014-11-07 13:03:40 +08:00
|
|
|
sender._ForcedNoxAPILookup = true
|
|
|
|
|
|
|
|
local steamid = sender:SteamID()
|
|
|
|
|
|
|
|
CACHE:Remove(steamid)
|
|
|
|
|
|
|
|
http.Fetch("http://www.noxiousnet.com/api/player/memberlevel?steamid="..steamid, function(body, len, headers, code)
|
|
|
|
local level = tonumber(body) or 0
|
|
|
|
|
|
|
|
if level == 1 or level == 2 then
|
2014-12-19 21:08:12 +08:00
|
|
|
pl:SetDTBool(15, true)
|
|
|
|
pl:PrintMessage(HUD_PRINTTALK, SUPPORTER_MESSAGE)
|
2014-11-07 13:03:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
CACHE:Set(steamid, level)
|
2018-05-02 06:38:08 +08:00
|
|
|
end)]]
|
2014-12-19 21:08:12 +08:00
|
|
|
end)
|