Skip to content

Commit

Permalink
Update Rank Manager plugin (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Miyoglow committed Dec 2, 2020
1 parent 21e1797 commit bcc7f71
Showing 1 changed file with 109 additions and 96 deletions.
205 changes: 109 additions & 96 deletions rankmanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ PLUGIN.name = "Overwatch Rank Manager"
PLUGIN.author = "Gary Tate, wowm0d"
PLUGIN.description = "Allows Overwatch to manage ranks."
PLUGIN.schema = "HL2 RP"
PLUGIN.version = "1.3"
PLUGIN.readme = [[
Overwatch Rank Manager for Helix
---
Installation:
- Edit the self.rankTable variable within PLUGIN:OnLoaded() to fit your rank needs.
- Edit the PLUGIN.rankTable variable at line 50 to fit your rank needs.
- Place rankmanager.Lua file into your Schema's Plugin directory.
Support for this plugin can be found here: https://discord.gg/mntpDMU
Expand All @@ -23,158 +24,170 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

PLUGIN.rankTable = PLUGIN.rankTable or {}

ix.lang.AddTable("english", {
cmdRankDemote = "Demote an active Overwatch functionary.",
cmdRankPromote = "Promote an active Overwatch functionary.",
cRankConfigDisabled = "Rank Management is currently disabled.",
cmdCharRankDemote = "Demote an active Overwatch functionary.",
cmdCharRankPromote = "Promote an active Overwatch functionary.",
cmdCharRankSet = "Set the rank of an active Overwatch functionary.",
cRankPromotion = "%s has promoted %s to %s.",
cRankDemotion = "%s has demoted %s to %s.",
cRankSet = "%s has set %s's rank to %s.",
cRankMaxRank = "%s is already the maximum rank.",
cRankMinRank = "%s is already the minimum rank.",
cRankInvalidRank = "%s is an invalid rank, cannot promote/demote.",
cRankSameRank = "%s is already %s rank.",
cRankInvalidRank = "%s is an invalid rank, cannot change their rank.",
cRankInvalidFaction = "%s is not in a valid faction.",
cRankInvalidInput = "%s is not a valid rank."
cRankInvalidInput = "'%s' is not a valid rank."
})

function PLUGIN:OnLoaded()
function PLUGIN:CanPlayerSetRanks(client)
if (client:IsDispatch()) then
return true
end
end

if (SERVER) then
timer.Simple(0, function()
self.rankTable = {
PLUGIN.rankTable = {
[FACTION_MPF] = {"00", "10", "20", "30", "40", "50", "60", "70", "80", "90", "RL"},
[FACTION_OTA] = {"OWS", "OWC", "EOW", "EOC"}
}
end)
end

local function tbl_upper(tbl)
local upper_tbl = {}
-- Ignore Below --

for key, value in ipairs(tbl) do
if (isstring(value)) then
upper_tbl[key] = string.upper(value)
end
end
PLUGIN.rankMap = {}

for _, ranks in next, PLUGIN.rankTable do
PLUGIN.rankMap[ranks] = {}

return upper_tbl
for index, rank in next, ranks do
PLUGIN.rankMap[ranks][rank:lower()] = index
end
end
end)
end

ix.command.Add("RankPromote", {
description = "@cmdRankPromote",
ix.command.Add("CharRankSet", {
description = "@cmdCharRankSet",
arguments = {
ix.type.character,
bit.bor(ix.type.string, ix.type.optional)
ix.type.string
},
OnCheckAccess = function(self, client)
return client:IsDispatch()
OnCheckAccess = function(_, client)
return hook.Run("CanPlayerSetRanks", client) == true
end,
OnRun = function(self, client, target, rank)
local ranks = PLUGIN.rankTable[target:GetFaction()]
local targetRanks = PLUGIN.rankTable[target:GetFaction()]

-- Checks if player is in a valid faction from rankTable
if (istable(ranks)) then
local name = target:GetName()
local newRank
if (!targetRanks) then
return "@cRankInvalidFaction", target:GetName()
end

if (rank) then
newRank = table.KeyFromValue(tbl_upper(ranks), string.upper(rank))
local newRank = PLUGIN.rankMap[targetRanks][rank:lower()]

if (!newRank) then
return "@cRankInvalidInput", rank
end
else
if (string.find(name, ranks[#ranks])) then
return "@cRankMaxRank", name
end
if (!newRank) then
return "@cRankInvalidInput", rank
end

for k, v in ipairs(ranks) do
if (string.find(name, v)) then
newRank = math.min(k + 1, #ranks)
local name = target:GetName()

break
end
for index, rank in next, targetRanks do
if (string.find(name, "[%D+]" .. rank .. "[%D+]")) then
if (newRank == index) then
return "@cRankSameRank", name, rank
end
end

if (newRank) then
newRank = ranks[newRank]
newRank = targetRanks[newRank]

local newName = name:gsub("%:([%w]+)%.",
string.format(":%s.", newRank)
)
target:SetName(string.gsub(name, "([%D+])" .. rank .. "([%D+])", "%1" .. newRank .. "%2"))

target:SetName(newName)

for _, v in ipairs(player.GetAll()) do
for _, v in next, player.GetAll() do
if (self:OnCheckAccess(v) or v == target:GetPlayer()) then
v:NotifyLocalized("cRankPromotion", client:GetName(), name, newName)
v:NotifyLocalized("cRankSet", client:GetName(), name, newRank)
end
end
else
return "@cRankInvalidRank", name

return
end
else
return "@cRankInvalidFaction", target:GetName()
end

return "@cRankInvalidRank", name
end
})

ix.command.Add("RankDemote", {
description = "@cmdRankDemote",
arguments = {
ix.type.character,
bit.bor(ix.type.string, ix.type.optional)
},
OnCheckAccess = function(self, client)
return client:IsDispatch()
ix.command.Add("CharRankPromote", {
description = "@cmdCharRankPromote",
arguments = ix.type.character,
OnCheckAccess = function(_, client)
return hook.Run("CanPlayerSetRanks", client) == true
end,
OnRun = function(self, client, target, rank)
local ranks = PLUGIN.rankTable[target:GetFaction()]
OnRun = function(self, client, target)
local targetRanks = PLUGIN.rankTable[target:GetFaction()]

-- Checks if player is in a valid faction from rankTable
if (istable(ranks)) then
local name = target:GetName()
local newRank
if (!targetRanks) then
return "@cRankInvalidFaction", target:GetName()
end

if (rank) then
newRank = table.KeyFromValue(tbl_upper(ranks), string.upper(rank))
local name = target:GetName()

if (!newRank) then
return "@cRankInvalidInput", rank
end
else
if (string.find(name, ranks[1])) then
return "@cRankMinRank", name
for index, rank in next, targetRanks do
if (string.find(name, "[%D+]" .. rank .. "[%D+]")) then
if (index == #targetRanks) then
return "@cRankMaxRank", name
end

for k, v in ipairs(ranks) do
if (string.find(name, v)) then
newRank = math.max(k - 1, 1)
local newRank = targetRanks[index + 1]

break
target:SetName(string.gsub(name, "([%D+])" .. rank .. "([%D+])", "%1" .. newRank .. "%2"))

for _, v in next, player.GetAll() do
if (self:OnCheckAccess(v) or v == target:GetPlayer()) then
v:NotifyLocalized("cRankPromotion", client:GetName(), name, newRank)
end
end

return
end
end

if (newRank) then
newRank = ranks[newRank]
return "@cRankInvalidRank", name
end
})

local newName = name:gsub("%:([%w]+)%.",
string.format(":%s.", newRank)
)
ix.command.Add("CharRankDemote", {
description = "@cmdCharRankDemote",
arguments = ix.type.character,
OnCheckAccess = function(_, client)
return hook.Run("CanPlayerSetRanks", client) == true
end,
OnRun = function(self, client, target)
local targetRanks = PLUGIN.rankTable[target:GetFaction()]

if (!targetRanks) then
return "@cRankInvalidFaction", target:GetName()
end

local name = target:GetName()

for index, rank in next, targetRanks do
if (string.find(name, "[%D+]" .. rank .. "[%D+]")) then
if (index == 1) then
return "@cRankMinRank", name
end

local newRank = targetRanks[index - 1]

target:SetName(newName)
target:SetName(string.gsub(name, "([%D+])" .. rank .. "([%D+])", "%1" .. newRank .. "%2"))

for _, v in ipairs(player.GetAll()) do
for _, v in next, player.GetAll() do
if (self:OnCheckAccess(v) or v == target:GetPlayer()) then
v:NotifyLocalized("cRankDemotion", client:GetName(), name, newName)
v:NotifyLocalized("cRankDemotion", client:GetName(), name, newRank)
end
end
else
return "@cRankInvalidRank", name

return
end
else
return "@cRankInvalidFaction", target:GetName()
end

return "@cRankInvalidRank", name
end
})

0 comments on commit bcc7f71

Please sign in to comment.