Make it so DataBuckets set_data can take time formats such as 1s, 1m, 1d, 1y

This commit is contained in:
Akkadius 2018-07-10 02:24:55 -05:00
parent 623b7b3eee
commit e4d8915c9d
5 changed files with 77 additions and 13 deletions

View File

@ -1,5 +1,16 @@
EQEMu Changelog (Started on Sept 24, 2003 15:50) EQEMu Changelog (Started on Sept 24, 2003 15:50)
------------------------------------------------------- -------------------------------------------------------
== 07/10/2018 ==
Akkadius: Adjusted DataBuckets to use other acceptable time formats
Example: quest::set_data('key', 'value', '1d');
- Acceptable inputs:
- 15s = 15 seconds
- s15 = 15 seconds
- 60m = 60 minutes
- 7d = 7 days
- 1y = 1 year
- 600 = 600 seconds
== 07/09/2018 == == 07/09/2018 ==
mackal: Rework of Task System, Shared Tasks still unsupported mackal: Rework of Task System, Shared Tasks still unsupported
- The tables now have better named columns, which hopefully won't need to be explained - The tables now have better named columns, which hopefully won't need to be explained

View File

@ -3,21 +3,34 @@
#include "../common/string_util.h" #include "../common/string_util.h"
#include "zonedb.h" #include "zonedb.h"
#include <ctime> #include <ctime>
#include <cctype>
#include <algorithm>
/** /**
* Persists data via bucket_name as key * Persists data via bucket_name as key
* @param bucket_key * @param bucket_key
* @param bucket_value * @param bucket_value
* @param expires_time
*/ */
void DataBucket::SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix) { void DataBucket::SetData(std::string bucket_key, std::string bucket_value, std::string expires_time) {
uint64 bucket_id = DataBucket::DoesBucketExist(bucket_key); uint64 bucket_id = DataBucket::DoesBucketExist(bucket_key);
std::string query; std::string query;
long long expires_time_unix = 0;
if (!expires_time.empty()) {
if (isalpha(expires_time[0]) || isalpha(expires_time[expires_time.length() - 1])) {
Log(Logs::General, Logs::Normal, "String check successful");
expires_time_unix = (long long) std::time(nullptr) + DataBucket::ParseStringTimeToInt(expires_time);
} else {
expires_time_unix = (long long) std::time(nullptr) + atoi(expires_time.c_str());
}
}
if (bucket_id > 0) { if (bucket_id > 0) {
std::string update_expired_time; std::string update_expired_time;
if (expires_at_unix > 0) { if (expires_time_unix > 0) {
update_expired_time = StringFormat(", `expires` = %u ", expires_at_unix); update_expired_time = StringFormat(", `expires` = %lld ", expires_time_unix);
} }
query = StringFormat( query = StringFormat(
@ -29,10 +42,10 @@ void DataBucket::SetData(std::string bucket_key, std::string bucket_value, uint3
} }
else { else {
query = StringFormat( query = StringFormat(
"INSERT INTO `data_buckets` (`key`, `value`, `expires`) VALUES ('%s', '%s', '%u')", "INSERT INTO `data_buckets` (`key`, `value`, `expires`) VALUES ('%s', '%s', '%lld')",
EscapeString(bucket_key).c_str(), EscapeString(bucket_key).c_str(),
EscapeString(bucket_value).c_str(), EscapeString(bucket_value).c_str(),
expires_at_unix expires_time_unix
); );
} }
@ -102,4 +115,43 @@ bool DataBucket::DeleteData(std::string bucket_key) {
auto results = database.QueryDatabase(query); auto results = database.QueryDatabase(query);
return results.Success(); return results.Success();
}
/**
* Converts string to integer for use when setting expiration times
* @param time_string
* @return
*/
uint32 DataBucket::ParseStringTimeToInt(std::string time_string)
{
uint32 duration = 0;
std::transform(time_string.begin(), time_string.end(), time_string.begin(), ::tolower);
if (time_string.length() < 1)
return 0;
std::string time_unit = time_string;
time_unit.erase(remove_if(time_unit.begin(), time_unit.end(), [](char c) { return !isdigit(c); }), time_unit.end());
Log(Logs::General, Logs::Normal, "ParseStringTimeToInt after erase %s", time_unit.c_str());
uint32 unit = static_cast<uint32>(atoi(time_unit.c_str()));
Log(Logs::General, Logs::Normal, "ParseStringTimeToInt seconds %u string %s", unit, time_string.c_str());
if (time_string.find('s') != std::string::npos)
duration = unit;
if (time_string.find('m') != std::string::npos)
duration = unit * 60;
if (time_string.find('h') != std::string::npos)
duration = unit * 3600;
if (time_string.find('d') != std::string::npos)
duration = unit * 86400;
if (time_string.find('y') != std::string::npos)
duration = unit * 31556926;
Log(Logs::General, Logs::Normal, "ParseStringTimeToInt returning %u", duration);
return duration;
} }

View File

@ -11,11 +11,12 @@
class DataBucket { class DataBucket {
public: public:
static void SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix = 0); static void SetData(std::string bucket_key, std::string bucket_value, std::string expires_time = "");
static bool DeleteData(std::string bucket_key); static bool DeleteData(std::string bucket_key);
static std::string GetData(std::string bucket_key); static std::string GetData(std::string bucket_key);
private: private:
static uint64 DoesBucketExist(std::string bucket_key); static uint64 DoesBucketExist(std::string bucket_key);
static uint32 ParseStringTimeToInt(std::string time_string);
}; };

View File

@ -3521,16 +3521,16 @@ XS(XS__set_data);
XS(XS__set_data) { XS(XS__set_data) {
dXSARGS; dXSARGS;
if (items != 2 && items != 3) { if (items != 2 && items != 3) {
Perl_croak(aTHX_ "Usage: quest::set_data(string key, string value, [uint32 expire_time_unix = 0])"); Perl_croak(aTHX_ "Usage: quest::set_data(string key, string value, [string expires_at = 0])");
} else { } else {
std::string key = (std::string) SvPV_nolen(ST(0)); std::string key = (std::string) SvPV_nolen(ST(0));
std::string value = (std::string) SvPV_nolen(ST(1)); std::string value = (std::string) SvPV_nolen(ST(1));
uint32 expires_at_unix = 0; std::string expires_at;
if (items == 3) if (items == 3)
expires_at_unix = (uint32) SvIV(ST(2)); expires_at = (std::string) SvPV_nolen(ST(2));
DataBucket::SetData(key, value, expires_at_unix); DataBucket::SetData(key, value, expires_at);
} }
XSRETURN_EMPTY; XSRETURN_EMPTY;
} }

View File

@ -826,8 +826,8 @@ void lua_set_data(std::string bucket_key, std::string bucket_value) {
DataBucket::SetData(bucket_key, bucket_value); DataBucket::SetData(bucket_key, bucket_value);
} }
void lua_set_data(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix) { void lua_set_data(std::string bucket_key, std::string bucket_value, std::string expires_at) {
DataBucket::SetData(bucket_key, bucket_value, expires_at_unix); DataBucket::SetData(bucket_key, bucket_value, expires_at);
} }
bool lua_delete_data(std::string bucket_key) { bool lua_delete_data(std::string bucket_key) {
@ -1677,7 +1677,7 @@ luabind::scope lua_register_general() {
luabind::def("say_link", (std::string(*)(const char*))&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("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))&lua_set_data),
luabind::def("set_data", (void(*)(std::string, std::string, uint32))&lua_set_data), luabind::def("set_data", (void(*)(std::string, std::string, std::string))&lua_set_data),
luabind::def("delete_data", (bool(*)(std::string))&lua_delete_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("get_guild_name_by_id", &lua_get_guild_name_by_id),
luabind::def("create_instance", &lua_create_instance), luabind::def("create_instance", &lua_create_instance),