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(); } }