Skip to content

Commit

Permalink
Update serverguard.lua
Browse files Browse the repository at this point in the history
Рефакторинг поддержки ServerGuard
  • Loading branch information
AMD-NICK committed Feb 22, 2022
1 parent cafc6f8 commit fec9008
Showing 1 changed file with 30 additions and 40 deletions.
70 changes: 30 additions & 40 deletions addons/igs-core/lua/igs/extensions/serverguard.lua
Original file line number Diff line number Diff line change
@@ -1,50 +1,40 @@
IGS.ITEMS.SG = IGS.ITEMS.SG or {
GROUPS = {}
}

local STORE_ITEM = FindMetaTable("IGSItem")

function STORE_ITEM:SetSGGroup(sUserGroup, iGroupWeight)
self:SetCanActivate(function(pl)
if pl:IsUserGroup(sUserGroup) then
return "У вас уже действует эта услуга"
end
end)
self:SetInstaller(function(pl)
local rankData = serverguard.ranks:GetRank(sUserGroup)
if rankData then
serverguard.player:SetRank(pl, sUserGroup, 0)
serverguard.player:SetImmunity(pl, rankData.immunity)
serverguard.player:SetTargetableRank(pl, rankData.targetable)
serverguard.player:SetBanLimit(pl, rankData.banlimit)
pl.IGSSGWeight = iGroupWeight
end
end)
self:SetValidator(function(pl)
if pl.IGSSGWeight then
return iGroupWeight < pl.IGSSGWeight
else
return serverguard.player:GetRank(pl) == sUserGroup
end
end)
local is_on_sale_ranks = {}

function STORE_ITEM:SetSGGroup(sUserGroup)
is_on_sale_ranks[sUserGroup] = true

self.sg_group = self:Insert(IGS.ITEMS.SG.GROUPS, sUserGroup)
return self
return self:SetInstaller(function(pl)
local rankData = serverguard.ranks:GetRank(sUserGroup)
assert(rankData, "IGS: В SetSGGroup указана несуществующая группа")
serverguard.player:SetRank(pl, sUserGroup, 0)
serverguard.player:SetImmunity(pl, rankData.immunity)
serverguard.player:SetTargetableRank(pl, rankData.targetable)
serverguard.player:SetBanLimit(pl, rankData.banlimit)
end):SetMeta("sggroup", sUserGroup)
end

if CLIENT then return end
local function checkGroups(pl)
local hasAccess = IGS.PlayerHasOneOf(pl, IGS.ITEMS.SG.GROUPS[ serverguard.player:GetRank(pl) ])
if hasAccess == nil then return end -- не отслеживается

if hasAccess then
return -- если имеется хоть одна покупка, то не снимаем права
end
-- addhook не подойдет (не будет автоснятия. Можно и отдельно, конечно)
hook.Add("IGS.PlayerPurchasesLoaded", "sggroup", function(pl, purchases)
if not serverguard then hook.Remove("IGS.PlayerPurchasesLoaded", "sggroup") return end
local prior

serverguard.player:SetRank(pl, "user", 0);
end
for uid in pairs(purchases or {}) do
local ITEM = IGS.GetItemByUID(uid)
if ITEM:GetMeta("sggroup") and (not prior or ITEM.id >= prior.id) then
prior = ITEM
end
end

hook.Add("IGS.PlayerPurchasesLoaded", "SGGroups", function(pl)
if next(IGS.ITEMS.SG.GROUPS) then -- группы продаются
checkGroups(pl)
if prior then
prior:Setup(pl)
else -- ни один не куплен (срок истек?)
local player_rank = serverguard.player:GetRank(pl) -- default: user
if is_on_sale_ranks[player_rank] then -- но ранг, который у игрока продается
serverguard.player:SetRank(pl, "user", 0) -- снимаем
end
end
end)

0 comments on commit fec9008

Please sign in to comment.