From 66dca8785c25cb922fe20752ee2524afc52adfdf Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 5 Feb 2021 14:19:15 +0200 Subject: [PATCH] Remove all method. --- module/Core/Privilege/Class.cpp | 42 ++++++++++++++++++++++++++++++--- module/Core/Privilege/Class.hpp | 6 +++++ module/Core/Privilege/Unit.cpp | 42 ++++++++++++++++++++++++++++++--- module/Core/Privilege/Unit.hpp | 6 +++++ 4 files changed, 90 insertions(+), 6 deletions(-) diff --git a/module/Core/Privilege/Class.cpp b/module/Core/Privilege/Class.cpp index 2177c9cd..1b792a67 100644 --- a/module/Core/Privilege/Class.cpp +++ b/module/Core/Privilege/Class.cpp @@ -247,6 +247,41 @@ void PvClass::ModifyPrivilege(StackStrF & tag, SQInteger value) ModifyPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value); } +// ------------------------------------------------------------------------------------------------ +void PvClass::RemoveAllPrivileges() +{ + // Discard all privileges but not before gaining ownership of them + PvStatusList list = std::move(mPrivileges); + // Go over all entries and see if this unit will gain or loose any privileges from this change + for (const auto & e : list) + { + // Get the value that we have now after the change + SQInteger current = GetEntryValue(e.first); + // Were they literally the same? + if (current == e.second) + { + continue; // Don't even bother + } + // Retrieve the associated entry + PvEntry & entry = ValidManager().ValidEntry(e.first); + // Is there someone that can identify this change? + if (!entry.mOnModify.IsNull()) + { + LightObj r = entry.mOnModify.Eval(current, e.second); + // Was this considered a change? + if (!r.IsNull()) + { + DoChanged(e.first, r.Cast< bool >(), e.second); + } + } + else + { + // By default we use > comparison to decide upgrades + DoChanged(e.first, e.second > current, e.second); + } + } +} + // ------------------------------------------------------------------------------------------------ void PvClass::AssignParent(const Ref & parent) { @@ -417,11 +452,12 @@ void Register_Privilege_Class(HSQUIRRELVM vm, Table & ns) // Member Methods .Func(_SC("Can"), &SqPvClass::Can) .Func(_SC("Assign"), &SqPvClass::AssignPrivilegeWithID) - .Func(_SC("AssignWithTag"), &SqPvClass::AssignPrivilegeWithTag) + .FmtFunc(_SC("AssignWithTag"), &SqPvClass::AssignPrivilegeWithTag) .Func(_SC("Remove"), &SqPvClass::RemovePrivilegeWithID) - .Func(_SC("RemoveWithTag"), &SqPvClass::RemovePrivilegeWithTag) + .FmtFunc(_SC("RemoveWithTag"), &SqPvClass::RemovePrivilegeWithTag) .Func(_SC("Modify"), &SqPvClass::ModifyPrivilegeWithID) - .Func(_SC("ModifyWithTag"), &SqPvClass::ModifyPrivilegeWithTag) + .FmtFunc(_SC("ModifyWithTag"), &SqPvClass::ModifyPrivilegeWithTag) + .Func(_SC("RemoveAllPrivileges"), &SqPvClass::RemoveAllPrivileges) .Func(_SC("GetUnit"), &SqPvClass::GetUnitWithID) .FmtFunc(_SC("GetUnitWithTag"), &SqPvClass::GetUnitWithTag) .Func(_SC("HaveUnit"), &SqPvClass::HaveUnitWithID) diff --git a/module/Core/Privilege/Class.hpp b/module/Core/Privilege/Class.hpp index 6f75a045..168aa191 100644 --- a/module/Core/Privilege/Class.hpp +++ b/module/Core/Privilege/Class.hpp @@ -262,6 +262,11 @@ struct PvClass */ void ModifyPrivilege(StackStrF & tag, SQInteger value); + /* -------------------------------------------------------------------------------------------- + * Remove all status values. Basically it reverts to the parent class privileges. + */ + void RemoveAllPrivileges(); + /* -------------------------------------------------------------------------------------------- * Change the parent class. */ @@ -347,6 +352,7 @@ struct SqPvClass 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); } + void RemoveAllPrivileges() { Valid().RemoveAllPrivileges(); } // -------------------------------------------------------------------------------------------- SQMOD_NODISCARD LightObj GetUnitWithID(SQInteger id) const { return Valid().GetUnitWithID(id); } SQMOD_NODISCARD LightObj GetUnitWithTag(StackStrF & tag) const { return Valid().GetUnitWithTag(tag); } diff --git a/module/Core/Privilege/Unit.cpp b/module/Core/Privilege/Unit.cpp index 9aaf137f..dea307d8 100644 --- a/module/Core/Privilege/Unit.cpp +++ b/module/Core/Privilege/Unit.cpp @@ -262,6 +262,41 @@ void PvUnit::ModifyPrivilege(StackStrF & tag, SQInteger value) ModifyPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value); } +// ------------------------------------------------------------------------------------------------ +void PvUnit::RemoveAllPrivileges() +{ + // Discard all privileges but not before gaining ownership of them + PvStatusList list = std::move(mPrivileges); + // Go over all entries and see if this unit will gain or loose any privileges from this change + for (const auto & e : list) + { + // Get the value that we have now after the change + SQInteger current = GetEntryValue(e.first); + // Were they literally the same? + if (current == e.second) + { + continue; // Don't even bother + } + // Retrieve the associated entry + PvEntry & entry = ValidManager().ValidEntry(e.first); + // Is there someone that can identify this change? + if (!entry.mOnModify.IsNull()) + { + LightObj r = entry.mOnModify.Eval(current, e.second); + // Was this considered a change? + if (!r.IsNull()) + { + DoChanged(e.first, r.Cast< bool >(), e.second); + } + } + else + { + // By default we use > comparison to decide upgrades + DoChanged(e.first, e.second > current, e.second); + } + } +} + // ------------------------------------------------------------------------------------------------ void PvUnit::AssignClass(const std::shared_ptr< PvClass > & cls) { @@ -388,11 +423,12 @@ void Register_Privilege_Unit(HSQUIRRELVM vm, Table & ns) // Member Methods .Func(_SC("Can"), &SqPvUnit::Can) .Func(_SC("Assign"), &SqPvUnit::AssignPrivilegeWithID) - .Func(_SC("AssignWithTag"), &SqPvUnit::AssignPrivilegeWithTag) + .FmtFunc(_SC("AssignWithTag"), &SqPvUnit::AssignPrivilegeWithTag) .Func(_SC("Remove"), &SqPvUnit::RemovePrivilegeWithID) - .Func(_SC("RemoveWithTag"), &SqPvUnit::RemovePrivilegeWithTag) + .FmtFunc(_SC("RemoveWithTag"), &SqPvUnit::RemovePrivilegeWithTag) .Func(_SC("Modify"), &SqPvUnit::ModifyPrivilegeWithID) - .Func(_SC("ModifyWithTag"), &SqPvUnit::ModifyPrivilegeWithTag) + .FmtFunc(_SC("ModifyWithTag"), &SqPvUnit::ModifyPrivilegeWithTag) + .Func(_SC("RemoveAll"), &SqPvUnit::RemoveAllPrivileges) ); } diff --git a/module/Core/Privilege/Unit.hpp b/module/Core/Privilege/Unit.hpp index 153b93a2..eec10d54 100644 --- a/module/Core/Privilege/Unit.hpp +++ b/module/Core/Privilege/Unit.hpp @@ -235,6 +235,11 @@ struct PvUnit */ void ModifyPrivilege(StackStrF & tag, SQInteger value); + /* -------------------------------------------------------------------------------------------- + * Remove all status values. Basically it reverts to the current class privileges. + */ + void RemoveAllPrivileges(); + /* -------------------------------------------------------------------------------------------- * Assign a new class. */ @@ -298,6 +303,7 @@ struct SqPvUnit 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); } + void RemoveAllPrivileges() { Valid().RemoveAllPrivileges(); } }; } // Namespace:: SqMod