From 75726fb838412a5577f71224ff48a6aaa56d9c87 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Mon, 20 Mar 2017 09:33:01 +1100 Subject: [PATCH] Fixed played minutes calculation and player profile stats --- includes/class-sp-player-list.php | 36 ++++++++++++++++++++++++------- includes/class-sp-player.php | 30 ++++++++++++++++++++------ 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index 5beb23ae..766bf9d7 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -404,14 +404,16 @@ class SP_Player_List extends SP_Custom_Post { $totals[ $player_id ]['eventsattended'] ++; // Continue with incrementing values if active in event - if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): + if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): $totals[ $player_id ]['eventsplayed'] ++; - $totals[ $player_id ]['eventminutes'] += $minutes; - // Adjust for substitution time + // Initialize played minutes + $played_minutes = $minutes; + + // Adjust for sendoffs and substitution time if ( sp_array_value( $player_performance, 'status' ) === 'sub' ): - $totals[ $player_id ]['eventminutes'] -= sp_array_value( sp_array_value( sp_array_value( sp_array_value( $timeline, $team_id ), $player_id ), 'sub' ), 0, 0 ); + // Substituted for another player $timeline_performance = sp_array_value( sp_array_value( $timeline, $team_id, array() ), $player_id, array() ); if ( empty( $timeline_performance ) ) continue; foreach ( $sendoffs as $sendoff_key ): @@ -420,9 +422,17 @@ class SP_Player_List extends SP_Custom_Post { $sendoff_times = array_filter( $sendoff_times ); $sendoff_time = end( $sendoff_times ); if ( ! $sendoff_time ) $sendoff_time = 0; - $totals[ $player_id ]['eventminutes'] += $sendoff_time - $minutes; + + // Count minutes until being sent off + $played_minutes = $sendoff_time; endforeach; + + // Subtract minutes prior to substitution + $substitution_time = sp_array_value( sp_array_value( sp_array_value( sp_array_value( $timeline, $team_id ), $player_id ), 'sub' ), 0, 0 ); + $played_minutes -= $substitution_time; else: + + // Starting lineup with possible substitution $subbed_out = false; foreach ( $timeline as $timeline_team => $timeline_players ): if ( ! is_array( $timeline_players ) ) continue; @@ -430,26 +440,36 @@ class SP_Player_List extends SP_Custom_Post { if ( 'sub' === sp_array_value( sp_array_value( $players, $timeline_player, array() ), 'status' ) && $player_id === (int) sp_array_value( sp_array_value( $players, $timeline_player, array() ), 'sub', 0 ) ): $substitution_time = sp_array_value( sp_array_value( sp_array_value( sp_array_value( $timeline, $team_id ), $timeline_player ), 'sub' ), 0, 0 ); if ( $substitution_time ): - $totals[ $player_id ]['eventminutes'] += $substitution_time - $minutes; + + // Count minutes until substitution + $played_minutes = $substitution_time; $subbed_out = true; endif; endif; endforeach; + // No need to check for sendoffs if subbed out if ( $subbed_out ) continue; + + // Check for sendoffs $timeline_performance = sp_array_value( $timeline_players, $player_id, array() ); if ( empty( $timeline_performance ) ) continue; foreach ( $sendoffs as $sendoff_key ): if ( ! array_key_exists( $sendoff_key, $timeline_performance ) ) continue; + if ( ! sp_array_value( $player_performance, $sendoff_key, 0 ) ) continue; $sendoff_times = sp_array_value( sp_array_value( sp_array_value( $timeline, $team_id ), $player_id ), $sendoff_key ); $sendoff_times = array_filter( $sendoff_times ); $sendoff_time = end( $sendoff_times ); - if ( ! $sendoff_time ) $sendoff_time = 0; - $totals[ $player_id ]['eventminutes'] += $sendoff_time - $minutes; + if ( false === $sendoff_time ) continue; + + // Count minutes until being sent off + $played_minutes = $sendoff_time; endforeach; endforeach; endif; + $totals[ $player_id ]['eventminutes'] += max( 0, $played_minutes ); + if ( sp_array_value( $player_performance, 'status' ) == 'lineup' ): $totals[ $player_id ]['eventsstarted'] ++; elseif ( sp_array_value( $player_performance, 'status' ) == 'sub' && sp_array_value( $player_performance, 'sub', 0 ) ): diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index ff6beefd..4c43872a 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -359,12 +359,12 @@ class SP_Player extends SP_Custom_Post { // Continue with incrementing values if active in event if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): $totals['eventsplayed'] ++; - $totals['eventminutes'] += $minutes; + $played_minutes = $minutes; // Adjust for substitution time if ( sp_array_value( $player_performance, 'status' ) === 'sub' ): - $totals['eventminutes'] -= sp_array_value( sp_array_value( sp_array_value( sp_array_value( $timeline, $team_id ), $this->ID ), 'sub' ), 0, 0 ); + // Substituted for another player $timeline_performance = sp_array_value( sp_array_value( $timeline, $team_id, array() ), $this->ID, array() ); if ( empty( $timeline_performance ) ) continue; foreach ( $sendoffs as $sendoff_key ): @@ -373,9 +373,17 @@ class SP_Player extends SP_Custom_Post { $sendoff_times = array_filter( $sendoff_times ); $sendoff_time = end( $sendoff_times ); if ( ! $sendoff_time ) $sendoff_time = 0; - $totals['eventminutes'] += $sendoff_time - $minutes; + + // Count minutes until being sent off + $played_minutes = $sendoff_time; endforeach; + + // Subtract minutes prior to substitution + $substitution_time = sp_array_value( sp_array_value( sp_array_value( sp_array_value( $timeline, $team_id ), $this->ID ), 'sub' ), 0, 0 ); + $played_minutes -= $substitution_time; else: + + // Starting lineup with possible substitution $subbed_out = false; foreach ( $timeline as $timeline_team => $timeline_players ): if ( ! is_array( $timeline_players ) ) continue; @@ -383,13 +391,18 @@ class SP_Player extends SP_Custom_Post { if ( 'sub' === sp_array_value( sp_array_value( $players, $timeline_player, array() ), 'status' ) && $this->ID === (int) sp_array_value( sp_array_value( $players, $timeline_player, array() ), 'sub', 0 ) ): $substitution_time = sp_array_value( sp_array_value( sp_array_value( sp_array_value( $timeline, $team_id ), $timeline_player ), 'sub' ), 0, 0 ); if ( $substitution_time ): - $totals['eventminutes'] += $substitution_time - $minutes; + + // Count minutes until substitution + $played_minutes = $substitution_time; $subbed_out = true; endif; endif; endforeach; + // No need to check for sendoffs if subbed out if ( $subbed_out ) continue; + + // Check for sendoffs $timeline_performance = sp_array_value( $timeline_players, $this->ID, array() ); if ( empty( $timeline_performance ) ) continue; foreach ( $sendoffs as $sendoff_key ): @@ -397,12 +410,16 @@ class SP_Player extends SP_Custom_Post { $sendoff_times = sp_array_value( sp_array_value( sp_array_value( $timeline, $team_id ), $this->ID ), $sendoff_key ); $sendoff_times = array_filter( $sendoff_times ); $sendoff_time = end( $sendoff_times ); - if ( ! $sendoff_time ) $sendoff_time = 0; - $totals['eventminutes'] += $sendoff_time - $minutes; + if ( false === $sendoff_time ) continue; + + // Count minutes until being sent off + $played_minutes = $sendoff_time; endforeach; endforeach; endif; + $totals['eventminutes'] += max( 0, $played_minutes ); + if ( sp_array_value( $player_performance, 'status' ) == 'lineup' ): $totals['eventsstarted'] ++; elseif ( sp_array_value( $player_performance, 'status' ) == 'sub' && sp_array_value( $player_performance, 'sub', 0 ) ): @@ -686,6 +703,7 @@ class SP_Player extends SP_Custom_Post { // Get totals as entered directly and filter out the empty values $total_data = sp_array_value( $data, 0, array() ); + $total_data = array_filter( $total_data, 'sp_filter_non_empty' ); // Get totals of all seasons as entered manually $totals = array();