forked from errorcodexero/standing_predictor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmp_reason.cpp
More file actions
490 lines (424 loc) · 13 KB
/
Copy pathcmp_reason.cpp
File metadata and controls
490 lines (424 loc) · 13 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
#include "cmp_reason.h"
#include "util.h"
#include "set.h"
#include "event_status.h"
#include "slots.h"
#include "status.h"
#include "print_r.h"
#include "tba.h"
#include "vector_void.h"
#include "optional.h"
#include "../tba/tba.h"
#include "set_limited.h"
#include "query.h"
using namespace std;
ENUM_CLASS_PRINT(Cmp_reason,CMP_REASON_ITEMS)
static std::optional<tba::Team_key> team_winners(tba::Award_Recipient const& a){
return a.team_key;
}
static std::vector<tba::Team_key> team_winners(std::vector<tba::Award_Recipient> const& a){
return flatten(MAP(team_winners,a));
}
std::vector<tba::Team_key> team_winners(tba::Award const& a){
return team_winners(a.recipient_list);
}
std::vector<tba::Team_key> team_winners(std::vector<tba::Award> const& a){
return flatten(MAP(team_winners,a));
}
static bool dcmp(tba::Event_type a){
using enum tba::Event_type;
switch(a){
case DISTRICT:
return 0;
case DISTRICT_CMP:
case DISTRICT_CMP_DIVISION:
return 1;
default:
PRINT(a);
nyi
}
}
static std::set<tba::Team_key> district_quals(
TBA_fetcher &tba_fetcher,
tba::District_key district,
std::set<tba::Team_key> const& attending_teams
){
//first, see if the district is done
auto x=tba::district_events(tba_fetcher,district);
auto f=filter([](auto x){ return dcmp(x.event_type); },x);
assert(f.size());
auto cmp_events=keys(f);
auto a=to_set(::mapf([&](auto x){ return event_status(tba_fetcher,x); },cmp_events));
if(a!=set{Event_status::COMPLETE}){
return {};
}
auto rank=district_rankings(tba_fetcher,district);
assert(rank);
auto r=*rank;
size_t slots=worlds_slots(district);
auto aw=flatten(::mapf([&](auto x){ return event_awards(tba_fetcher,x); },cmp_events));
std::set<tba::Award_type> auto_qual{
tba::Award_type::CHAIRMANS,
tba::Award_type::ENGINEERING_INSPIRATION,
tba::Award_type::ROOKIE_ALL_STAR
};
auto f2=filter([=](auto x){ return auto_qual.count(x.award_type); },aw);
auto by_award=to_set(team_winners(f2));
//if some of the award winners aren't attending, then more slots for teams by points
auto extra_by_award=(by_award-attending_teams).size();
slots+=extra_by_award;
std::set<tba::Team_key> by_pts;
for(size_t i=0;i<r.size() && by_pts.size()<slots;i++){
auto team=r[i].team_key;
if(!by_award.count(team)){
if(!attending_teams.count(team)){
//if not going, don't count it against the total.
slots++;
}
by_pts|=team;
}
}
return by_award|by_pts;
}
std::vector<tba::District_key> district_keys(TBA_fetcher &tba_fetcher,tba::Year year){
return keys(districts(tba_fetcher,year));
}
static std::set<tba::Team_key> district_quals(
TBA_fetcher &tba_fetcher,
tba::Year year,
std::set<tba::Team_key> const& attending_teams //so that can fill in for declines
){
return or_all(::mapf(
[&](auto x){ return district_quals(tba_fetcher,x,attending_teams); },
district_keys(tba_fetcher,year)
));
}
static std::set<tba::Team_key> regional_quals(TBA_fetcher &tba_fetcher,tba::Year year){
auto e=events(tba_fetcher,year);
auto f=filter([](auto x){ return x.event_type==tba::Event_type::REGIONAL; },e);
auto m=sorted(::mapf([](auto x){ return make_pair(x.week,x.key); },f));
auto m2=seconds(m);
//print_r(m2);
std::set<tba::Team_key> r;
for(auto event:m2){
auto aw=event_awards(tba_fetcher,event);
//print_r(mapf([](auto x){ return x.award_type; },aw));
std::set<tba::Award_type> advancing{
tba::Award_type::CHAIRMANS,
tba::Award_type::WINNER,
tba::Award_type::WILDCARD,
tba::Award_type::ENGINEERING_INSPIRATION
};
auto f=filter([=](auto x){ return advancing.count(x.award_type); },aw);
r|=team_winners(f);
}
return r;
}
static std::optional<tba::Team_key> ei(TBA_fetcher &tba_fetcher,tba::Event_key event_key){
auto a=tba::event_awards(tba_fetcher,event_key);
//PRINT(event_key);
auto f=filter([](auto x){ return x.award_type==tba::Award_type::ENGINEERING_INSPIRATION; },a);
if(f.empty()){
return std::nullopt;
}
assert(f.size()==1);
auto x=f[0].recipient_list;
assert(x.size()==1);
auto x2=x[0].team_key;
assert(x2);
return *x2;
}
static std::set<tba::Team_key> cmp_ei(TBA_fetcher &tba_fetcher,tba::Year year){
auto events=events_year(tba_fetcher,year);
auto f=filter([](auto x){ return x.event_type==tba::Event_type::CMP_DIVISION; },events);
auto event_keys=keys(f);
//TODO: if nothign from the fields, then look for something from the main field.
auto found=to_set(nonempty(mapf([&](auto x){ return ei(tba_fetcher,x); },event_keys)));
if(found.size()){
return found;
}
auto f2=filter([](auto x){ return x.event_type==tba::Event_type::CMP_FINALS; },events);
auto m=mapf([&](auto x){ return ei(tba_fetcher,x.key); },f2);
return to_set(flatten(m));
}
static std::set<tba::Team_key> cmp_chairmans(TBA_fetcher &tba_fetcher,tba::Year year){
auto events=events_year(tba_fetcher,year);
auto f=filter([](auto x){ return x.event_type==tba::Event_type::CMP_FINALS; },events);
auto event_keys=keys(f);
auto m=mapf([&](auto x){ return chairmans(tba_fetcher,x); },event_keys);
return to_set(flatten(m));
}
static set<tba::Team_key> chairmans_pre_qualifying(TBA_fetcher &tba_fetcher,tba::Year current_year){
//the ones besides last year's.
set<tba::Team_key> r;
auto at=current_year-2;
for(int years_found=0;years_found<=10 && at>tba::Year{1992};--at){
auto found=cmp_chairmans(tba_fetcher,at);
if(found.size()){
//cout<<at<<":"<<found<<"\n";
r|=found;
years_found++;
}
}
return r;
}
set<tba::Team_key> pre_qualifying(TBA_fetcher &tba_fetcher,tba::Year current_year){
tba::Year year=current_year-1;
//PRINT(ei(tba_fetcher,year));
auto e1=tba::events_year(tba_fetcher,year);
//print_r(e1);
auto m=mapf([](auto x){ return x.event_type; },e1);
//PRINT(to_set(m));
auto f1=filter(
[](auto x){
return x.event_type==tba::Event_type::CMP_FINALS;
},
e1
);
/*if(f1.size()!=1){
PRINT(year);
print_r(f1);
assert(0);
}*/
assert(f1.size()==1 || f1.size()==2);
//auto f=f1[0];
std::set<tba::Team_key> teams;
for(auto f:f1){
std::set<tba::Award_type> prequalified_award_types{
tba::Award_type::WINNER,
tba::Award_type::CHAIRMANS,
tba::Award_type::CHAIRMANS_FINALIST
};
auto key=f.key;
//PRINT(key);
auto e=tba::event_awards(tba_fetcher,key);
auto e2=filter([=](auto x){ return prequalified_award_types.count(x.award_type); },e);
//print_r(e2);
auto m2=to_set(nonempty(flatten(mapf(
[](auto x){
return mapf([](auto y){ return y.team_key; },x.recipient_list);
},
e2
))));
teams|=m2;
}
auto c=chairmans_pre_qualifying(tba_fetcher,current_year);
return c|teams|cmp_ei(tba_fetcher,year);
//print_r(m2);
//want to find:
//-championship winners
//-impact award finalists
//-EI award winners
//-impact award winner
//
//chairmans from the last 10 FRC championships
//nyi
}
static std::set<tba::Team_key> original_teams(TBA_fetcher &tba_fetcher){
auto f=filter(
[](auto const& team){ return team.rookie_year==tba::Year{1992}; },
teams(tba_fetcher)
);
return to_set(keys(f));
}
static bool cmp_event(tba::Event_type e){
switch(e){
case tba::Event_type::CMP_FINALS:
case tba::Event_type::CMP_DIVISION:
return 1;
case tba::Event_type::REGIONAL:
case tba::Event_type::PRESEASON:
case tba::Event_type::DISTRICT_CMP:
case tba::Event_type::DISTRICT:
case tba::Event_type::DISTRICT_CMP_DIVISION:
case tba::Event_type::OFFSEASON:
return 0;
default:
PRINT(e);
nyi
}
}
static bool cmp_event(tba::Event const& a){
return cmp_event(a.event_type);
}
std::set<tba::Team_key> cmp_teams(TBA_fetcher &tba_fetcher,tba::Year year){
auto f=filter(
[](auto const& x){ return cmp_event(x); },
tba::events(tba_fetcher,year)
);
auto m=mapf([&](auto x){ return event_teams_keys(tba_fetcher,x.key); },f);
return to_set(flatten(m));
}
static std::set<tba::Team_key> hof(TBA_fetcher &tba_fetcher,tba::Year current_year){
//teams that were in the hall of fame at the beginning of the specified year
auto years=range(tba::Year{1992},current_year);
return or_all(mapf(
[&](auto x){ return cmp_chairmans(tba_fetcher,x); },
years
));
}
static auto regionals(TBA_fetcher &tba_fetcher,tba::Year year){
auto f=filter(
[](auto x){ return x.event_type==tba::Event_type::REGIONAL; },
events_year(tba_fetcher,year)
);
return keys(f);
}
static std::vector<tba::Team_key> winners(TBA_fetcher &tba_fetcher,tba::Event_key event){
auto aw=event_awards(tba_fetcher,event);
auto f=filter([](auto x){ return x.award_type==tba::Award_type::WINNER; },aw);
return team_winners(f);
}
static std::optional<tba::Team_key> winning_alliance_second_pick(TBA_fetcher &tba_fetcher,tba::Event_key event){
auto w=winners(tba_fetcher,event);
if(w.empty()){
return std::nullopt;
}
auto ea=event_alliances(tba_fetcher,event);
if(!ea || ea->empty()){
//no data about alliances, hope that the order of the awards preserves pick order!
if(w.size()<3){
//happened w/ the data for 2005fl.
return std::nullopt;
}
return w[2];
}
auto f=filter(
[=](auto x){ return (to_set(x.picks)&to_set(w)).size(); },
*ea
);
if(f.size()!=1){
PRINT(event);
PRINT(w);
print_r(*ea);
print_r(f);
nyi
}
auto fu=f[0];
assert(fu.picks.size()>=3);
return fu.picks[2];
#if 0
PRINT(w);
nyi
auto aw=event_awards(tba_fetcher,event);
if(aw.empty()){
return std::nullopt;
}
PRINT(event);
print_r(aw);
nyi
//can't just do it this way because sometimes the last match result is missing
//even though the event is over, so need to look at awards to figure out who won.
auto e=event_alliances(tba_fetcher,event);
//probably because the event has not yet happened.
if(!e) return std::nullopt;
auto f1=filter([](auto x){ return winning(x.status); },*e);
if(f1.empty()){
return std::nullopt;
}
assert(f1.size()==1);
auto f=f1[0];
/*if(f.picks.size()!=3){
PRINT(event);
print_r(f);
}*/
assert(f.picks.size()>=3);
return f.picks[2];
#endif
}
static std::set<tba::Team_key> rookie_all_star(TBA_fetcher &tba_fetcher,tba::Event_key const& event){
auto aw=event_awards(tba_fetcher,event);
auto f=filter([](auto x){ return x.award_type==tba::Award_type::ROOKIE_ALL_STAR; },aw);
//This can be >1 team. Example: 2004on
return to_set(team_winners(f));
}
static std::set<tba::Team_key> regional_rookie_all_stars(TBA_fetcher &tba_fetcher,tba::Year year){
return or_all(mapf(
[&](auto x){ return rookie_all_star(tba_fetcher,x); },
regionals(tba_fetcher,year)
));
}
static std::set<tba::Team_key> regional_winning_alliance_second_pick(TBA_fetcher &tba_fetcher,tba::Year year){
auto m=mapf(
[&](auto x){ return winning_alliance_second_pick(tba_fetcher,x); },
regionals(tba_fetcher,year)
);
return to_set(nonempty(m));
}
static std::optional<tba::Team_key> winning_backups(TBA_fetcher &tba_fetcher,tba::Event_key event){
auto w=winners(tba_fetcher,event);
if(w.size()<4){
return std::nullopt;
}
auto ea=event_alliances(tba_fetcher,event);
if(!ea || ea->empty()){
//in case don't have the alliance directly recorded.
return w[3];
}
assert(ea);
auto e=*ea;
assert(e.size());
auto f=filter_unique([=](auto x){ return (to_set(w)&to_set(x.picks)).size(); },e);
if(f.picks.size()==3){
auto r=to_set(w)-to_set(f.picks);
assert(r.size()==1);
return *r.begin();
}
//PRINT(event);
//PRINT(f.picks);
assert(f.picks.size()>=4);
return f.picks[3];
}
static std::set<tba::Team_key> regional_winning_backups(TBA_fetcher& tba_fetcher,tba::Year year){
return to_set(nonempty(mapf(
[&](auto x){ return winning_backups(tba_fetcher,x); },
regionals(tba_fetcher,year)
)));
}
static std::set<tba::Team_key> priority_waitlist(TBA_fetcher &tba_fetcher,tba::Year year){
std::set<tba::Team_key> r;
//Criteria for the priority waitlist:
//1) HOF teams not from the last 10 years
r|=hof(tba_fetcher,year)-chairmans_pre_qualifying(tba_fetcher,year);
//2) original and sustaining teams
r|=original_teams(tba_fetcher);
//3) regional winning alliance second pick
r|=regional_winning_alliance_second_pick(tba_fetcher,year);
//4) regional regional rookie all star
r|=regional_rookie_all_stars(tba_fetcher,year);
//5) regional winning alliance backup teams
r|=regional_winning_backups(tba_fetcher,year);
return r;
}
map<tba::Team_key,Cmp_reason> cmp_reasons(TBA_fetcher &tba_fetcher,tba::Year year){
auto teams=cmp_teams(tba_fetcher,year);
map<tba::Team_key,Cmp_reason> r;
auto f=[&](Cmp_reason reason,std::set<tba::Team_key> team_list){
for(auto elem:team_list){
r[elem]=reason;
}
teams-=team_list;
};
f(Cmp_reason::PRE_QUALIFIED,pre_qualifying(tba_fetcher,year));
f(Cmp_reason::REGIONAL,regional_quals(tba_fetcher,year));
f(Cmp_reason::DISTRICT,district_quals(tba_fetcher,year,teams));
f(Cmp_reason::PRIORITY_WAITLIST,priority_waitlist(tba_fetcher,year));
for(auto t:teams){
r[t]=Cmp_reason::WAITLIST;
}
return r;
#if 0
auto f=filter(
//[](auto x){ return x.event_type==tba::Event_type::CMP_FINALS; },
//[](auto x){ return cmp_event(x.event_type==tba::Event_type::CMP_FINALS; },
[](auto x){ return cmp_event(x); },
tba::events(tba_fetcher,year)
);
auto teams=flatten(mapf([&](auto x){ return teams(tba_fetcher,x); },f));
print_r(m);
/*for(auto event:f){
auto t=to_set(event_teams_keys(tba_fetcher,event.key));
nyi
}*/
#endif
}