Repository navigation
Expand file tree
/
Copy pathTeam.cpp
More file actions
142 lines (124 loc) · 2.62 KB
/
Copy pathTeam.cpp
File metadata and controls
142 lines (124 loc) · 2.62 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
#include "Team.hpp"
#include "Game.hpp"
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
Team::Team(int id, string name)
{
_id = id;
_name = name;
_owp = -1;
_rpi = -1;
}
int Team::id()
{
return _id;
}
string Team::name()
{
return _name;
}
void Team::addGame(Game *game)
{
if(game->isWinner(this)) {
_gamesWon.push_back(game);
}
_gamesPlayed.push_back(game);
_owp = -1;
_rpi = -1;
}
std::vector<Game *> Team::gamesWon()
{
return _gamesWon;
}
std::vector<Game *> Team::gamesPlayed()
{
return _gamesPlayed;
}
float Team::winningPercentage()
{
return (float)_gamesWon.size() / (float)_gamesPlayed.size();
}
float Team::weightedWinningPercentage()
{
float won = 0;
float played = 0;
for (std::vector<Game *>::iterator i = _gamesPlayed.begin(); i != _gamesPlayed.end(); ++i)
{
if((*i)->isWinner(this)) {
if ((*i)->turf() == 'A')
{
won += 1.4;
played += 1.4;
} else if (((*i)->turf() == 'H')) {
won += 0.6;
played += 0.6;
} else {
won += 1;
played += 1;
}
} else {
if ((*i)->turf() == 'A')
{
played += 1.4;
} else if (((*i)->turf() == 'H')) {
played += 0.6;
} else {
played += 1;
}
}
}
return won/played;
}
float Team::winningPercentageExcluding(Team *excludedTeam)
{
float won = 0;
float played = 0;
for (std::vector<Game *>::iterator i = _gamesPlayed.begin(); i != _gamesPlayed.end(); ++i)
{
if(!((*i)->isPlayer(excludedTeam))) {
played++;
if(((*i)->isWinner(this))) {
won++;
}
}
}
return played ? won / played : 0;
}
float Team::opponentsWinningPercentage()
{
if(_owp != -1)
return _owp;
std::vector<float> wps;
wps.resize(_gamesPlayed.size());
std::transform(_gamesPlayed.begin(), _gamesPlayed.end(), wps.begin(), [this](Game *game) {
return game->otherPlayer(this)->winningPercentageExcluding(this);
});
float sum_wps = std::accumulate(wps.begin(), wps.end(), 0.0);
_owp = sum_wps / wps.size() ;
return _owp;
}
float Team::opponentsOpponentsWinningPercentage()
{
std::vector<float> owps;
owps.resize(_gamesPlayed.size());
std::transform(_gamesPlayed.begin(), _gamesPlayed.end(), owps.begin(), [this](Game *game) {
return game->otherPlayer(this)->opponentsWinningPercentage();
});
float sum_wps = std::accumulate(owps.begin(), owps.end(), 0.0);
return sum_wps / owps.size() ;
}
float Team::ratingsPercentageIndex()
{
if (_rpi != -1)
return _rpi;
_rpi = this->weightedWinningPercentage() * 0.25 +
this->opponentsWinningPercentage() * 0.5 +
this->opponentsOpponentsWinningPercentage() * 0.25;
return _rpi;
}
Team::~Team()
{
}