webDbConnection.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 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. $homeWinPercent = round((($homeTeamResultsHome['wins'] + $homeTeamResultsAway['lost']) / ($homeTeamCountHome + $homeTeamCountAway)) * 100,2);
  80. $drawPercent = round((($drawTeamResultsHome['draws'] + $drawTeamResultsAway['draws']) / ($homeTeamCountHome + $homeTeamCountAway)) * 100, 2);
  81. $awayWinPercent = round((($awayTeamResultsAway['wins'] + $awayTeamResultsHome['lost']) / ($homeTeamCountHome + $homeTeamCountAway)) * 100, 2);
  82. $match['homeWinPercent'] = $homeWinPercent;
  83. $match['drawPercent'] = $drawPercent;
  84. $match['awayWinPercent'] = $awayWinPercent;
  85. $matches[$i] = $match;
  86. }
  87. return $matches;
  88. }
  89. public function getLeagueInfo($leagueId) {
  90. $sql = "SELECT * FROM League WHERE id = " . $leagueId;
  91. $result = $this->getSqlAsArray($sql);
  92. return $result;
  93. }
  94. public function getTeamResults($teamId, $numResults, $isHomeTeam) {
  95. $sql = "";
  96. if ($isHomeTeam) {
  97. $sql = "SELECT count(case when homeScore > awayScore then 1 end) wins, " .
  98. "count(case when awayScore > homeScore then 1 end) lost, " .
  99. "count(case when homeScore = awayScore then 1 end) draws " .
  100. "FROM (SELECT * FROM SoccerResults WHERE homeTeam = " . $teamId . " AND " .
  101. "HomeScore >= 0 AND awayScore >= 0 AND DATE(gameDate) < DATE(NOW()) ORDER BY gameDate DESC LIMIT " . $numResults . ") as t";
  102. } else {
  103. $sql = "SELECT count(case when homeScore < awayScore then 1 end) wins, " .
  104. "count(case when awayScore < homeScore then 1 end) lost, " .
  105. "count(case when homeScore = awayScore then 1 end) draws " .
  106. "FROM (SELECT * FROM SoccerResults WHERE awayTeam = " . $teamId . " AND " .
  107. "HomeScore >= 0 AND awayScore >= 0 AND DATE(gameDate) < DATE(NOW()) ORDER BY gameDate DESC LIMIT " . $numResults . ") as t";
  108. }
  109. return $this->getSqlAsArray($sql);
  110. }
  111. public function addBet($matchId, $betOdds, $betAmount, $betType) {
  112. $sql = "INSERT INTO bets (matchId, odds, amount, betOn) VALUES ($matchId, $betOdds, $betAmount, '$betType')";
  113. $this->getSqlAsArray($sql);
  114. }
  115. private function getGamesBySeason($leagueId, $season) {
  116. }
  117. public function getBettingStats($leagueId) {
  118. }
  119. }