Add items_list() to Api.

This commit is contained in:
King_DuckZ 2020-06-22 20:53:56 +02:00
parent 939bcffc61
commit fd147c4f2a
6 changed files with 249 additions and 0 deletions

View file

@ -26,5 +26,13 @@ int main(int argc, char* argv[]) {
auto whoami = oro_api.who_am_i();
std::cout << "master id: " << whoami.second.master_id << '\n';
}
{
auto items = oro_api.items_list();
for (const auto& item : items.second.items) {
std::cout << "Item ID " << item.item_id << ' ' << item.name << '\n';
}
std::cout << "Total items in DB: " << items.second.items.size() << '\n';
}
return 0;
}

View file

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

View file

@ -21,10 +21,29 @@ BOOST_FUSION_ADAPT_STRUCT(
(unsigned int, master_id)
)
BOOST_FUSION_ADAPT_STRUCT(
oro::Item,
(unsigned int, item_id)
(std::string, unique_name)
(std::string, name)
(oro::ItemTypeWrapper, type)
(boost::optional<oro::ItemSubtypeWrapper>, subtype)
(unsigned int, npc_price)
(boost::optional<unsigned int>, slots)
)
BOOST_FUSION_ADAPT_STRUCT(
oro::Items,
(oro::Timestamp, generation_timestamp)
(int, version)
(std::vector<oro::Item>, items)
)
namespace oro {
namespace {
constexpr const char g_endpoint_ping[] = "api/v1/ping";
constexpr const char g_endpoint_whoami[] = "api/v1/whoami";
constexpr const char g_endpoint_items_list[] = "api/v1/items/list";
template <typename T>
std::pair<Header, T> call_rest_api (
@ -96,4 +115,8 @@ std::pair<Header, WhoAmI> Api::who_am_i() {
return call_rest_api<oro::WhoAmI>(*m_client, m_prefix + g_endpoint_whoami, m_api_key, m_client_name, m_client_purpose);
}
std::pair<Header, Items> Api::items_list() {
return call_rest_api<oro::Items>(*m_client, m_prefix + g_endpoint_items_list, m_api_key, m_client_name, m_client_purpose);
}
} //namespace oro

View file

@ -2,6 +2,7 @@
#include "ping.hpp"
#include "whoami.hpp"
#include "items.hpp"
#include <string>
#include <utility>
#include <memory>
@ -56,6 +57,7 @@ public:
std::pair<Header, Ping> ping();
std::pair<Header, WhoAmI> who_am_i();
std::pair<Header, Items> items_list();
private:
std::string m_prefix;

120
src/oro/items.cpp Normal file
View file

@ -0,0 +1,120 @@
#include "items.hpp"
#include <stdexcept>
namespace oro {
ItemTypeWrapper::ItemTypeWrapper (const std::string& str) {
*this = str;
}
ItemTypeWrapper& ItemTypeWrapper::operator= (const std::string& str) {
if ("IT_HEALING" == str)
value = ItemType::Healing;
else if ("IT_UNKNOWN" == str)
value = ItemType::Unknown;
else if ("IT_USABLE" == str)
value = ItemType::Usable;
else if ("IT_ETC" == str)
value = ItemType::Etc;
else if ("IT_WEAPON" == str)
value = ItemType::Weapon;
else if ("IT_ARMOR" == str)
value = ItemType::Armor;
else if ("IT_CARD" == str)
value = ItemType::Card;
else if ("IT_PETEGG" == str)
value = ItemType::PetEgg;
else if ("IT_PETARMOR" == str)
value = ItemType::PetArmor;
else if ("IT_UNKNOWN2" == str)
value = ItemType::Unknown2;
else if ("IT_AMMO" == str)
value = ItemType::Ammo;
else if ("IT_DELAYCONSUME" == str)
value = ItemType::DelayConsume;
else if ("IT_CASH" == str)
value = ItemType::Cash;
else
throw std::runtime_error("Unknown item type \"" + str + "\"");
return *this;
}
ItemSubtypeWrapper::ItemSubtypeWrapper (const std::string& str) {
*this = str;
}
ItemSubtypeWrapper& ItemSubtypeWrapper::operator= (const std::string& str) {
//For IT_WEAPON:
if ("W_FIST" == str)
value = ItemSubtype::Fist;
else if ("W_DAGGER" == str)
value = ItemSubtype::Dagger;
else if ("W_1HSWORD" == str)
value = ItemSubtype::OneHSword;
else if ("W_2HSWORD" == str)
value = ItemSubtype::TwoHSword;
else if ("W_1HSPEAR" == str)
value = ItemSubtype::OneHSpear;
else if ("W_2HSPEAR" == str)
value = ItemSubtype::TwoHSpear;
else if ("W_1HAXE" == str)
value = ItemSubtype::OneHAxe;
else if ("W_2HAXE" == str)
value = ItemSubtype::TwoHAxe;
else if ("W_MACE" == str)
value = ItemSubtype::Mace;
else if ("W_2HMACE" == str)
value = ItemSubtype::TwoHMace;
else if ("W_STAFF" == str)
value = ItemSubtype::Staff;
else if ("W_2HSTAFF" == str)
value = ItemSubtype::TwoHStaff;
else if ("W_BOW" == str)
value = ItemSubtype::Bow;
else if ("W_KNUCKLE" == str)
value = ItemSubtype::Knuckle;
else if ("W_MUSICAL" == str)
value = ItemSubtype::Musical;
else if ("W_WHIP" == str)
value = ItemSubtype::Whip;
else if ("W_BOOK" == str)
value = ItemSubtype::Book;
else if ("W_KATAR" == str)
value = ItemSubtype::Katar;
else if ("W_REVOLVER" == str)
value = ItemSubtype::Revolver;
else if ("W_RIFLE" == str)
value = ItemSubtype::Rifle;
else if ("W_GATLING" == str)
value = ItemSubtype::Gatling;
else if ("W_SHOTGUN" == str)
value = ItemSubtype::Shotgun;
else if ("W_GRENADE" == str)
value = ItemSubtype::Grenade;
else if ("W_HUUMA" == str)
value = ItemSubtype::Huuma;
//For IT_AMMO:
else if ("A_ARROW" == str)
value = ItemSubtype::Arrow;
else if ("A_DAGGER" == str)
value = ItemSubtype::ThrowingDagger;
else if ("A_BULLET" == str)
value = ItemSubtype::Bullet;
else if ("A_SHELL" == str)
value = ItemSubtype::Shell;
else if ("A_GRENADE" == str)
value = ItemSubtype::GrenadeAmmo;
else if ("A_SHURIKEN" == str)
value = ItemSubtype::Shuriken;
else if ("A_KUNAI" == str)
value = ItemSubtype::Kunai;
else if ("A_CANNONBALL" == str)
value = ItemSubtype::Cannonball;
else if ("A_THROWWEAPON" == str)
value = ItemSubtype::ThrowWeapon;
else
throw std::runtime_error("Unknown item subtype \"" + str + "\"");
return *this;
}
} //namespace oro

95
src/oro/items.hpp Normal file
View file

@ -0,0 +1,95 @@
#pragma once
#include "datatypes.hpp"
#include <vector>
#include <string>
#include <boost/optional.hpp>
namespace oro {
enum class ItemType {
Healing, //Healing items
Unknown, //Unknown item type
Usable, //Other usable items
Etc, //Misc items
Weapon, //Weapons
Armor, //Armors
Card, //Cards
PetEgg, //Pet eggs
PetArmor, //Pet accessories
Unknown2, //Unknown items (unused)
Ammo, //Ammunitions
DelayConsume, //Consumable items (triggering additional actions)
Cash //Cash shop items
};
enum class ItemSubtype {
//For IT_WEAPON:
Fist, //Unknown weapon (unused)
Dagger, //Daggers
OneHSword, //1-hand swords
TwoHSword, //2-hand swords
OneHSpear, //1-hand spears
TwoHSpear, //2-hand spears
OneHAxe, //1-hand axes
TwoHAxe, //2-hand axes
Mace, //1-hand maces
TwoHMace, //2-hand maces
Staff, //Staves
TwoHStaff, //2-hand staves
Bow, //Bows
Knuckle, //Knuckles
Musical, //Musical instruments
Whip, //Whips
Book, //Books
Katar, //Katars
Revolver, //Pistols
Rifle, //Rifles
Gatling, //Gatling guns
Shotgun, //Shotguns
Grenade, //Grenades
Huuma, //Huuma shuriken
//For IT_AMMO:
Arrow, //Arrows
ThrowingDagger, //Throwing daggers
Bullet, //Bullets
Shell, //Shells
GrenadeAmmo, //Grenades
Shuriken, //Shuriken
Kunai, //Kunai
Cannonball, //Cannon balls
ThrowWeapon, //Other throwing weapons
Unknown
};
struct ItemTypeWrapper {
ItemTypeWrapper() = default;
ItemTypeWrapper (const std::string& str);
ItemTypeWrapper& operator= (const std::string& str);
operator ItemType() const {return value;}
ItemType value {ItemType::Unknown};
};
struct ItemSubtypeWrapper {
ItemSubtypeWrapper() = default;
ItemSubtypeWrapper (const std::string& str);
ItemSubtypeWrapper& operator= (const std::string& str);
operator ItemSubtype() const {return value;}
ItemSubtype value {ItemSubtype::Unknown};
};
struct Item {
unsigned int item_id;
std::string unique_name;
std::string name;
ItemTypeWrapper type;
boost::optional<ItemSubtypeWrapper> subtype;
unsigned int npc_price;
boost::optional<unsigned int> slots;
};
struct Items : BaseJsonReply {
std::vector<Item> items;
};
} //namespace oro