This repository was archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathitemlocation.cpp
142 lines (126 loc) · 4.69 KB
/
itemlocation.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "itemlocation.h"
#include <QString>
#include "rapidjson_util.h"
ItemLocation::ItemLocation():
x_(0), y_(0), w_(0), h_(0),
socketed_(false),
type_(ItemLocationType::STASH)
{}
ItemLocation::ItemLocation(const rapidjson::Value &root):
ItemLocation()
{
FromItemJson(root);
}
ItemLocation::ItemLocation(int tab_id, std::string name, ItemLocationType type) :
ItemLocation()
{
type_ = type;
tab_id_ = tab_id;
if (type_ == ItemLocationType::STASH)
tab_label_ = name;
else
character_ = name;
}
void ItemLocation::FromItemJson(const rapidjson::Value &root) {
if (root.HasMember("_type")) {
type_ = static_cast<ItemLocationType>(root["_type"].GetInt());
if (type_ == ItemLocationType::STASH) {
tab_label_ = root["_tab_label"].GetString();
tab_id_ = root["_tab"].GetInt();
} else {
character_ = root["_character"].GetString();
}
socketed_ = false;
if (root.HasMember("_socketed"))
socketed_ = root["_socketed"].GetBool();
// socketed items have x/y pointing to parent
if (socketed_) {
x_ = root["_x"].GetInt();
y_ = root["_y"].GetInt();
}
}
if (root.HasMember("x") && root.HasMember("y") && root["x"].IsInt() && root["y"].IsInt()) {
x_ = root["x"].GetInt();
y_ = root["y"].GetInt();
}
if (root.HasMember("w") && root.HasMember("h") && root["w"].IsInt() && root["h"].IsInt()) {
w_ = root["w"].GetInt();
h_ = root["h"].GetInt();
}
if (root.HasMember("inventoryId") && root["inventoryId"].IsString())
inventory_id_ = root["inventoryId"].GetString();
}
void ItemLocation::ToItemJson(rapidjson::Value *root_ptr, rapidjson_allocator &alloc) {
auto &root = *root_ptr;
rapidjson::Value string_val(rapidjson::kStringType);
root.AddMember("_type", static_cast<int>(type_), alloc);
if (type_ == ItemLocationType::STASH) {
root.AddMember("_tab", tab_id_, alloc);
string_val.SetString(tab_label_.c_str(), alloc);
root.AddMember("_tab_label", string_val, alloc);
} else {
string_val.SetString(character_.c_str(), alloc);
root.AddMember("_character", string_val, alloc);
}
if (socketed_) {
root.AddMember("_x", x_, alloc);
root.AddMember("_y", y_, alloc);
}
root.AddMember("_socketed", socketed_, alloc);
}
std::string ItemLocation::GetHeader() const {
if (type_ == ItemLocationType::STASH) {
QString format("#%1, \"%2\"");
return format.arg(tab_id_ + 1).arg(tab_label_.c_str()).toStdString();
} else {
return character_;
}
}
QRectF ItemLocation::GetRect() const {
QRectF result;
position itemPos;
itemPos.x = x_;
itemPos.y = y_;
if ((!inventory_id_.empty()) && (type_ == ItemLocationType::CHARACTER)) {
if (inventory_id_ == "MainInventory") {
itemPos.y += POS_MAP.at(inventory_id_).y;
} else if (inventory_id_ == "Flask") {
itemPos.x += POS_MAP.at(inventory_id_).x;
itemPos.y = POS_MAP.at(inventory_id_).y;
} else if (POS_MAP.count(inventory_id_)) {
itemPos = POS_MAP.at(inventory_id_);
}
}
result.setX(MINIMAP_SIZE * itemPos.x / INVENTORY_SLOTS);
result.setY(MINIMAP_SIZE * itemPos.y / INVENTORY_SLOTS);
result.setWidth(MINIMAP_SIZE * w_ / INVENTORY_SLOTS);
result.setHeight(MINIMAP_SIZE * h_ / INVENTORY_SLOTS);
return result;
}
std::string ItemLocation::GetForumCode(const std::string &league) const {
if (type_ == ItemLocationType::STASH) {
QString format("[linkItem location=\"Stash%1\" league=\"%2\" x=\"%3\" y=\"%4\"]");
return format.arg(QString::number(tab_id_ + 1), league.c_str(), QString::number(x_), QString::number(y_)).toStdString();
} else {
QString format("[linkItem location=\"%1\" character=\"%2\" x=\"%3\" y=\"%4\"]");
return format.arg(inventory_id_.c_str(), character_.c_str(), QString::number(x_), QString::number(y_)).toStdString();
}
}
bool ItemLocation::IsValid() const {
return ( (!tab_label_.empty() && type_ == ItemLocationType::STASH)
||(!character_.empty() && type_ == ItemLocationType::CHARACTER));
}
std::string ItemLocation::GetUniqueHash() const {
if (!IsValid())
return "";
if (type_ == ItemLocationType::STASH)
return "stash:" + tab_label_;
else
return "character:" + character_;
}
bool ItemLocation::operator<(const ItemLocation &rhs) const {
if (type_ == ItemLocationType::STASH)
return std::tie(type_,tab_id_) < std::tie(rhs.type_,rhs.tab_id_);
else
return std::tie(type_,character_) < std::tie(rhs.type_,rhs.character_);
}