forked from andor9/tyrant_optimize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.cpp
More file actions
412 lines (395 loc) · 13.7 KB
/
read.cpp
File metadata and controls
412 lines (395 loc) · 13.7 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "read.h"
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
#include <boost/regex.hpp>
#include <cstring>
#include <map>
#include <unordered_set>
#include <fstream>
#include <iostream>
#include <exception>
#include "tyrant.h"
#include "card.h"
#include "cards.h"
#include "deck.h"
void load_custom_decks(Decks& decks, Cards& all_cards, const char * filename)
{
if(boost::filesystem::exists(filename))
{
read_custom_decks(decks, all_cards, filename);
}
}
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, 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);
}
DeckList & normalize(DeckList & decklist)
{
long double factor_sum = 0;
for (const auto & it : decklist)
{
factor_sum += it.second;
}
if (factor_sum > 0)
{
for (auto & it : decklist)
{
it.second /= factor_sum;
}
}
return decklist;
}
DeckList expand_deck_to_list(std::string deck_name, const Decks& decks)
{
static std::unordered_set<std::string> expanding_decks;
if (expanding_decks.count(deck_name))
{
std::cerr << "Warning: circular referred deck: " << deck_name << std::endl;
return DeckList();
}
auto deck_string = deck_name;
const auto & deck = decks.by_name.find(deck_name);
if (deck != decks.by_name.end())
{
deck_string = deck->second->deck_string;
if (deck_string.find_first_of(";:") != std::string::npos || decks.by_name.find(deck_string) != decks.by_name.end())
{
// deck_name refers to a deck list
expanding_decks.insert(deck_name);
auto && decklist = parse_deck_list(deck_string, decks);
expanding_decks.erase(deck_name);
return normalize(decklist);
}
}
if (deck_string.length() >= 3 && deck_string.front() == '/' && deck_string.back() == '/')
{
// deck_name is, or refers to, a regex
DeckList res;
std::string regex_string(deck_string, 1, deck_string.length() - 2);
boost::regex regex(regex_string);
boost::smatch smatch;
expanding_decks.insert(deck_name);
for (const auto & deck: decks.by_name)
{
if (boost::regex_search(deck.first, smatch, regex))
{
auto && decklist = expand_deck_to_list(deck.first, decks);
for (const auto & it : decklist)
{
res[it.first] += it.second;
}
}
}
expanding_decks.erase(deck_name);
if (res.size() == 0)
{
std::cerr << "Warning: regular expression matches nothing: /" << regex_string << "/." << std::endl;
}
return normalize(res);
}
else
{
return {{deck_name, 1}};
}
}
DeckList parse_deck_list(std::string list_string, const Decks& decks)
{
DeckList res;
boost::tokenizer<boost::char_delimiters_separator<char>> list_tokens{list_string, boost::char_delimiters_separator<char>{false, ";", ""}};
for(const auto & list_token : list_tokens)
{
boost::tokenizer<boost::char_delimiters_separator<char>> deck_tokens{list_token, boost::char_delimiters_separator<char>{false, ":", ""}};
auto deck_token = deck_tokens.begin();
auto deck_name = *deck_token;
double factor = 1.0;
++ deck_token;
if (deck_token != deck_tokens.end())
{
try
{
factor = boost::lexical_cast<long double>(*deck_token);
}
catch (const boost::bad_lexical_cast & e)
{
std::cerr << "Warning: Is ':' a typo? Skip deck [" << list_token << "]\n";
continue;
}
}
auto && decklist = expand_deck_to_list(deck_name, decks);
for (const auto & it : decklist)
{
res[it.first] += it.second * factor;
}
}
return res;
}
void parse_card_spec(const Cards& all_cards, std::string& card_spec, unsigned& card_id, unsigned& card_num, char& num_sign, char& mark)
{
// static std::set<std::string> recognized_abbr;
auto card_spec_iter = card_spec.begin();
card_id = 0;
card_num = 1;
num_sign = 0;
mark = 0;
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[0] == '!')
{
mark = card_name[0];
card_name.erase(0, 1);
}
// If card name is not found, try find card id quoted in '[]' in name, ignoring other characters.
std::string simple_name{simplify_name(card_name)};
const auto && abbr_it = all_cards.player_cards_abbr.find(simple_name);
if(abbr_it != all_cards.player_cards_abbr.end())
{
// if(recognized_abbr.count(card_name) == 0)
// {
// std::cout << "Recognize abbreviation " << card_name << ": " << abbr_it->second << std::endl;
// recognized_abbr.insert(card_name);
// }
simple_name = simplify_name(abbr_it->second);
}
auto card_it = all_cards.player_cards_by_name.find(simple_name);
auto card_id_iter = advance_until(simple_name.begin(), simple_name.end(), [](char c){return(c=='[');});
if(card_it != all_cards.player_cards_by_name.end())
{
card_id = card_it->second->m_id;
}
else if(card_id_iter != simple_name.end())
{
++ card_id_iter;
card_id_iter = read_token(card_id_iter, simple_name.end(), [](char c){return(c==']');}, card_id);
}
if(card_spec_iter != card_spec.end() && (*card_spec_iter == '#' || *card_spec_iter == '('))
{
++card_spec_iter;
if(card_spec_iter != card_spec.end())
{
if(strchr("+-$", *card_spec_iter))
{
num_sign = *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);
}
if(card_id == 0)
{
throw std::runtime_error("Unknown card: " + card_name);
}
}
unsigned read_card_abbrs(Cards& all_cards, const std::string& filename)
{
if(!boost::filesystem::exists(filename))
{
return(0);
}
std::ifstream abbr_file(filename);
if(!abbr_file.is_open())
{
std::cerr << "Error: Card abbreviation file " << filename << " could not be opened\n";
return(2);
}
unsigned num_line(0);
abbr_file.exceptions(std::ifstream::badbit);
try
{
while(abbr_file && !abbr_file.eof())
{
std::string abbr_string;
getline(abbr_file, abbr_string);
++num_line;
if(abbr_string.size() == 0 || strncmp(abbr_string.c_str(), "//", 2) == 0)
{
continue;
}
std::string abbr_name;
auto abbr_string_iter = read_token(abbr_string.begin(), abbr_string.end(), [](char c){return(c == ':');}, abbr_name);
if(abbr_string_iter == abbr_string.end() || abbr_name.empty())
{
std::cerr << "Error in card abbreviation file " << filename << " at line " << num_line << ", could not read the name.\n";
continue;
}
abbr_string_iter = advance_until(abbr_string_iter + 1, abbr_string.end(), [](const char& c){return(c != ' ');});
if(all_cards.player_cards_by_name.find(abbr_name) != all_cards.player_cards_by_name.end())
{
std::cerr << "Warning in card abbreviation file " << filename << " at line " << num_line << ": ignored because the name has been used by an existing card." << std::endl;
}
else
{
all_cards.player_cards_abbr[simplify_name(abbr_name)] = std::string{abbr_string_iter, abbr_string.end()};
}
}
}
catch (std::exception& e)
{
std::cerr << "Exception while parsing the card abbreviation file " << filename;
if(num_line > 0)
{
std::cerr << " at line " << num_line;
}
std::cerr << ": " << e.what() << ".\n";
return(3);
}
return(0);
}
// Error codes:
// 2 -> file not readable
// 3 -> error while parsing file
unsigned read_custom_decks(Decks& decks, Cards& all_cards, std::string filename)
{
std::ifstream decks_file(filename);
if(!decks_file.is_open())
{
std::cerr << "Error: Custom deck 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::string deck_string;
getline(decks_file, deck_string);
++num_line;
if(deck_string.size() == 0 || strncmp(deck_string.c_str(), "//", 2) == 0)
{
continue;
}
std::string deck_name;
auto deck_string_iter = read_token(deck_string.begin(), deck_string.end(), [](char c){return(strchr(":,", c));}, deck_name);
if(deck_string_iter == deck_string.end() || deck_name.empty())
{
std::cerr << "Error in custom deck file " << filename << " at line " << num_line << ", could not read the deck name.\n";
continue;
}
deck_string_iter = advance_until(deck_string_iter + 1, deck_string.end(), [](const char& c){return(c != ' ');});
auto deck_iter = decks.by_name.find(deck_name);
if(deck_iter != decks.by_name.end())
{
std::cerr << "Warning in custom deck file " << filename << " at line " << num_line << ", name conflicts, overrides " << deck_iter->second->short_description() << std::endl;
}
decks.decks.push_back(Deck{all_cards, DeckType::custom_deck, num_line, deck_name});
Deck* deck = &decks.decks.back();
deck->set(std::string{deck_string_iter, deck_string.end()});
decks.by_name[deck_name] = deck;
std::stringstream alt_name;
alt_name << decktype_names[deck->decktype] << " #" << deck->id;
decks.by_name[alt_name.str()] = deck;
}
}
catch (std::exception& e)
{
std::cerr << "Exception while parsing the custom deck file " << filename;
if(num_line > 0)
{
std::cerr << " at line " << num_line;
}
std::cerr << ": " << e.what() << ".\n";
return(3);
}
return(0);
}
void add_owned_card(Cards& all_cards, std::map<unsigned, unsigned>& owned_cards, std::string& card_spec)
{
unsigned card_id{0};
unsigned card_num{1};
char num_sign{0};
char mark{0};
parse_card_spec(all_cards, card_spec, card_id, card_num, num_sign, mark);
all_cards.by_id(card_id); // check that the id is valid
assert(mark == 0);
if(num_sign == 0)
{
owned_cards[card_id] = card_num;
}
else if(num_sign == '+')
{
owned_cards[card_id] += card_num;
}
else if(num_sign == '-')
{
owned_cards[card_id] = owned_cards[card_id] > card_num ? owned_cards[card_id] - card_num : 0;
}
}
void read_owned_cards(Cards& all_cards, std::map<unsigned, unsigned>& owned_cards, std::string filename)
{
std::ifstream owned_file{filename};
if(!owned_file.good())
{
// try parse the string as a cards instead of as a filename
try
{
std::string card_list(filename);
boost::tokenizer<boost::char_delimiters_separator<char>> card_tokens{card_list, boost::char_delimiters_separator<char>{false, ",", ""}};
auto token_iter = card_tokens.begin();
for (; token_iter != card_tokens.end(); ++token_iter)
{
std::string card_spec(*token_iter);
add_owned_card(all_cards, owned_cards, card_spec);
}
}
catch (std::exception& e)
{
std::cerr << "Error: Failed to parse owned cards: '" << filename << "' is neither a file nor a valid set of cards (" << e.what() << ")" << std::endl;
exit(0);
}
return;
}
unsigned num_line(0);
while(owned_file && !owned_file.eof())
{
std::string card_spec;
getline(owned_file, card_spec);
++num_line;
if(card_spec.size() == 0 || strncmp(card_spec.c_str(), "//", 2) == 0)
{
continue;
}
try
{
add_owned_card(all_cards, owned_cards, card_spec);
}
catch(std::exception& e)
{
std::cerr << "Error in owned cards file " << filename << " at line " << num_line << " while parsing card '" << card_spec << "': " << e.what() << "\n";
}
}
}