SoccerMatchAnalysis.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. include_once 'Database.php';
  3. include_once 'SoccerMatch.php';
  4. class SoccerMatchAnalysis
  5. {
  6. private SoccerMatch $match;
  7. private Database $database;
  8. private $leagueTable;
  9. private $leagueTableUpdated;
  10. public function __construct(SoccerMatch $match)
  11. {
  12. $this->match = $match;
  13. $this->database = new Database();
  14. }
  15. /**
  16. * Antalet mål som hemmalaget gjort de senaste "gamesLookback" matcherna -
  17. * antalet mål för bortalaget under samma period
  18. *
  19. * @param int $gamesLookback - hur många matcher bakåt i tiden som ska kontrolleras
  20. * @return float med skillnaden i mål mellan lagen hemma/borta
  21. * @throws \Exception
  22. */
  23. public function getScoringDiffLastGames(int $gamesLookback): float
  24. {
  25. $homeRes = 0;
  26. $awayRes = 0;
  27. $sql =
  28. 'SELECT * FROM ' .
  29. '(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) ht ' .
  30. 'UNION DISTINCT ' .
  31. '(SELECT * FROM SoccerResults WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?)';
  32. $stmt = $this->database->getConnection()->prepare($sql);
  33. $homeTeamId = $this->match->getHomeTeam()->getTeamId();
  34. $leagueId = $this->match->getHomeTeam()->getTeamLeagueId();
  35. $gameDate = $this->match->getGameDate();
  36. $awayTeamId = $this->match->getAwayTeam()->getTeamId();
  37. $stmt->bindParam(1, $homeTeamId, PDO::PARAM_INT);
  38. $stmt->bindParam(2, $leagueId, PDO::PARAM_INT);
  39. $stmt->bindParam(3, $gameDate);
  40. $stmt->bindParam(4, $gamesLookback, PDO::PARAM_INT);
  41. $stmt->bindParam(5, $awayTeamId, PDO::PARAM_INT);
  42. $stmt->bindParam(6, $leagueId, PDO::PARAM_INT);
  43. $stmt->bindParam(7, $gameDate);
  44. $stmt->bindParam(8, $gamesLookback, PDO::PARAM_INT);
  45. $stmt->execute();
  46. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  47. foreach ($rows as $row) {
  48. if (
  49. $this->match->getHomeTeam()->getTeamId() ===
  50. (int) $row['homeTeamId']
  51. ) {
  52. $homeRes += (int) $row['homeScore'];
  53. }
  54. if (
  55. $this->match->getAwayTeam()->getTeamId() ===
  56. (int) $row['awayTeamId']
  57. ) {
  58. $awayRes += (int) $row['awayScore'];
  59. }
  60. }
  61. return $homeRes - $awayRes;
  62. }
  63. /**
  64. *
  65. * Plocka fram antalet mål som ett specifikt lag gjort under de senaste
  66. * <gamesLookback> matcherna
  67. *
  68. * @param int $gamesLookback - hur många matcher bakåt i tiden som ska kontrolleras
  69. * @param bool $homeTeam - är det hemma laget som ska kontrolleras i matchen
  70. * @return int antalet mål som är gjorda av bestämt lag.
  71. */
  72. public function scoringTotal(int $gamesLookback, bool $homeTeam): int
  73. {
  74. $result = 0;
  75. $sql =
  76. 'SELECT * FROM ' .
  77. '(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) ht ' .
  78. 'UNION DISTINCT ' .
  79. '(SELECT * FROM SoccerResults WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?)';
  80. try {
  81. $stat = $this->database->getConnection()->prepare($sql);
  82. $team = $homeTeam
  83. ? $this->match->getHomeTeam()
  84. : $this->match->getAwayTeam();
  85. $stat->bindValue(1, $team->getTeamId(), PDO::PARAM_INT);
  86. $stat->bindValue(2, $team->getTeamLeagueId(), PDO::PARAM_INT);
  87. $stat->bindValue(3, $this->match->getGameDate());
  88. $stat->bindValue(4, $gamesLookback, PDO::PARAM_INT);
  89. $stat->bindValue(5, $team->getTeamId(), PDO::PARAM_INT);
  90. $stat->bindValue(6, $team->getTeamLeagueId(), PDO::PARAM_INT);
  91. $stat->bindValue(7, $this->match->getGameDate());
  92. $stat->bindValue(8, $gamesLookback, PDO::PARAM_INT);
  93. $stat->execute();
  94. $rs = $stat->fetchAll(PDO::FETCH_ASSOC);
  95. foreach ($rs as $row) {
  96. if ($row['homeTeamId'] == $team->getTeamId()) {
  97. $result += $row['homeScore'];
  98. } elseif ($row['awayTeamId'] == $team->getTeamId()) {
  99. $result += $row['awayScore'];
  100. }
  101. }
  102. } catch (PDOException $e) {
  103. echo 'Error: ' . $e->getMessage();
  104. }
  105. return $result;
  106. }
  107. /**
  108. * Hämta tabell positionen för hemma eller borta laget
  109. *
  110. * @param bool $homeTeam - är det hemma laget som ska kontrolleras för matchen?
  111. * @return int position för specifierat lag
  112. */
  113. public function getTablePosition(bool $homeTeam): int
  114. {
  115. $result = 0;
  116. $this->updateLeagueTable();
  117. $teamName = $homeTeam
  118. ? $this->match->getHomeTeam()->getTeamName()
  119. : $this->match->getAwayTeam()->getTeamName();
  120. $standingOptional = array_filter($this->leagueTable, function ($p) use (
  121. $teamName
  122. ) {
  123. return $p->getTeamName() == $teamName;
  124. });
  125. if (!empty($standingOptional)) {
  126. $standing = reset($standingOptional);
  127. $result = array_search($standing, $this->leagueTable);
  128. }
  129. return $result;
  130. }
  131. private function updateLeagueTable()
  132. {
  133. global $leagueTableUpdated, $leagueTable, $database, $match;
  134. if ($leagueTableUpdated != $match->getGameDate()->format('Y-m-d')) {
  135. $leagueTable = $database->getLeagueTable(
  136. $match->getHomeTeam()->getTeamLeagueId(),
  137. $match->getSeason(),
  138. $match->getHomeTeam()->getCountryId(),
  139. $match->getGameDate()->format('Y-m-d')
  140. );
  141. $leagueTableUpdated = $match->getGameDate()->format('Y-m-d');
  142. }
  143. }
  144. /**
  145. *
  146. * @return Integer - hur många platser det är mellan hemma och borta laget
  147. */
  148. public function diffInStanding()
  149. {
  150. global $leagueTable, $match;
  151. $result = 0;
  152. $this->updateLeagueTable();
  153. $homeTeamStandingOptional = array_values(
  154. array_filter($leagueTable, function ($p) use ($match) {
  155. return $p->getTeamName() ==
  156. $match->getHomeTeam()->getTeamName();
  157. })
  158. );
  159. $awayTeamStandingOptional = array_values(
  160. array_filter($leagueTable, function ($p) use ($match) {
  161. return $p->getTeamName() ==
  162. $match->getAwayTeam()->getTeamName();
  163. })
  164. );
  165. if (
  166. !empty($homeTeamStandingOptional) &&
  167. !empty($awayTeamStandingOptional)
  168. ) {
  169. $homeStanding = $homeTeamStandingOptional[0];
  170. $awayStanding = $awayTeamStandingOptional[0];
  171. $result =
  172. array_search($homeStanding, $leagueTable) -
  173. array_search($awayStanding, $leagueTable);
  174. }
  175. return $result;
  176. }
  177. /**
  178. *
  179. * Vinst förlust ratio för om man är enbart hemma eller bortalag.
  180. *
  181. * @param gamesLookback
  182. * @param homeTeam
  183. * @return int där vinst ger +1 lika ger 0 och förlust get -1
  184. */
  185. public function winLossRatio($gamesLookback, $homeTeam)
  186. {
  187. global $match, $database;
  188. $result = 0;
  189. $team = $homeTeam
  190. ? $this->match->getHomeTeam()
  191. : $this->match->getAwayTeam();
  192. $teamSql = $homeTeam ? 'homeTeamId = ? ' : 'awayTeamId = ? ';
  193. $sql =
  194. 'SELECT * FROM SoccerResults WHERE ' .
  195. $teamSql .
  196. 'AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?';
  197. $stmt = $database->getConnection()->prepare($sql);
  198. $stmt->bindValue(1, $team->getTeamId(), PDO::PARAM_INT);
  199. $stmt->bindValue(2, $team->getTeamLeagueId(), PDO::PARAM_INT);
  200. $stmt->bindValue(3, $this->match->getGameDate(), PDO::PARAM_STR);
  201. $stmt->bindValue(4, $gamesLookback, PDO::PARAM_INT);
  202. $stmt->execute();
  203. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  204. $homeScore = $row['homeScore'];
  205. $awayScore = $row['awayScore'];
  206. if ($homeTeam) {
  207. if ($homeScore > $awayScore) {
  208. $result++;
  209. } elseif ($homeScore < $awayScore) {
  210. $result--;
  211. }
  212. } else {
  213. if ($homeScore > $awayScore) {
  214. $result--;
  215. } elseif ($homeScore < $awayScore) {
  216. $result++;
  217. }
  218. }
  219. }
  220. return $result;
  221. }
  222. public function winLossRatioHomeAndAway($homeTeam, $gamesLookback)
  223. {
  224. $result = 0;
  225. $sql = 'SELECT gameDate, homeTeamId, awayTeamId, homeScore, awayScore, leagueId
  226. FROM
  227. (SELECT gameDate, homeTeamId, awayTeamId, homeScore, awayScore, leagueId
  228. FROM SoccerResults
  229. WHERE homeTeamId = ? AND leagueId = ? AND gameDate < ?
  230. ORDER BY gameDate DESC
  231. LIMIT ?) a
  232. UNION ALL
  233. SELECT gameDate, homeTeamId, awayTeamId, homeScore, awayScore, leagueId
  234. FROM SoccerResults
  235. WHERE awayTeamId = ? AND leagueId = ? AND gameDate < ?
  236. ORDER BY gameDate DESC
  237. LIMIT ?';
  238. $team = $homeTeam
  239. ? $this->match->getHomeTeam()
  240. : $this->match->getAwayTeam();
  241. try {
  242. $teamId = $team->getTeamId();
  243. $leagueId = $team->getTeamLeagueId();
  244. $gameDate = $this->match->getGameDate();
  245. $stat = $this->database->getConnection()->prepare($sql);
  246. $stat->bindParam(1, $teamId, PDO::PARAM_INT);
  247. $stat->bindParam(2, $leagueId, PDO::PARAM_INT);
  248. $stat->bindParam(3, $gameDate, PDO::PARAM_STR);
  249. $stat->bindParam(4, $gamesLookback, PDO::PARAM_INT);
  250. $stat->bindParam(5, $teamId, PDO::PARAM_INT);
  251. $stat->bindParam(6, $leagueId, PDO::PARAM_INT);
  252. $stat->bindParam(7, $gameDate, PDO::PARAM_STR);
  253. $stat->bindParam(8, $gamesLookback, PDO::PARAM_INT);
  254. $stat->execute();
  255. $rs = $stat->fetchAll(PDO::FETCH_ASSOC);
  256. foreach ($rs as $row) {
  257. if (
  258. $row['homeTeamId'] == $team->getTeamId() &&
  259. $row['homeScore'] > $row['awayScore']
  260. ) {
  261. $result++;
  262. } elseif (
  263. $row['awayTeamId'] == $team->getTeamId() &&
  264. $row['awayScore'] > $row['homeScore']
  265. ) {
  266. $result++;
  267. } else {
  268. $result--;
  269. }
  270. }
  271. } catch (PDOException $e) {
  272. echo $e->getMessage();
  273. }
  274. return $result;
  275. }
  276. public function goalsScoredHomeAndAway($homeTeam, $gameLookback)
  277. {
  278. $result = 0;
  279. $team = $homeTeam
  280. ? $this->match->getHomeTeam()
  281. : $this->match->getAwayTeam();
  282. $sql =
  283. 'SELECT * FROM ' .
  284. '(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) a ' .
  285. 'UNION ALL ' .
  286. '(SELECT * FROM SoccerResults WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) ' .
  287. 'ORDER BY gameDate DESC ' .
  288. 'LIMIT ?';
  289. try {
  290. $stat = $this->database->getConnection()->prepare($sql);
  291. $stat->bindParam(1, $team->getTeamId());
  292. $stat->bindParam(2, $team->getTeamLeagueId());
  293. $stat->bindParam(3, $this->match->getGameDate());
  294. $stat->bindParam(4, $gameLookback, PDO::PARAM_INT);
  295. $stat->bindParam(5, $team->getTeamId());
  296. $stat->bindParam(6, $team->getTeamLeagueId());
  297. $stat->bindParam(7, $this->match->getGameDate());
  298. $stat->bindParam(8, $gameLookback, PDO::PARAM_INT);
  299. $stat->bindParam(9, $gameLookback, PDO::PARAM_INT);
  300. $stat->execute();
  301. while ($row = $stat->fetch(PDO::FETCH_ASSOC)) {
  302. if ($row['homeTeamId'] == $team->getTeamId()) {
  303. $result += $row['homeScore'];
  304. }
  305. if ($row['awayTeamId'] == $team->getTeamId()) {
  306. $result += $row['awayScore'];
  307. }
  308. }
  309. } catch (PDOException $e) {
  310. $e->getMessage();
  311. }
  312. return $result;
  313. }
  314. public function goalsScoredHomeOrAway($homeTeam, $gameLookback)
  315. {
  316. $result = 0;
  317. $team = $homeTeam
  318. ? $this->match->getHomeTeam()
  319. : $this->match->getAwayTeam();
  320. $homeOrAway = $homeTeam ? 'home' : 'away';
  321. $sql =
  322. 'SELECT SUM(' .
  323. $homeOrAway .
  324. 'Score) FROM SoccerResults WHERE ' .
  325. $homeOrAway .
  326. 'TeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ? ' .
  327. 'ORDER BY gameDate DESC';
  328. try {
  329. $stat = $this->database->getConnection()->prepare($sql);
  330. $stat->bindParam(1, $team->getTeamId());
  331. $stat->bindParam(2, $team->getTeamLeagueId());
  332. $stat->bindParam(3, $this->match->getGameDate());
  333. $stat->bindParam(4, $gameLookback, PDO::PARAM_INT);
  334. $stat->execute();
  335. while ($row = $stat->fetch(PDO::FETCH_ASSOC)) {
  336. $result += $row[$homeOrAway . 'Score'];
  337. }
  338. } catch (PDOException $e) {
  339. $e->getMessage();
  340. }
  341. return $result;
  342. }
  343. }