From 4da00718f7d09ddb0404e89a6784d0ccab871fb3 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 5 Feb 2021 14:06:13 +0200 Subject: [PATCH] Assign/Remove/Modify methods. --- module/Core/Privilege/Class.cpp | 42 ++++++++++++++++++++----- module/Core/Privilege/Class.hpp | 54 +++++++++++++++++++++++---------- module/Core/Privilege/Unit.cpp | 39 +++++++++++++++++++++--- module/Core/Privilege/Unit.hpp | 28 +++++++++++++++-- 4 files changed, 132 insertions(+), 31 deletions(-) diff --git a/module/Core/Privilege/Class.cpp b/module/Core/Privilege/Class.cpp index 58f8b381..2177c9cd 100644 --- a/module/Core/Privilege/Class.cpp +++ b/module/Core/Privilege/Class.cpp @@ -146,7 +146,7 @@ void PvClass::DoChanged(SQInteger id, bool status, SQInteger value) const } // ------------------------------------------------------------------------------------------------ -void PvClass::AssignStatus(SQInteger id, SQInteger value) +void PvClass::AssignPrivilege(SQInteger id, SQInteger value) { // Find the current status of this entry SQInteger current = GetEntryValue(id); @@ -167,7 +167,7 @@ void PvClass::AssignStatus(SQInteger id, SQInteger value) } // ------------------------------------------------------------------------------------------------ -void PvClass::RemoveStatus(SQInteger id) +void PvClass::RemovePrivilege(SQInteger id) { // Look for the status of this value auto itr = mPrivileges.find(id); @@ -197,7 +197,7 @@ void PvClass::RemoveStatus(SQInteger id) } // ------------------------------------------------------------------------------------------------ -void PvClass::ModifyStatus(SQInteger id, SQInteger value) +void PvClass::ModifyPrivilege(SQInteger id, SQInteger value) { // Find the current status of this entry SQInteger current = GetEntryValue(id); @@ -229,6 +229,24 @@ void PvClass::ModifyStatus(SQInteger id, SQInteger value) } } +// ------------------------------------------------------------------------------------------------ +void PvClass::AssignPrivilege(StackStrF & tag, SQInteger value) +{ + AssignPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value); +} + +// ------------------------------------------------------------------------------------------------ +void PvClass::RemovePrivilege(StackStrF & tag) +{ + RemovePrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID); +} + +// ------------------------------------------------------------------------------------------------ +void PvClass::ModifyPrivilege(StackStrF & tag, SQInteger value) +{ + ModifyPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value); +} + // ------------------------------------------------------------------------------------------------ void PvClass::AssignParent(const Ref & parent) { @@ -253,13 +271,13 @@ void PvClass::AssignParent(const Ref & parent) // ------------------------------------------------------------------------------------------------ bool PvClass::Can(SQInteger id) const { + // Get the current status of the specified entry + SQInteger current = GetEntryValue(id); // Retrieve the function responsible for the query event const Function & query = GetOnQuery(id); // Is there someone that can arbitrate this request? if (!query.IsNull()) { - // Get the current status of the specified entry - SQInteger current = GetEntryValue(id); // Attempt arbitration LightObj r = query.Eval(current); // If NULL or false the request was denied @@ -268,6 +286,11 @@ bool PvClass::Can(SQInteger id) const return true; // Request allowed } } + // We use the >= comparison to settle arbitration + else if (current >= ValidManager().ValidEntry(id).mDefault) + { + return true; + } // Request failed, no arbitration return false; } @@ -392,12 +415,17 @@ void Register_Privilege_Class(HSQUIRRELVM vm, Table & ns) .CbFunc(_SC("OnLost"), &SqPvClass::SetOnLost) .CbFunc(_SC("OnGained"), &SqPvClass::SetOnGained) // Member Methods - .Func(_SC("Can"), &SqPvUnit::Can) + .Func(_SC("Can"), &SqPvClass::Can) + .Func(_SC("Assign"), &SqPvClass::AssignPrivilegeWithID) + .Func(_SC("AssignWithTag"), &SqPvClass::AssignPrivilegeWithTag) + .Func(_SC("Remove"), &SqPvClass::RemovePrivilegeWithID) + .Func(_SC("RemoveWithTag"), &SqPvClass::RemovePrivilegeWithTag) + .Func(_SC("Modify"), &SqPvClass::ModifyPrivilegeWithID) + .Func(_SC("ModifyWithTag"), &SqPvClass::ModifyPrivilegeWithTag) .Func(_SC("GetUnit"), &SqPvClass::GetUnitWithID) .FmtFunc(_SC("GetUnitWithTag"), &SqPvClass::GetUnitWithTag) .Func(_SC("HaveUnit"), &SqPvClass::HaveUnitWithID) .FmtFunc(_SC("HaveUnitWithTag"), &SqPvClass::HaveUnitWithTag) - // Member Overloads ); } diff --git a/module/Core/Privilege/Class.hpp b/module/Core/Privilege/Class.hpp index 650cd443..6f75a045 100644 --- a/module/Core/Privilege/Class.hpp +++ b/module/Core/Privilege/Class.hpp @@ -130,6 +130,19 @@ struct PvClass */ PvClass & operator = (PvClass && o) = delete; + /* -------------------------------------------------------------------------------------------- + * Update the hash of the specified unit. + */ + void UpdateUnitHash(SQInteger id, size_t hash) + { + auto itr = mUnits.find(PvIdentity(id)); + // Only update if it exists + if (itr != mUnits.end()) + { + itr->first.mHash = hash; + } + } + /* -------------------------------------------------------------------------------------------- * Modify the associated user tag. */ @@ -221,18 +234,33 @@ struct PvClass * Assign a status value. Does not care if a parent (class or global) has the same status. * Later if the parent changes this status, we will keep having this status value. */ - void AssignStatus(SQInteger id, SQInteger value); + void AssignPrivilege(SQInteger id, SQInteger value); /* -------------------------------------------------------------------------------------------- * Remove a status value. If the specified status value is not assigned, nothing happens. */ - void RemoveStatus(SQInteger id); + void RemovePrivilege(SQInteger id); /* -------------------------------------------------------------------------------------------- * Assign a status value. If a parent (class or global) has the same value, nothing changes. * Same as AssignStatus but the status will not be enforced if we have it (inherited or not). */ - void ModifyStatus(SQInteger id, SQInteger value); + void ModifyPrivilege(SQInteger id, SQInteger value); + + /* -------------------------------------------------------------------------------------------- + * See AssignPrivilege(). + */ + void AssignPrivilege(StackStrF & tag, SQInteger value); + + /* -------------------------------------------------------------------------------------------- + * See RemovePrivilege(). + */ + void RemovePrivilege(StackStrF & tag); + + /* -------------------------------------------------------------------------------------------- + * See ModifyPrivilege(). + */ + void ModifyPrivilege(StackStrF & tag, SQInteger value); /* -------------------------------------------------------------------------------------------- * Change the parent class. @@ -263,19 +291,6 @@ struct PvClass * See if a unit with a certain tag inherits this class. */ bool HaveUnitWithTag(StackStrF & tag); - - /* -------------------------------------------------------------------------------------------- - * Update the hash of the specified unit. - */ - void UpdateUnitHash(SQInteger id, size_t hash) - { - auto itr = mUnits.find(PvIdentity(id)); - // Only update if it exists - if (itr != mUnits.end()) - { - itr->first.mHash = hash; - } - } }; /* ------------------------------------------------------------------------------------------------ @@ -326,6 +341,13 @@ struct SqPvClass // -------------------------------------------------------------------------------------------- bool Can(LightObj & obj) const; // -------------------------------------------------------------------------------------------- + void AssignPrivilegeWithID(SQInteger id, SQInteger value) { Valid().AssignPrivilege(id, value); } + void AssignPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().AssignPrivilege(tag, value); } + void RemovePrivilegeWithID(SQInteger id) { Valid().RemovePrivilege(id); } + void RemovePrivilegeWithTag(StackStrF & tag) { Valid().RemovePrivilege(tag); } + void ModifyPrivilegeWithID(SQInteger id, SQInteger value) { Valid().ModifyPrivilege(id, value); } + void ModifyPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().ModifyPrivilege(tag, value); } + // -------------------------------------------------------------------------------------------- SQMOD_NODISCARD LightObj GetUnitWithID(SQInteger id) const { return Valid().GetUnitWithID(id); } SQMOD_NODISCARD LightObj GetUnitWithTag(StackStrF & tag) const { return Valid().GetUnitWithTag(tag); } SQMOD_NODISCARD bool HaveUnitWithID(SQInteger id) const { return Valid().HaveUnitWithID(id); } diff --git a/module/Core/Privilege/Unit.cpp b/module/Core/Privilege/Unit.cpp index 4f8fa586..9aaf137f 100644 --- a/module/Core/Privilege/Unit.cpp +++ b/module/Core/Privilege/Unit.cpp @@ -161,7 +161,7 @@ void PvUnit::DoChanged(SQInteger id, bool status, SQInteger value) const } // ------------------------------------------------------------------------------------------------ -void PvUnit::AssignStatus(SQInteger id, SQInteger value) +void PvUnit::AssignPrivilege(SQInteger id, SQInteger value) { // Find the current status of this entry SQInteger current = GetEntryValue(id); @@ -182,7 +182,7 @@ void PvUnit::AssignStatus(SQInteger id, SQInteger value) } // ------------------------------------------------------------------------------------------------ -void PvUnit::RemoveStatus(SQInteger id) +void PvUnit::RemovePrivilege(SQInteger id) { // Look for the status of this value auto itr = mPrivileges.find(id); @@ -212,7 +212,7 @@ void PvUnit::RemoveStatus(SQInteger id) } // ------------------------------------------------------------------------------------------------ -void PvUnit::ModifyStatus(SQInteger id, SQInteger value) +void PvUnit::ModifyPrivilege(SQInteger id, SQInteger value) { // Find the current status of this entry SQInteger current = GetEntryValue(id); @@ -244,6 +244,24 @@ void PvUnit::ModifyStatus(SQInteger id, SQInteger value) } } +// ------------------------------------------------------------------------------------------------ +void PvUnit::AssignPrivilege(StackStrF & tag, SQInteger value) +{ + AssignPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value); +} + +// ------------------------------------------------------------------------------------------------ +void PvUnit::RemovePrivilege(StackStrF & tag) +{ + RemovePrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID); +} + +// ------------------------------------------------------------------------------------------------ +void PvUnit::ModifyPrivilege(StackStrF & tag, SQInteger value) +{ + ModifyPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value); +} + // ------------------------------------------------------------------------------------------------ void PvUnit::AssignClass(const std::shared_ptr< PvClass > & cls) { @@ -265,13 +283,13 @@ void PvUnit::AssignClass(const std::shared_ptr< PvClass > & cls) // ------------------------------------------------------------------------------------------------ bool PvUnit::Can(SQInteger id) const { + // Get the current status of the specified entry + SQInteger current = GetEntryValue(id); // Retrieve the function responsible for the query event const Function & query = GetOnQuery(id); // Is there someone that can arbitrate this request? if (!query.IsNull()) { - // Get the current status of the specified entry - SQInteger current = GetEntryValue(id); // Attempt arbitration LightObj r = query.Eval(current); // If NULL or false the request was denied @@ -280,6 +298,11 @@ bool PvUnit::Can(SQInteger id) const return true; // Request allowed } } + // We use the >= comparison to settle arbitration + else if (current >= ValidManager().ValidEntry(id).mDefault) + { + return true; + } // Request failed, no arbitration return false; } @@ -364,6 +387,12 @@ void Register_Privilege_Unit(HSQUIRRELVM vm, Table & ns) .CbFunc(_SC("OnGained"), &SqPvUnit::SetOnGained) // Member Methods .Func(_SC("Can"), &SqPvUnit::Can) + .Func(_SC("Assign"), &SqPvUnit::AssignPrivilegeWithID) + .Func(_SC("AssignWithTag"), &SqPvUnit::AssignPrivilegeWithTag) + .Func(_SC("Remove"), &SqPvUnit::RemovePrivilegeWithID) + .Func(_SC("RemoveWithTag"), &SqPvUnit::RemovePrivilegeWithTag) + .Func(_SC("Modify"), &SqPvUnit::ModifyPrivilegeWithID) + .Func(_SC("ModifyWithTag"), &SqPvUnit::ModifyPrivilegeWithTag) ); } diff --git a/module/Core/Privilege/Unit.hpp b/module/Core/Privilege/Unit.hpp index 970f7b87..153b93a2 100644 --- a/module/Core/Privilege/Unit.hpp +++ b/module/Core/Privilege/Unit.hpp @@ -207,18 +207,33 @@ struct PvUnit * Assign a status value. Does not care if a parent (class or global) has the same status. * Later if the parent changes this status, we will keep having this status value. */ - void AssignStatus(SQInteger id, SQInteger value); + void AssignPrivilege(SQInteger id, SQInteger value); /* -------------------------------------------------------------------------------------------- * Remove a status value. If the specified status value is not assigned, nothing happens. */ - void RemoveStatus(SQInteger id); + void RemovePrivilege(SQInteger id); /* -------------------------------------------------------------------------------------------- * Assign a status value. If a parent (class or global) has the same value, nothing changes. * Same as AssignStatus but the status will not be enforced if we have it (inherited or not). */ - void ModifyStatus(SQInteger id, SQInteger value); + void ModifyPrivilege(SQInteger id, SQInteger value); + + /* -------------------------------------------------------------------------------------------- + * See AssignPrivilege(). + */ + void AssignPrivilege(StackStrF & tag, SQInteger value); + + /* -------------------------------------------------------------------------------------------- + * See RemovePrivilege(). + */ + void RemovePrivilege(StackStrF & tag); + + /* -------------------------------------------------------------------------------------------- + * See ModifyPrivilege(). + */ + void ModifyPrivilege(StackStrF & tag, SQInteger value); /* -------------------------------------------------------------------------------------------- * Assign a new class. @@ -276,6 +291,13 @@ struct SqPvUnit SQMOD_NODISCARD LightObj GetManager() const; // -------------------------------------------------------------------------------------------- bool Can(LightObj & obj) const; + // -------------------------------------------------------------------------------------------- + void AssignPrivilegeWithID(SQInteger id, SQInteger value) { Valid().AssignPrivilege(id, value); } + void AssignPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().AssignPrivilege(tag, value); } + void RemovePrivilegeWithID(SQInteger id) { Valid().RemovePrivilege(id); } + void RemovePrivilegeWithTag(StackStrF & tag) { Valid().RemovePrivilege(tag); } + void ModifyPrivilegeWithID(SQInteger id, SQInteger value) { Valid().ModifyPrivilege(id, value); } + void ModifyPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().ModifyPrivilege(tag, value); } }; } // Namespace:: SqMod