webDbConnection.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. mysqli_report( MYSQLI_REPORT_STRICT );
  3. require_once __DIR__ . '/dbFunctions.php';
  4. class WebDbConnection {
  5. private $dbCon;
  6. private $analyzerConnection;
  7. private $today;
  8. private $leagueName;
  9. public function __construct() {
  10. $this->dbCon = new dbFunctions();
  11. }
  12. private function getSqlAsArray($sql) {
  13. $mysql = $this->dbCon->getConnection();
  14. $result = $mysql->query( $sql );
  15. if ($result === false) {
  16. echo $mysql->error . " from sql " . $sql;
  17. return false;
  18. }
  19. $returnArray = array ();
  20. if (strpos( $sql, "UPDATE" ) === false) {
  21. foreach ( $result as $key => $value ) {
  22. $returnArray [$key] = $value;
  23. }
  24. }
  25. return $returnArray;
  26. }
  27. public function getCountries() {
  28. $mysql = $this->dbCon->getConnection();
  29. $sql = "SELECT id, name FROM Country WHERE id IN (SELECT countryId FROM SoccerResults WHERE DATE(gameDate) >= DATE(NOW())) GROUP BY name ORDER BY prio DESC, name ASC";
  30. // $result = $mysql->query( $sql );
  31. // if ($result === false) {
  32. // echo $result->error;
  33. // }
  34. return $this->getSqlAsArray($sql);
  35. }
  36. public function getLeagues() {
  37. $mysql = $this->dbCon->getConnection();
  38. $sql = "SELECT id, name, countryId FROM League WHERE id IN (SELECT leagueId FROM SoccerResults WHERE DATE(gameDate) >= DATE(NOW())) GROUP BY countryId, name ORDER BY prio DESC, name ASC";
  39. // $result = $mysql->query( $sql );
  40. // if ($result === false) {
  41. // echo $result->error;
  42. // }
  43. return $this->getSqlAsArray($sql);
  44. }
  45. public function getMatches($countryId, $leagueId ) {
  46. $mysql = $this->dbCon->getConnection();
  47. $sql = "SELECT res.*, " .
  48. "hTeam.name as homeTeamName, aTeam.name as awayTeamName " .
  49. "FROM SoccerResults as res " .
  50. "Join Team as hTeam ON res.homeTeam = hTeam.id " .
  51. "Join Team as aTeam ON res.awayTeam = aTeam.id " .
  52. "WHERE " .
  53. "DATE(gameDate) >= DATE(NOW()) " .
  54. "AND res.leagueId = " . $leagueId .
  55. " AND res.countryId = " . $countryId .
  56. " AND homeScore <= 0" .
  57. " ORDER BY gameDate ASC";
  58. $result = $this->getSqlAsArray($sql);
  59. return $this->prepareMatches($result, $leagueId, $countryId);
  60. }
  61. private function prepareMatches($matches, $leagueId, $countryId) {
  62. for ($i = 0; $i < count($matches); $i++) {
  63. $match = $matches[$i];
  64. $league = $this->getLeagueInfo($leagueId);
  65. $lookbackHome = $league[0]['lookbackHome'];
  66. $lookbackDraw = $league[0]['lookbackDraw'];
  67. $lookbackAway = $league[0]['lookbackAway'];
  68. $betMarginHome = $league[0]['betMarginHome'];
  69. $betMarginDraw = $league[0]['betMarginDraw'];
  70. $betMarginAway = $league[0]['betMarginAway'];
  71. $homeTeamResultsHome = $this->getTeamResults($match['homeTeam'], $lookbackHome, true)[0];
  72. $homeTeamResultsAway = $this->getTeamResults($match['awayTeam'], $lookbackHome, false)[0];
  73. $drawTeamResultsHome = $this->getTeamResults($match['homeTeam'], $lookbackDraw, true)[0];
  74. $drawTeamResultsAway = $this->getTeamResults($match['awayTeam'], $lookbackDraw, false)[0];
  75. $awayTeamResultsHome = $this->getTeamResults($match['homeTeam'], $lookbackAway, true)[0];
  76. $awayTeamResultsAway = $this->getTeamResults($match['awayTeam'], $lookbackAway, false)[0];
  77. $homeTeamCountHome = $homeTeamResultsHome['wins'] + $homeTeamResultsHome['draws'] + $homeTeamResultsHome['lost'];
  78. $homeTeamCountAway = $homeTeamResultsAway['wins'] + $homeTeamResultsAway['draws'] + $homeTeamResultsAway['lost'];
  79. $drawTeamCountHome = $drawTeamResultsHome['wins'] + $drawTeamResultsHome['draws'] + $drawTeamResultsHome['lost'];
  80. $drawTeamCountAway = $drawTeamResultsAway['wins'] + $drawTeamResultsAway['draws'] + $drawTeamResultsAway['lost'];
  81. $awayTeamCountHome = $awayTeamResultsHome['wins'] + $awayTeamResultsHome['draws'] + $awayTeamResultsHome['lost'];
  82. $awayTeamCountAway = $awayTeamResultsAway['wins'] + $awayTeamResultsAway['draws'] + $awayTeamResultsAway['lost'];
  83. $homeWinPercent = round((($homeTeamResultsHome['wins'] + $homeTeamResultsAway['lost']) / ($homeTeamCountHome + $homeTeamCountAway)) * 100,2);
  84. $drawPercent = round((($drawTeamResultsHome['draws'] + $drawTeamResultsAway['draws']) / ($drawTeamCountHome + $drawTeamCountAway)) * 100, 2);
  85. $awayWinPercent = round((($awayTeamResultsAway['wins'] + $awayTeamResultsHome['lost']) / ($awayTeamCountHome + $awayTeamCountAway)) * 100, 2);
  86. $match['homeWinPercent'] = $homeWinPercent;
  87. $match['drawPercent'] = $drawPercent;
  88. $match['awayWinPercent'] = $awayWinPercent;
  89. $matches[$i] = $match;
  90. }
  91. return $matches;
  92. }
  93. public function getLeagueInfo($leagueId) {
  94. $sql = "SELECT * FROM League WHERE id = " . $leagueId;
  95. $result = $this->getSqlAsArray($sql);
  96. return $result;
  97. }
  98. public function getTeamResults($teamId, $numResults, $isHomeTeam) {
  99. $sql = "";
  100. if ($isHomeTeam) {
  101. $sql = "SELECT count(case when homeScore > awayScore then 1 end) wins, " .
  102. "count(case when awayScore > homeScore then 1 end) lost, " .
  103. "count(case when homeScore = awayScore then 1 end) draws " .
  104. "FROM (SELECT * FROM SoccerResults WHERE homeTeam = " . $teamId . " AND " .
  105. "HomeScore >= 0 AND awayScore >= 0 AND DATE(gameDate) < DATE(NOW()) ORDER BY gameDate DESC LIMIT " . $numResults . ") as t";
  106. } else {
  107. $sql = "SELECT count(case when homeScore < awayScore then 1 end) wins, " .
  108. "count(case when awayScore < homeScore then 1 end) lost, " .
  109. "count(case when homeScore = awayScore then 1 end) draws " .
  110. "FROM (SELECT * FROM SoccerResults WHERE awayTeam = " . $teamId . " AND " .
  111. "HomeScore >= 0 AND awayScore >= 0 AND DATE(gameDate) < DATE(NOW()) ORDER BY gameDate DESC LIMIT " . $numResults . ") as t";
  112. }
  113. return $this->getSqlAsArray($sql);
  114. }
  115. public function addBet($matchId, $betOdds, $betAmount, $betType) {
  116. $sql = "INSERT INTO Bets (matchId, odds, amount, betOn) VALUES ($matchId, $betOdds, $betAmount, '$betType')";
  117. $this->getSqlAsArray($sql);
  118. }
  119. private function getGamesBySeason($leagueId, $season) {
  120. }
  121. public function getBettingStats($leagueId) {
  122. $sql = "SELECT * FROM Bets INNER JOIN SoccerResults sr ON matchId = sr.id WHERE leagueId = $leagueId AND Season = (SELECT MAX(season) FROM SoccerResults WHERE leagueId = $leagueId)";
  123. return $this->getSqlAsArray($sql);
  124. }
  125. public function getBettingSummary() {
  126. $sql = "SELECT *, l.name as leagueName FROM Bets INNER JOIN SoccerResults sr ON matchId = sr.id INNER JOIN League l ON leagueId = l.id INNER JOIN Country c ON l.countryId = c.id WHERE DATE(gameDate) > DATE_SUB(CURDATE(), INTERVAL 1 YEAR)";
  127. return $this->getSqlAsArray($sql);
  128. }
  129. public function getDayBettingsStats() {
  130. $sql = "SELECT DATE(gameDate) as date, ROUND(SUM(
  131. CASE
  132. WHEN betOn = '1' AND sr.homeScore > sr.awayScore THEN (amount * odds) -amount
  133. WHEN betOn = 'X' AND sr.homeScore = sr.awayScore THEN (amount * odds) -amount
  134. WHEN betOn = '2' AND sr.homeScore < sr.awayScore THEN (amount * odds) -amount
  135. ELSE -amount
  136. END
  137. ),2 ) as sumAmount,
  138. SUM(
  139. CASE
  140. WHEN betOn = '1' AND sr.homeScore > sr.awayScore AND homeScore > -1 AND awayScore > -1 THEN 1
  141. WHEN betOn = 'X' AND sr.homeScore = sr.awayScore AND homeScore > -1 AND awayScore > -1 THEN 1
  142. WHEN betOn = '2' AND sr.homeScore < sr.awayScore AND homeScore > -1 AND awayScore > -1 THEN 1
  143. ELSE 0
  144. END as wins,
  145. SUM(
  146. CASE
  147. WHEN homeScore <= -1 AND awayScore <= -1 THEN 1
  148. ELSE 0
  149. END as pending
  150. SUM(amount) as betAmount,
  151. COUNT(gameDate) as numGames
  152. FROM `Bets` INNER JOIN SoccerResults sr ON matchId = sr.id GROUP BY DATE(sr.gameDate) ORDER BY sr.gameDate ASC";
  153. return $this->getSqlAsArray($sql);
  154. }
  155. }