Add market_list() to Api.

This commit is contained in:
King_DuckZ 2020-06-23 10:19:02 +02:00
parent fd5c29ca54
commit 5b480f3ae5
6 changed files with 132 additions and 0 deletions

View file

@ -36,5 +36,19 @@ int main(int argc, char* argv[]) {
auto icons = oro_api.items_icons();
std::cout << "Total icons in DB: " << icons.second.icons.size() << '\n';
}
{
auto shops = oro_api.market_list();
for (const auto& shop : shops.second.shops) {
std::cout << "Shop \"" << shop.title << "\" by " << shop.owner <<
" in " << shop.location.map <<
" at <" << shop.location.x << ", " << shop.location.y << "> ";
if (shop.type == oro::ShopType::Buying)
std::cout << "buying ";
else
std::cout << "selling ";
std::cout << shop.items.size() << " items\n";
}
}
return 0;
}

View file

@ -24,6 +24,7 @@ executable(meson.project_name(),
'oro/api.cpp',
'oro/dateconv.cpp',
'oro/items.cpp',
'oro/shops.cpp',
project_config_file,
install: true,
dependencies: [restc_cpp_dep],

View file

@ -52,6 +52,43 @@ BOOST_FUSION_ADAPT_STRUCT(
(std::vector<oro::Icon>, icons)
)
BOOST_FUSION_ADAPT_STRUCT(
oro::Location,
(std::string, map)
(int, x)
(int, y)
)
BOOST_FUSION_ADAPT_STRUCT(
oro::ShopItem,
(unsigned int, items_id)
(unsigned int, amount)
(unsigned int, price)
(unsigned int, refine)
(std::vector<unsigned int>, cards)
(unsigned int, star_crumbs)
(boost::optional<std::string>, element)
(boost::optional<unsigned int>, creator)
(boost::optional<bool>, beloved)
)
BOOST_FUSION_ADAPT_STRUCT(
oro::Shop,
(std::string, title)
(std::string, owner)
(oro::Location, location)
(oro::Timestamp, creation_date)
(oro::ShopTypeWrapper, type)
(std::vector<oro::ShopItem>, items)
)
BOOST_FUSION_ADAPT_STRUCT(
oro::Shops,
(oro::Timestamp, generation_timestamp)
(int, version)
(std::vector<oro::Shop>, shops)
)
namespace oro {
namespace {
constexpr const std::int64_t g_max_json_mem = 1024 * 1024 * 20;
@ -59,6 +96,7 @@ namespace {
constexpr const char g_endpoint_whoami[] = "api/v1/whoami";
constexpr const char g_endpoint_items_list[] = "api/v1/items/list";
constexpr const char g_endpoint_items_icons[] = "api/v1/items/icons";
constexpr const char g_endpoint_market_list[] = "api/v1/market/list";
template <typename T>
std::pair<Header, T> call_rest_api (
@ -138,4 +176,8 @@ std::pair<Header, Icons> Api::items_icons() {
return call_rest_api<oro::Icons>(*m_client, m_prefix + g_endpoint_items_icons, m_api_key, m_client_name, m_client_purpose);
}
std::pair<Header, Shops> Api::market_list() {
return call_rest_api<oro::Shops>(*m_client, m_prefix + g_endpoint_market_list, m_api_key, m_client_name, m_client_purpose);
}
} //namespace oro

View file

@ -4,6 +4,7 @@
#include "whoami.hpp"
#include "items.hpp"
#include "icons.hpp"
#include "shops.hpp"
#include <string>
#include <utility>
#include <memory>
@ -60,6 +61,7 @@ public:
std::pair<Header, WhoAmI> who_am_i();
std::pair<Header, Items> items_list();
std::pair<Header, Icons> items_icons();
std::pair<Header, Shops> market_list();
private:
std::string m_prefix;

20
src/oro/shops.cpp Normal file
View file

@ -0,0 +1,20 @@
#include "shops.hpp"
#include <stdexcept>
namespace oro {
ShopTypeWrapper::ShopTypeWrapper (const std::string& str) {
*this = str;
}
ShopTypeWrapper& ShopTypeWrapper::operator= (const std::string& str) {
if ("V" == str)
value = ShopType::Vending;
else if ("B" == str)
value = ShopType::Buying;
else
throw std::runtime_error("Unknown shop type '" + str + "'");
return *this;
}
} //namespace oro

53
src/oro/shops.hpp Normal file
View file

@ -0,0 +1,53 @@
#pragma once
#include "datatypes.hpp"
#include <vector>
#include <string>
#include <boost/optional.hpp>
namespace oro {
enum class ShopType {
Unknown,
Vending,
Buying
};
struct ShopTypeWrapper {
ShopTypeWrapper() = default;
ShopTypeWrapper (const std::string& str);
ShopTypeWrapper& operator= (const std::string& str);
operator ShopType() const {return value;}
ShopType value {ShopType::Unknown};
};
struct Location {
std::string map;
int x;
int y;
};
struct ShopItem {
unsigned int items_id{};
unsigned int amount{};
unsigned int price{};
unsigned int refine{};
std::vector<unsigned int> cards;
unsigned int star_crumbs{};
boost::optional<std::string> element;
boost::optional<unsigned int> creator;
boost::optional<bool> beloved;
};
struct Shop {
std::string title;
std::string owner;
Location location;
Timestamp creation_date;
ShopTypeWrapper type;
std::vector<ShopItem> items;
};
struct Shops : BaseJsonReply {
std::vector<Shop> shops;
};
} //namespace oro