00001 #include "tournament.h"
00002
00003 const int Tournament::VALID_TEAMCOUNTS[] = {4,8,16,32};
00004
00005 Tournament::Tournament(void) {}
00006
00007 Tournament::Tournament(const int teamcnt, QString title)
00008 {
00009 if(isValidTeamcount(teamcnt))
00010 {
00011 this->teamcnt = teamcnt;
00012 this->bracketcnt = teamcnt/4;
00013 this->title = title;
00014 initTeams();
00015 initGames();
00016 initPicks();
00017 }
00018 else
00019 throw 1;
00020 }
00021
00022 Tournament::~Tournament(void)
00023 {
00024 for(int i=0; i<games.size(); i++)
00025 {
00026 delete games[i];
00027 }
00028
00029 for(int i=0; i<teams.size(); i++)
00030 {
00031 delete teams[i];
00032 }
00033 }
00034
00035 void Tournament::setTeamCount(int num)
00036 {
00037 if(isValidTeamcount(num))
00038 {
00039 teamcnt = num;
00040 bracketcnt = num/4;
00041 }
00042 else throw 1;
00043 }
00044
00045 int Tournament::getTeamCount(void)
00046 {
00047 return teamcnt;
00048 }
00049
00050 int Tournament::getBracketCount(void)
00051 {
00052 return bracketcnt;
00053 }
00054
00055 QString Tournament::getTitle(void) const
00056 {
00057 return title;
00058 }
00059
00060 void Tournament::setTeams(QVector<Team*> &teams)
00061 {
00062 this->teams = teams;
00063 }
00064
00065 QVector<Team*> Tournament::getTeams(void) const
00066 {
00067 return teams;
00068 }
00069
00070 QVector<Game*> Tournament::getGames(void) const
00071 {
00072 return games;
00073 }
00074
00075 void Tournament::addGame(Game* game)
00076 {
00077 games.append(game);
00078 }
00079
00080 void Tournament::addTeam(Team* team)
00081 {
00082 teams.append(team);
00083 }
00084
00085 void Tournament::setTitle(QString title)
00086 {
00087 this->title = title;
00088 }
00089
00090 void Tournament::initTeams(void)
00091 {
00092 teams = QVector<Team*>();
00093
00094 for (int i=0; i<teamcnt; i++)
00095 {
00096 addTeam(new Team(i,QString("Team ").append(QString().setNum(i+1)), " ", ""));
00097 }
00098 }
00099
00100 void Tournament::initGames(void)
00101 {
00102 games = QVector<Game*> ();
00103 int result[2] = {0,0};
00104
00105 for (int i=0; i<teamcnt-1; i++)
00106 {
00107 if(i<teamcnt/2)
00108 games.insert(i, new Game(teamcnt-2-i, teams[2*i], teams[2*i+1], result, 0, 0));
00109 else
00110 games.insert(i, new Game(teamcnt-2-i, new Team(), new Team(), result, games[abs((teamcnt-2-i)*2+4-teamcnt)], games[abs((teamcnt-i-2)*2+3-teamcnt)]));
00111 }
00112 }
00113
00114 Team::Round Tournament::getMinRound()
00115 {
00116 return minRound;
00117 }
00118
00119 void Tournament::initPicks(void) {
00120 if(teams.size()==4) minRound = Team::HALF;
00121 else if(teams.size()==8) minRound = Team::QUARTER;
00122 else if(teams.size()==16) minRound = Team::EIGHT;
00123 else minRound = Team::SIXTEEN;
00124 }
00125
00126 Game* Tournament::getGameByID(int id)
00127 {
00128 for (int i=0; i<games.size(); i++)
00129 {
00130 if(games[i]->getID() == id)
00131 return games[i];
00132 }
00133 return 0;
00134 }
00135
00136 bool Tournament::setPickForTeam(int id, Team::Round round)
00137 {
00138 bool correct = false;
00139 Team team;
00140
00141 for(int i=0; i<teams.size(); i++)
00142 {
00143 if(teams[i]->getId()==id)
00144 {
00145 teams[i]->setPick(round);
00146 correct = true;
00147 }
00148 }
00149 return correct;
00150 }
00151
00152 bool Tournament::isValidTeamcount(const int &val)
00153 {
00154 return std::binary_search(VALID_TEAMCOUNTS, VALID_TEAMCOUNTS+sizeof(VALID_TEAMCOUNTS)/sizeof(int), val);
00155 }