diff --git a/zone/data_bucket.cpp b/zone/data_bucket.cpp index 67c1e04c4..1ccb21953 100644 --- a/zone/data_bucket.cpp +++ b/zone/data_bucket.cpp @@ -76,6 +76,31 @@ std::string DataBucket::GetData(std::string bucket_key) { return std::string(row[0]); } +/** + * Retrieves data expires time via bucket_name as key + * @param bucket_key + * @return + */ +std::string DataBucket::GetDataExpires(std::string bucket_key) { + std::string query = StringFormat( + "SELECT `expires` 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 diff --git a/zone/data_bucket.h b/zone/data_bucket.h index 87d3ab0b3..88a344637 100644 --- a/zone/data_bucket.h +++ b/zone/data_bucket.h @@ -14,6 +14,7 @@ public: static void SetData(std::string bucket_key, std::string bucket_value, std::string expires_time = ""); static bool DeleteData(std::string bucket_key); static std::string GetData(std::string bucket_key); + static std::string GetDataExpires(std::string bucket_key); private: static uint64 DoesBucketExist(std::string bucket_key); static uint32 ParseStringTimeToInt(std::string time_string); diff --git a/zone/embparser_api.cpp b/zone/embparser_api.cpp index 1a251b8ec..633bcda53 100644 --- a/zone/embparser_api.cpp +++ b/zone/embparser_api.cpp @@ -3596,6 +3596,21 @@ XS(XS__get_data) { XSRETURN(1); } +XS(XS__get_data_expires); +XS(XS__get_data_expires) { + dXSARGS; + if (items != 1) + Perl_croak(aTHX_ "Usage: quest::get_data_expires(string bucket_key)"); + + dXSTARG; + std::string key = (std::string) SvPV_nolen(ST(0)); + + sv_setpv(TARG, DataBucket::GetDataExpires(key).c_str()); + XSprePUSH; + PUSHTARG; + XSRETURN(1); +} + XS(XS__set_data); XS(XS__set_data) { dXSARGS; diff --git a/zone/lua_general.cpp b/zone/lua_general.cpp index 65fe2c575..955b69d5e 100644 --- a/zone/lua_general.cpp +++ b/zone/lua_general.cpp @@ -822,6 +822,10 @@ std::string lua_get_data(std::string bucket_key) { return DataBucket::GetData(bucket_key); } +std::string lua_get_data_expires(std::string bucket_key) { + return DataBucket::GetDataExpires(bucket_key); +} + void lua_set_data(std::string bucket_key, std::string bucket_value) { DataBucket::SetData(bucket_key, bucket_value); }