Implement ApiNap::items_list()
This commit is contained in:
parent
2aaf500b66
commit
d66c020ad8
3 changed files with 52 additions and 2 deletions
|
@ -19,6 +19,7 @@
|
|||
#include "private/v1_endpoints.hpp"
|
||||
#include "private/dateconv.hpp"
|
||||
#include "duckhandy/int_conv.hpp"
|
||||
#include <optional>
|
||||
|
||||
namespace oro {
|
||||
|
||||
|
@ -49,6 +50,16 @@ void fill_base_json (const simdjson::dom::element& doc, BaseJsonReply& out) {
|
|||
out.version = static_cast<int>(doc["version"].get_int64());
|
||||
}
|
||||
|
||||
template <typename R, typename T=R>
|
||||
std::optional<R> 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<R>(retval)};
|
||||
else
|
||||
return {};
|
||||
}
|
||||
|
||||
} //unnamed namespace
|
||||
|
||||
ApiNap::ApiNap (
|
||||
|
@ -89,6 +100,24 @@ std::pair<Header, WhoAmI> ApiNap::who_am_i() {
|
|||
}
|
||||
|
||||
std::pair<Header, Items> ApiNap::items_list() {
|
||||
return fetch_and_parse<Items>(
|
||||
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<unsigned int>(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<ItemSubtypeWrapper, std::string_view>(item, "subtype");
|
||||
new_entry.npc_price = static_cast<unsigned int>(item["npc_price"].get_uint64());
|
||||
new_entry.slots = get_optional<unsigned int, uint64_t>(item, "slots");
|
||||
out.items.push_back(std::move(new_entry));
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
std::pair<Header, Icons> ApiNap::items_icons() {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "datatypes.hpp"
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
@ -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<int>(value); }
|
||||
ItemSubtype value {ItemSubtype::Unknown};
|
||||
|
|
Loading…
Reference in a new issue