First noxapi implementation. No functionality yet.

This is a test to see if the cache feature is working and the API calls
are within reasonable amounts.
This commit is contained in:
JetBoom 2014-12-19 08:08:12 -05:00
parent 8cbfdaf59f
commit 11c4a73414
3 changed files with 59 additions and 16 deletions

View file

@ -1 +1 @@
-- nothing

View file

@ -1,13 +1,13 @@
-- TODO: Make requests buffer for 3 seconds or so using ?steamids=a,b,c,d local SUPPORTER_MESSAGE = "JetBoom says: thank you for supporting my gamemodes!"
-- TODO: _SUPPORTER_ needs to be networked
local SUPPORTER_MESSAGE = "JetBoom says \"thank you for supporting my gamemodes!\" to you."
local CACHE = { local CACHE = {
MaxSize = 128, MaxSize = 128,
BufferTime = 6,
Cache = {} Cache = {}
} }
local Buffer = {}
function CACHE:Set(steamid, memberlevel, nolookup) function CACHE:Set(steamid, memberlevel, nolookup)
if nolookup then if nolookup then
table.insert(self.Cache, {steamid, memberlevel}) table.insert(self.Cache, {steamid, memberlevel})
@ -67,27 +67,69 @@ function CACHE:Load()
end end
end end
--[[hook.Add("PlayerInitialSpawn", "noxapi", function(pl) function CACHE:BufferRequest()
if NDB then return end local IDS = {}
for i=1, math.min(10, #Buffer) do
IDS[#IDS + 1] = {Buffer[1][1], Buffer[1][2]}
table.remove(Buffer, 1)
end
local SIDS = {}
for k, v in pairs(IDS) do
SIDS[k] = v[1]
end
http.Fetch("http://www.noxiousnet.com/api/player/memberlevel?steamids="..table.concat(SIDS, ","), function(body, len, headers, code)
local levels = string.Explode(",", body)
if #levels == #SIDS then
for k, v in pairs(levels) do
local steamid = IDS[k][1]
local pl = IDS[k][2]
if pl and pl:IsValid() then
pl:SetDTBool(15, true)
pl:PrintMessage(HUD_PRINTTALK, SUPPORTER_MESSAGE)
end
CACHE:Set(steamid, level)
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)
if NDB or pl:IsBot() then return end
local steamid = pl:SteamID() local steamid = pl:SteamID()
local memberlevel = CACHE:Get(steamid) local memberlevel = CACHE:Get(steamid)
if memberlevel then if memberlevel then
if level == 1 or level == 2 then if level == 1 or level == 2 then
pl._SUPPORTER_ = true pl:SetDTBool(15, true)
pl:PrintMessage(HUD_PRINTCONSOLE, SUPPORTER_MESSAGE) pl:PrintMessage(HUD_PRINTTALK, SUPPORTER_MESSAGE)
end end
else else
http.Fetch("http://www.noxiousnet.com/api/player/memberlevel?steamid="..steamid, function(body, len, headers, code) http.Fetch("http://www.noxiousnet.com/api/player/memberlevel?steamid="..steamid, function(body, len, headers, code)
local level = tonumber(body) or 0 local level = tonumber(body) or 0
if level == 1 or level == 2 then if level == 1 or level == 2 then
pl._SUPPORTER_ = true pl:SetDTBool(15, true)
pl:PrintMessage(HUD_PRINTCONSOLE, SUPPORTER_MESSAGE) pl:PrintMessage(HUD_PRINTTALK, SUPPORTER_MESSAGE)
end end
CACHE:Set(steamid, level) CACHE:Set(steamid, level)
end) end)
table.insert(Buffer, {steamid, pl})
CACHE:WaitForBuffer()
end end
end) end)
@ -106,7 +148,8 @@ hook.Add("ShutDown", "noxapi", function()
end) end)
concommand.Add("noxapi_forcerefresh", function(sender, command, arguments) concommand.Add("noxapi_forcerefresh", function(sender, command, arguments)
if NDB or sender._ForcedNoxAPILookup or sender._SUPPORTER_ then return end if sender._ForcedNoxAPILookup or sender:IsNoxSupporter() then return end
sender._ForcedNoxAPILookup = true sender._ForcedNoxAPILookup = true
local steamid = sender:SteamID() local steamid = sender:SteamID()
@ -117,10 +160,10 @@ concommand.Add("noxapi_forcerefresh", function(sender, command, arguments)
local level = tonumber(body) or 0 local level = tonumber(body) or 0
if level == 1 or level == 2 then if level == 1 or level == 2 then
pl._SUPPORTER_ = true pl:SetDTBool(15, true)
pl:PrintMessage(HUD_PRINTCONSOLE, SUPPORTER_MESSAGE) pl:PrintMessage(HUD_PRINTTALK, SUPPORTER_MESSAGE)
end end
CACHE:Set(steamid, level) CACHE:Set(steamid, level)
end) end)
end)]] end)

View file

@ -7,5 +7,5 @@ function meta:IsNoxSupporter()
return memberlevel == MEMBER_GOLD or memberlevel == MEMBER_DIAMOND return memberlevel == MEMBER_GOLD or memberlevel == MEMBER_DIAMOND
end end
return self._SUPPORTER_ return self:GetDTBool(15)
end end