public function up() { $this->addColumn('{{%users_tournaments}}', 'points', $this->integer()->defaultValue(NULL)); $tournaments = UsersTournaments::find()->all(); foreach ($tournaments as $one) { $games = ArrayHelper::getColumn(Result::find()->select('id_game')->where(['id_tournament' => $one->id_tournament])->all(), 'id_game'); $points = Forecasts::find()->select(['sum(points) as points'])->joinWith('idUser')->where(['in', 'id_game', $games])->andWhere(['id_user' => $one->id_user])->scalar(); $one->points = $points; $one->save(false); } }
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; }
protected function matchWebDB() { foreach ($this->gamesFromDB as $gameDB) { foreach ($this->gamesFromWeb as $k => $gameWeb) { if ($gameWeb['tour'] == $gameDB->tour && $gameWeb['id_team_home'] == $gameDB->id_team_home && $gameWeb['id_team_guest'] == $gameDB->id_team_guest) { if ($gameDB->date_time_game != $gameWeb['date_time_game']) { $gameDB->date_time_game = $gameWeb['date_time_game']; } if ($gameDB->score_home !== $gameWeb['score_home']) { $gameDB->score_home = $gameWeb['score_home']; } if ($gameDB->score_guest !== $gameWeb['score_guest']) { $gameDB->score_guest = $gameWeb['score_guest']; } $dirtyAttr = $gameDB->getDirtyAttributes(); if (!empty($dirtyAttr)) { $gameDB->save(false); } $unset = ArrayHelper::remove($this->gamesFromWeb, $k); continue; } //if home and guest switched if ($gameWeb['tour'] == $gameDB->tour && $gameWeb['id_team_home'] == $gameDB->id_team_guest && $gameWeb['id_team_guest'] == $gameDB->id_team_home) { $gameDB->id_team_home = $gameWeb['id_team_home']; $gameDB->id_team_guest = $gameWeb['id_team_guest']; if ($gameDB->date_time_game != $gameWeb['date_time_game']) { $gameDB->date_time_game = $gameWeb['date_time_game']; } //need to switch forecast home <=> guest $forecasts = Forecasts::find()->where(['id_game' => $gameDB->id_game])->all(); foreach ($forecasts as $forecast) { $temp = $forecast->fscore_home; $forecast->fscore_home = $forecast->fscore_guest; $forecast->fscore_guest = $temp; $forecast->save(false); } $gameDB->save(false); $unset = ArrayHelper::remove($this->gamesFromWeb, $k); continue; } } } }
private static function getRecentGamesGroupedByTourWithForecast($tournament, $user) { $trn = Tournaments::findOne($tournament); //first we need to get the ids of teams $id_teams = ArrayHelper::getColumn(TeamTournaments::find()->where(['id_tournament' => $tournament])->all(), 'id'); $teamsCount = count($id_teams); $games = self::find()->with('idTeamHome.idTeam', 'idTeamGuest.idTeam')->where(['or', ['in', 'id_team_home', $id_teams], ['in', 'id_team_guest', $id_teams]])->andWhere(['<', 'date_time_game', time()])->orderBy(['tour' => SORT_DESC])->addOrderBy(['date_time_game' => SORT_DESC])->limit($teamsCount)->indexBy('id_game')->asArray()->all(); $forecasts = Forecasts::find()->where(['in', 'id_game', ArrayHelper::getColumn($games, 'id_game')])->andWhere(['id_user' => $user])->indexBy('id_game')->asArray()->all(); $tours = array_unique(ArrayHelper::getColumn($games, 'tour')); $return = []; foreach ($tours as $tour) { foreach ($games as $k => $game) { if ($game['tour'] == $tour) { $return[$tour][$k] = ArrayHelper::remove($games, $k); if (isset($forecasts[$k])) { $return[$tour][$k]['fpoints'] = $forecasts[$k]['points']; $return[$tour][$k]['id_user_forecast'] = $forecasts[$k]['id_user']; $return[$tour][$k]['fscore_home'] = $forecasts[$k]['fscore_home']; $return[$tour][$k]['fscore_guest'] = $forecasts[$k]['fscore_guest']; $return[$tour][$k]['f_id'] = $forecasts[$k]['id']; $return[$tour][$k]['f_date'] = $forecasts[$k]['date']; } } } } $dataProvider = []; foreach ($return as $k => $one) { $dataProvider[$k] = new ArrayDataProvider(['allModels' => $one]); } return $dataProvider; }