diff --git a/changelog.txt b/changelog.txt index ec97647c4..1217ee9d5 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,9 @@ EQEMu Changelog (Started on Sept 24, 2003 15:50) ------------------------------------------------------- +== 07/07/2018 == +Akkadius: Implemented a much better replacement for qglobals called 'DataBuckets' + - A much more detailed example can be found at: https://github.com/EQEmu/Server/wiki/Data-Buckets + == 07/05/2018 == Uleat: Reintegration of inventory-based EQDictionary references - Standardized 'CONSTANT_DECLARATION' and 'enumerationValue' tokens for most of the affected references diff --git a/common/database.cpp b/common/database.cpp index 94a6a2a32..447037ae7 100644 --- a/common/database.cpp +++ b/common/database.cpp @@ -1752,6 +1752,15 @@ void Database::ClearRaidDetails(uint32 rid) { std::cout << "Unable to clear raid details: " << results.ErrorMessage() << std::endl; } +void Database::PurgeAllDeletedDataBuckets() { + std::string query = StringFormat( + "DELETE FROM `data_buckets` WHERE (`expires` < %lld AND `expires` > 0)", + (long long) std::time(nullptr) + ); + + QueryDatabase(query); +} + // returns 0 on error or no raid for that character, or // the raid id that the character is a member of. uint32 Database::GetRaidID(const char* name) diff --git a/common/database.h b/common/database.h index 6f3fd0642..c6d90e422 100644 --- a/common/database.h +++ b/common/database.h @@ -221,6 +221,8 @@ public: void GetRaidLeadershipInfo(uint32 rid, char* maintank = nullptr, char* assist = nullptr, char* puller = nullptr, char *marknpc = nullptr, RaidLeadershipAA_Struct* RLAA = nullptr); void SetRaidGroupLeaderInfo(uint32 gid, uint32 rid); + void PurgeAllDeletedDataBuckets(); + /* Database Conversions 'database_conversions.cpp' */ bool CheckDatabaseConversions(); diff --git a/common/version.h b/common/version.h index a222c057c..31345e996 100644 --- a/common/version.h +++ b/common/version.h @@ -30,7 +30,7 @@ Manifest: https://github.com/EQEmu/Server/blob/master/utils/sql/db_update_manifest.txt */ -#define CURRENT_BINARY_DATABASE_VERSION 9122 +#define CURRENT_BINARY_DATABASE_VERSION 9123 #ifdef BOTS #define CURRENT_BINARY_BOTS_DATABASE_VERSION 9019 #else diff --git a/utils/sql/db_update_manifest.txt b/utils/sql/db_update_manifest.txt index db0a04ca8..c6d5f0333 100644 --- a/utils/sql/db_update_manifest.txt +++ b/utils/sql/db_update_manifest.txt @@ -376,6 +376,7 @@ 9120|2018_02_13_Heading.sql|SELECT value FROM variables WHERE varname = 'fixed_heading'|empty| 9121|2018_02_18_bug_reports.sql|SHOW TABLES LIKE 'bug_reports'|empty| 9122|2018_03_07_ucs_command.sql|SELECT * FROM `command_settings` WHERE `command` LIKE 'ucs'|empty| +9123|2018_07_07_data_buckets.sql|SHOW TABLES LIKE 'data_buckets'|empty| # Upgrade conditions: # This won't be needed after this system is implemented, but it is used database that are not diff --git a/utils/sql/git/required/2018_07_07_data_buckets.sql b/utils/sql/git/required/2018_07_07_data_buckets.sql new file mode 100644 index 000000000..d2a842f64 --- /dev/null +++ b/utils/sql/git/required/2018_07_07_data_buckets.sql @@ -0,0 +1,8 @@ +CREATE TABLE `data_buckets` ( + `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT, + `key` varchar(100) DEFAULT NULL, + `value` text, + `expires` int(11) unsigned DEFAULT '0', + PRIMARY KEY (`id`), + KEY `key_index` (`key`) USING BTREE +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; \ No newline at end of file diff --git a/world/net.cpp b/world/net.cpp index c9a3f134c..7065ea6c3 100644 --- a/world/net.cpp +++ b/world/net.cpp @@ -85,6 +85,7 @@ union semun { #include "console.h" #include "../common/net/servertalk_server.h" +#include "../zone/data_bucket.h" ClientList client_list; GroupLFPList LFPGroupList; @@ -309,6 +310,9 @@ int main(int argc, char** argv) { } } + Log(Logs::General, Logs::World_Server, "Purging expired data buckets..."); + database.PurgeAllDeletedDataBuckets(); + Log(Logs::General, Logs::World_Server, "Loading zones.."); database.LoadZoneNames(); Log(Logs::General, Logs::World_Server, "Clearing groups.."); @@ -389,6 +393,7 @@ int main(int argc, char** argv) { Log(Logs::General, Logs::World_Server, "Purging expired instances"); database.PurgeExpiredInstances(); + Timer PurgeInstanceTimer(450000); PurgeInstanceTimer.Start(450000); @@ -545,9 +550,9 @@ int main(int argc, char** argv) { client_list.Process(); - if (PurgeInstanceTimer.Check()) - { + if (PurgeInstanceTimer.Check()) { database.PurgeExpiredInstances(); + database.PurgeAllDeletedDataBuckets(); } if (EQTimeTimer.Check()) { diff --git a/zone/CMakeLists.txt b/zone/CMakeLists.txt index b9aa7631e..0414a8d2b 100644 --- a/zone/CMakeLists.txt +++ b/zone/CMakeLists.txt @@ -19,6 +19,7 @@ SET(zone_sources client_process.cpp command.cpp corpse.cpp + data_bucket.cpp doors.cpp effects.cpp embparser.cpp @@ -155,6 +156,7 @@ SET(zone_headers command.h common.h corpse.h + data_bucket.h doors.h embparser.h embperl.h diff --git a/zone/data_bucket.cpp b/zone/data_bucket.cpp new file mode 100644 index 000000000..29ec9e0e7 --- /dev/null +++ b/zone/data_bucket.cpp @@ -0,0 +1,105 @@ +#include "data_bucket.h" +#include +#include "../common/string_util.h" +#include "zonedb.h" +#include + +/** + * Persists data via bucket_name as key + * @param bucket_key + * @param bucket_value + */ +void DataBucket::SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix) { + uint64 bucket_id = DataBucket::DoesBucketExist(bucket_key); + + std::string query; + + if (bucket_id > 0) { + std::string update_expired_time; + if (expires_at_unix > 0) { + update_expired_time = StringFormat(", `expires` = %u ", expires_at_unix); + } + + query = StringFormat( + "UPDATE `data_buckets` SET `value` = '%s' %s WHERE `id` = %i", + EscapeString(bucket_value).c_str(), + EscapeString(update_expired_time).c_str(), + bucket_id + ); + } + else { + query = StringFormat( + "INSERT INTO `data_buckets` (`key`, `value`, `expires`) VALUES ('%s', '%s', '%u')", + EscapeString(bucket_key).c_str(), + EscapeString(bucket_value).c_str(), + expires_at_unix + ); + } + + database.QueryDatabase(query); +} + +/** + * Retrieves data via bucket_name as key + * @param bucket_key + * @return + */ +std::string DataBucket::GetData(std::string bucket_key) { + std::string query = StringFormat( + "SELECT `value` from `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0) LIMIT 1", + bucket_key.c_str(), + (long long) std::time(nullptr) + ); + + auto results = database.QueryDatabase(query); + if (!results.Success()) { + return std::string(); + } + + if (results.RowCount() != 1) + return std::string(); + + auto row = results.begin(); + + return std::string(row[0]); +} + +/** + * Checks for bucket existence by bucket_name key + * @param bucket_key + * @return + */ +uint64 DataBucket::DoesBucketExist(std::string bucket_key) { + std::string query = StringFormat( + "SELECT `id` from `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0) LIMIT 1", + EscapeString(bucket_key).c_str(), + (long long) std::time(nullptr) + ); + + auto results = database.QueryDatabase(query); + if (!results.Success()) { + return 0; + } + + auto row = results.begin(); + if (results.RowCount() != 1) + return 0; + + return std::stoull(row[0]); +} + +/** + * Deletes data bucket by key + * @param bucket_key + * @return + */ +bool DataBucket::DeleteData(std::string bucket_key) { + std::string query = StringFormat( + "DELETE FROM `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0)", + EscapeString(bucket_key).c_str() + ); + + auto results = database.QueryDatabase(query); + + return results.Success(); +} \ No newline at end of file diff --git a/zone/data_bucket.h b/zone/data_bucket.h new file mode 100644 index 000000000..364ff1cc0 --- /dev/null +++ b/zone/data_bucket.h @@ -0,0 +1,22 @@ +// +// Created by Akkadius on 7/7/18. +// + +#ifndef EQEMU_DATABUCKET_H +#define EQEMU_DATABUCKET_H + + +#include +#include "../common/types.h" + +class DataBucket { +public: + static void SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix = 0); + static bool DeleteData(std::string bucket_key); + static std::string GetData(std::string bucket_key); +private: + static uint64 DoesBucketExist(std::string bucket_key); +}; + + +#endif //EQEMU_DATABUCKET_H diff --git a/zone/embparser_api.cpp b/zone/embparser_api.cpp index 04b2882d2..7725ea37a 100644 --- a/zone/embparser_api.cpp +++ b/zone/embparser_api.cpp @@ -31,6 +31,7 @@ #include "queryserv.h" #include "questmgr.h" #include "zone.h" +#include "data_bucket.h" extern Zone *zone; extern QueryServ *QServ; @@ -3501,6 +3502,54 @@ XS(XS__UpdateZoneHeader) { XSRETURN_EMPTY; } +XS(XS__get_data); +XS(XS__get_data) { + dXSARGS; + if (items != 1) + Perl_croak(aTHX_ "Usage: quest::get_data(string bucket_key)"); + + dXSTARG; + std::string key = (std::string) SvPV_nolen(ST(0)); + + sv_setpv(TARG, DataBucket::GetData(key).c_str()); + XSprePUSH; + PUSHTARG; + XSRETURN(1); +} + +XS(XS__set_data); +XS(XS__set_data) { + dXSARGS; + if (items != 2 && items != 3) { + Perl_croak(aTHX_ "Usage: quest::set_data(string key, string value, [uint32 expire_time_unix = 0])"); + } else { + std::string key = (std::string) SvPV_nolen(ST(0)); + std::string value = (std::string) SvPV_nolen(ST(1)); + + uint32 expires_at_unix = 0; + if (items == 3) + expires_at_unix = (uint32) SvIV(ST(2)); + + DataBucket::SetData(key, value, expires_at_unix); + } + XSRETURN_EMPTY; +} + +XS(XS__delete_data); +XS(XS__delete_data) { + dXSARGS; + if (items != 1) + Perl_croak(aTHX_ "Usage: quest::delete_data(string bucket_key)"); + + dXSTARG; + std::string key = (std::string) SvPV_nolen(ST(0)); + + XSprePUSH; + PUSHu((IV) DataBucket::DeleteData(key)); + + XSRETURN(1); +} + /* This is the callback perl will look for to setup the @@ -3548,6 +3597,9 @@ EXTERN_C XS(boot_quest) { newXS(strcpy(buf, "GetTimeSeconds"), XS__GetTimeSeconds, file); newXS(strcpy(buf, "GetZoneID"), XS__GetZoneID, file); newXS(strcpy(buf, "GetZoneLongName"), XS__GetZoneLongName, file); + newXS(strcpy(buf, "get_data"), XS__get_data, file); + newXS(strcpy(buf, "set_data"), XS__set_data, file); + newXS(strcpy(buf, "delete_data"), XS__delete_data, file); newXS(strcpy(buf, "IsBeneficialSpell"), XS__IsBeneficialSpell, file); newXS(strcpy(buf, "IsEffectInSpell"), XS__IsEffectInSpell, file); newXS(strcpy(buf, "IsRunning"), XS__IsRunning, file); diff --git a/zone/lua_general.cpp b/zone/lua_general.cpp index 7f59bc918..b7a075c17 100644 --- a/zone/lua_general.cpp +++ b/zone/lua_general.cpp @@ -22,6 +22,7 @@ #include "qglobals.h" #include "encounter.h" #include "lua_encounter.h" +#include "data_bucket.h" struct Events { }; struct Factions { }; @@ -817,6 +818,22 @@ std::string lua_say_link(const char *phrase) { return std::string(text); } +std::string lua_get_data(std::string bucket_key) { + return DataBucket::GetData(bucket_key); +} + +void lua_set_data(std::string bucket_key, std::string bucket_value) { + DataBucket::SetData(bucket_key, bucket_value); +} + +void lua_set_data(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix) { + DataBucket::SetData(bucket_key, bucket_value, expires_at_unix); +} + +bool lua_delete_data(std::string bucket_key) { + return DataBucket::DeleteData(bucket_key); +} + const char *lua_get_guild_name_by_id(uint32 guild_id) { return quest_manager.getguildnamebyid(guild_id); } @@ -1658,6 +1675,10 @@ luabind::scope lua_register_general() { luabind::def("say_link", (std::string(*)(const char*,bool,const char*))&lua_say_link), luabind::def("say_link", (std::string(*)(const char*,bool))&lua_say_link), luabind::def("say_link", (std::string(*)(const char*))&lua_say_link), + luabind::def("get_data", (std::string(*)(std::string))&lua_get_data), + luabind::def("set_data", (void(*)(std::string, std::string))&lua_set_data), + luabind::def("set_data", (void(*)(std::string, std::string, uint32))&lua_set_data), + luabind::def("delete_data", (bool(*)(std::string))&lua_delete_data), luabind::def("get_guild_name_by_id", &lua_get_guild_name_by_id), luabind::def("create_instance", &lua_create_instance), luabind::def("destroy_instance", &lua_destroy_instance),