| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- include_once 'Database.php';
- include_once 'SoccerMatch.php';
- class SoccerMatchAnalysis
- {
- private SoccerMatch $match;
- private Database $database;
- private $leagueTable;
- private $leagueTableUpdated;
- public function __construct(SoccerMatch $match)
- {
- $this->match = $match;
- $this->database = new Database();
- }
- /**
- * Antalet mål som hemmalaget gjort de senaste "gamesLookback" matcherna -
- * antalet mål för bortalaget under samma period
- *
- * @param int $gamesLookback - hur många matcher bakåt i tiden som ska kontrolleras
- * @return float med skillnaden i mål mellan lagen hemma/borta
- * @throws \Exception
- */
- public function getScoringDiffLastGames(int $gamesLookback): float
- {
- $homeRes = 0;
- $awayRes = 0;
- $sql =
- 'SELECT * FROM ' .
- '(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) ht ' .
- 'UNION DISTINCT ' .
- '(SELECT * FROM SoccerResults WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?)';
- $stmt = $this->database->getConnection()->prepare($sql);
- $homeTeamId = $this->match->getHomeTeam()->getTeamId();
- $leagueId = $this->match->getHomeTeam()->getTeamLeagueId();
- $gameDate = $this->match->getGameDate();
- $awayTeamId = $this->match->getAwayTeam()->getTeamId();
- $stmt->bindParam(1, $homeTeamId, PDO::PARAM_INT);
- $stmt->bindParam(2, $leagueId, PDO::PARAM_INT);
- $stmt->bindParam(3, $gameDate);
- $stmt->bindParam(4, $gamesLookback, PDO::PARAM_INT);
- $stmt->bindParam(5, $awayTeamId, PDO::PARAM_INT);
- $stmt->bindParam(6, $leagueId, PDO::PARAM_INT);
- $stmt->bindParam(7, $gameDate);
- $stmt->bindParam(8, $gamesLookback, PDO::PARAM_INT);
- $stmt->execute();
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($rows as $row) {
- if (
- $this->match->getHomeTeam()->getTeamId() ===
- (int) $row['homeTeamId']
- ) {
- $homeRes += (int) $row['homeScore'];
- }
- if (
- $this->match->getAwayTeam()->getTeamId() ===
- (int) $row['awayTeamId']
- ) {
- $awayRes += (int) $row['awayScore'];
- }
- }
- return $homeRes - $awayRes;
- }
- /**
- *
- * Plocka fram antalet mål som ett specifikt lag gjort under de senaste
- * <gamesLookback> matcherna
- *
- * @param int $gamesLookback - hur många matcher bakåt i tiden som ska kontrolleras
- * @param bool $homeTeam - är det hemma laget som ska kontrolleras i matchen
- * @return int antalet mål som är gjorda av bestämt lag.
- */
- public function scoringTotal(int $gamesLookback, bool $homeTeam): int
- {
- $result = 0;
- $sql =
- 'SELECT * FROM ' .
- '(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) ht ' .
- 'UNION DISTINCT ' .
- '(SELECT * FROM SoccerResults WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?)';
- try {
- $stat = $this->database->getConnection()->prepare($sql);
- $team = $homeTeam
- ? $this->match->getHomeTeam()
- : $this->match->getAwayTeam();
- $stat->bindValue(1, $team->getTeamId(), PDO::PARAM_INT);
- $stat->bindValue(2, $team->getTeamLeagueId(), PDO::PARAM_INT);
- $stat->bindValue(3, $this->match->getGameDate());
- $stat->bindValue(4, $gamesLookback, PDO::PARAM_INT);
- $stat->bindValue(5, $team->getTeamId(), PDO::PARAM_INT);
- $stat->bindValue(6, $team->getTeamLeagueId(), PDO::PARAM_INT);
- $stat->bindValue(7, $this->match->getGameDate());
- $stat->bindValue(8, $gamesLookback, PDO::PARAM_INT);
- $stat->execute();
- $rs = $stat->fetchAll(PDO::FETCH_ASSOC);
- foreach ($rs as $row) {
- if ($row['homeTeamId'] == $team->getTeamId()) {
- $result += $row['homeScore'];
- } elseif ($row['awayTeamId'] == $team->getTeamId()) {
- $result += $row['awayScore'];
- }
- }
- } catch (PDOException $e) {
- echo 'Error: ' . $e->getMessage();
- }
- return $result;
- }
- /**
- * Hämta tabell positionen för hemma eller borta laget
- *
- * @param bool $homeTeam - är det hemma laget som ska kontrolleras för matchen?
- * @return int position för specifierat lag
- */
- public function getTablePosition(bool $homeTeam): int
- {
- $result = 0;
- $this->updateLeagueTable();
- $teamName = $homeTeam
- ? $this->match->getHomeTeam()->getTeamName()
- : $this->match->getAwayTeam()->getTeamName();
- $standingOptional = array_filter($this->leagueTable, function ($p) use (
- $teamName
- ) {
- return $p->getTeamName() == $teamName;
- });
- if (!empty($standingOptional)) {
- $standing = reset($standingOptional);
- $result = array_search($standing, $this->leagueTable);
- }
- return $result;
- }
- private function updateLeagueTable()
- {
- global $leagueTableUpdated, $leagueTable, $database, $match;
- if ($leagueTableUpdated != $match->getGameDate()->format('Y-m-d')) {
- $leagueTable = $database->getLeagueTable(
- $match->getHomeTeam()->getTeamLeagueId(),
- $match->getSeason(),
- $match->getHomeTeam()->getCountryId(),
- $match->getGameDate()->format('Y-m-d')
- );
- $leagueTableUpdated = $match->getGameDate()->format('Y-m-d');
- }
- }
- /**
- *
- * @return Integer - hur många platser det är mellan hemma och borta laget
- */
- public function diffInStanding()
- {
- global $leagueTable, $match;
- $result = 0;
- $this->updateLeagueTable();
- $homeTeamStandingOptional = array_values(
- array_filter($leagueTable, function ($p) use ($match) {
- return $p->getTeamName() ==
- $match->getHomeTeam()->getTeamName();
- })
- );
- $awayTeamStandingOptional = array_values(
- array_filter($leagueTable, function ($p) use ($match) {
- return $p->getTeamName() ==
- $match->getAwayTeam()->getTeamName();
- })
- );
- if (
- !empty($homeTeamStandingOptional) &&
- !empty($awayTeamStandingOptional)
- ) {
- $homeStanding = $homeTeamStandingOptional[0];
- $awayStanding = $awayTeamStandingOptional[0];
- $result =
- array_search($homeStanding, $leagueTable) -
- array_search($awayStanding, $leagueTable);
- }
- return $result;
- }
- /**
- *
- * Vinst förlust ratio för om man är enbart hemma eller bortalag.
- *
- * @param gamesLookback
- * @param homeTeam
- * @return int där vinst ger +1 lika ger 0 och förlust get -1
- */
- public function winLossRatio($gamesLookback, $homeTeam)
- {
- global $match, $database;
- $result = 0;
- $team = $homeTeam
- ? $this->match->getHomeTeam()
- : $this->match->getAwayTeam();
- $teamSql = $homeTeam ? 'homeTeamId = ? ' : 'awayTeamId = ? ';
- $sql =
- 'SELECT * FROM SoccerResults WHERE ' .
- $teamSql .
- 'AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?';
- $stmt = $database->getConnection()->prepare($sql);
- $stmt->bindValue(1, $team->getTeamId(), PDO::PARAM_INT);
- $stmt->bindValue(2, $team->getTeamLeagueId(), PDO::PARAM_INT);
- $stmt->bindValue(3, $this->match->getGameDate(), PDO::PARAM_STR);
- $stmt->bindValue(4, $gamesLookback, PDO::PARAM_INT);
- $stmt->execute();
- while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- $homeScore = $row['homeScore'];
- $awayScore = $row['awayScore'];
- if ($homeTeam) {
- if ($homeScore > $awayScore) {
- $result++;
- } elseif ($homeScore < $awayScore) {
- $result--;
- }
- } else {
- if ($homeScore > $awayScore) {
- $result--;
- } elseif ($homeScore < $awayScore) {
- $result++;
- }
- }
- }
- return $result;
- }
- public function winLossRatioHomeAndAway($homeTeam, $gamesLookback)
- {
- $result = 0;
- $sql = 'SELECT gameDate, homeTeamId, awayTeamId, homeScore, awayScore, leagueId
- FROM
- (SELECT gameDate, homeTeamId, awayTeamId, homeScore, awayScore, leagueId
- FROM SoccerResults
- WHERE homeTeamId = ? AND leagueId = ? AND gameDate < ?
- ORDER BY gameDate DESC
- LIMIT ?) a
- UNION ALL
- SELECT gameDate, homeTeamId, awayTeamId, homeScore, awayScore, leagueId
- FROM SoccerResults
- WHERE awayTeamId = ? AND leagueId = ? AND gameDate < ?
- ORDER BY gameDate DESC
- LIMIT ?';
- $team = $homeTeam
- ? $this->match->getHomeTeam()
- : $this->match->getAwayTeam();
- try {
- $teamId = $team->getTeamId();
- $leagueId = $team->getTeamLeagueId();
- $gameDate = $this->match->getGameDate();
- $stat = $this->database->getConnection()->prepare($sql);
- $stat->bindParam(1, $teamId, PDO::PARAM_INT);
- $stat->bindParam(2, $leagueId, PDO::PARAM_INT);
- $stat->bindParam(3, $gameDate, PDO::PARAM_STR);
- $stat->bindParam(4, $gamesLookback, PDO::PARAM_INT);
- $stat->bindParam(5, $teamId, PDO::PARAM_INT);
- $stat->bindParam(6, $leagueId, PDO::PARAM_INT);
- $stat->bindParam(7, $gameDate, PDO::PARAM_STR);
- $stat->bindParam(8, $gamesLookback, PDO::PARAM_INT);
- $stat->execute();
- $rs = $stat->fetchAll(PDO::FETCH_ASSOC);
- foreach ($rs as $row) {
- if (
- $row['homeTeamId'] == $team->getTeamId() &&
- $row['homeScore'] > $row['awayScore']
- ) {
- $result++;
- } elseif (
- $row['awayTeamId'] == $team->getTeamId() &&
- $row['awayScore'] > $row['homeScore']
- ) {
- $result++;
- } else {
- $result--;
- }
- }
- } catch (PDOException $e) {
- echo $e->getMessage();
- }
- return $result;
- }
- public function goalsScoredHomeAndAway($homeTeam, $gameLookback)
- {
- $result = 0;
- $team = $homeTeam
- ? $this->match->getHomeTeam()
- : $this->match->getAwayTeam();
- $sql =
- 'SELECT * FROM ' .
- '(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) a ' .
- 'UNION ALL ' .
- '(SELECT * FROM SoccerResults WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) ' .
- 'ORDER BY gameDate DESC ' .
- 'LIMIT ?';
- try {
- $stat = $this->database->getConnection()->prepare($sql);
- $stat->bindParam(1, $team->getTeamId());
- $stat->bindParam(2, $team->getTeamLeagueId());
- $stat->bindParam(3, $this->match->getGameDate());
- $stat->bindParam(4, $gameLookback, PDO::PARAM_INT);
- $stat->bindParam(5, $team->getTeamId());
- $stat->bindParam(6, $team->getTeamLeagueId());
- $stat->bindParam(7, $this->match->getGameDate());
- $stat->bindParam(8, $gameLookback, PDO::PARAM_INT);
- $stat->bindParam(9, $gameLookback, PDO::PARAM_INT);
- $stat->execute();
- while ($row = $stat->fetch(PDO::FETCH_ASSOC)) {
- if ($row['homeTeamId'] == $team->getTeamId()) {
- $result += $row['homeScore'];
- }
- if ($row['awayTeamId'] == $team->getTeamId()) {
- $result += $row['awayScore'];
- }
- }
- } catch (PDOException $e) {
- $e->getMessage();
- }
- return $result;
- }
- public function goalsScoredHomeOrAway($homeTeam, $gameLookback)
- {
- $result = 0;
- $team = $homeTeam
- ? $this->match->getHomeTeam()
- : $this->match->getAwayTeam();
- $homeOrAway = $homeTeam ? 'home' : 'away';
- $sql =
- 'SELECT SUM(' .
- $homeOrAway .
- 'Score) FROM SoccerResults WHERE ' .
- $homeOrAway .
- 'TeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ? ' .
- 'ORDER BY gameDate DESC';
- try {
- $stat = $this->database->getConnection()->prepare($sql);
- $stat->bindParam(1, $team->getTeamId());
- $stat->bindParam(2, $team->getTeamLeagueId());
- $stat->bindParam(3, $this->match->getGameDate());
- $stat->bindParam(4, $gameLookback, PDO::PARAM_INT);
- $stat->execute();
- while ($row = $stat->fetch(PDO::FETCH_ASSOC)) {
- $result += $row[$homeOrAway . 'Score'];
- }
- } catch (PDOException $e) {
- $e->getMessage();
- }
- return $result;
- }
- }
|