Skip to content

Commit

Permalink
[Databuckets] Fix issue with expired databuckets not being expired an…
Browse files Browse the repository at this point in the history
…d returned properly (#3504)
  • Loading branch information
Akkadius authored Jul 18, 2023
1 parent fb3159b commit 4c2f9a4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 57 deletions.
64 changes: 21 additions & 43 deletions zone/data_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,11 @@ void DataBucket::SetData(const std::string &bucket_key, const std::string &bucke

void DataBucket::SetData(const DataBucketKey &k)
{
auto r = DataBucketsRepository::GetWhere(
database,
fmt::format(
"{} `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
DataBucket::GetScopedDbFilters(k),
Strings::Escape(k.key),
(long long) std::time(nullptr)
)
);

// if we have an entry, use it
auto b = DataBucketsRepository::NewEntity();
if (!r.empty()) {
b = r[0];
auto r = GetData(k);
// if we have an entry, use it
if (r.id > 0) {
b = r;
}

if (k.character_id > 0) {
Expand Down Expand Up @@ -72,26 +63,31 @@ std::string DataBucket::GetData(const std::string &bucket_key)
{
DataBucketKey k = {};
k.key = bucket_key;
return GetData(k);
return GetData(k).value;
}

std::string DataBucket::GetData(const DataBucketKey &k)
DataBucketsRepository::DataBuckets DataBucket::GetData(const DataBucketKey &k)
{
auto r = DataBucketsRepository::GetWhere(
database,
fmt::format(
"{} `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
"{} `key` = '{}' LIMIT 1",
DataBucket::GetScopedDbFilters(k),
k.key,
(long long) std::time(nullptr)
k.key
)
);

if (r.empty()) {
return {};
}

return r[0].value;
// if the entry has expired, delete it
if (r[0].expires > 0 && r[0].expires < (long long) std::time(nullptr)) {
DeleteData(k);
return {};
}

return r[0];
}

std::string DataBucket::GetDataExpires(const std::string &bucket_key)
Expand Down Expand Up @@ -174,40 +170,22 @@ bool DataBucket::DeleteData(const DataBucketKey &k)

std::string DataBucket::GetDataExpires(const DataBucketKey &k)
{
auto r = DataBucketsRepository::GetWhere(
database,
fmt::format(
"{} `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
DataBucket::GetScopedDbFilters(k),
k.key,
(long long) std::time(nullptr)
)
);

if (r.empty()) {
auto r = GetData(k);
if (r.id == 0) {
return {};
}

return fmt::format("{}", r[0].expires);
return fmt::format("{}", r.expires);
}

std::string DataBucket::GetDataRemaining(const DataBucketKey &k)
{
auto r = DataBucketsRepository::GetWhere(
database,
fmt::format(
"{} `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
DataBucket::GetScopedDbFilters(k),
k.key,
(long long) std::time(nullptr)
)
);

if (r.empty()) {
auto r = GetData(k);
if (r.id == 0) {
return "0";
}

return fmt::format("{}", r[0].expires - (long long) std::time(nullptr));
return fmt::format("{}", r.expires - (long long) std::time(nullptr));
}

std::string DataBucket::GetScopedDbFilters(const DataBucketKey &k)
Expand Down
2 changes: 1 addition & 1 deletion zone/data_bucket.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DataBucket {
// scoped bucket methods
static void SetData(const DataBucketKey& k);
static bool DeleteData(const DataBucketKey& k);
static std::string GetData(const DataBucketKey& k);
static DataBucketsRepository::DataBuckets GetData(const DataBucketKey& k);
static std::string GetDataExpires(const DataBucketKey& k);
static std::string GetDataRemaining(const DataBucketKey& k);
static std::string CheckBucketKey(const Mob* mob, const DataBucketKey& k);
Expand Down
8 changes: 4 additions & 4 deletions zone/exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1276,10 +1276,10 @@ uint8 Client::GetCharMaxLevelFromBucket()
DataBucketKey k = GetScopedBucketKeys();
k.key = "CharMaxLevel";

auto bucket_value = DataBucket::GetData(k);
if (!bucket_value.empty()) {
if (Strings::IsNumber(bucket_value)) {
return static_cast<uint8>(Strings::ToUnsignedInt(bucket_value));
auto b = DataBucket::GetData(k);
if (!b.value.empty()) {
if (Strings::IsNumber(b.value)) {
return static_cast<uint8>(Strings::ToUnsignedInt(b.value));
}
}

Expand Down
6 changes: 3 additions & 3 deletions zone/mob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8062,9 +8062,9 @@ std::string Mob::GetBucket(std::string bucket_name)
DataBucketKey k = GetScopedBucketKeys();
k.key = bucket_name;

std::string bucket_value = DataBucket::GetData(k);
if (!bucket_value.empty()) {
return bucket_value;
auto b = DataBucket::GetData(k);
if (!b.value.empty()) {
return b.value;
}
return {};
}
Expand Down
12 changes: 6 additions & 6 deletions zone/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5880,14 +5880,14 @@ bool Client::SpellBucketCheck(uint16 spell_id, uint32 character_id) {
DataBucketKey k = GetScopedBucketKeys();
k.key = spell_bucket_name;

auto bucket_value = DataBucket::GetData(k);
if (!bucket_value.empty()) {
if (Strings::IsNumber(bucket_value) && Strings::IsNumber(spell_bucket_value)) {
if (Strings::ToInt(bucket_value) >= Strings::ToInt(spell_bucket_value)) {
auto b = DataBucket::GetData(k);
if (!b.value.empty()) {
if (Strings::IsNumber(b.value) && Strings::IsNumber(spell_bucket_value)) {
if (Strings::ToInt(b.value) >= Strings::ToInt(spell_bucket_value)) {
return true; // If value is greater than or equal to spell bucket value, allow scribing.
}
} else {
if (bucket_value == spell_bucket_value) {
if (b.value == spell_bucket_value) {
return true; // If value is equal to spell bucket value, allow scribing.
}
}
Expand All @@ -5899,7 +5899,7 @@ bool Client::SpellBucketCheck(uint16 spell_id, uint32 character_id) {
spell_bucket_name
);

bucket_value = DataBucket::GetData(old_bucket_name);
auto bucket_value = DataBucket::GetData(old_bucket_name);
if (!bucket_value.empty()) {
if (Strings::IsNumber(bucket_value) && Strings::IsNumber(spell_bucket_value)) {
if (Strings::ToInt(bucket_value) >= Strings::ToInt(spell_bucket_value)) {
Expand Down

0 comments on commit 4c2f9a4

Please sign in to comment.