Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarItemDescriptor.hpp
blob: 5dab058308800a6b97b7d4bb893f266b217b41d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once

#include "StarJson.hpp"
#include "StarDataStream.hpp"
#include "StarBiMap.hpp"

namespace Star {

STAR_CLASS(ItemDescriptor);
STAR_CLASS(Item);
STAR_STRUCT(VersionedJson);

class ItemDescriptor {
public:
  // Loads ItemDescriptor from store format.
  static ItemDescriptor loadStore(Json const& store);

  ItemDescriptor();
  ItemDescriptor(String name, uint64_t count, Json parameters = Json());

  // Populate from a configuration JsonArray containing up to 3 elements, the
  // name, count, and then any item parameters.  If the json is a map, looks
  // for keys 'name', 'parameters', and 'count'.
  explicit ItemDescriptor(Json const& spec);

  String const& name() const;
  uint64_t count() const;
  Json const& parameters() const;

  ItemDescriptor singular() const;
  ItemDescriptor withCount(uint64_t count) const;
  ItemDescriptor multiply(uint64_t count) const;
  ItemDescriptor applyParameters(JsonObject const& parameters) const;

  // Descriptor is the default constructed ItemDescriptor()
  bool isNull() const;

  // Descriptor is not null
  explicit operator bool() const;

  // True if descriptor is null OR if descriptor is size 0
  bool isEmpty() const;

  bool operator==(ItemDescriptor const& rhs) const;
  bool operator!=(ItemDescriptor const& rhs) const;

  bool matches(ItemDescriptor const& other, bool exactMatch = false) const;
  bool matches(ItemConstPtr const& other, bool exactMatch = false) const;

  // Stores ItemDescriptor to versioned structure not meant for human reading / writing.
  Json diskStore() const;

  // Converts ItemDescriptor to spec format
  Json toJson() const;

  friend DataStream& operator>>(DataStream& ds, ItemDescriptor& itemDescriptor);
  friend DataStream& operator<<(DataStream& ds, ItemDescriptor const& itemDescriptor);

  friend std::ostream& operator<<(std::ostream& os, ItemDescriptor const& descriptor);

  friend struct hash<ItemDescriptor>;

private:
  ItemDescriptor(String name, uint64_t count, Json parameters, Maybe<size_t> parametersHash);

  size_t parametersHash() const;

  String m_name;
  uint64_t m_count;
  Json m_parameters;
  mutable Maybe<size_t> m_parametersHash;
};

template <>
struct hash<ItemDescriptor> {
  size_t operator()(ItemDescriptor const& v) const;
};

}

template <> struct fmt::formatter<Star::ItemDescriptor> : ostream_formatter {};