From 23676cf17b54ee518da90ce05699aea20a5a876d Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Tue, 25 Jul 2023 22:37:32 -0700 Subject: [PATCH] rarityTable --- dDatabase/Tables/CDRarityTableTable.cpp | 35 ++++--------------------- dDatabase/Tables/CDRarityTableTable.h | 6 ++--- 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/dDatabase/Tables/CDRarityTableTable.cpp b/dDatabase/Tables/CDRarityTableTable.cpp index b0295a59..c5cbfef7 100644 --- a/dDatabase/Tables/CDRarityTableTable.cpp +++ b/dDatabase/Tables/CDRarityTableTable.cpp @@ -1,31 +1,16 @@ #include "CDRarityTableTable.h" void CDRarityTableTable::LoadValuesFromDatabase() { - - // First, get the size of the table - unsigned int size = 0; - auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM RarityTable"); - while (!tableSize.eof()) { - size = tableSize.getIntField(0, 0); - - tableSize.nextRow(); - } - - tableSize.finalize(); - - // Reserve the size - this->entries.reserve(size); - // Now get the data auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM RarityTable"); while (!tableData.eof()) { CDRarityTable entry; - entry.id = tableData.getIntField("id", -1); + uint32_t id = tableData.getIntField("id", -1); entry.randmax = tableData.getFloatField("randmax", -1); entry.rarity = tableData.getIntField("rarity", -1); entry.RarityTableIndex = tableData.getIntField("RarityTableIndex", -1); - this->entries.push_back(entry); + this->entries.insert_or_assign(id, entry); tableData.nextRow(); } @@ -33,17 +18,7 @@ void CDRarityTableTable::LoadValuesFromDatabase() { } //! Queries the table with a custom "where" clause -std::vector CDRarityTableTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) - >> cpplinq::where(predicate) - >> cpplinq::to_vector(); - - return data; +const std::optional CDRarityTableTable::Get(uint32_t id) { + auto it = this->entries.find(id); + return it != this->entries.end() ? std::make_optional(it->second) : std::nullopt; } - -//! Gets all the entries in the table -const std::vector& CDRarityTableTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/Tables/CDRarityTableTable.h b/dDatabase/Tables/CDRarityTableTable.h index 2d28e4a5..a0fe4464 100644 --- a/dDatabase/Tables/CDRarityTableTable.h +++ b/dDatabase/Tables/CDRarityTableTable.h @@ -28,13 +28,11 @@ struct CDRarityTable { class CDRarityTableTable : public CDTable { private: - std::vector entries; + std::unordered_map entries; public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause - std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; + const std::optional Get(uint32_t predicate); };