Beispiel #1
0
 public static function generate($tournament)
 {
     $options = [];
     $options['where'][] = ['match.tournament_id = ?', $tournament->getId()];
     $matchCount = \Own\Bus\Match\Data::count($options);
     if ($matchCount == 0) {
         $tournamentTime = $tournament->getStartDate()->add(new \DateInterval('PT1M'));
         if (count($tournament->getTournamentPlayers()) >= $tournament->getSize()) {
             $position = $tournament->getSize() - 1;
             $players = self::getOrderedPlayers($tournament);
             $i = 0;
             while ($position >= $tournament->getSize() / 2) {
                 $matchTime = $tournamentTime->getTimestamp() + 60 * ($tournament->getSize() - $position) * (6 + 256 / $tournament->getSize()) * (Engine::DAY / 24);
                 $match = new \Own\Bus\Match\Model();
                 $match->setTournamentId($tournament->getId());
                 $match->setSurface($tournament->getSurface());
                 $match->setBestOfSets($tournament->getBestOfSets());
                 $match->setPosition($position);
                 $playerMatch1 = new \Own\Bus\PlayerMatch\Model();
                 $playerMatch1->setPlayerId($players[$i]['id']);
                 $playerMatch1->setHasViewed($players[$i]['hasViewed']);
                 if (isset($players[$i]['seed'])) {
                     $playerMatch1->setSeed($players[$i]['seed'] + 1);
                 }
                 $playerMatch1->save();
                 if ($players[$i]['userId'] != 0) {
                     \Own\Bus\Notification\Service::create($players[$i]['id'], 0, 'tournamentStart', [['scheduledMatch', $matchTime]]);
                 }
                 $i++;
                 $playerMatch2 = new \Own\Bus\PlayerMatch\Model();
                 $playerMatch2->setPlayerId($players[$i]['id']);
                 $playerMatch2->setHasViewed($players[$i]['hasViewed']);
                 if (isset($players[$i]['seed'])) {
                     $playerMatch2->setSeed($players[$i]['seed'] + 1);
                 }
                 $playerMatch2->save();
                 if ($players[$i]['userId'] != 0) {
                     \Own\Bus\Notification\Service::create($players[$i]['id'], 0, 'tournamentStart', [['scheduledMatch', $matchTime]]);
                 }
                 $i++;
                 $match->setPlayerMatch1Id($playerMatch1->getId());
                 $match->setPlayerMatch2Id($playerMatch2->getId());
                 $match->setScheduled(date('Y-m-d H:i:s', $matchTime));
                 $match->setType(MatchType::TOURNAMENT);
                 $match->setStatus(MatchStatus::READY);
                 $match->save();
                 $position--;
             }
         } else {
             Util\Log::log(Util\Code::CRON, 'Tournament not generated, tournamentId: ' . $tournament->getId() . ', ' . count($tournament->getTournamentPlayers()) . ' / ' . $tournament->getSize(), __FILE__, __LINE__);
             return false;
         }
     }
     return true;
 }
Beispiel #2
0
 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();
     }
 }
Beispiel #3
0
 public static function cleanHasViewed()
 {
     $players = Data::loadPlayersWithOldMatches();
     foreach ($players as $player) {
         \Own\Bus\Notification\Service::create($player['player_id'], 0, 'oldMatches', [['oldMatch', $player['total']]]);
     }
     return Data::cleanHasViewed();
 }
Beispiel #4
0
 public function updateAfterMatch($isWinner, $matchId, $score, $games, $surface, $matchType, $position, $opponent)
 {
     $result = [];
     $level = $this->calculateLevel();
     $bonusDiffLevel = min(8, max(-8, round(($opponent->calculateLevel() - $level) / 4)));
     $bonusGames = ($games + 30) / 6 - 2;
     // between 5 and 14
     // experience
     $xp = 0;
     if ($matchType != MatchType::PRACTICE) {
         $xp = $bonusDiffLevel + $bonusGames + $level / 10;
         if (!$isWinner) {
             $xp /= 6;
         } else {
             if (!$this->isBonusDayXp()) {
                 $xp /= 4;
             }
         }
         if ($this->userId == 0) {
             $xp /= 2;
         }
         $xp = max(1, round($xp));
         $this->experience += $xp;
         // bonus day XP
         if ($this->isBonusDayXp() && $isWinner) {
             $this->lastMatchWon = new \Rebond\Util\DateTime();
         }
     }
     $result['levelUp'] = $level < $this->calculateLevel() ? true : false;
     $result['xp'] = $xp;
     // confidence
     $confidence = $this->confidence / -80;
     if ($isWinner) {
         $confidence += 20 + $bonusDiffLevel - $bonusGames;
     } else {
         $confidence += -15 + $bonusDiffLevel + $bonusGames;
     }
     if ($matchType == MatchType::PRACTICE) {
         $confidence = $isWinner ? $confidence / 2 : $confidence / 4;
     }
     $confidence = round($confidence);
     $this->confidence = min(1000, max(0, $this->confidence + 10 * $confidence));
     $result['confidence'] = $confidence;
     // mental
     $mental = $bonusGames + $this->mental / 50;
     if ($isWinner) {
         $mental -= 6;
     }
     if ($matchType == MatchType::PRACTICE) {
         $mental = $isWinner ? $mental / 4 : $mental / 2;
     }
     $mental = round($mental);
     $this->mental = max(0, $this->mental - 10 * $mental);
     $result['mental'] = -$mental;
     // game play
     $gameplay = 16 + $bonusDiffLevel / 2 - round($this->gameplay / 80);
     if (!$isWinner) {
         $gameplay -= 6;
     }
     if ($matchType == MatchType::PRACTICE) {
         $gameplay = $isWinner ? $gameplay / 2 : $gameplay / 4;
     }
     $gameplay = round($gameplay);
     $this->gameplay = max(0, min(1000, $this->gameplay + 10 * $gameplay));
     $result['gameplay'] = $gameplay;
     // surface
     if ($surface == Surface::CLAY) {
         $surfaceIncrease = 1 + $this->adaptation * 2 + (100 - $this->surfaceClay) / 6;
         $this->surfaceClay += round($surfaceIncrease);
         $this->surfaceGrass -= round($surfaceIncrease / 3);
         $this->surfaceHard -= round($surfaceIncrease / 3);
     } else {
         if ($surface == Surface::GRASS) {
             $surfaceIncrease = 1 + $this->adaptation * 2 + (100 - $this->surfaceGrass) / 6;
             $this->surfaceGrass += round($surfaceIncrease);
             $this->surfaceClay -= round($surfaceIncrease / 3);
             $this->surfaceHard -= round($surfaceIncrease / 3);
         } else {
             if ($surface == Surface::HARD) {
                 $surfaceIncrease = 1 + $this->adaptation * 2 + (100 - $this->surfaceHard) / 6;
                 $this->surfaceHard += round($surfaceIncrease);
                 $this->surfaceClay -= round($surfaceIncrease / 3);
                 $this->surfaceGrass -= round($surfaceIncrease / 3);
             }
         }
     }
     $this->surfaceClay = min(100, $this->surfaceClay);
     $this->surfaceClay = max(0, $this->surfaceClay);
     $this->surfaceGrass = min(100, $this->surfaceGrass);
     $this->surfaceGrass = max(0, $this->surfaceGrass);
     $this->surfaceHard = min(100, $this->surfaceHard);
     $this->surfaceHard = max(0, $this->surfaceHard);
     $total = $this->surfaceClay + $this->surfaceGrass + $this->surfaceHard;
     if ($total > 100) {
         $this->surfaceClay = round($this->surfaceClay / $total * 100);
         $this->surfaceGrass = round($this->surfaceGrass / $total * 100);
         $this->surfaceHard = round($this->surfaceHard / $total * 100);
     }
     // tournament
     if ($matchType == MatchType::TOURNAMENT && (!$isWinner || $position == 1)) {
         $this->isInTournament = false;
     }
     // league point
     $points = 0;
     if ($matchType == MatchType::LEAGUE) {
         $points = 20 + round(($opponent->getLeaguePoint() - $this->leaguePoint) / 10);
         if (!$isWinner) {
             $points = min(-2, $points - 34);
         } else {
             $points = max(2, $points);
         }
         if (abs($this->leaguePoint) >= 100) {
             $points = round($points / 2);
         }
         $this->leaguePoint += $points;
         $result['leaguePoint'] = $points;
     }
     $this->lastMatchPlayed = new \Rebond\Util\DateTime();
     $this->isInMatch = false;
     // notifications
     if ($this->getUserId() == 0) {
         return;
     }
     $info = [];
     if ($result['xp'] != 0) {
         $info[] = ['xp', $result['xp']];
     }
     $info[] = ['surface', round($surfaceIncrease), $surface];
     $info[] = ['confidence', $result['confidence']];
     $info[] = ['mental', $result['mental']];
     $info[] = ['gameplay', $result['gameplay']];
     if (isset($result['leaguePoint'])) {
         $info[] = ['leaguePoint', $result['leaguePoint']];
     }
     \Own\Bus\Notification\Service::create($this->getId(), $matchId, $matchType, $info);
     if ($result['levelUp']) {
         \Own\Bus\Notification\Service::create($this->getId(), 0, 'levelUp', [['newLevel', $this->calculateLevel()]]);
     }
     if ($this->experience < 34 * 20 && $this->getExperienceLeft() > 20 && $this->getExperienceLeft() - $result['xp'] < 20) {
         \Own\Bus\Notification\Service::create($this->getId(), 0, 'masteryPoints', [['newMasteryPoints', $this->getExperienceLeft()]]);
     }
 }