From d66c020ad8e2950648e623ab6533be16195129a8 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 3 Sep 2020 23:47:33 +0100 Subject: [PATCH] Implement ApiNap::items_list() --- src/oro/api_nap.cpp | 29 +++++++++++++++++++++++++++++ src/oro/items.cpp | 20 ++++++++++++++++++-- src/oro/items.hpp | 5 +++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/oro/api_nap.cpp b/src/oro/api_nap.cpp index 87d05a0..5a3c55e 100644 --- a/src/oro/api_nap.cpp +++ b/src/oro/api_nap.cpp @@ -19,6 +19,7 @@ #include "private/v1_endpoints.hpp" #include "private/dateconv.hpp" #include "duckhandy/int_conv.hpp" +#include namespace oro { @@ -49,6 +50,16 @@ void fill_base_json (const simdjson::dom::element& doc, BaseJsonReply& out) { out.version = static_cast(doc["version"].get_int64()); } +template +std::optional get_optional (const simdjson::dom::element& elem, const char* name) { + T retval; + simdjson::error_code error = elem[name].get(retval); + if (not error) + return {static_cast(retval)}; + else + return {}; +} + } //unnamed namespace ApiNap::ApiNap ( @@ -89,6 +100,24 @@ std::pair ApiNap::who_am_i() { } std::pair ApiNap::items_list() { + return fetch_and_parse( + g_endpoint_items_list, + [](const simdjson::dom::element& doc, Items& out) { + auto items = doc["items"].get_array(); + out.items.reserve(items.size()); + for (const auto& item : items) { + Item new_entry; + new_entry.item_id = static_cast(item["item_id"].get_uint64()); + new_entry.unique_name = item["unique_name"]; + new_entry.name = item["name"]; + new_entry.type = item["type"].get_string(); + new_entry.subtype = get_optional(item, "subtype"); + new_entry.npc_price = static_cast(item["npc_price"].get_uint64()); + new_entry.slots = get_optional(item, "slots"); + out.items.push_back(std::move(new_entry)); + } + } + ); } std::pair ApiNap::items_icons() { diff --git a/src/oro/items.cpp b/src/oro/items.cpp index 8c71d26..4407ae3 100644 --- a/src/oro/items.cpp +++ b/src/oro/items.cpp @@ -23,7 +23,15 @@ ItemTypeWrapper::ItemTypeWrapper (const std::string& str) { *this = str; } +ItemTypeWrapper::ItemTypeWrapper (std::string_view str) { + *this = str; +} + ItemTypeWrapper& ItemTypeWrapper::operator= (const std::string& str) { + return this->operator=(std::string_view(str)); +} + +ItemTypeWrapper& ItemTypeWrapper::operator= (std::string_view str) { if ("IT_HEALING" == str) value = ItemType::Healing; else if ("IT_UNKNOWN" == str) @@ -51,7 +59,7 @@ ItemTypeWrapper& ItemTypeWrapper::operator= (const std::string& str) { else if ("IT_CASH" == str) value = ItemType::Cash; else - throw std::runtime_error("Unknown item type \"" + str + "\""); + throw std::runtime_error("Unknown item type \"" + std::string(str) + "\""); return *this; } @@ -59,7 +67,15 @@ ItemSubtypeWrapper::ItemSubtypeWrapper (const std::string& str) { *this = str; } +ItemSubtypeWrapper::ItemSubtypeWrapper (std::string_view str) { + *this = str; +} + ItemSubtypeWrapper& ItemSubtypeWrapper::operator= (const std::string& str) { + return this->operator=(std::string_view(str)); +} + +ItemSubtypeWrapper& ItemSubtypeWrapper::operator= (std::string_view str) { //For IT_WEAPON: if ("W_FIST" == str) value = ItemSubtype::Fist; @@ -131,7 +147,7 @@ ItemSubtypeWrapper& ItemSubtypeWrapper::operator= (const std::string& str) { value = ItemSubtype::ThrowWeapon; else - throw std::runtime_error("Unknown item subtype \"" + str + "\""); + throw std::runtime_error("Unknown item subtype \"" + std::string(str) + "\""); return *this; } } //namespace oro diff --git a/src/oro/items.hpp b/src/oro/items.hpp index 94e5e68..36d5ca4 100644 --- a/src/oro/items.hpp +++ b/src/oro/items.hpp @@ -18,6 +18,7 @@ #pragma once #include "datatypes.hpp" +#include #include #include #include @@ -83,7 +84,9 @@ namespace oro { struct ItemTypeWrapper { ItemTypeWrapper() = default; ItemTypeWrapper (const std::string& str); + ItemTypeWrapper (std::string_view str); ItemTypeWrapper& operator= (const std::string& str); + ItemTypeWrapper& operator= (std::string_view str); operator ItemType() const {return value;} ItemType value {ItemType::Unknown}; }; @@ -91,7 +94,9 @@ namespace oro { struct ItemSubtypeWrapper { ItemSubtypeWrapper() = default; ItemSubtypeWrapper (const std::string& str); + ItemSubtypeWrapper (std::string_view str); ItemSubtypeWrapper& operator= (const std::string& str); + ItemSubtypeWrapper& operator= (std::string_view str); operator ItemSubtype() const {return value;} operator int() const { return static_cast(value); } ItemSubtype value {ItemSubtype::Unknown};