From 3ade0f043f4ae8d8dff6a37bf11a46e05fe2bf74 Mon Sep 17 00:00:00 2001 From: Julian Date: Tue, 3 Apr 2018 21:06:22 +0200 Subject: [PATCH] Add bonuspoint, corr and chart calculation and update displaying of chart. --- acp/bets_module.php | 3 + acp/results_module.php | 3 + block/my_chart.php | 16 +-- controller/main.php | 3 + includes/constants.php | 2 + includes/functions.php | 230 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 249 insertions(+), 8 deletions(-) diff --git a/acp/bets_module.php b/acp/bets_module.php index f2b26e1..8ffb630 100644 --- a/acp/bets_module.php +++ b/acp/bets_module.php @@ -426,6 +426,9 @@ class bets_module $league_info = league_info($season, $league); $cash = $this->request->variable('cash', false); save_ranking_matchday($season, $league, $matchday, $cash); + bonuspoints($season, $league, $matchday); + corr($season, $league, $matchday); + chart($season, $league, $matchday); } break; } diff --git a/acp/results_module.php b/acp/results_module.php index 976733d..fd1f5f4 100644 --- a/acp/results_module.php +++ b/acp/results_module.php @@ -443,6 +443,9 @@ class results_module $cash = $this->request->variable('cash', false); save_ranking_matchday($season, $league, $matchday, $cash); + bonuspoints($season, $league, $matchday); + corr($season, $league, $matchday); + chart($season, $league, $matchday); // Patch delevirey if ($league_info['bet_in_time'] == 1) diff --git a/block/my_chart.php b/block/my_chart.php index 7ef19fd..b474712 100644 --- a/block/my_chart.php +++ b/block/my_chart.php @@ -22,17 +22,17 @@ $username2 = ''; $username3 = ''; $username4 = ''; // Calculate rank total -$sql = 'SELECT +$sql = "SELECT r.user_id, u.username, - SUM(r.points) AS points_total - FROM ' . FOOTB_RANKS . ' AS r + (SELECT SUM(c.rank_total) FROM " . FOOTB_MY_CHART . " AS c WHERE c.user_id = (r.user_id) AND c.season = r.season AND c.league = r.league AND c.matchday = $matchday) AS points_total + FROM " . FOOTB_RANKS . ' AS r LEFT JOIN ' . USERS_TABLE . " AS u ON (u.user_id = r.user_id) WHERE r.season = $season AND r.league = $league AND r.matchday <= $matchday GROUP BY r.user_id - ORDER BY points_total DESC, LOWER(u.username) ASC"; + ORDER BY points_total ASC, LOWER(u.username) ASC"; $result = $db->sql_query($sql); $current_ranks = $db->sql_fetchrowset($result); @@ -158,7 +158,7 @@ if ($total_users > 3 AND $total_users <= 50) $ranks_dayl_1 = ''; $points_1 = ''; $sql = 'SELECT * - FROM ' . FOOTB_RANKS . " + FROM ' . FOOTB_MY_CHART . " WHERE season = $season AND league = $league AND matchday <= $matchday @@ -211,7 +211,7 @@ if ($total_users > 3 AND $total_users <= 50) if ($user3 != 0) { $sql = 'SELECT * - FROM ' . FOOTB_RANKS . " + FROM ' . FOOTB_MY_CHART . " WHERE season = $season AND league = $league AND matchday <= $matchday @@ -238,7 +238,7 @@ if ($total_users > 3 AND $total_users <= 50) if ($user4 != 0) { $sql = 'SELECT * - FROM ' . FOOTB_RANKS . " + FROM ' . FOOTB_MY_CHART . " WHERE season = $season AND league = $league AND matchday <= $matchday @@ -267,7 +267,7 @@ if ($total_users > 3 AND $total_users <= 50) $sql = 'SELECT MIN(points) As points_min, MAX(points) As points_max - FROM ' . FOOTB_RANKS . " + FROM ' . FOOTB_MY_CHART . " WHERE season = $season AND league = $league AND matchday <= $matchday diff --git a/controller/main.php b/controller/main.php index 5a87053..2d71747 100644 --- a/controller/main.php +++ b/controller/main.php @@ -732,6 +732,9 @@ class main } calculate_extra_points($season, $league, $matchday); save_ranking_matchday($season, $league, $matchday); + bonuspoints($season, $league, $matchday); + corr($season, $league, $matchday); + chart($season, $league, $matchday); break; case 'join': join_league($season, $league, $user->data['user_id']); diff --git a/includes/constants.php b/includes/constants.php index 90a7c67..24f243f 100644 --- a/includes/constants.php +++ b/includes/constants.php @@ -16,12 +16,14 @@ global $table_prefix; // Config constants define('FOOTB_BETS', $table_prefix . 'footb_bets'); +define('FOOTB_CORR', $table_prefix . 'footb_corr'); define('FOOTB_EXTRA_BETS', $table_prefix . 'footb_extra_bets'); define('FOOTB_EXTRA', $table_prefix . 'footb_extra'); define('FOOTB_LEAGUES', $table_prefix . 'footb_leagues'); define('FOOTB_MATCHDAYS', $table_prefix . 'footb_matchdays'); define('FOOTB_MATCHES', $table_prefix . 'footb_matches'); define('FOOTB_MATCHES_HIST', $table_prefix . 'footb_matches_hist'); +define('FOOTB_MY_CHART', $table_prefix . 'footb_my_chart'); define('FOOTB_POINTS', $table_prefix . 'footb_points'); define('FOOTB_RANKS', $table_prefix . 'footb_rank_matchdays'); define('FOOTB_SEASONS', $table_prefix . 'footb_seasons'); diff --git a/includes/functions.php b/includes/functions.php index 101e604..2096e04 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -2564,4 +2564,234 @@ function select_points($creator = 'm', $sum = false) } return $select_part; } + +function bonuspoints($season, $league, $matchday) +{ + global $db, $config, $lang, $league_info, $phpbb_root_path, $phpEx; + if ($league < 40) + { + $league2 = $league + 50; + $sql = 'DELETE FROM ' . FOOTB_BONUS_DAY . " WHERE season = '$season' AND league = '$league2' AND matchday = '$matchday'"; + $result = $db->sql_query($sql); + $sql = 'REPLACE INTO ' . FOOTB_BONUS_DAY . ' (season, league, matchday, team_id, rank) + SELECT season, (league + 50), matchday, (user_id + 2000), rank FROM ' . FOOTB_RANKS . " WHERE season = '$season' AND league = '$league' and matchday = '$matchday'"; + $result = $db->sql_query($sql); + $sql = 'SELECT team_id_home FROM ' . FOOTB_MATCHES . " WHERE season = '$season' AND league = '$league2' AND matchday = '$matchday'"; + $result = $db->sql_query($sql); + $id = array(); + while( $row = $db->sql_fetchrow($result)) + { + array_push ( $id, $row['team_id_home']); + } + $i = 0; + while($id[$i]) + { + $tmp = $id[$i]; + $sql = 'UPDATE ' . FOOTB_BONUS_DAY . " SET home = '1' WHERE season = '$season' AND league = '$league2' AND matchday = '$matchday' AND team_id = '$tmp'"; + $result = $db->sql_query($sql); + $i++; + } + $sql = 'SELECT rank FROM ' . FOOTB_BONUS_DAY . " WHERE season = '$season' AND league = '$league2' AND matchday = '$matchday'" ; + $result = $db->sql_query($sql); + $ranks = array(); + while( $row = $db->sql_fetchrow($result)) + { + array_push ( $ranks, $row['rank']); + } + $anzahl = array_count_values ($ranks); + If(isset($anzahl[1])) + { + $z1 = $anzahl[1]; + } + Else + { + $z1 = "0"; + } + If(isset($anzahl[2])) + { + $z2 = $anzahl[2]; + } + Else + { + $z2 = "0"; + } + If(isset($anzahl[3])) + { + $z3 = $anzahl[3]; + } + Else + { + $z3 = "0"; + } + + If ($z1 > 2) + { + $b1 = 6 / $z1; + } + Elseif ($z1 == 2) + { + $b1 = 2.5; + } + Else + { + $b1 = 3; + } + If ($z2 > 1) + { + $b2 = 3 / $z2; + } + Elseif ($z2 == 1) + { + $b2 = 2; + } + Else + { + $b2 = 0; + } + If ($z3 > 0) + { + $b3 = 1 / $z3; + } + Else + { + $b3 = 0; + } + $sql = + 'UPDATE ' . FOOTB_BONUS_DAY . " + SET bonuspoints = '$b1' + WHERE season = '$season' AND league = '$league2' and matchday = '$matchday' AND rank = '1'"; + $result = $db->sql_query($sql); + $sql = + 'UPDATE ' . FOOTB_BONUS_DAY . " + SET bonuspoints = '$b2' + WHERE season = '$season' AND league = '$league2' and matchday = '$matchday' AND rank = '2'"; + $result = $db->sql_query($sql); + $sql = + 'UPDATE ' . FOOTB_BONUS_DAY . " + SET bonuspoints = '$b3' + WHERE season = '$season' AND league = '$league2' and matchday = '$matchday' AND rank = '3'"; + $result = $db->sql_query($sql); + $sql = + 'UPDATE ' . FOOTB_BONUS_DAY . " SET `team_id` = `team_id`+2000 WHERE `team_id`<2000"; + $result = $db->sql_query($sql); + $sql = 'DELETE FROM ' . FOOTB_BONUS_DAY . " WHERE season = '$season' AND league = '$league2' AND matchday = '$matchday' AND bonuspoints = '0'"; + $result = $db->sql_query($sql); + } +} + +function corr($season, $league, $matchday) +{ + global $db, $config, $lang, $league_info, $phpbb_root_path, $phpEx; + if ($league < 40) + { + $league2 = $league + 50; + $sql = 'DELETE FROM ' . FOOTB_CORR . " WHERE season = '$season' AND league = '$league2' AND matchday = '$matchday'"; + $result = $db->sql_query($sql); + $sql = 'SELECT status FROM ' . FOOTB_MATCHDAYS . " WHERE season = '$season' AND league = '$league' AND matchday = '$matchday'"; + $result = $db->sql_query($sql); + while($row = $db->sql_fetchrow($result)) + { + if ($row['status'] > 0) + { + $sql = 'SELECT match_no FROM ' . FOOTB_MATCHES . " WHERE season = '$season' AND league = '$league2' AND matchday = '$matchday'"; + $result = $db->sql_query($sql); + $game = array(); + while( $row = $db->sql_fetchrow($result)) + { + array_push ($game,$row['match_no']); + } + foreach($game as $nr) + { + $sql = 'SELECT team_id_home, team_id_guest, goals_home, goals_guest FROM ' . FOOTB_MATCHES . " WHERE season = '$season' AND league = '$league2' AND match_no = '$nr'"; + $result = $db->sql_query($sql); + $row = $db->sql_fetchrow($result); + if ($row['goals_home'] == 0 and $row['goals_guest'] == 0) + { + $sql = 'SELECT b.goals_home, b.goals_guest FROM ' . FOOTB_BETS . ' AS b LEFT JOIN ' . FOOTB_MATCHES . " AS m ON (m.season = b.season AND m.league = b.league AND m.match_no = b.match_no) WHERE m.season = $season AND m.league = $league AND m.matchday = $matchday AND b.user_id = $row[team_id_home] - 2000"; + $result = $db->sql_query($sql); + $home_empty = true; + while($row_home = $db->sql_fetchrow($result)) + { + if (!empty($row_home['goals_home']) or !empty($row_home['goals_guest'])) + { + $home_empty = false; + } + } + $sql = 'SELECT b.goals_home, b.goals_guest FROM ' . FOOTB_BETS . ' AS b LEFT JOIN ' . FOOTB_MATCHES . " AS m ON (m.season = b.season AND m.league = b.league AND m.match_no = b.match_no) WHERE m.season = $season AND m.league = $league AND m.matchday = $matchday AND b.user_id = $row[team_id_guest] - 2000"; + $result = $db->sql_query($sql); + $guest_empty = true; + while($row_guest = $db->sql_fetchrow($result)) + { + if (!empty($row_guest['goals_home']) or !empty($row_guest['goals_guest'])) + { + $guest_empty = false; + } + } + if ($home_empty == true) + { + $sql = 'REPLACE INTO ' . FOOTB_CORR . "(season, league, matchday, team_id, win, lost, draw, points, home) VALUES ($season, $league2, $matchday, $row[team_id_home], 0, 1, -1, -1, 0)"; + $result = $db->sql_query($sql); + } + if ($guest_empty == true) + { + $sql = 'REPLACE INTO ' . FOOTB_CORR . "(season, league, matchday, team_id, win, lost, draw, points, home) VALUES ($season, $league2, $matchday, $row[team_id_guest], 0, 1, -1, -1, 1)"; + $result = $db->sql_query($sql); + } + } + } + } + } + } +} + +function chart($season, $league, $matchday) +{ + global $db, $config, $lang, $league_info, $phpbb_root_path, $phpEx; + $league2 = $league + 50; + $sql = 'DELETE + FROM ' . FOOTB_MY_CHART . " + WHERE season = $season + AND league = $league + AND matchday = $matchday"; + $result = $db->sql_query($sql); + $sql = 'REPLACE INTO ' . FOOTB_MY_CHART . ' + SELECT * FROM ' . FOOTB_RANKS . " + WHERE season = $season + AND league = $league + AND matchday = $matchday"; + $result = $db->sql_query($sql); + $sql = "SELECT + t.*, + SUM(IF(m.team_id_home = t.team_id, + IF(goals_home + 0 > goals_guest, 3, IF(goals_home = goals_guest, 1, 0)), + IF(goals_home + 0 < goals_guest, 3, IF(goals_home = goals_guest, 1, 0)) + ) + ) + IFNULL((SELECT SUM(b.bonuspoints) FROM " . FOOTB_BONUS_DAY . " AS b WHERE b.team_id = t.team_id AND b.season = t.season AND b.league = t.league AND b.matchday <= $matchday),0) + + IFNULL((SELECT SUM(c.points) FROM " . FOOTB_CORR . " AS c WHERE c.team_id = t.team_id AND c.season = t.season AND c.league = t.league AND c.matchday <= $matchday),0)AS points, + SUM(IF(m.team_id_home = t.team_id, goals_home - goals_guest , goals_guest - goals_home)) AS goals_diff, + SUM(IF(m.team_id_home = t.team_id, goals_home , goals_guest)) AS goals + FROM " . FOOTB_TEAMS . ' AS t + LEFT JOIN ' . FOOTB_MATCHES . " AS m ON (m.season = t.season AND m.league = t.league + AND (m.team_id_home = t.team_id OR m.team_id_guest = t.team_id) AND m.group_id = t.group_id) + WHERE t.season = $season + AND t.league = $league2 + AND m.matchday <= $matchday + AND m.status IN (2, 3,5,6) + GROUP BY t.team_id + ORDER BY t.group_id ASC, points DESC, goals_diff DESC, goals DESC, t.team_name ASC"; + $result = $db->sql_query($sql); + $i = 1; + while( $row = $db->sql_fetchrow($result)) + { + $user_i = $row['team_id'] - 2000; + $sql2 = 'UPDATE ' . FOOTB_MY_CHART . " + SET rank_total = $i + WHERE season = $season + AND league = $league + AND matchday = $matchday + AND user_id = $user_i"; + $result2 = $db->sql_query($sql2); + $i++; + } +} ?> \ No newline at end of file