webDbConnection.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. " ORDER BY gameDate ASC";
  57. $result = $this->getSqlAsArray($sql);
  58. return $this->prepareMatches($result, $leagueId, $countryId);
  59. }
  60. private function prepareMatches($matches, $leagueId, $countryId) {
  61. foreach ($matches as $match) {
  62. var_dump($match);
  63. $league = $this->getLeagueInfo($leagueId);
  64. echo "</br></br> League </br>";
  65. var_dump($league[0]);
  66. $lookback = $league[0]['lookback'];
  67. $betMargin = $league[0]['betMargin'];
  68. $homeTeamResults = $this->getTeamResults($match['homeTeam'], $lookback, true)[0];
  69. $awayTeamResults = $this->getTeamResults($match['awayTeam'], $lookback, false)[0];
  70. echo "</br></br> HomeTeam </br>";
  71. var_dump($homeTeamResults);
  72. echo "</br></br> AwayTeam </br>";
  73. var_dump($awayTeamResults);
  74. $homeTeamCount = $homeTeamResults['wins'] + $homeTeamResults['draws'] + $homeTeamResults['lost'];
  75. $awayTeamCount = $awayTeamResults['wins'] + $awayTeamResults['draws'] + $awayTeamResults['lost'];
  76. $homeWinPercent = round((($homeTeamResults['wins'] + $awayTeamResults['lost']) / ($homeTeamCount + $awayTeamCount)) * 100,2);
  77. $drawPercent = round((($homeTeamResults['draws'] + $awayTeamResults['draws']) / ($homeTeamCount + $awayTeamCount)) * 100, 2);
  78. $awayWinPercent = round((($awayTeamResults['wins'] + $homeTeamResults['lost']) / ($homeTeamCount + $awayTeamCount)) * 100, 2);
  79. $match['homeWinPercent'] = $homeWinPercent;
  80. $match['drawPercent'] = $drawPercent;
  81. $match['awayWinPercent'] = $awayWinPercent;
  82. echo "</br></br> matchDoneEdit added percentages " . $homeWinPercent . ", $drawPercent, $awayWinPercent </br>";
  83. var_dump($match);
  84. die();
  85. /*
  86. final float homeWinPercent = (homeTeamResults.getWins() + awayTeamResults.getLosses()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount())* 100;
  87. final float drawPercent = (homeTeamResults.getDraws() + awayTeamResults.getDraws()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount()) * 100;
  88. final float awayWinPercent = (homeTeamResults.getLosses() + awayTeamResults.getWins()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount()) * 100;
  89. match.put("homeWin", String.format("%.02f", homeWinPercent) + "(" + String.format("%.02f", (100 / homeWinPercent) * betMargin) + ")");
  90. match.put("draw", String.format("%.02f", drawPercent) + "(" + String.format("%.02f", (100 / drawPercent ) * betMargin) + ")");
  91. match.put("awayWin", String.format("%.02f", awayWinPercent) + "(" + String.format("%.02f", (100 / awayWinPercent) * betMargin) + ")");
  92. matches.add(match);
  93. }
  94. originalMatches = matches;
  95. matchTable.getItems().addAll(matches);
  96. */
  97. }
  98. }
  99. private function getLeagueInfo($leagueId) {
  100. $sql = "SELECT * FROM League WHERE id = " . $leagueId;
  101. $result = $this->getSqlAsArray($sql);
  102. return $result;
  103. }
  104. public function getTeamResults($teamId, $numResults, $isHomeTeam) {
  105. $sql = "";
  106. if ($isHomeTeam) {
  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 homeTeam = " . $teamId . " AND " .
  111. "HomeScore >= 0 AND awayScore >= 0 AND DATE(gameDate) < DATE(NOW()) ORDER BY gameDate DESC LIMIT " . $numResults . ") as t";
  112. } else {
  113. $sql = "SELECT count(case when homeScore < awayScore then 1 end) wins, " .
  114. "count(case when awayScore < homeScore then 1 end) lost, " .
  115. "count(case when homeScore = awayScore then 1 end) draws " .
  116. "FROM (SELECT * FROM SoccerResults WHERE awayTeam = " . $teamId . " AND " .
  117. "HomeScore >= 0 AND awayScore >= 0 AND DATE(gameDate) < DATE(NOW()) ORDER BY gameDate DESC LIMIT " . $numResults . ") as t";
  118. }
  119. return $this->getSqlAsArray($sql);
  120. }
  121. }