public function ranking() { $id = Converter::toInt('id', 'get', $this->player->getLeagueId()); $league = \Own\Bus\League\Data::loadById($id); if (!isset($league)) { $league = $this->player->getLeague(); } // player not logged in if (!isset($league)) { Session::redirect('/league'); } // view $this->setTpl(); $cacheTime = $this->app->site()->getCacheTime(); $cache = \Rebond\Util\Cache::getCache('league-ranking', $league->getId(), $cacheTime); if (isset($cache)) { // layout $this->tplLayout->set('column1', $cache); } else { $options = []; $options['clearSelect'] = true; $options['select'][] = \Own\Bus\Player\Data::getList(['id', 'user_id', 'country', 'experience', 'username', 'league_ranking', 'league_point', 'league_diff']); $options['select'][] = \Rebond\Core\User\Data::getList(['id', 'avatar_id'], 'player_user'); $options['select'][] = \Rebond\Core\Media\Data::getList([], 'player_user_avatar'); $options['leftJoin'][] = 'core_user player_user ON player_user.id = player.user_id'; $options['leftJoin'][] = 'core_media player_user_avatar ON player_user_avatar.id = player_user.avatar_id'; $options['where'][] = 'player.active = 1'; $options['where'][] = 'player.league_id = ' . $league->getId(); $options['order'][] = 'player.league_ranking, player.created_date'; $players = \Own\Bus\Player\Data::loadAll($options); // main $tplMain = new Template(Template::SITE, ['www']); $tplMain->set('league', $league); $tplMain->set('player', $this->player); $tplMain->set('players', $players); // layout $cache = $tplMain->render('league-ranking'); $this->tplLayout->set('column1', $cache); // cache \Rebond\Util\Cache::saveCache('league-ranking', $league->getId(), $cacheTime, $cache); } // template $this->tplMaster->set('layout', $this->tplLayout->render('layout-center')); return $this->tplMaster->render('tpl-default'); }
public function endTime() { $options = []; $options['where'][] = ['configuration.property = ?', 'nextWeek']; $nextWeek = \Own\Bus\Configuration\Data::load($options); $options = []; $options['where'][] = ['configuration.property = ?', 'nextMonth']; $nextMonth = \Own\Bus\Configuration\Data::load($options); $options = []; $options['where'][] = ['configuration.property = ?', 'nextYear']; $nextYear = \Own\Bus\Configuration\Data::load($options); // week (day * 7) if ((int) $nextWeek->getValue() < time()) { \Own\Bus\League\Data::updateSurfaceBatch(); \Own\Bus\Player\Data::resetLeagueDiff(); $this->log('leagues surface', true); // award achievements } // 2 days before the end of the month if ((int) $nextMonth->getValue() < time() + 2 * Engine::DAY * 3600) { \Own\Bus\Notification\Service::createForAll('endOfTheMonth', [['endOfTheMonth', $nextMonth->getValue()]]); } // month (day * 7 * 4) if ((int) $nextMonth->getValue() < time()) { // change players league $options = []; $options['clearSelect'] = true; $options['select'][] = \Own\Bus\League\Data::getList(['id', 'classification']); $options['order'][] = 'league.classification'; $leagues = \Own\Bus\League\Data::loadAll($options); $leaguesInfo = []; $moved = []; foreach ($leagues as $league) { $leaguesInfo[$league->getId()] = []; $leaguesInfo[$league->getId()]['classification'] = $league->getClassification(); $leaguesInfo[$league->getId()]['up'] = Engine::classificationMove(true, $league->getClassification()); $leaguesInfo[$league->getId()]['down'] = Engine::classificationMove(false, $league->getClassification()); $options = []; $options['where'][] = ['player.league_id = ?', $league->getId()]; $options['where'][] = 'player.active = 1'; $leaguesInfo[$league->getId()]['count'] = \Own\Bus\Player\Data::count($options); } // going down foreach ($leagues as $league) { if ($league->getClassification() != Classification::AMATEUR) { $options = []; $options['clearSelect'] = true; $options['select'][] = \Own\Bus\Player\Data::getList(['id', 'league_id']); $options['where'][] = 'player.active = 1'; $options['where'][] = ['player.league_id = ?', $league->getId()]; if (count($moved) > 0) { $options['where'][] = ['player.id NOT IN (?)', $moved]; } $options['order'][] = 'player.league_ranking DESC'; $options['limit'][] = Engine::classificationMove(false, $league->getClassification()); $players = \Own\Bus\Player\Data::loadAll($options); foreach ($players as $player) { $leagueId = Engine::findLeagueId(false, $player->getLeagueId(), $leaguesInfo); $player->setLeagueId($leagueId); $player->save(); $moved[] = $player->getId(); if ($player->getUserId() != 0) { \Own\Bus\Notification\Service::create($this->getId(), 0, 'leagueDown', [['leagueDown', $leaguesInfo[$leagueId]['classification']]]); } } } } // going up foreach ($leagues as $league) { if ($league->getClassification() != Classification::MASTERS) { $options = []; $options['clearSelect'] = true; $options['select'][] = \Own\Bus\Player\Data::getList(['id', 'league_id']); $options['where'][] = 'player.active = 1'; $options['where'][] = ['player.league_id = ?', $league->getId()]; if (count($moved) > 0) { $options['where'][] = ['player.id NOT IN (?)', $moved]; } $options['order'][] = 'player.league_ranking ASC'; $options['limit'][] = Engine::classificationMove(true, $league->getClassification()); $players = \Own\Bus\Player\Data::loadAll($options); foreach ($players as $player) { $leagueId = Engine::findLeagueId(true, $player->getLeagueId(), $leaguesInfo); $player->setLeagueId($leagueId); $player->save(); if ($player->getUserId() != 0) { \Own\Bus\Notification\Service::create($this->getId(), 0, 'leagueUp', [['leagueUp', $leaguesInfo[$leagueId]['classification']]]); } } } } \Own\Bus\Player\Data::resetLeagueRanking(); $this->log('players leagues update'); // reset tour and race diff \Own\Bus\Player\Data::resetTourAndRaceDiff(); $this->log('reset tour and race diff', true); // remove/add player \Own\Bus\Player\Service::removeAndAddPlayer(); $this->log('remove/add player'); // award achievements } // one month before the end of the year if ((int) $nextYear->getValue() < time() + Engine::DAY * 28 * 3600) { \Own\Bus\Notification\Service::createForAll('endOfTheYear', [['endOfTheYear', $nextYear->getValue()]]); } // year if ((int) $nextYear->getValue() < time()) { \Own\Bus\Notification\Service::createForAll('endRanking'); // reset race ranking \Own\Bus\Player\Data::resetRaceRanking(); $this->log('race ranking reset', true); // award achievements } if ((int) $nextYear->getValue() < time()) { $nextYear->setValue((int) $nextYear->getValue() + 336 * Engine::DAY * 60 * 60); $nextYear->save(); } if ((int) $nextMonth->getValue() < time()) { $nextMonth->setValue((int) $nextMonth->getValue() + 7 * 4 * Engine::DAY * 60 * 60); $nextMonth->save(); } if ((int) $nextWeek->getValue() < time()) { $nextWeek->setValue((int) $nextWeek->getValue() + 7 * Engine::DAY * 60 * 60); $nextWeek->save(); } }
public static function removeAndAddPlayer() { $options = []; $options['clearSelect'] = true; $options['select'][] = \Own\Bus\Player\Data::getList(['id', 'username']); $options['where'][] = 'player.user_id = 0'; $options['where'][] = 'player.active = 1'; $players = \Own\Bus\Player\Data::loadAll($options); if (count($players) > 0) { $db = new Util\Data(); $pick = \Own\Bus\Engine::dice(0, count($players) - 1); $remove = 'UPDATE bus_player SET active = 0 WHERE id = ' . $players[$pick]->getId(); $db->execute($remove); Util\Log::log(Util\Code::CRON, 'player retired: ' . $players[$pick]->getUsername() . ' (' . $players[$pick]->getId() . ')', __FILE__, __LINE__); $leagues = \Own\Bus\League\Data::loadAll(); $player = new \Own\Bus\Player\Model(); $player->setRandom(1); $player->setActive(true); if (count($leagues) > 0) { $player->setLeagueId(\Own\Bus\Engine::findLeague($leagues, 1)); } $player->save(); Util\Log::log(Util\Code::CRON, 'new player: ' . $player->getUsername() . ' (' . $player->getId() . ')', __FILE__, __LINE__); } }
private static function link($linkTournament = false, $linkLeague = false, $players = false, $needBothPlayers = true) { $join = 'join'; if (!$needBothPlayers) { $join = 'leftJoin'; } $option = []; $option['clearSelect'] = true; $options['select'][] = self::getList(['id', 'player_match1_id', 'player_match2_id', 'surface', 'current_set', 'best_of_sets', 'type', 'winner_id', 'position', 'tournament_id', 'league_id', 'status', 'scheduled']); $options['select'][] = \Own\Bus\PlayerMatch\Data::getList(['id', 'player_id', 'seed', 'has_viewed', 'set1', 'set2', 'set3', 'set4', 'set5', 'points', 'level'], 'match_player_match1'); $options['select'][] = \Own\Bus\PlayerMatch\Data::getList(['id', 'player_id', 'seed', 'has_viewed', 'set1', 'set2', 'set3', 'set4', 'set5', 'points', 'level'], 'match_player_match2'); $options['select'][] = \Own\Bus\Player\Data::getList(['id', 'username', 'user_id', 'country', 'experience'], 'match_player_match1_player'); if ($players) { $options['select'][] = \Rebond\Core\User\Data::getList(['id', 'avatar_id'], 'match_player_match1_player_user'); $options['select'][] = \Rebond\Core\Media\Data::getList([], 'match_player_match1_player_user_avatar'); } $options['select'][] = \Own\Bus\Player\Data::getList(['id', 'username', 'user_id', 'country', 'experience'], 'match_player_match2_player'); if ($players) { $options['select'][] = \Rebond\Core\User\Data::getList(['id', 'avatar_id'], 'match_player_match2_player_user'); $options['select'][] = \Rebond\Core\Media\Data::getList([], 'match_player_match2_player_user_avatar'); } if ($linkTournament) { $options['select'][] = \Own\Bus\Tournament\Data::getList(['id', 'title', 'size', 'classification'], 'match_tournament'); } if ($linkLeague) { $options['select'][] = \Own\Bus\League\Data::getList(['id', 'title'], 'match_league'); } $options[$join][] = 'bus_player_match match_player_match1 ON match_player_match1.id = match.player_match1_id'; $options[$join][] = 'bus_player_match match_player_match2 ON match_player_match2.id = match.player_match2_id'; $options[$join][] = 'bus_player match_player_match1_player ON match_player_match1_player.id = match_player_match1.player_id'; if ($players) { $options['leftJoin'][] = 'core_user match_player_match1_player_user ON match_player_match1_player_user.id = match_player_match1_player.user_id'; $options['leftJoin'][] = 'core_media match_player_match1_player_user_avatar ON match_player_match1_player_user_avatar.id = match_player_match1_player_user.avatar_id'; } $options[$join][] = 'bus_player match_player_match2_player ON match_player_match2_player.id = match_player_match2.player_id'; if ($players) { $options['leftJoin'][] = 'core_user match_player_match2_player_user ON match_player_match2_player_user.id = match_player_match2_player.user_id'; $options['leftJoin'][] = 'core_media match_player_match2_player_user_avatar ON match_player_match2_player_user_avatar.id = match_player_match2_player_user.avatar_id'; } if ($linkTournament) { $options['leftJoin'][] = 'bus_tournament match_tournament ON match_tournament.id = match.tournament_id'; } if ($linkLeague) { $options['leftJoin'][] = 'bus_league match_league ON match_league.id = match.league_id'; } return $options; }