From bb66ed2e0771612d83a2ed08e321637f647912a5 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Wed, 11 Jan 2017 15:14:09 +1100 Subject: [PATCH] Adjust minutes calculation in player statistics for substitution time --- includes/class-sp-player-list.php | 17 +++++++++++++++++ includes/class-sp-player.php | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index 256b2cba..da66df1d 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -250,6 +250,7 @@ class SP_Player_List extends SP_Custom_Post { foreach ( $events as $i => $event ): $results = (array)get_post_meta( $event->ID, 'sp_results', true ); $team_performance = get_post_meta( $event->ID, 'sp_players', true ); + $timeline = (array)get_post_meta( $event->ID, 'sp_timeline', true ); $minutes = get_post_meta( $event->ID, 'sp_minutes', true ); if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 ); @@ -320,6 +321,22 @@ class SP_Player_List extends SP_Custom_Post { $totals[ $player_id ]['eventsplayed'] ++; $totals[ $player_id ]['eventminutes'] += $minutes; + // Adjust for 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 ); + else: + foreach ( $timeline as $timeline_team => $timeline_players ): + foreach ( $timeline_players as $timeline_player => $timeline_performance ): + 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; + endif; + endif; + endforeach; + endforeach; + endif; + 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 3151bef6..eda8eb55 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -336,6 +336,7 @@ class SP_Player extends SP_Custom_Post { foreach( $events as $i => $event ): $results = (array)get_post_meta( $event->ID, 'sp_results', true ); $team_performance = (array)get_post_meta( $event->ID, 'sp_players', true ); + $timeline = (array)get_post_meta( $event->ID, 'sp_timeline', true ); $minutes = get_post_meta( $event->ID, 'sp_minutes', true ); if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 ); @@ -405,6 +406,22 @@ class SP_Player extends SP_Custom_Post { $totals['eventsplayed'] ++; $totals['eventminutes'] += $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 ); + else: + foreach ( $timeline as $timeline_team => $timeline_players ): + foreach ( $timeline_players as $timeline_player => $timeline_performance ): + 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; + endif; + endif; + endforeach; + endforeach; + endif; + 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 ) ):