From a4b027db582d23b3bb230774b19e3717e5007895 Mon Sep 17 00:00:00 2001 From: Akkadius Date: Sun, 24 May 2020 20:45:44 -0500 Subject: [PATCH] Export quest::is_content_flag_enabled and quest::set_content_flag --- common/content/world_content_service.cpp | 54 +++++++++++++++------ common/content/world_content_service.h | 3 +- zone/embparser_api.cpp | 34 ++++++++++++++ zone/lua_general.cpp | 16 ++++++- zone/main.cpp | 15 +----- zone/zone_store.cpp | 60 ++++++++++++++++++++++++ zone/zone_store.h | 4 ++ 7 files changed, 155 insertions(+), 31 deletions(-) diff --git a/common/content/world_content_service.cpp b/common/content/world_content_service.cpp index 64f6a1230..949ebcc0d 100644 --- a/common/content/world_content_service.cpp +++ b/common/content/world_content_service.cpp @@ -48,21 +48,6 @@ void WorldContentService::SetExpansionContext() ); } -void WorldContentService::SetCurrentExpansion(int current_expansion) -{ - WorldContentService::current_expansion = current_expansion; -} - -const std::vector &WorldContentService::GetContentFlags() const -{ - return content_flags; -} - -void WorldContentService::SetContentFlags(std::vector content_flags) -{ - WorldContentService::content_flags = content_flags; -} - std::string WorldContentService::GetCurrentExpansionName() { if (content_service.GetCurrentExpansion() == Expansion::EXPANSION_ALL) { @@ -75,3 +60,42 @@ std::string WorldContentService::GetCurrentExpansionName() return "Unknown Expansion"; } + +/** + * @param current_expansion + */ +void WorldContentService::SetCurrentExpansion(int current_expansion) +{ + WorldContentService::current_expansion = current_expansion; +} + +/** + * @return + */ +const std::vector &WorldContentService::GetContentFlags() const +{ + return content_flags; +} + +/** + * @param content_flags + */ +void WorldContentService::SetContentFlags(std::vector content_flags) +{ + WorldContentService::content_flags = content_flags; +} + +/** + * @param content_flag + * @return + */ +bool WorldContentService::IsContentFlagEnabled(const std::string& content_flag) +{ + for (auto &flag : GetContentFlags()) { + if (flag == content_flag) { + return true; + } + } + + return false; +} diff --git a/common/content/world_content_service.h b/common/content/world_content_service.h index 89abba257..662ec20cc 100644 --- a/common/content/world_content_service.h +++ b/common/content/world_content_service.h @@ -159,10 +159,11 @@ public: bool IsCurrentExpansionTormentOfVelious() { return current_expansion == Expansion::ExpansionNumber::TormentOfVelious; } private: - int current_expansion; + int current_expansion{}; std::vector content_flags; public: const std::vector &GetContentFlags() const; + bool IsContentFlagEnabled(const std::string& content_flag); void SetContentFlags(std::vector content_flags); void SetExpansionContext(); }; diff --git a/zone/embparser_api.cpp b/zone/embparser_api.cpp index 6e1147848..0fa7cdbe9 100644 --- a/zone/embparser_api.cpp +++ b/zone/embparser_api.cpp @@ -4883,6 +4883,34 @@ XS(XS__IsCurrentExpansionTormentOfVelious) { XSprePUSH; PUSHu((IV) RETVAL); XSRETURN(1); } +XS(XS__IsContentFlagEnabled); +XS(XS__IsContentFlagEnabled) { + dXSARGS; + if (items != 1) { + Perl_croak(aTHX_ "Usage: quest::is_content_flag_enabled(string flag_name)"); + } + + std::string flag_name = (std::string) SvPV_nolen(ST(0)); + + bool RETVAL; dXSTARG; + RETVAL = content_service.IsContentFlagEnabled(flag_name); + XSprePUSH; PUSHu((IV) RETVAL); XSRETURN(1); +} + +XS(XS__SetContentFlag); +XS(XS__SetContentFlag) +{ + dXSARGS; + if (items != 2) { + Perl_croak(aTHX_ "Usage: quest::set_content_flag(string flag_name, enabled)"); + } + + std::string flag_name = (std::string) SvPV_nolen(ST(0)); + bool enabled = (int) SvIV(ST(1)) != 0; + ZoneStore::SetContentFlag(flag_name, enabled); + XSRETURN_EMPTY; +} + /* This is the callback perl will look for to setup the quest package's XSUBs @@ -5216,6 +5244,12 @@ EXTERN_C XS(boot_quest) { newXS(strcpy(buf, "is_current_expansion_the_burning_lands"), XS__IsCurrentExpansionTheBurningLands, file); newXS(strcpy(buf, "is_current_expansion_torment_of_velious"), XS__IsCurrentExpansionTormentOfVelious, file); + /** + * Content flags + */ + newXS(strcpy(buf, "is_content_flag_enabled"), XS__IsContentFlagEnabled, file); + newXS(strcpy(buf, "set_content_flag"), XS__SetContentFlag, file); + XSRETURN_YES; } diff --git a/zone/lua_general.cpp b/zone/lua_general.cpp index 99d188219..3ced07d03 100644 --- a/zone/lua_general.cpp +++ b/zone/lua_general.cpp @@ -1726,6 +1726,14 @@ bool lua_is_current_expansion_torment_of_velious() { return content_service.IsCurrentExpansionTormentOfVelious(); } +bool lua_is_content_flag_enabled(std::string content_flag){ + return content_service.IsContentFlagEnabled(content_flag); +} + +void lua_set_content_flag(std::string flag_name, bool enabled){ + ZoneStore::SetContentFlag(flag_name, enabled); +} + #define LuaCreateNPCParse(name, c_type, default_value) do { \ cur = table[#name]; \ if(luabind::type(cur) != LUA_TNIL) { \ @@ -2205,7 +2213,13 @@ luabind::scope lua_register_general() { luabind::def("is_current_expansion_empires_of_kunark", &lua_is_current_expansion_empires_of_kunark), luabind::def("is_current_expansion_ring_of_scale", &lua_is_current_expansion_ring_of_scale), luabind::def("is_current_expansion_the_burning_lands", &lua_is_current_expansion_the_burning_lands), - luabind::def("is_current_expansion_torment_of_velious", &lua_is_current_expansion_torment_of_velious) + luabind::def("is_current_expansion_torment_of_velious", &lua_is_current_expansion_torment_of_velious), + + /** + * Content flags + */ + luabind::def("is_content_flag_enabled", (bool(*)(std::string*))&lua_is_content_flag_enabled), + luabind::def("set_content_flag", (void(*)(std::string*, bool*))&lua_set_content_flag) ]; } diff --git a/zone/main.cpp b/zone/main.cpp index 85da66f9e..3fbb6c8d7 100644 --- a/zone/main.cpp +++ b/zone/main.cpp @@ -409,20 +409,7 @@ int main(int argc, char** argv) { content_service.SetExpansionContext(); - std::vector set_content_flags; - auto content_flags = ContentFlagsRepository::GetWhere("enabled = 1"); - set_content_flags.reserve(content_flags.size()); - - for (auto &flags: content_flags) { - set_content_flags.push_back(flags.flag_name); - - LogInfo( - "Enabled content flag [{}]", - flags.flag_name - ); - } - - content_service.SetContentFlags(set_content_flags); + ZoneStore::LoadContentFlags(); #ifdef BOTS LogInfo("Loading bot commands"); diff --git a/zone/zone_store.cpp b/zone/zone_store.cpp index bcaaf1f44..2401230e7 100644 --- a/zone/zone_store.cpp +++ b/zone/zone_store.cpp @@ -19,6 +19,8 @@ */ #include "zone_store.h" +#include "../common/repositories/content_flags_repository.h" +#include "../common/content/world_content_service.h" ZoneStore::ZoneStore() = default; ZoneStore::~ZoneStore() = default; @@ -28,6 +30,10 @@ void ZoneStore::LoadZones() zones = ZoneRepository::All(); } +/** + * @param in_zone_name + * @return + */ uint32 ZoneStore::GetZoneID(const char *in_zone_name) { if (in_zone_name == nullptr) { @@ -39,6 +45,10 @@ uint32 ZoneStore::GetZoneID(const char *in_zone_name) return GetZoneID(zone_name); } +/** + * @param zone_name + * @return + */ uint32 ZoneStore::GetZoneID(std::string zone_name) { for (auto &z: zones) { @@ -130,3 +140,53 @@ ZoneRepository::Zone ZoneStore::GetZone(const char *in_zone_name) return ZoneRepository::Zone(); } + +/** + * @return + */ +void ZoneStore::LoadContentFlags() +{ + std::vector set_content_flags; + auto content_flags = ContentFlagsRepository::GetWhere("enabled = 1"); + + for (auto &flags: content_flags) { + set_content_flags.push_back(flags.flag_name); + + LogInfo( + "Enabled content flag [{}]", + flags.flag_name + ); + } + + content_service.SetContentFlags(set_content_flags); +} + +/** + * Sets the value in the database and proceeds to load content flags into the server context again + * + * @param content_flag_name + * @param enabled + */ +void ZoneStore::SetContentFlag(const std::string &content_flag_name, bool enabled) +{ + auto content_flags = ContentFlagsRepository::GetWhere( + fmt::format("flag_name = '{}'", content_flag_name) + ); + + auto content_flag = ContentFlagsRepository::NewEntity(); + if (!content_flags.empty()) { + content_flag = content_flags.front(); + } + + content_flag.enabled = enabled ? 1 : 0; + content_flag.flag_name = content_flag_name; + + if (!content_flags.empty()) { + ContentFlagsRepository::UpdateOne(content_flag); + } + else { + ContentFlagsRepository::InsertOne(content_flag); + } + + LoadContentFlags(); +} diff --git a/zone/zone_store.h b/zone/zone_store.h index 4057336d5..8513c51fa 100644 --- a/zone/zone_store.h +++ b/zone/zone_store.h @@ -23,6 +23,7 @@ #include "zonedb.h" #include "../common/repositories/zone_repository.h" +#include "../common/repositories/base/base_content_flags_repository.h" class ZoneStore { public: @@ -40,6 +41,9 @@ public: std::string GetZoneName(uint32 zone_id); std::string GetZoneLongName(uint32 zone_id); const char *GetZoneName(uint32 zone_id, bool error_unknown = false); + + static void LoadContentFlags(); + static void SetContentFlag(const std::string& content_flag_name, bool enabled); }; extern ZoneStore zone_store;