public static function getForecastedStandings($id_tournament, $id_user) { //getting list of games for the tournament $games = Games::getGamesForTournament($id_tournament); $gameIDs = array_map(function ($item) { return $item->id_game; }, $games); //getting forecasts for the user for the tournament $forecasts = Forecasts::find()->where(['id_game' => $gameIDs, 'id_user' => $id_user])->indexBy('id_game')->asArray()->all(); //if there's a forecast for the game -> put forecast score as a game score + recalculating points foreach ($games as &$game) { if (isset($forecasts[$game->id_game])) { $game->score_home = $forecasts[$game->id_game]['fscore_home']; $game->score_guest = $forecasts[$game->id_game]['fscore_guest']; $game->getGamePoints(); } } $games = ArrayHelper::toArray($games); $teams = array_unique(array_merge(array_map(function ($value) { return $value['id_team_home']; }, $games), array_map(function ($value) { return $value['id_team_guest']; }, $games))); $teamObjects = TeamTournaments::find()->where(['id' => $teams])->joinWith('idTeam')->indexBy('id')->all(); $result = array_map(function ($item) use($games, $teamObjects) { $row['pts'] = 0; $row['participant'] = $item; $row['id'] = $item; $row['team_logo'] = $teamObjects[$item]->idTeam->team_logo; $row['team_name'] = $teamObjects[$item]->idTeam->team_name; $row['games_played'] = 0; foreach ($games as $game) { if ($game['id_team_home'] == $item) { $row['pts'] += $game['points_home']; if (isset($game['points_home'])) { $row['games_played'] += 1; } } if ($game['id_team_guest'] == $item) { $row['pts'] += $game['points_guest']; if (isset($game['points_guest'])) { $row['games_played'] += 1; } } } return $row; }, $teams); usort($result, function ($a, $b) { return $b['pts'] - $a['pts']; }); return $result; }
public function actionToursWithWrongNumberGames() { $tournaments = Tournaments::getAutoprocessTournaments(); foreach ($tournaments as $one) { $games = Games::getGamesForTournament($one->id_tournament); $gamesPerTour = array_fill(1, $one->num_tours, 0); array_walk($games, function ($item) use(&$gamesPerTour) { $gamesPerTour[$item->tour]++; }); $message = ''; foreach ($gamesPerTour as $k => $element) { if ($element != $one->num_tours / 4 + 1 / 2) { $message .= $one->tournament_name . ' ' . $k . ' tour has wrong number of games' . "\r \n"; } } if (strlen($message) > 0) { Yii::error($message, 'console'); } } }