Export quest::is_content_flag_enabled and quest::set_content_flag

This commit is contained in:
Akkadius 2020-05-24 20:45:44 -05:00
parent 50c266982f
commit a4b027db58
7 changed files with 155 additions and 31 deletions

View File

@ -48,21 +48,6 @@ void WorldContentService::SetExpansionContext()
);
}
void WorldContentService::SetCurrentExpansion(int current_expansion)
{
WorldContentService::current_expansion = current_expansion;
}
const std::vector<std::string> &WorldContentService::GetContentFlags() const
{
return content_flags;
}
void WorldContentService::SetContentFlags(std::vector<std::string> 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<std::string> &WorldContentService::GetContentFlags() const
{
return content_flags;
}
/**
* @param content_flags
*/
void WorldContentService::SetContentFlags(std::vector<std::string> 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;
}

View File

@ -159,10 +159,11 @@ public:
bool IsCurrentExpansionTormentOfVelious() { return current_expansion == Expansion::ExpansionNumber::TormentOfVelious; }
private:
int current_expansion;
int current_expansion{};
std::vector<std::string> content_flags;
public:
const std::vector<std::string> &GetContentFlags() const;
bool IsContentFlagEnabled(const std::string& content_flag);
void SetContentFlags(std::vector<std::string> content_flags);
void SetExpansionContext();
};

View File

@ -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;
}

View File

@ -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)
];
}

View File

@ -409,20 +409,7 @@ int main(int argc, char** argv) {
content_service.SetExpansionContext();
std::vector<std::string> 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");

View File

@ -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<std::string> 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();
}

View File

@ -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;