forked from andor9/tyrant_optimize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.cpp
282 lines (264 loc) · 10.2 KB
/
read.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "read.h"
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <boost/tokenizer.hpp>
#include <cstring>
#include <vector>
#include <fstream>
#include "card.h"
#include "cards.h"
#include "deck.h"
namespace {
const char* base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
// Converts `pairs' pairs of cards in `hash' to a deck.
// Stores resulting card IDs in `ids'.
void hash_to_ids(const char* hash, size_t pairs,
std::vector<unsigned int>& ids)
{
unsigned int last_id = 0;
for (size_t i = 0; i < pairs; ++i)
{
const char* p0 = strchr(base64_chars, hash[2 * i]);
const char* p1 = strchr(base64_chars, hash[2 * i + 1]);
if (!p0 || !p1)
{
throw std::runtime_error(hash);
}
size_t index0 = p0 - base64_chars;
size_t index1 = p1 - base64_chars;
unsigned int id = (index0 << 6) + index1;
if (id < 4000)
{
ids.push_back(id);
last_id = id;
}
else for (unsigned int j = 0; j < id - 4001; ++j)
{
ids.push_back(last_id);
}
}
}
}
// Constructs and returns a deck from `hash'.
// The caller is responsible for freeing the deck.
DeckIface* hash_to_deck(const char* hash, const Cards& cards)
{
std::vector<unsigned int> ids;
size_t pairs = strlen(hash) / 2;
hash_to_ids(hash, pairs, ids);
return new DeckRandom(cards, ids);
}
void load_decks(Decks& decks, Cards& cards)
{
if(boost::filesystem::exists("Custom.txt"))
{
try
{
read_custom_decks(cards, std::string{"Custom.txt"}, decks.custom_decks);
}
catch(const std::runtime_error& e)
{
std::cout << "Exception while loading custom decks: " << e.what() << "\n";
}
}
}
std::vector<std::pair<std::string, double> > parse_deck_list(std::string list_string)
{
std::vector<std::pair<std::string, double> > res;
boost::tokenizer<boost::char_delimiters_separator<char> > list_tokens{list_string, boost::char_delimiters_separator<char>{false, ";", ""}};
for(auto list_token = list_tokens.begin(); list_token != list_tokens.end(); ++list_token)
{
boost::tokenizer<boost::char_delimiters_separator<char> > deck_tokens{*list_token, boost::char_delimiters_separator<char>{false, ":", ""}};
auto deck_token = deck_tokens.begin();
res.push_back(std::make_pair(*deck_token, 1.0d));
++deck_token;
if(deck_token != deck_tokens.end())
{
res.back().second = boost::lexical_cast<double>(*deck_token);
}
}
return(res);
}
template<typename Iterator, typename Functor> Iterator advance_until(Iterator it, Iterator it_end, Functor f)
{
while(it != it_end)
{
if(f(*it))
{
break;
}
++it;
}
return(it);
}
// take care that "it" is 1 past current.
template<typename Iterator, typename Functor> Iterator recede_until(Iterator it, Iterator it_beg, Functor f)
{
if(it == it_beg) { return(it_beg); }
--it;
do
{
if(f(*it))
{
return(++it);
}
--it;
} while(it != it_beg);
return(it_beg);
}
template<typename Iterator, typename Functor, typename Token> Iterator read_token(Iterator it, Iterator it_end, Functor f, boost::optional<Token>& token)
{
Iterator token_start = advance_until(it, it_end, [](const char& c){return(c != ' ');});
Iterator token_end_after_spaces = advance_until(token_start, it_end, f);
if(token_start != token_end_after_spaces)
{
Iterator token_end = recede_until(token_end_after_spaces, token_start, [](const char& c){return(c != ' ');});
token = boost::lexical_cast<Token>(std::string{token_start, token_end});
return(token_end_after_spaces);
}
return(token_end_after_spaces);
}
// Error codes:
// 2 -> file not readable
// 3 -> error while parsing file
unsigned read_custom_decks(Cards& cards, std::string filename, std::map<std::string, DeckIface*>& custom_decks)
{
std::ifstream decks_file(filename.c_str());
if(!decks_file.is_open())
{
std::cerr << "File " << filename << " could not be opened\n";
return(2);
}
unsigned num_line(0);
decks_file.exceptions(std::ifstream::badbit);
try
{
while(decks_file && !decks_file.eof())
{
std::vector<unsigned> card_ids;
std::string deck_string;
getline(decks_file, deck_string);
++num_line;
if(deck_string.size() > 0)
{
if(strncmp(deck_string.c_str(), "//", 2) == 0)
{
continue;
}
boost::tokenizer<boost::char_delimiters_separator<char> > deck_tokens{deck_string, boost::char_delimiters_separator<char>{false, ":,", ""}};
auto token_iter = deck_tokens.begin();
boost::optional<std::string> deck_name;
if(token_iter != deck_tokens.end())
{
read_token(token_iter->begin(), token_iter->end(), [](char c){return(false);}, deck_name);
if(!deck_name || (*deck_name).size() == 0)
{
std::cerr << "Error in file " << filename << " at line " << num_line << ", could not read the deck name.\n";
continue;
}
}
else
{
std::cerr << "Error in file " << filename << " at line " << num_line << ", could not read the deck name.\n";
continue;
}
++token_iter;
for(; token_iter != deck_tokens.end(); ++token_iter)
{
std::string card_spec(*token_iter);
try
{
auto card_spec_iter = card_spec.begin();
boost::optional<std::string> card_name;
card_spec_iter = read_token(card_spec_iter, card_spec.end(), [](char c){return(c=='[' || c=='#' || c=='\r');}, card_name);
if(!card_name)
{
std::cerr << "Error in file " << filename << " at line " << num_line << " while parsing card " << card_spec << " in deck " << deck_name << "\n";
break;
}
else
{
boost::optional<unsigned> card_id;
if(*card_spec_iter == '[')
{
++card_spec_iter;
card_spec_iter = read_token(card_spec_iter, card_spec.end(), [](char c){return(c==']');}, card_id);
card_spec_iter = advance_until(card_spec_iter, card_spec.end(), [](char c){return(c!=' ');});
}
boost::optional<unsigned> card_num;
if(*card_spec_iter == '#')
{
++card_spec_iter;
card_spec_iter = read_token(card_spec_iter, card_spec.end(), [](char c){return(c < '0' || c > '9');}, card_num);
}
unsigned resolved_id{card_id ? *card_id : 0};
if(resolved_id == 0)
{
auto card_it = cards.player_cards_by_name.find(*card_name);
if(card_it != cards.player_cards_by_name.end())
{
resolved_id = card_it->second->m_id;
}
else
{
std::cerr << "Error in file " << filename << " at line " << num_line << " while parsing card " << card_spec << " in deck " << *deck_name << ": card not found\n";
break;
}
}
for(unsigned i(0); i < (card_num ? *card_num : 1); ++i)
{
card_ids.push_back(resolved_id);
}
}
}
catch(boost::bad_lexical_cast e)
{
std::cerr << "Error in file " << filename << " at line " << num_line << " while parsing card " << card_spec << " in deck " << deck_name << "\n";
}
}
if(deck_name)
{
custom_decks.insert({*deck_name, new DeckRandom{cards, card_ids}});
}
}
}
}
catch (std::ifstream::failure e)
{
std::cerr << "Exception while parsing the file " << filename << " (badbit is set).\n";
e.what();
return(3);
}
return(0);
}
void read_owned_cards(Cards& cards, std::map<unsigned, unsigned>& owned_cards)
{
std::ifstream owned_file{"ownedcards.txt"};
if(!owned_file.good())
{
std::cerr << "Warning: The file 'ownedcards.txt' does not exist. This will result in you not owning any cards.\n";
return;
}
std::string owned_str{(std::istreambuf_iterator<char>(owned_file)), std::istreambuf_iterator<char>()};
boost::tokenizer<boost::char_delimiters_separator<char> > tok{owned_str, boost::char_delimiters_separator<char>{false, "()\n", ""}};
for(boost::tokenizer<boost::char_delimiters_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg)
{
std::string name{*beg};
++beg;
assert(beg != tok.end());
unsigned num{static_cast<unsigned>(atoi((*beg).c_str()))};
auto card_itr = cards.player_cards_by_name.find(name);
if(card_itr == cards.player_cards_by_name.end())
{
std::cerr << "Error in file ownedcards.txt, the card \"" << name << "\" does not seem to be a valid card.\n";
}
else
{
owned_cards[card_itr->second->m_id] = num;
}
}
}