/
indcrkdb.cpp
938 lines (799 loc) · 33 KB
/
indcrkdb.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
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/*************************************************************************************************\
* *
* "IndCrkDB.cpp" - *
* *
* Author - Tom McDonnell 2005 *
* *
\*************************************************************************************************/
// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
#include "IndCrkDB.h"
#include "IndCrkRecords.h"
#include <TomsLibrary/misc.h>
#include <list>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
// MEMBER FUNCTION DEFINITIONS ////////////////////////////////////////////////////////////////////
// file I/O functions //
/*
* Read match data from file "fileName" into list 'l'.
* PROBLEM: maybe don't want to output to screen directly.
*/
bool indCrkDB::readFromFile(void)
{
using std::cout;
using std::endl;
matchRecord *mPtr;
cout << "Reading data from file \"" << fileName << "\"." << endl;
// open file
std::ifstream file(fileName.c_str());
if (file == NULL)
{
cout << "File \"" << fileName << "\" could not be read." << endl;
return false;
}
// read file
int lines = 1; // to enable file format error to be located
while (file.peek() != EOF)
{
mPtr = new matchRecord;
if (mPtr == NULL)
{
std::cerr << "Out of memory." << endl;
exit(0);
}
mPtr->readFromFile(file, lines);
cout << "."; // progress indicator
db.insert(db.end(), *mPtr);
}
// close file
file.close();
cout << endl
<< db.size() << " match records read successfully." << endl;
return true;
}
/*
*
*/
bool indCrkDB::writeToFile(void) const
{
std::ofstream file(fileName.c_str(), std::ios_base::trunc);
std::list<matchRecord>::const_iterator p;
for (p = db.begin(); p != db.end(); ++p)
p->writeToFile(file);
return true;
}
/*
* integrity check function
*/
void indCrkDB::checkDBintegrityAndPrintResults(void) const
{
using std::cout;
using std::endl;
std::list<matchRecord>::const_iterator p;
for (p = db.begin(); p != db.end(); ++p)
{
cout << endl
<< p->getDate() << ": " << p->getTeamName() << " Vs " << p->getOppTeamName() << endl;
p->checkDataIntegrityAndPrintResults();
}
}
// print functions //
/*
*
*/
void indCrkDB::printMatchRecord(const titleRecord &teamName,
const dateRecord &date, std::ostream &out) const
{
std::list<matchRecord>::const_iterator p = constFirstMatchWithDate(date);
if (p->getDate() == date && p->getTeamName() == teamName)
p->printRecord(out);
}
// database manipulation functions //
/*
* Insert matchRecord into database.
*/
void indCrkDB::insertMatchRecord(const matchRecord &m)
{
insertSorted(m);
// PROBLEM: must handle failure case
}
/*
*
*/
void deleteMatchRecord(const dateRecord &, const titleRecord &teamName)
{
}
/*
* Note: Does not update file.
*/
bool indCrkDB::getUserToEditMatchRecord(const dateRecord &date, const titleRecord &teamName)
{
// find match with date matching or after that specified
std::list<matchRecord>::iterator p = firstMatchWithDate(date);
while (p->getDate() == date)
{
if (p->getTeamName() == teamName)
{
p->getUserToEditData();
// sort match record list (date may have changed)
sortByDate();
return true;
}
++p;
}
return false;
}
/*
*
*/
void indCrkDB::compilePlayerRecords(const nameRecord &playerName,
const titleRecord &teamName,
const titleRecord &oppTeamName,
const dateBeginEnd &period,
playerRecordsStruct &r ) const
{
// initialise
r.playerName = playerName;
r.teamName = teamName;
r.period = period;
// compile player totals and best/worst performance records
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(r.period.begin);
p != db.end() && p->getDate() <= r.period.end; ++p) // order important
{
if (p->getTeamName() == r.teamName && p->playerPlayed(r.playerName)
&& (oppTeamName == titleRecord() || p->getOppTeamName() == oppTeamName))
{
r.matchesPlayed++;
p->getPlayerStats(r.playerName, r);
}
}
// calculate player's averages
r.avgRunsScrdPerInns = static_cast<double>(r.runsScored)
/ static_cast<double>(r.innsBatted);
r.avgWktsTakenPerOver = static_cast<double>(r.wicketsTaken)
/ static_cast<double>(r.oversBowled);
r.avgRunsConcPerOver = static_cast<double>(r.runsConceded)
/ static_cast<double>(r.oversBowled);
r.avgMatchContr = matchContribution(r.innsBatted, r.runsScored, r.oversBowled, r.runsConceded);
}
/*
* The list compiled here may be sorted by
* any criteria to form player ranking tables
*/
void indCrkDB::compilePlayerRecords(const titleRecord &teamName,
const titleRecord &oppTeamName,
const dateBeginEnd &period,
std::list<playerRecordsStruct> &playerRecordsList) const
{
// clear player records list
playerRecordsList.clear();
// compile player names list
std::vector<nameRecord> uNames; compilePlayersList(period, teamName, uNames);
playerRecordsStruct tempRecordsStruct;
std::vector<nameRecord>::const_iterator namePtr;
for (namePtr = uNames.begin(); namePtr != uNames.end(); ++namePtr)
{
// initialise tempRecordsStruct
tempRecordsStruct = playerRecordsStruct();
compilePlayerRecords(*namePtr, teamName, oppTeamName, period, tempRecordsStruct);
playerRecordsList.push_back(tempRecordsStruct);
}
}
/*
*
*/
void indCrkDB::compilePlayerHistory(const nameRecord &playerName,
const titleRecord &teamName,
const titleRecord &oppTeamName,
const dateBeginEnd &period,
playerHistoryStruct &h ) const
{
// initialise
h.period = period;
h.playerName = playerName;
h.teamName = teamName;
// compile player history
playerMatchStats matchStats;
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == teamName && p->playerPlayed(playerName)
&& (oppTeamName == titleRecord() || p->getOppTeamName() == oppTeamName))
{
p->getPlayerStats(playerName, matchStats);
h.matchStatsVect.push_back(matchStats);
}
}
/*
*
*/
void indCrkDB::compileTeamRecords(const titleRecord &teamName,
const titleRecord &oppTeamName,
const dateBeginEnd &period,
teamRecordsStruct &r ) const
{
using teamRecordsStruct::overall;
using teamRecordsStruct::batting1st;
using teamRecordsStruct::batting2nd;
using teamRecordsStruct::withFullTeam;
using teamRecordsStruct::missing1Player;
using teamRecordsStruct::missing2Players;
std::vector<nameRecord> uNames;
// initialise
r.teamName = teamName;
r.period = period;
r.longestWstreak = 0;
r.longestLstreak = 0;
int currentWstreak = 0,
currentLstreak = 0;
dateBeginEnd currentWstreakPeriod,
currentLstreakPeriod;
// compile team best/worst performances
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(r.period.begin);
p != db.end() && p->getDate() <= r.period.end; ++p) // order important
{
if (p->getTeamName() == r.teamName
&& (oppTeamName == titleRecord() || p->getOppTeamName() == oppTeamName))
{
// tally results in 'overall' category
r.category[overall].matchesPlayed++;
if (p->getTeamScore() > p->getOppTeamScore()) r.category[overall].matchesWon++;
else if (p->getTeamScore() < p->getOppTeamScore()) r.category[overall].matchesLost++;
else r.category[overall].matchesDrawn++;
r.category[overall].runsScored += p->getTeamScore(); // note: includes penalties
r.category[overall].runsConceded += p->getOppTeamScore(); // note: includes penalties
r.category[overall].wicketsTaken += p->getTeamWicketTally();
// tally results in 'batting1st' category
if (p->battedFirst())
{
r.category[batting1st].matchesPlayed++;
if (p->getTeamScore() > p->getOppTeamScore()) r.category[batting1st].matchesWon++;
else if (p->getTeamScore() < p->getOppTeamScore())
r.category[batting1st].matchesLost++;
else
r.category[batting1st].matchesDrawn++;
r.category[batting1st].runsScored += p->getTeamScore(); // note:includes pens
r.category[batting1st].runsConceded += p->getOppTeamScore(); // note:includes pens
r.category[batting1st].wicketsTaken += p->getTeamWicketTally();
}
// tally results in 'batting2nd' category
else {
r.category[batting2nd].matchesPlayed++;
if (p->getTeamScore() > p->getOppTeamScore()) r.category[batting2nd].matchesWon++;
else if (p->getTeamScore() < p->getOppTeamScore())
r.category[batting2nd].matchesLost++;
else
r.category[batting2nd].matchesDrawn++;
r.category[batting2nd].runsScored += p->getTeamScore(); // note:includes pens
r.category[batting2nd].runsConceded += p->getOppTeamScore(); // note:includes pens
r.category[batting2nd].wicketsTaken += p->getTeamWicketTally();
}
// count players in team
uNames.clear();
p->findUniquePlayerNames(uNames);
// tally results in 'withFullTeam' category
if (uNames.size() == 8)
{
r.category[withFullTeam].matchesPlayed++;
if (p->getTeamScore() > p->getOppTeamScore())
r.category[withFullTeam].matchesWon++;
else if (p->getTeamScore() < p->getOppTeamScore())
r.category[withFullTeam].matchesLost++;
else
r.category[withFullTeam].matchesDrawn++;
r.category[withFullTeam].runsScored += p->getTeamScore(); // note: includes pens.
r.category[withFullTeam].runsConceded += p->getOppTeamScore(); // note: includes pens.
r.category[withFullTeam].wicketsTaken += p->getTeamWicketTally();
}
// tally results in 'missing1Player' category
else if (uNames.size() == 7)
{
r.category[missing1Player].matchesPlayed++;
if (p->getTeamScore() > p->getOppTeamScore())
r.category[missing1Player].matchesWon++;
else if (p->getTeamScore() < p->getOppTeamScore())
r.category[missing1Player].matchesLost++;
else
r.category[missing1Player].matchesDrawn++;
r.category[missing1Player].runsScored += p->getTeamScore(); // note: includes pens
r.category[missing1Player].runsConceded += p->getOppTeamScore(); // note: includes pens
r.category[missing1Player].wicketsTaken += p->getTeamWicketTally();
}
// tally results in 'missing2Players' category
else if (uNames.size() == 6)
{
r.category[missing2Players].matchesPlayed++;
if (p->getTeamScore() > p->getOppTeamScore())
r.category[missing2Players].matchesWon++;
else if (p->getTeamScore() < p->getOppTeamScore())
r.category[missing2Players].matchesLost++;
else
r.category[missing2Players].matchesDrawn++;
r.category[missing2Players].runsScored += p->getTeamScore(); // note:includes pens
r.category[missing2Players].runsConceded += p->getOppTeamScore(); // note:includes pens
r.category[missing2Players].wicketsTaken += p->getTeamWicketTally();
}
// keep track of streaks
if (p->getTeamScore() > p->getOppTeamScore())
{
// win
if (currentWstreak++ == 0)
{
currentWstreakPeriod.begin = p->getDate();
currentWstreakPeriod.end = p->getDate();
}
else
currentWstreakPeriod.end = p->getDate();
// PROBLEM: need to account for when there are equal longest streaks
if (currentWstreak > r.longestWstreak)
{
r.longestWstreak = currentWstreak;
r.longestWstreakPeriod = currentWstreakPeriod;
}
currentLstreak = 0;
}
else if (p->getTeamScore() < p->getOppTeamScore())
{
// loss
if (currentLstreak++ == 0)
{
currentLstreakPeriod.begin = p->getDate();
currentLstreakPeriod.end = p->getDate();
}
else
currentLstreakPeriod.end = p->getDate();
// PROBLEM: need to account for when there are equal longest streaks
if (currentLstreak > r.longestLstreak)
{
r.longestLstreak = currentLstreak;
r.longestLstreakPeriod = currentLstreakPeriod;
}
currentWstreak = 0;
}
p->getTeamStats(r); // keep track of highest/lowest scores etc.
}
}
for (int i = 0; i < 6; ++i)
{
if (r.category[i].matchesPlayed != 0) // avoid dividing by zero
{
// calculate team's win rate
r.category[i].winRate = 100 * ( static_cast<double>(r.category[i].matchesWon)
/ static_cast<double>(r.category[i].matchesPlayed));
// calculate team's averages
r.category[i].avgTeamScore = static_cast<double>(r.category[i].runsScored)
/ static_cast<double>(r.category[i].matchesPlayed);
r.category[i].avgOppTeamScore = static_cast<double>(r.category[i].runsConceded)
/ static_cast<double>(r.category[i].matchesPlayed);
r.category[i].avgWicketTally = static_cast<double>(r.category[i].wicketsTaken)
/ static_cast<double>(r.category[i].matchesPlayed);
}
else
{
r.category[i].winRate = 0.0;
r.category[i].avgTeamScore = 0.0;
r.category[i].avgOppTeamScore = 0.0;
r.category[i].avgWicketTally = 0.0;
}
r.category[i].avgMargin = r.category[i].avgTeamScore - r.category[i].avgOppTeamScore;
}
}
/*
*
*/
void indCrkDB::compileTeamHistory(const titleRecord &teamName,
const titleRecord &oppTeamName,
const dateBeginEnd &period,
teamHistoryStruct &h ) const
{
// initialise
h.period = period;
h.teamName = teamName;
// compile team history
teamMatchStats matchStats;
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == teamName
&& (oppTeamName == titleRecord() || p->getOppTeamName() == oppTeamName))
{
p->getTeamStats(matchStats);
h.matchStatsVect.push_back(matchStats);
}
}
/*
*
*/
void indCrkDB::compileTeamIndividualRecords(const titleRecord &teamName,
const titleRecord &oppTeamName,
const dateBeginEnd &period,
teamIndividualRecordsStruct &r) const
{
// initialise
r.teamName = teamName;
r.period = period;
// compile player names list
std::vector<nameRecord> uNames;
compilePlayersList(period, teamName, uNames);
// compile best/worst individual performances within team
std::vector<nameRecord>::const_iterator namePtr;
for (namePtr = uNames.begin(); namePtr != uNames.end(); ++namePtr)
// note: since not initializing namePtr,
// overwriting players records with better/worse ones when they are found
compilePlayerRecords(*namePtr, teamName, oppTeamName, period, r.bestPlayerRecords);
// compile best/worst batting partnerships
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(r.period.begin);
p != db.end() && p->getDate() <= r.period.end; ++p) // order important
{
if (p->getTeamName() == r.teamName
&& (oppTeamName == titleRecord() || p->getOppTeamName() == oppTeamName))
{
int i;
// Find best batting partnership(s)
for (i = 0; i < p->nPlayersPerTeam ; i += 2)
{
// test whether this batting pship is better than the best seen so far
if ( p->getBattingRec(i).runsScored + p->getBattingRec(i + 1).runsScored
> r.bestBattPship1.front().runsScored + r.bestBattPship2.front().runsScored)
{
// remove all but one record from both lists
while (r.bestBattPship1.size() > 1) r.bestBattPship1.pop_back();
while (r.bestBattPship2.size() > 1) r.bestBattPship2.pop_back();
// update remaining records
r.bestBattPship1.front() = playerInnsRecord(p->getBattingRec(i).runsScored,
p->getBattingRec(i).batsmansName,
p->getOppTeamName(), p->getDate());
r.bestBattPship2.front() = playerInnsRecord(p->getBattingRec(i + 1).runsScored,
p->getBattingRec(i + 1).batsmansName,
p->getOppTeamName(), p->getDate() );
}
else
// test whether this batting pship is equal to the best seen so far
if ( p->getBattingRec(i).runsScored + p->getBattingRec(i + 1).runsScored
== r.bestBattPship1.front().runsScored + r.bestBattPship2.front().runsScored)
{
// add record to list
r.bestBattPship1.push_back(playerInnsRecord(p->getBattingRec(i).runsScored,
p->getBattingRec(i).batsmansName,
p->getOppTeamName(), p->getDate() ));
r.bestBattPship2.push_back(playerInnsRecord(p->getBattingRec(i + 1).runsScored,
p->getBattingRec(i + 1).batsmansName,
p->getOppTeamName(), p->getDate() ));
}
}
// Find worst batting partnership(s)
for (i = 0; i < p->nPlayersPerTeam ; i += 2)
{
// test whether this batting pship is worse than the worst seen so far
if ( p->getBattingRec(i).runsScored + p->getBattingRec(i + 1).runsScored
< r.wrstBattPship1.front().runsScored + r.wrstBattPship2.front().runsScored)
{
// remove all but one record from both lists
while (r.wrstBattPship1.size() > 1) r.wrstBattPship1.pop_back();
while (r.wrstBattPship2.size() > 1) r.wrstBattPship2.pop_back();
// update remaining records
r.wrstBattPship1.front() = playerInnsRecord(p->getBattingRec(i).runsScored,
p->getBattingRec(i).batsmansName,
p->getOppTeamName(), p->getDate());
r.wrstBattPship2.front() = playerInnsRecord(p->getBattingRec(i + 1).runsScored,
p->getBattingRec(i + 1).batsmansName,
p->getOppTeamName(), p->getDate() );
}
else
// test whether this batting pship is equal to the worst seen so far
if ( p->getBattingRec(i).runsScored + p->getBattingRec(i + 1).runsScored
== r.wrstBattPship1.front().runsScored + r.wrstBattPship2.front().runsScored)
{
// add record to list
r.wrstBattPship1.push_back(playerInnsRecord(p->getBattingRec(i).runsScored,
p->getBattingRec(i).batsmansName,
p->getOppTeamName(), p->getDate() ));
r.wrstBattPship2.push_back(playerInnsRecord(p->getBattingRec(i + 1).runsScored,
p->getBattingRec(i + 1).batsmansName,
p->getOppTeamName(), p->getDate() ));
}
}
}
}
}
// simple queries //
/*
* Test whether player "playerName" played for team "teamName"
* in any match on the date specified.
*/
bool indCrkDB::playerPlayed(const nameRecord &playerName,
const titleRecord &teamName,
const dateRecord &date ) const
{
std::list<matchRecord>::const_iterator p = constFirstMatchWithDate(date);
if (p->getDate() == date && p->getTeamName() == teamName && p->playerPlayed(playerName))
return true;
else
return false;
}
/*
* Test whether player "playerName" played for team "teamName"
* in any match in the period specified.
*/
bool indCrkDB::playerPlayed(const nameRecord &playerName,
const titleRecord &teamName,
const dateBeginEnd &period ) const
{
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if ( p->getTeamName() == teamName
&& p->playerPlayed(playerName) )
return true;
return false;
}
/*
* Test whether player "playerName" played for team "teamName" in any match.
*/
bool indCrkDB::playerPlayed(const nameRecord &playerName, const titleRecord &teamName) const
{
std::list<matchRecord>::const_iterator p;
for (p = db.begin(); p != db.end(); ++p)
if ( p->getTeamName() == teamName
&& p->playerPlayed(playerName) )
return true;
return false;
}
/*
* Test whether team "teamName" played in any match in the period specified.
*/
bool indCrkDB::teamPlayed(const titleRecord &teamName, const dateRecord &date) const
{
std::list<matchRecord>::const_iterator p = constFirstMatchWithDate(date);
if (p->getDate() == date && p->getTeamName() == teamName)
return true;
else
return false;
}
/*
* Test whether team "teamName" played in any match in the period specified.
*/
bool indCrkDB::teamPlayed(const titleRecord &teamName, const dateBeginEnd &period) const
{
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == teamName)
return true;
return false;
}
/*
* Test whether team "teamName" played in any match.
*/
bool indCrkDB::teamPlayed(const titleRecord &teamName) const
{
std::list<matchRecord>::const_iterator p;
for (p = db.begin(); p != db.end(); ++p)
if (p->getTeamName() == teamName)
return true;
return false;
}
/*
* Test whether team "teamName" played in any match in the period specified.
*/
bool indCrkDB::teamPlayedOppTeam(const titleRecord &teamName,
const titleRecord &oppTeamName,
const dateRecord &date ) const
{
std::list<matchRecord>::const_iterator p = constFirstMatchWithDate(date);
if (p->getDate() == date && p->getTeamName() == teamName && p->getOppTeamName() == oppTeamName)
return true;
else
return false;
}
/*
* Test whether team "teamName" played in any match in the period specified.
*/
bool indCrkDB::teamPlayedOppTeam(const titleRecord &teamName,
const titleRecord &oppTeamName,
const dateBeginEnd &period ) const
{
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == teamName && p->getOppTeamName() == oppTeamName)
return true;
return false;
}
/*
* Test whether team "teamName" played in any match.
*/
bool indCrkDB::teamPlayedOppTeam(const titleRecord &teamName,
const titleRecord &oppTeamName) const
{
std::list<matchRecord>::const_iterator p;
for (p = db.begin(); p != db.end(); ++p)
if (p->getTeamName() == teamName && p->getOppTeamName() == oppTeamName)
return true;
return false;
}
/*
* Test whether opposition team "oppTeamName" played in any match in the period specified.
*/
bool indCrkDB::oppTeamPlayed(const titleRecord &oppTeamName, const dateRecord &date) const
{
std::list<matchRecord>::const_iterator p = constFirstMatchWithDate(date);
if (p->getDate() == date && p->getOppTeamName() == oppTeamName)
return true;
else
return false;
}
/*
* Test whether opposition team "oppTeamName" played in any match in the period specified.
*/
bool indCrkDB::oppTeamPlayed(const titleRecord &oppTeamName, const dateBeginEnd &period) const
{
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getOppTeamName() == oppTeamName)
return true;
return false;
}
/*
* Test whether opposition team "oppTeamName" played in any match.
*/
bool indCrkDB::oppTeamPlayed(const titleRecord &oppTeamName) const
{
std::list<matchRecord>::const_iterator p;
for (p = db.begin(); p != db.end(); ++p)
if (p->getOppTeamName() == oppTeamName)
return true;
return false;
}
// database maintenance functions //
/*
*
*/
void indCrkDB::compileTeamsList(const dateBeginEnd &period, std::vector<titleRecord> &v) const
{
v.clear();
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (std::find(v.begin(), v.end(), p->getTeamName()) == v.end())
v.insert(v.end(), p->getTeamName());
}
/*
*
*/
void indCrkDB::compilePlayersList(const dateBeginEnd &period, std::vector<nameRecord> &v) const
{
v.clear();
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
p->findUniquePlayerNames(v);
}
/*
*
*/
void indCrkDB::compilePlayersList(const dateBeginEnd &period,
const titleRecord &teamName, std::vector<nameRecord> &v) const
{
v.clear();
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == teamName)
p->findUniquePlayerNames(v);
}
/*
*
*/
void indCrkDB::compilePlayersList(const dateBeginEnd &period,
const titleRecord &teamName,
const titleRecord &oppTeamName,
std::vector<nameRecord> &v ) const
{
v.clear();
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == teamName && p->getOppTeamName() == oppTeamName)
p->findUniquePlayerNames(v);
}
/*
* Compile list of all opposition teams in database in given period.
*/
void indCrkDB::compileOppTeamsList(const dateBeginEnd &period, std::vector<titleRecord> &) const
{
}
/*
* Compile list of all opposition teams who have played in
* matches Vs team "teamName" in database in given period.
*/
void indCrkDB::compileOppTeamsList(const dateBeginEnd &period,
const titleRecord &teamName, std::vector<titleRecord> &v) const
{
v.clear();
std::list<matchRecord>::const_iterator p;
for (p = constFirstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == teamName)
if (std::find(v.begin(), v.end(), p->getOppTeamName()) == v.end())
v.insert(v.end(), p->getOppTeamName());
}
/*
* Replace old player name with new in matches within specified period.
* Note: does not update file.
*/
void indCrkDB::updatePlayerNames(const dateBeginEnd &period, const titleRecord &teamName,
const nameRecord &oldName, const nameRecord &newName )
{
std::list<matchRecord>::iterator p;
for (p = firstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == teamName && p->playerPlayed(oldName))
p->replacePlayerName(oldName, newName);
}
/*
* Note: does not update file.
*/
void indCrkDB::updateTeamNames(const dateBeginEnd &period,
const titleRecord &oldTeamName, const titleRecord &newTeamName)
{
std::list<matchRecord>::iterator p;
for (p = firstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getTeamName() == oldTeamName)
p->replaceTeamName(newTeamName);
}
/*
* Note: does not update file.
*/
void indCrkDB::updateOppTeamNames(const dateBeginEnd &period, const titleRecord &teamName,
const titleRecord &oldOppTeamName,
const titleRecord &newOppTeamName)
{
std::list<matchRecord>::iterator p;
for (p = firstMatchWithDate(period.begin);
p != db.end() && p->getDate() <= period.end; ++p) // order important
if (p->getOppTeamName() == oldOppTeamName)
p->replaceOppTeamName(newOppTeamName);
}
// PRIVATE MEMBER FUNCTIONS ///////////////////////////////////////////////////////////////////////
/*
* Returns a const iterator pointing to the first match
* with the date equal to or after the date specified.
* Assumes that the list is sorted.
*/
std::list<matchRecord>::const_iterator
indCrkDB::constFirstMatchWithDate(const dateRecord &date) const
{
std::list<matchRecord>::const_iterator p = db.begin();
while (p != db.end() && p->getDate() < date) // order important
++p;
return p;
}
/*
* Same as above but not const.
*/
std::list<matchRecord>::iterator
indCrkDB::firstMatchWithDate(const dateRecord &date)
{
std::list<matchRecord>::iterator p = db.begin();
while (p != db.end() && p->getDate() < date) // order important
++p;
return p;
}
/*
*
*/
void indCrkDB::insertSorted(const matchRecord &m)
{
db.insert(db.end(), m); // PROBLEM: should be able to insert into correct position
// sort match record list
db.sort(std::greater<matchRecord>());
}
/*****************************************END*OF*FILE*********************************************/