void CSeasonGenerator::generateSeason(CSinglePlayerGame &game) { CEventManager *eventMngr = CGameEngine::getInstance()->getEventManager(); IDAOFactory *daoFactory = game.getIDAOFactory(); IPfSeasonsDAO *seasonsDAO = daoFactory->getIPfSeasonsDAO(); IPfCompetitionsBySeasonDAO *competitionsBySeasonDAO = daoFactory->getIPfCompetitionsBySeasonDAO(); IPfTeamsDAO *teamsDAO = daoFactory->getIPfTeamsDAO(); IPfTeamsByCompetitionsDAO *teamsByCompetitionsDAO = daoFactory->getIPfTeamsByCompetitionsDAO(); CPfSeasons *season = seasonsDAO->findLastSeason(); if( season==NULL || season->getXSeason_str()=="" ){ throw PFEXCEPTION("[CSeasonGenerator::generateSeason] Last season was not found, new season generation is aborted."); } int year = season->getNYear()+1; std::ostringstream sseason; sseason << year << "/" << (year+1); // the min & max date necesary for the season events CDate minDate = CDate::MAX_DATE; CDate maxDate = CDate::MIN_DATE; // create the new season CPfSeasons newSeason; newSeason.setNYear(year); newSeason.setSSeason(sseason.str()); daoFactory->getIPfSeasonsDAO()->insertReg(&newSeason); // retrieve the competitions associated with the last season std::vector<CPfCompetitionsBySeason*> *competitionsBySeasonList = competitionsBySeasonDAO->findByXFkSeason(season->getXSeason_str()); // for each competition, create a new competition by season and insert the relevant matches std::vector<CPfCompetitionsBySeason*>::iterator itCompetitionsBySeason; for( itCompetitionsBySeason=competitionsBySeasonList->begin(); itCompetitionsBySeason!=competitionsBySeasonList->end(); itCompetitionsBySeason++ ){ CPfCompetitionsBySeason *competitionBySeason = *itCompetitionsBySeason; // create the new competition by season CPfCompetitionsBySeason *newCompetitionBySeason = new CPfCompetitionsBySeason(); newCompetitionBySeason->setDBeginCompetition(getSameWeekDayOneYearLater(competitionBySeason->getDBeginCompetition())); newCompetitionBySeason->setDEndCompetition(getSameWeekDayOneYearLater(competitionBySeason->getDEndCompetition())); newCompetitionBySeason->setXFkCompetition_str(competitionBySeason->getXFkCompetition_str()); newCompetitionBySeason->setXFkSeason_str(newSeason.getXSeason_str()); competitionsBySeasonDAO->insertReg(newCompetitionBySeason); // retrieve the teams registered in previous season on the same competition std::vector<CPfTeams*> *teamsList = teamsDAO->findByXFKCompetitionAndXFKSeason(competitionBySeason->getXFkCompetition_str(), competitionBySeason->getXFkSeason_str()); std::vector<CPfTeams*>::iterator itTeams; for( itTeams=teamsList->begin(); itTeams!=teamsList->end(); itTeams++ ){ CPfTeams *team = *itTeams; // the team will be associate with the competition for the new season CPfTeamsByCompetitions *newTeamByCompetition = new CPfTeamsByCompetitions(); newTeamByCompetition->setXFkCompetitionBySeason_str(newCompetitionBySeason->getXCompetitionBySeason_str()); newTeamByCompetition->setXFkTeam_str(team->getXTeam_str()); teamsByCompetitionsDAO->insertReg(newTeamByCompetition); delete newTeamByCompetition; } eventMngr->addEvent(new CStartCompetitionEvent(newCompetitionBySeason->getDBeginCompetition())); //Shuffle teams to create random calendar std::random_shuffle(teamsList->begin(), teamsList->end()); generateLeagueMatches(game, *newCompetitionBySeason, teamsList); // TODO: Maybe not all competitions are leagues eventMngr->addEvent(new CEndCompetitionEvent(newCompetitionBySeason->getDEndCompetition())); // Retrieve the min & max date for the season events if( newCompetitionBySeason->getDBeginCompetition()<minDate ){ minDate = newCompetitionBySeason->getDBeginCompetition(); } if( newCompetitionBySeason->getDEndCompetition()>maxDate ){ maxDate = newCompetitionBySeason->getDEndCompetition(); } teamsDAO->freeVector(teamsList); delete newCompetitionBySeason; } minDate.setSec(minDate.getSec()-1); maxDate.setSec(maxDate.getSec()+1); // enqueue the start & end season events eventMngr->addEvent(new CStartSeasonEvent(minDate)); eventMngr->addEvent(new CEndSeasonEvent(maxDate)); game.getOptionManager()->setGameCurrentSeason(newSeason.getXSeason()); competitionsBySeasonDAO->freeVector(competitionsBySeasonList); delete season; }
void CSinglePlayerGame::loadGameEvents() { int xSeason = m_optionManager->getGameCurrentSeason(); CEventManager *eventMngr = CGameEngine::getInstance()->getEventManager(); IPfCompetitionPhasesBySeasonDAO *competitionPhasesBySeasonDAO = m_daoFactory->getIPfCompetitionPhasesBySeasonDAO(); IPfCompetitionPhasesDAO *competitionPhasesDAO = m_daoFactory->getIPfCompetitionPhasesDAO(); IPfMatchesDAO *matchesDAO = m_daoFactory->getIPfMatchesDAO(); // the min & max date necesary for the season events CDate minDate = CDate::MAX_DATE; CDate maxDate = CDate::MIN_DATE; // retrieve the competitions for the current season std::vector<CPfCompetitionPhasesBySeason*> *competitionPhasesBySeasonList = competitionPhasesBySeasonDAO->findByXFkSeason(xSeason); std::vector<CPfCompetitionPhasesBySeason*>::iterator itCompetitionPhasesBySeason; bool someCompetitionStarted = false; for( itCompetitionPhasesBySeason=competitionPhasesBySeasonList->begin(); itCompetitionPhasesBySeason!=competitionPhasesBySeasonList->end(); itCompetitionPhasesBySeason++ ){ CPfCompetitionPhasesBySeason *competitionPhaseBySeason = *itCompetitionPhasesBySeason; CPfCompetitionPhases *competitionPhase = competitionPhasesDAO->findByXCompetitionPhase(competitionPhaseBySeason->getXFkCompetitionPhase_str()); // for each competition are retrieved the respective matches std::vector<CPfMatches*> *matchesList = matchesDAO->findByXFkCompetitionAndXFkSeason(competitionPhase->getXFkCompetition_str(), competitionPhaseBySeason->getXFkSeason_str()); std::vector<CPfMatches*>::iterator itMatches; bool someMatchPlayed = false; for( itMatches=matchesList->begin(); itMatches!=matchesList->end(); itMatches++ ){ CPfMatches *match = *itMatches; if( match->getLPlayed() ){ someMatchPlayed = true; someCompetitionStarted = true; }else{ eventMngr->addEvent(new CMatchEvent(match->getDMatch(), match->getXMatch())); } } if( !someMatchPlayed ){ eventMngr->addEvent(new CStartCompetitionEvent(competitionPhaseBySeason->getDBeginCompetitionPhase())); } eventMngr->addEvent(new CEndCompetitionEvent(competitionPhaseBySeason->getDEndCompetitionPhase())); // Retrieve the min & max date for the season events if( competitionPhaseBySeason->getDBeginCompetitionPhase()<minDate ){ minDate = competitionPhaseBySeason->getDBeginCompetitionPhase(); } if( competitionPhaseBySeason->getDEndCompetitionPhase()>maxDate ){ maxDate = competitionPhaseBySeason->getDEndCompetitionPhase(); } matchesDAO->freeVector(matchesList); } minDate.setSec(minDate.getSec()-1); maxDate.setSec(maxDate.getSec()+1); // enqueue the start & end season events if( !someCompetitionStarted ){ eventMngr->addEvent(new CStartSeasonEvent(minDate)); } eventMngr->addEvent(new CEndSeasonEvent(maxDate)); competitionPhasesBySeasonDAO->freeVector(competitionPhasesBySeasonList); }