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

summaryrefslogtreecommitdiff
path: root/source/game/StarInventoryTypes.cpp
blob: 46892a87ca5231acad857781954e449fc05cf41a (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
82
83
84
85
86
87
88
89
90
91
#include "StarInventoryTypes.hpp"
#include "StarFormat.hpp"
#include "StarJsonExtra.hpp"

namespace Star {

EnumMap<EquipmentSlot> const EquipmentSlotNames{
  {EquipmentSlot::Head, "head"},
  {EquipmentSlot::Chest, "chest"},
  {EquipmentSlot::Legs, "legs"},
  {EquipmentSlot::Back, "back"},
  {EquipmentSlot::HeadCosmetic, "headCosmetic"},
  {EquipmentSlot::ChestCosmetic, "chestCosmetic"},
  {EquipmentSlot::LegsCosmetic, "legsCosmetic"},
  {EquipmentSlot::BackCosmetic, "backCosmetic"}
};

InventorySlot jsonToInventorySlot(Json const& json) {
  String type = json.getString("type");
  Json location = json.get("location", Json());
  if (type == "equipment")
    return EquipmentSlotNames.getLeft(location.toString());
  else if (type == "swap")
    return SwapSlot();
  else if (type == "trash")
    return TrashSlot();
  else
    return BagSlot(type, location.toUInt());
}

Json jsonFromInventorySlot(InventorySlot const& slot) {
  if (slot.is<EquipmentSlot>()) {
    return JsonObject{
      {"type", "equipment"},
      {"location", EquipmentSlotNames.getRight(slot.get<EquipmentSlot>())}
    };
  } else if (slot.is<SwapSlot>()) {
    return JsonObject{{"type", "swap"}};
  } else if (slot.is<TrashSlot>()) {
    return JsonObject{{"type", "trash"}};
  } else {
    auto bagSlot = slot.get<BagSlot>();
    return JsonObject{
      {"type", bagSlot.first},
      {"location", bagSlot.second}
    };
  }
}

std::ostream& operator<<(std::ostream& ostream, InventorySlot const& slot) {
  Json json = jsonFromInventorySlot(slot);

  String type = json.getString("type");
  Json location = json.get("location", {});

  if (location.isNull())
    format(ostream, "InventorySlot{type: {}}", type);
  if (location.isType(Json::Type::String))
    format(ostream, "InventorySlot{type: {}, location: {}}", type, location.toString());
  else
    format(ostream, "InventorySlot{type: {}, location: {}}", type, location.toInt());

  return ostream;
}

EnumMap<EssentialItem> const EssentialItemNames{
  {EssentialItem::BeamAxe, "beamaxe"},
  {EssentialItem::WireTool, "wiretool"},
  {EssentialItem::PaintTool, "painttool"},
  {EssentialItem::InspectionTool, "inspectiontool"}
};

SelectedActionBarLocation jsonToSelectedActionBarLocation(Json const& json) {
  if (json.isType(Json::Type::String))
    return EssentialItemNames.getLeft(json.toString());
  else if (json.isNull())
    return SelectedActionBarLocation();
  else
    return (CustomBarIndex)json.toUInt();
}

Json jsonFromSelectedActionBarLocation(SelectedActionBarLocation const& location) {
  if (location.is<CustomBarIndex>())
    return location.get<CustomBarIndex>();
  else if (location.is<EssentialItem>())
    return EssentialItemNames.getRight(location.get<EssentialItem>());
  else
    return Json();
}

}