-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwecbot_test.cpp
More file actions
634 lines (477 loc) · 17.6 KB
/
wecbot_test.cpp
File metadata and controls
634 lines (477 loc) · 17.6 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#include <libxml/HTMLparser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstdlib> // needed for ato[i/f] functions :(
#include <vector>
#include <typeinfo>
#include <sstream>
#include <stdexcept>
class StatUser {
public:
int pos;
int number;
float pct;
StatUser() {
pos = 0;
number = 0;
pct = 0.0f;
}
};
class GameHistory {
public:
unsigned games;
unsigned placement;
float average;
GameHistory() {
games = 0;
placement = 0;
average = 0.0f;
}
};
class Season {
public:
std::string seasonDate;
unsigned position;
float score;
unsigned playedGames;
unsigned wonGames;
float average;
Season() {
position = 0;
score = 0.0f;
playedGames = 0;
wonGames = 0;
average = 0.0f;
}
};
class DataUser {
public:
std::vector<StatUser> currentSeasonStats;
std::vector<StatUser> globalStats;
std::vector<Season> previousSeasons;
Season currentSeasson;
GameHistory gameHistory;
unsigned totGames;
DataUser() {
totGames = 0;
}
};
// TO compile:
// g++ -o wecbot_test wecbot_test.cpp -I /usr/include/libxml2/ -lxml2
void print_xpath_nodes_cpp(xmlNodeSetPtr nodes, std::ostream& output);
void print_xpath_nodes_test(xmlNodeSetPtr nodes);
//void print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output);
void getUserStats(std::vector<StatUser>& statsUser, xmlNodePtr node, xmlXPathContextPtr xpathCtx);
void getPreviousSeasonStats(std::vector<Season>& seasonData, xmlNodePtr node, xmlXPathContextPtr xpathCtx);
void getGameHistory(GameHistory& gameHistory, xmlNodePtr node, xmlXPathContextPtr xpathCtx);
unsigned getTotalGames(std::vector<StatUser> stats) ;
bool check4DirectRule(DataUser& dataUser, std::string& reason);
bool check4VeteranRule(DataUser& dataUser, std::string& reason);
int main(int argc, char** argv) {
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
doc = htmlReadFile(argv[1], NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);
if (doc == NULL) {
std::cerr << "error: could not parse file" << argv[1] << std::endl;
return 1;
}
xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL) {
std::cerr << "Error: unable to create new XPath context" << std::endl;
xmlFreeDoc(doc);
return 1;
}
const char* xpathExpr= "//*[@class=\"pf_container\"]";
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression((const xmlChar *)xpathExpr, xpathCtx);
if(xpathObj == NULL) {
std::cerr << "Error: unable to evaluate xpath expression" << xpathExpr << std::endl;
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return 1;
}
//print_xpath_nodes_cpp(xpathObj->nodesetval, std::cout); // here can we set an ofstream
// print_xpath_nodes(xpathObj->nodesetval, stdout); // Old C way
print_xpath_nodes_test(xpathObj->nodesetval);
/*free the document */
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return 0;
}
void print_xpath_nodes_cpp(xmlNodeSetPtr nodes, std::ostream& output) {
xmlNodePtr cur;
int size;
int i;
size = (nodes) ? nodes->nodeNr : 0;
output << "Result (" << size << " nodes):" << std::endl;
for(i = 0; i < size; ++i) {
if (!nodes->nodeTab[i]) continue;
if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns;
ns = (xmlNsPtr)nodes->nodeTab[i];
cur = (xmlNodePtr)ns->next;
if(cur->ns) {
output << "= namespace\"" << ns->prefix << "\"=\"" <<
ns->href << "\" for node " << cur->ns->href << ":" << cur->name << std::endl;
} else {
output << "= namespace \"" << ns->prefix << "\"=\"" << ns->href << "\" for node " << cur->name << std::endl;
}
} else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
cur = nodes->nodeTab[i];
if(cur->ns) {
output << "= element node \"" << cur->ns->href << ":" << cur->name << "\"" << std::endl;
} else {
output << "= element node \"" << cur->name <<"\"" << std::endl;
}
} else {
cur = nodes->nodeTab[i];
output << "= node \""<< cur->name << "\": type " << cur->type << std::endl;
}
}
}
/*void
print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output) {
xmlNodePtr cur;
int size;
int i;
size = (nodes) ? nodes->nodeNr : 0;
fprintf(output, "Result (%d nodes):\n", size);
for(i = 0; i < size; ++i) {
if (!nodes->nodeTab[i]) continue;
if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns;
ns = (xmlNsPtr)nodes->nodeTab[i];
cur = (xmlNodePtr)ns->next;
if(cur->ns) {
fprintf(output, "= namespace \"%s\"=\"%s\" for node %s:%s\n",
ns->prefix, ns->href, cur->ns->href, cur->name);
} else {
fprintf(output, "= namespace \"%s\"=\"%s\" for node %s\n",
ns->prefix, ns->href, cur->name);
}
} else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
cur = nodes->nodeTab[i];
if(cur->ns) {
fprintf(output, "= element node \"%s:%s\"\n",
cur->ns->href, cur->name);
} else {
fprintf(output, "= element node \"%s\"\n",
cur->name);
}
} else {
cur = nodes->nodeTab[i];
fprintf(output, "= node \"%s\": type %d\n", cur->name, cur->type);
}
}
}*/
void findData(xmlDoc* xmlDoc, const char *expr) {
}
void print_xpath_nodes_test(xmlNodeSetPtr nodes) {
xmlNodePtr cur;
int size;
int i;
DataUser dataUser;
size = (nodes) ? nodes->nodeNr : 0;
//std::cout << "Result (" << size << " nodes):" << std::endl;
for(i = 0; i < size; ++i) {
if (!nodes->nodeTab[i]) continue;
if (nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
const char* xpathExpr= "h4";
xmlXPathContextPtr xpathCtx = xmlXPathNewContext(nodes->nodeTab[i]->doc);
if (!xpathCtx) {
std::cerr << "Unable to create context" << std::endl;
return;
}
xmlXPathObjectPtr xpathObj = xmlXPathNodeEval(nodes->nodeTab[i], (const xmlChar *)xpathExpr, xpathCtx);
if (xpathObj) {
if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
//std::cout << "H4 has " << xpathObj->nodesetval->nodeNr << " elements" << std::endl;
for (int j = 0; j < xpathObj->nodesetval->nodeNr; j++) {
xmlNodePtr currNode = xpathObj->nodesetval->nodeTab[j];
if (currNode->children && currNode->children->type == XML_TEXT_NODE) {
xmlChar * content = currNode->children->content;
std::string ctnt = std::string((char * ) content);
if (ctnt == "Rank-O-Meter Running Season") {
getUserStats(dataUser.currentSeasonStats, nodes->nodeTab[i], xpathCtx);
}
else if (ctnt == "All Time-Rank-O-Meter") {
getUserStats(dataUser.globalStats, nodes->nodeTab[i], xpathCtx);
}
}
else if (currNode->children && currNode->children->type == XML_ELEMENT_NODE) {
if (currNode->children->children && currNode->children->children->type == XML_ELEMENT_NODE &&
currNode->children->children->children && currNode->children->children->children->type == XML_TEXT_NODE) { // Game History
std::string ctnt = std::string((char *) currNode->children->children->children->content);
if (ctnt.find("Game History") != std::string::npos) {
//std::cout << "In GameHistory <------- " << std::endl;
getGameHistory(dataUser.gameHistory, nodes->nodeTab[i]->parent, xpathCtx);
}
}
else if (currNode->children->children && currNode->children->children->type == XML_TEXT_NODE) { // Previous Season
std::string ctnt = std::string((char *) currNode->children->children->content);
if (ctnt.find("Previous Seasons Ranking Statistics") != std::string::npos) {
//std::cout << "In HERE <------- " << std::endl;
getPreviousSeasonStats(dataUser.previousSeasons, nodes->nodeTab[i], xpathCtx);
}
}
}
}
}
}
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
}
}
dataUser.totGames = getTotalGames(dataUser.globalStats);
std::cout << "=== Total Games: " << dataUser.totGames << " ===" << std::endl;
std::vector<StatUser> seasonStats = dataUser.currentSeasonStats;
std::cout << "=== Season Stats ===" << std::endl;
for (std::vector<StatUser>::iterator it = seasonStats.begin(); it != seasonStats.end(); ++it) {
std::cout << "Pos: " << it->pos << "; Count: " << it->number << "; Pct: " << it->pct << std::endl;
}
std::vector<StatUser> globalStats = dataUser.globalStats;
std::cout << "=== Global Stats ===" << std::endl;
for (std::vector<StatUser>::iterator it = globalStats.begin(); it != globalStats.end(); ++it) {
std::cout << "Pos: " << it->pos << "; Count: " << it->number << "; Pct: " << it->pct << std::endl;
}
std::vector<Season> previousSeasons = dataUser.previousSeasons;
std::cout << "=== Previous Seasons ===" << std::endl;
for (std::vector<Season>::iterator it = previousSeasons.begin(); it != previousSeasons.end(); ++it) {
std::cout << "SeasonDate: " << it->seasonDate << "; position: " << it->position << "; score: " << it->score;
std::cout << "; playedGames: " << it->playedGames << "; wonGames: " << it->wonGames << "; average: " << it->average << std::endl;
}
std::cout << " == History == " << std::endl;
std::cout << "Games " << dataUser.gameHistory.games << std::endl;
std::cout << "Placement " << dataUser.gameHistory.placement << std::endl;
std::cout << "Average " << dataUser.gameHistory.average << std::endl;
std::string reason;
std::cout << " == Direct Rule == " << std::endl;
check4DirectRule(dataUser, reason);
std::cout << reason << std::endl;
std::cout << " == Veterans Rule == " << std::endl;
check4VeteranRule(dataUser, reason);
std::cout << reason << std::endl;
}
void getUserStats(std::vector<StatUser>& statsUser, xmlNodePtr node, xmlXPathContextPtr xpathCtx) {
xmlXPathObjectPtr xpathTr = xmlXPathNodeEval(node, (const xmlChar *) "table/tr", xpathCtx);
if (xpathTr && xpathTr->nodesetval) {
//std::cout << "TR has " << xpathTr->nodesetval->nodeNr << " elements" << std::endl;
for (int k = 1; k < xpathTr->nodesetval->nodeNr; k++) { // element 0 is header
xmlXPathObjectPtr xpathTd = xmlXPathNodeEval(xpathTr->nodesetval->nodeTab[k], (const xmlChar *) "td", xpathCtx);
//std::cout << "TD has " << xpathTd->nodesetval->nodeNr << " elements" << std::endl;
StatUser stat;
bool parseable = true;
for (int l = 0; l < xpathTd->nodesetval->nodeNr; l++) {
char* data;
try {
data = (char*) xpathTd->nodesetval->nodeTab[l]->children->content;
}
catch (std::exception &cException) {std::cerr << "Standard exception at " << __LINE__ << ": " << cException.what() << "; " << typeid(cException).name() << std::endl;}
switch (l) {
case 0:
stat.pos = std::atoi((char *)data);
break;
case 1:
try {
stat.number = std::atoi((char *)data);
}
catch (std::invalid_argument &eInv) {parseable = false; stat.number = 0;}
break;
case 2:
if (parseable)
stat.pct = std::atof((char *)data);
else
stat.pct = 0.0f;
break;
default:
break;
}
}
statsUser.push_back(stat);
xmlXPathFreeObject(xpathTd);
}
}
xmlXPathFreeObject(xpathTr);
}
void getPreviousSeasonStats(std::vector<Season>& seasonData, xmlNodePtr node, xmlXPathContextPtr xpathCtx) {
xmlXPathObjectPtr xpathTr = xmlXPathNodeEval(node, (const xmlChar *) "table/tr", xpathCtx);
if (xpathTr && xpathTr->nodesetval) {
std::string currentSeason;
Season season;
bool existsData = false;
for (int k = 0; k < xpathTr->nodesetval->nodeNr; k++) { // 1 per line (not equivalent to season)
xmlXPathObjectPtr xpathTd = xmlXPathNodeEval(xpathTr->nodesetval->nodeTab[k], (const xmlChar *) "td", xpathCtx);
if (xpathTd->nodesetval->nodeNr < 2) continue; // Is a blank line.
std::string data;
std::string value;
try {
data = std::string((char*) xpathTd->nodesetval->nodeTab[0]->children->content);
value = std::string((char*) xpathTd->nodesetval->nodeTab[1]->children->content);
}
catch (std::exception &cException) {std::cerr << "Standard exception at " << __LINE__ << ": " << cException.what() << "; " << typeid(cException).name() << std::endl;}
std::string seasonDate = data.substr(0, data.find(" "));
{
std::string beta = "Beta";
std::string::size_type n = seasonDate.find(beta);
if (n != std::string::npos) { // Cas temporades "Beta"
seasonDate = data.substr(0, data.find(" ", beta.length() + 1));
}
}
if (currentSeason != seasonDate) {
if (existsData) {
seasonData.push_back(season);
season = Season(); // call constructor for new object
}
season.seasonDate = seasonDate;
currentSeason = seasonDate;
existsData = true;
}
if (data.find("ranking position") != std::string::npos) {
season.position = std::atoi(value.c_str());
}
else if (data.find("score") != std::string::npos) {
season.score = std::atof(value.c_str());
}
else if (data.find("played games") != std::string::npos) {
season.playedGames = std::atoi(value.c_str());
}
else if (data.find("won games") != std::string::npos) {
season.wonGames = std::atoi(value.c_str());
}
else if (data.find("average points") != std::string::npos) {
season.average = std::atof(value.c_str());
}
xmlXPathFreeObject(xpathTd);
}
seasonData.push_back(season);
}
xmlXPathFreeObject(xpathTr);
}
void getGameHistory(GameHistory& gameHistory, xmlNodePtr node, xmlXPathContextPtr xpathCtx) {
unsigned weights[] = {24, 16, 10, 6, 3, 2, 1, 0, 0, 0};
unsigned games = 0;
unsigned placement = 0;
float average = 0.0f;
xmlXPathObjectPtr xpathTr = xmlXPathNodeEval(node, (const xmlChar *) "table/tr", xpathCtx);
if (xpathTr && xpathTr->nodesetval) {
//std::cout << "TR has " << xpathTr->nodesetval->nodeNr << " elements" << std::endl;
for (int k = 1; k < xpathTr->nodesetval->nodeNr && games < 100; k++) { // element 0 is header, get only first 100 games
xmlXPathObjectPtr xpathTd = xmlXPathNodeEval(xpathTr->nodesetval->nodeTab[k], (const xmlChar *) "td", xpathCtx);
//std::cout << "TD has " << xpathTd->nodesetval->nodeNr << " elements" << std::endl; // Should have 4
if (xpathTd->nodesetval->nodeNr < 4) continue;
xmlNodePtr currNode = xpathTd->nodesetval->nodeTab[3]; // Get fourth element
if (currNode->children && currNode->children->children && currNode->children->children->type == XML_TEXT_NODE) {
char* data = (char *) currNode->children->children->content;
unsigned position = 0;
try {
position = std::atoi(data);
}
catch (std::invalid_argument &eInv) {} // Do nothing
if (position > 0) {
games++;
placement += weights[position - 1];
}
}
xmlXPathFreeObject(xpathTd);
}
}
if (games > 0) average = static_cast<double>(placement)/static_cast<double>(games);
gameHistory.games = games;
gameHistory.placement = placement;
gameHistory.average = average;
xmlXPathFreeObject(xpathTr);
}
unsigned getTotalGames(std::vector<StatUser> stats) {
unsigned count = 0;
std::vector<StatUser>::iterator it_beg = stats.begin();
std::vector<StatUser>::iterator it_end = stats.end();
while (it_beg != it_end) {
count += it_beg->number;
++it_beg;
}
return count;
}
//bool check4DirectRule(std::vector<StatUser>& statsUser, std::string& reason) {
bool check4DirectRule(DataUser& dataUser, std::string& reason) {
std::ostringstream buff;
bool result = false;
if (dataUser.previousSeasons.size() < 2) {
buff << "No enough Seasons (" << dataUser.previousSeasons.size() + 1 << ")";
}
else if (dataUser.totGames < 300) {
buff << "Not enough games (" << dataUser.totGames << ")";
}
else {
unsigned totGames = dataUser.totGames;
unsigned valGames = 0;
std::vector<StatUser>::iterator it_beg = dataUser.globalStats.begin();
std::vector<StatUser>::iterator it_end = dataUser.globalStats.end();
while (it_beg != it_end) {
if (it_beg->pos >= 1 && it_beg->pos <= 4) valGames += it_beg->number;
++it_beg;
}
if (totGames < 300) {
buff << "Not enough games (" << totGames << ")";
}
else {
float pct = static_cast<double>(valGames)/static_cast<double>(totGames);
if (pct >= 0.6) {
buff << "Passes " << pct;
result = true;
}
else {
buff << "NOT Passes " << pct;
}
}
}
reason = buff.str();
return result;
}
bool check4VeteranRule(DataUser& dataUser, std::string& reason) {
std::ostringstream buff;
bool result = false;
if (dataUser.previousSeasons.size() < 4) {
buff << "Not enough Seasons (" << dataUser.previousSeasons.size() << ")";
}
else if (dataUser.totGames < 1000) {
buff << "Not enough games (" << dataUser.totGames << ")";
}
else {
unsigned games = 0;
unsigned gamesTot = 0;
float average = 0.0f;
games = dataUser.gameHistory.games;
gamesTot += games;
float avg = static_cast<double>(games)*dataUser.gameHistory.average;
std::vector<Season> previousSeasons = dataUser.previousSeasons;
std::vector<Season>::iterator it_beg = previousSeasons.begin();
std::vector<Season>::iterator it_end = previousSeasons.end();
while (it_beg != it_end) {
games = it_beg->playedGames;
average = it_beg->average;
if (games > 100) games = 100;
gamesTot += games;
avg += games*average;
if (gamesTot >= 300) break;
++it_beg;
}
avg = avg/static_cast<double>(gamesTot);
if (avg >= 9.0) {
buff << "Passes " << avg;
result = true;
}
else {
buff << "NOT Passes " << avg;
}
}
reason = buff.str();
return result;
}