Argentum Online - Servidor
item_container.h
1 #ifndef ITEM_CONTAINER_H
2 #define ITEM_CONTAINER_H
3 
4 #include <array>
5 #include <vector>
6 
7 #include "../../../include/nlohmann/json.hpp"
8 #include "../../../include/types.h"
9 #include "item.h"
10 #include "gold.h"
11 
12 // Segun mock el inventario tiene 12 slots -> el oro no ocupa un slot
13 // en el inventario.
14 
15 #define INV_SIZE 12
16 
17 class FullItemContainerException : public std::exception {
18  public:
19  const char* what() const throw();
20 };
21 
22 class EmptySlotException : public std::exception {
23  public:
24  const char* what() const throw();
25 };
26 
27 class OutOfRangeSlotException : public std::exception {
28  public:
29  const char* what() const throw();
30 };
31 
32 typedef struct inventory {
33  ItemId items_ids[INV_SIZE];
34  uint32_t items_stacks[INV_SIZE];
35  unsigned int current_gold;
36 } inventory_t;
37 
38 inline void to_json(nlohmann::json& j, const inventory_t& i) {
39  j["items_ids"] = i.items_ids;
40  j["items_stacks"] = i.items_stacks;
41  j["curr_gold"] = i.current_gold;
42 }
43 
44 inline void from_json(const nlohmann::json& j, inventory_t& i) {
45  j["items_ids"].get_to(i.items_ids);
46  j["items_stacks"].get_to(i.items_stacks);
47  j["curr_gold"].get_to(i.current_gold);
48 }
49 
51  private:
52  Gold *gold;
53  std::vector<Item*> item_container;
54  std::map<ItemId, SlotId> item_id_to_slot;
55  nlohmann::json _get_data(bool need_sprite_id) const;
56 
57  public:
58  ItemContainer();
59 
61  ItemContainer& operator=(ItemContainer&& other);
62 
63  ItemContainer(unsigned int slots_amount);
69  ItemContainer(const nlohmann::json& inv_json);
88  void add(Item* item);
103  void add(Item* item, uint32_t stack);
112  Item* remove(SlotId slot_id);
122  Item* remove(SlotId slot_id, uint32_t stack);
129  std::vector<Item*> remove_all();
138  SlotId get_available_slot(ItemId item_id);
139  const Item& get_item(SlotId slot_id) const;
140  bool slot_is_free(SlotId slot_id) const;
141  bool has_item(ItemId item_id);
142  unsigned int get_gold_stack() const;
149  void add_gold(Gold* gold);
159  void add_gold(Gold* gold, uint32_t stack);
166  Gold* remove_gold();
176  Gold* remove_gold(uint32_t stack);
177  bool is_in_range(SlotId slotId) const;
178  bool has_slots_left() const;
179 
185  nlohmann::json get_persist_data() const;
191  nlohmann::json get_data() const;
192 };
193 
194 #endif // ITEM_CONTAINER_H
195 
196 /*
197  Metodologia al agregar (la que es mas complicada):
198  Al agregar el item lo hago de la siguiente manera:
199  Pregunto si hay slots libres
200  a) Si los hay,
201  *) A/B
202  b) Si no los hay,
203  *) A/B
204 
205  A/B:
206  Me fijo si tengo en algun slot un item con el mismo
207  ItemId (para sumarle el stack del item ingresante al que ya esta),
208  pero aca hay varios casos:
209  *) No hay slots con ese itemId
210  a*) Lo agrego al primer slot disponible (numericamente)
211  b*) Exception
212  *) Hay slot/slots, devuelta en 2 casos:
213  **) Un unico slot:
214  Si tiene stack al maximo
215  a*) Lo agrego al primer slot disponible
216  (numericamente)
217  b*) Exception
218  Si no tiene el stack al maximo:
219  Le sumo todo lo que pueda al stack del item del
220  inv: a*) La diferencia va al primer slot disponible (numericamente) b*)
221  La diferencia queda en el "piso"
222  **) Varios slots:
223  Si tienen todos stack al maximo
224  a*) Lo agrego al primer slot disponible
225  (numericamente)
226  b*) Exception
227  No tienen todos el stack al maximo:
228  Debo ir llenando los slots en orden numerico
229  (SlotId): En caso de quedar diferencia. a*) Lo agrego al primer slot
230  disponible (numericamente) b*) Diferencia queda en el piso como en el
231  anterior.
232 */
inventory
Definition: item_container.h:32
ItemContainer::get_available_slot
SlotId get_available_slot(ItemId item_id)
Devuelve el SlotId asignado/a asignar al item con item_id. Lanza FullContainerException en caso de qu...
Definition: item_container.cpp:183
Item
Definition: item.h:45
EmptySlotException
Definition: item_container.h:22
ItemContainer::get_persist_data
nlohmann::json get_persist_data() const
Informacion para persistir el inventario en el personaje.
Definition: item_container.cpp:146
OutOfRangeSlotException
Definition: item_container.h:27
ItemContainer
Definition: item_container.h:50
ItemContainer::remove_all
std::vector< Item * > remove_all()
Remueve todos los items del inventario, devolviendo un vector que los contiene a todos.
Definition: item_container.cpp:123
ItemContainer::add
void add(Item *item)
Agrega el item entero (con todo su stack) al inventario. Lo invalida haciendo delete del puntero (en ...
Definition: item_container.cpp:69
ItemContainer::~ItemContainer
~ItemContainer()
Se destruyen los items que quedaron almacenados en el inventario, junto con el oro.
Definition: item_container.cpp:62
FullItemContainerException
Definition: item_container.h:17
ItemContainer::add_gold
void add_gold(Gold *gold)
Agrega la totalidad del stack del oro al inventario, seteando su stack en 0 (no invalida el puntero)
Definition: item_container.cpp:194
Gold
Definition: gold.h:6
ItemContainer::remove_gold
Gold * remove_gold()
Remueve la totalidad del oro del inventario (setea el stack en 0), devolviendo un nuevo puntero a Gol...
Definition: item_container.cpp:211
ItemContainer::get_data
nlohmann::json get_data() const
Informacion a enviar al cliente.
Definition: item_container.cpp:132
ItemContainer::remove
Item * remove(SlotId slot_id)
Remueve en su totalidad el item que se encuentra en el slot_id, devolviendo un puntero a este....
Definition: item_container.cpp:98