Merge branch 'master' into tasks

This commit is contained in:
Michael Cook (mackal)
2018-07-08 01:47:11 -04:00
16 changed files with 253 additions and 4 deletions
+2
View File
@@ -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
+105
View File
@@ -0,0 +1,105 @@
#include "data_bucket.h"
#include <utility>
#include "../common/string_util.h"
#include "zonedb.h"
#include <ctime>
/**
* 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();
}
+22
View File
@@ -0,0 +1,22 @@
//
// Created by Akkadius on 7/7/18.
//
#ifndef EQEMU_DATABUCKET_H
#define EQEMU_DATABUCKET_H
#include <string>
#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
+52
View File
@@ -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);
+21
View File
@@ -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),