#include "StarInventoryTypes.hpp" #include "StarFormat.hpp" #include "StarJsonExtra.hpp" namespace Star { EnumMap 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()) { return JsonObject{ {"type", "equipment"}, {"location", EquipmentSlotNames.getRight(slot.get())} }; } else if (slot.is()) { return JsonObject{{"type", "swap"}}; } else if (slot.is()) { return JsonObject{{"type", "trash"}}; } else { auto bagSlot = slot.get(); 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 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()) return location.get(); else if (location.is()) return EssentialItemNames.getRight(location.get()); else return Json(); } }