Database.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. include_once 'Team.php';
  3. include_once 'SoccerMatch.php';
  4. include_once 'League.php';
  5. include_once 'Bet.php';
  6. class Database
  7. {
  8. private $conn;
  9. function openConnection()
  10. {
  11. $dbhost = 'nordh.xyz';
  12. $dbuser = 'OddsNy';
  13. $dbPass = 'Odds1_Ny_Password';
  14. $db = 'new_odds';
  15. try {
  16. $this->conn = new PDO(
  17. "mysql:host=$dbhost;dbname=$db",
  18. $dbuser,
  19. $dbPass
  20. );
  21. $this->conn->setAttribute(
  22. PDO::ATTR_ERRMODE,
  23. PDO::ERRMODE_EXCEPTION
  24. );
  25. } catch (Exception $e) {
  26. echo 'Exception ' . $e->getMessage();
  27. }
  28. }
  29. function closeConnection($conn)
  30. {
  31. $conn = null;
  32. }
  33. function getConnection()
  34. {
  35. if (!isset($this->conn)) {
  36. $this->openConnection();
  37. }
  38. return $this->conn;
  39. }
  40. function getTodaysPrioMatches()
  41. {
  42. $this->openConnection();
  43. $result = [];
  44. $sql = 'SELECT sr.*,
  45. ht.id as homeTeamId, ht.name as homeTeamName, ht.countryId as homeTeamCountryId, ht.leagueId as homeTeamLeagueId,
  46. awt.id as awayTeamId, awt.name as awayTeamName, awt.countryId as awayTeamCountryId, awt.leagueId as awayTeamLeagueId,
  47. l.name as leagueName,
  48. c.name as countryName
  49. FROM SoccerResults sr
  50. INNER JOIN Team ht on ht.id = sr.homeTeamId
  51. INNER JOIN Team awt on awt.id = sr.awayTeamId
  52. INNER JOIN League l ON l.id = sr.leagueId
  53. INNER JOIN Country c ON c.id = sr.countryId
  54. WHERE sr.leagueId IN (SELECT id FROM League WHERE prio = 1) AND DATE(gameDate) = DATE(NOW())';
  55. $stat = $this->conn->query($sql);
  56. $matches = $stat->fetchAll();
  57. foreach ($matches as $match) {
  58. $homeTeam = new Team(
  59. $match['homeTeamId'],
  60. $match['homeTeamName'],
  61. $match['homeTeamCountryId'],
  62. $match['countryName'],
  63. $match['homeTeamLeagueId'],
  64. $match['leagueName']
  65. );
  66. $awayTeam = new Team(
  67. $match['awayTeamId'],
  68. $match['awayTeamName'],
  69. $match['awayTeamCountryId'],
  70. $match['countryName'],
  71. $match['awayTeamLeagueId'],
  72. $match['leagueName']
  73. );
  74. $m = new SoccerMatch(
  75. $match['id'],
  76. $homeTeam,
  77. $awayTeam,
  78. $match['odds1'],
  79. $match['oddsX'],
  80. $match['odds2'],
  81. $match['homeScore'],
  82. $match['awayScore'],
  83. $match['gameDate'],
  84. $match['season']
  85. );
  86. $result[] = $m;
  87. }
  88. $this->closeConnection($this->conn);
  89. return $result;
  90. }
  91. public function getLeagueTable($leagueId, $season, $countryId, $date)
  92. {
  93. $this->openConnection();
  94. $result = [];
  95. $sql = "SELECT teamName.name AS teamName,
  96. COUNT(*) AS played,
  97. SUM(CASE WHEN homeScore > awayScore THEN 1 ELSE 0 END) AS wins,
  98. SUM(CASE WHEN awayScore > homeScore THEN 1 ELSE 0 END) AS lost,
  99. SUM(CASE WHEN homeScore = awayScore THEN 1 ELSE 0 END) AS draws,
  100. SUM(homeScore) AS homeScore,
  101. SUM(awayScore) AS awayScore,
  102. SUM(homeScore - awayScore) AS goal_diff,
  103. SUM(CASE WHEN homeScore > awayScore THEN 3 ELSE 0 END + CASE WHEN homeScore = awayScore THEN 1 ELSE 0 END) AS score,
  104. season
  105. FROM
  106. (SELECT hometeamId AS team,
  107. homeScore,
  108. awayScore,
  109. season,
  110. gameDate,
  111. leagueId AS league,
  112. countryId AS country
  113. FROM SoccerResults
  114. WHERE homeScore != -1 AND awayScore != -1 AND DATE(gameDate) < DATE(?)
  115. UNION ALL
  116. SELECT awayteamId AS team,
  117. awayScore,
  118. homeScore,
  119. season,
  120. gameDate,
  121. leagueId AS league,
  122. countryId AS country
  123. FROM SoccerResults
  124. WHERE homeScore != -1 AND awayScore != -1 AND DATE(gameDate) < DATE(?)) a
  125. INNER JOIN Team AS teamName ON a.team = teamName.id
  126. WHERE season = ? AND league = ? AND country = ?
  127. GROUP BY teamName.id
  128. ORDER BY score DESC, goal_diff DESC;";
  129. $stmt = $this->conn->prepare($sql);
  130. $stmt->bindParam(1, $season);
  131. $stmt->bindParam(2, $leagueId);
  132. $stmt->bindParam(3, $countryId);
  133. $stmt->bindParam(4, $date);
  134. $stmt->execute();
  135. while ($row = $stmt->fetch()) {
  136. $ts = [
  137. 'teamName' => $row['teamName'],
  138. 'wins' => $row['wins'],
  139. 'lost' => $row['lost'],
  140. 'draws' => $row['draws'],
  141. 'score' => $row['score'],
  142. 'homeScore' => $row['homeScore'],
  143. 'awayScore' => $row['awayScore'],
  144. 'goal_diff' => $row['goal_diff'],
  145. ];
  146. array_push($result, $ts);
  147. }
  148. $this->closeConnection($this->conn);
  149. return $result;
  150. }
  151. function getLeagueInfo($leagueId)
  152. {
  153. $this->openConnection();
  154. $sql = 'SELECT * FROM League WHERE id = ?';
  155. $result = null;
  156. try {
  157. $stat = $this->conn->prepare($sql);
  158. $stat->bindParam(1, $leagueId);
  159. $stat->execute();
  160. while ($row = $stat->fetch()) {
  161. $result = new League(
  162. $row['id'],
  163. $row['name'],
  164. $row['scoringDiffLastGame'],
  165. $row['scoringTotal'],
  166. $row['winLossRatioHomeAndAway'],
  167. $row['winLossRatio'],
  168. $row['drawDiffHomeAway'],
  169. $row['drawDiffTotalGoals'],
  170. $row['drawWinningForm'],
  171. $row['drawWinningFormHomeAway']
  172. );
  173. }
  174. } catch (PDOException $e) {
  175. echo 'Connection failed: ' . $e->getMessage();
  176. }
  177. $this->closeConnection($this->conn);
  178. return $result;
  179. }
  180. public function getOutstandingBets()
  181. {
  182. $result = null;
  183. $this->openConnection();
  184. $sql = 'SELECT sr.gameDate as gameDate, l.name as leagueName,
  185. ht.name as homeTeamName, awt.name as awayTeamName,
  186. abt.bet as betOn, abt.betAmount as betAmount, abt.betOdds as betOdds, abt.status as betStatus
  187. FROM AnalysisBetTable abt
  188. INNER JOIN SoccerResults sr ON abt.matchId = sr.id
  189. INNER JOIN League l ON l.id = sr.leagueId
  190. INNER JOIN Team ht on ht.id = sr.homeTeamId
  191. INNER JOIN Team awt on awt.id = sr.awayTeamId
  192. WHERE status IN ("LOST", "OPEN", "COVERED")';
  193. try {
  194. $stat = $this->conn->prepare($sql);
  195. $stat->execute();
  196. while ($row = $stat->fetch()) {
  197. $result[] = new Bet(
  198. $row['gameDate'],
  199. $row['leagueName'],
  200. $row['homeTeamName'] . '-' . $row['awayTeamName'],
  201. $row['betOn'],
  202. $row['betAmount'],
  203. $row['betOdds'],
  204. $row['betStatus']
  205. );
  206. }
  207. } catch (Exception $e) {
  208. echo 'Getting outstanding bets failed ' . $e->getMessage();
  209. }
  210. $this->closeConnection($this->conn);
  211. return $result;
  212. }
  213. }