-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathStatsDatabase.cpp
More file actions
252 lines (209 loc) · 6.53 KB
/
Copy pathStatsDatabase.cpp
File metadata and controls
252 lines (209 loc) · 6.53 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Stats Database
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <QFile>
#include <QSaveFile>
#include "Common/Cpp/Time.h"
#include "StatsDatabase.h"
#include <iostream>
using std::cout;
using std::endl;
namespace PokemonAutomation{
const std::map<std::string, std::string> STATS_DATABASE_ALIASES{
{"Dex Rec Finder", "PokemonSwSh:DexRecFinder"},
{"Day Skipper (JPN)", "PokemonSwSh:DaySkipperJPN"},
{"Day Skipper (EU)", "PokemonSwSh:DaySkipperEU"},
{"Day Skipper (US)", "PokemonSwSh:DaySkipperUS"},
{"Day Skipper (JPN) - 7.8k", "PokemonSwSh:DaySkipperJPN7p8k"},
{"Purple Beam Finder", "PokemonSwSh:PurpleBeamFinder"},
{"Auto-Host Multi-Game", "PokemonSwSh:AutoHostMultiGame"},
{"Auto-Host Rolling", "PokemonSwSh:AutoHostRolling"},
{"Stats Reset", "PokemonSwSh:StatsReset"},
{"Shiny Hunt Autonomous - Regi", "PokemonSwSh:ShinyHuntAutonomousRegi"},
{"Shiny Hunt Autonomous - Swords Of Justice", "PokemonSwSh:ShinyHuntAutonomousSwordsOfJustice"},
{"Shiny Hunt Autonomous - Strong Spawn", "PokemonSwSh:ShinyHuntAutonomousStrongSpawn"},
{"Shiny Hunt Autonomous - Regigigas2", "PokemonSwSh:ShinyHuntAutonomousRegigigas2"},
{"Shiny Hunt Autonomous - IoA Trade", "PokemonSwSh:ShinyHuntAutonomousIoATrade"},
{"Shiny Hunt Autonomous - Berry Tree", "PokemonSwSh:ShinyHuntAutonomousBerryTree"},
{"Shiny Hunt Autonomous - Whistling", "PokemonSwSh:ShinyHuntAutonomousWhistling"},
{"Shiny Hunt Autonomous - Fishing", "PokemonSwSh:ShinyHuntAutonomousFishing"},
{"Shiny Hunt Autonomous - Overworld", "PokemonSwSh:ShinyHuntAutonomousOverworld"},
{"PokemonSV:StatsResetBloodmoon", "PokemonSV:StatsResetEventBattle"},
{"PokemonLZA:ShinyHunt-Bench", "PokemonLZA:ShinyHunt-BenchSit"},
{"PokemonLZA:BerryBuyer", "PokemonLZA:StallBuyer"},
{"PokemonFRLG:LuckyEggFarmer", "PokemonFRLG:HeldItemFarmerSafariZone"},
};
StatLine::StatLine(StatsTracker& tracker)
: m_time(current_time_to_str())
, m_stats(tracker.to_str(StatsTracker::SAVE_TO_STATS_FILE))
{}
StatLine::StatLine(const std::string& line){
size_t pos = line.find(" - ");
if (pos == std::string::npos){
m_time = line;
return;
}
m_time = line.substr(0, pos);
const char* ptr = line.c_str() + pos + 3;
// Skip to stats.
while (true){
char ch = *ptr;
if (ch < 32) return;
if (('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z')) break;
ptr++;
}
m_stats = ptr;
}
std::string StatLine::to_str() const{
return m_time + " - " + m_stats;
#if 0
std::string str = m_time + " - ";
str += m_tracker
? m_tracker->to_str()
: m_stats;
return m_time + " - " +
(m_tracker ? m_tracker->to_str() : m_stats);
#endif
}
void StatList::operator+=(StatsTracker& tracker){
m_list.emplace_back(tracker);
}
void StatList::operator+=(const std::string& line){
m_list.emplace_back(line);
}
std::string StatList::to_str() const{
std::string str;
for (const StatLine& line : m_list){
str += line.to_str();
str += "\r\n";
}
return str;
}
void StatList::aggregate(StatsTracker& tracker) const{
for (const StatLine& line : m_list){
tracker.parse_and_append_line(line.stats());
// cout << tracker.to_str() << endl;
}
}
#if 0
StatList* StatSet::find(const std::string& label){
auto iter = m_data.find(label);
return iter == m_data.end()
? nullptr
: &iter->second;
}
#endif
StatList& StatSet::operator[](const std::string& identifier){
return m_data[identifier];
}
std::string StatSet::to_str() const{
std::string str;
for (const auto& item : m_data){
if (item.second.size() == 0){
continue;
}
str += "================================================================================\r\n";
str += item.first;
str += "\r\n";
str += "\r\n";
str += item.second.to_str();
str += "\r\n";
}
return str;
}
void StatSet::save_to_file(const std::string& filepath){
QFile file(QString::fromStdString(filepath));
if (file.open(QIODevice::WriteOnly)){
std::string data = to_str();
file.write(data.c_str(), data.size());
}
}
void StatSet::open_from_file(const std::string& filepath){
QFile file(QString::fromStdString(filepath));
if (!file.open(QIODevice::ReadOnly)){
return;
}
std::string str = file.readAll().data();
load_from_string(str.c_str());
}
bool StatSet::update_file(
const std::string& filepath,
const std::string& identifier,
StatsTracker& tracker
){
QFile file(QString::fromStdString(filepath));
while (!file.open(QIODevice::ReadWrite)){
return false;
}
std::string data = file.readAll().data();
StatSet set;
set.load_from_string(data.c_str());
set[identifier] += tracker;
data = set.to_str();
file.seek(0);
file.write(data.c_str(), data.size());
file.resize(data.size());
return true;
}
bool StatSet::get_line(std::string& line, const char*& ptr){
line.clear();
for (;; ptr++){
char ch = *ptr;
if (ch == '\0'){
// cout << line << endl;
ptr++;
return false;
}
if (ch == '\r'){
continue;
}
if (ch == '\n'){
// cout << line << endl;
ptr++;
return true;
}
line += ch;
}
}
void StatSet::load_from_string(const char* ptr){
m_data.clear();
// Find first section.
while (true){
std::string line;
if (!get_line(line, ptr)){
return;
}
if (line[0] == '='){
break;
}
}
while (true){
std::string line;
if (!get_line(line, ptr)){
return;
}
if (line.empty()){
continue;
}
auto iter = STATS_DATABASE_ALIASES.find(line);
if (iter != STATS_DATABASE_ALIASES.end()){
line = iter->second;
}
StatList& program = m_data[line];
while (true){
if (!get_line(line, ptr)){
return;
}
if (line.empty()){
continue;
}
if (line[0] == '='){
break;
}
program += line;
}
}
}
}