Add L10 counter and fix streak counter
This commit is contained in:
@@ -402,6 +402,7 @@ if ( !function_exists( 'sportspress_get_equation_selector' ) ) {
|
|||||||
case 'outcome':
|
case 'outcome':
|
||||||
$options[ __( 'Outcomes', 'sportspress' ) ] = sportspress_get_equation_optgroup_array( $postid, 'sp_outcome', array( 'max' => '↑', 'min' => '↓' ) );
|
$options[ __( 'Outcomes', 'sportspress' ) ] = sportspress_get_equation_optgroup_array( $postid, 'sp_outcome', array( 'max' => '↑', 'min' => '↓' ) );
|
||||||
$options[ __( 'Outcomes', 'sportspress' ) ]['$streak'] = __( 'Streak', 'sportspress' );
|
$options[ __( 'Outcomes', 'sportspress' ) ]['$streak'] = __( 'Streak', 'sportspress' );
|
||||||
|
$options[ __( 'Outcomes', 'sportspress' ) ]['$last10'] = __( 'Last 10', 'sportspress' );
|
||||||
break;
|
break;
|
||||||
case 'column':
|
case 'column':
|
||||||
$options[ __( 'Columns', 'sportspress' ) ] = sportspress_get_equation_optgroup_array( $postid, 'sp_column' );
|
$options[ __( 'Columns', 'sportspress' ) ] = sportspress_get_equation_optgroup_array( $postid, 'sp_column' );
|
||||||
@@ -923,10 +924,29 @@ if ( !function_exists( 'sportspress_get_eos_safe_slug' ) ) {
|
|||||||
if ( !function_exists( 'sportspress_solve' ) ) {
|
if ( !function_exists( 'sportspress_solve' ) ) {
|
||||||
function sportspress_solve( $equation, $vars ) {
|
function sportspress_solve( $equation, $vars ) {
|
||||||
|
|
||||||
// Return direct value if streak
|
if ( str_replace( ' ', '', $equation ) == '$streak' ):
|
||||||
if ( str_replace( ' ', '', $equation ) == '$streak' )
|
|
||||||
|
// Return direct value
|
||||||
return sportspress_array_value( $vars, 'streak', 0 );
|
return sportspress_array_value( $vars, 'streak', 0 );
|
||||||
|
|
||||||
|
elseif ( str_replace( ' ', '', $equation ) == '$last10' ):
|
||||||
|
|
||||||
|
// Return imploded string
|
||||||
|
$last10 = sportspress_array_value( $vars, 'last10', array( 0 ) );
|
||||||
|
if ( array_sum( $last10 ) > 0 ):
|
||||||
|
return implode( '-', $last10 );
|
||||||
|
else:
|
||||||
|
return '—';
|
||||||
|
endif;
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
// Remove unnecessary variables from vars before calculating
|
||||||
|
unset( $vars['streak'] );
|
||||||
|
unset( $vars['last10'] );
|
||||||
|
|
||||||
|
endif;
|
||||||
|
|
||||||
// Clearance to begin calculating remains true if all equation variables are in vars
|
// Clearance to begin calculating remains true if all equation variables are in vars
|
||||||
$clearance = true;
|
$clearance = true;
|
||||||
|
|
||||||
@@ -975,12 +995,25 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) {
|
|||||||
// Initialize streaks counter
|
// Initialize streaks counter
|
||||||
$streaks = array();
|
$streaks = array();
|
||||||
|
|
||||||
|
// Initialize last 10s counter
|
||||||
|
$last10s = array();
|
||||||
|
|
||||||
foreach ( $team_ids as $team_id ):
|
foreach ( $team_ids as $team_id ):
|
||||||
if ( ! $team_id )
|
if ( ! $team_id )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$streaks[ $team_id ] = array( 'name' => '', 'count' => 0 );
|
// Initialize team streaks counter
|
||||||
|
$streaks[ $team_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 );
|
||||||
|
|
||||||
|
// Initialize team last 10 counter
|
||||||
|
$last10s[ $team_id ] = array();
|
||||||
|
|
||||||
|
// Add outcome types to team last 10 counter
|
||||||
|
foreach( $outcome_labels as $key => $value ):
|
||||||
|
$last10s[ $team_id ][ $key ] = 0;
|
||||||
|
endforeach;
|
||||||
|
|
||||||
|
// Initialize team totals
|
||||||
$totals[ $team_id ] = array( 'eventsplayed' => 0, 'streak' => 0 );
|
$totals[ $team_id ] = array( 'eventsplayed' => 0, 'streak' => 0 );
|
||||||
|
|
||||||
foreach ( $result_labels as $key => $value ):
|
foreach ( $result_labels as $key => $value ):
|
||||||
@@ -1028,18 +1061,30 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) {
|
|||||||
foreach ( $team_result as $key => $value ):
|
foreach ( $team_result as $key => $value ):
|
||||||
|
|
||||||
if ( $key == 'outcome' ):
|
if ( $key == 'outcome' ):
|
||||||
|
|
||||||
|
// Increment events played and outcome count
|
||||||
if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $value, $totals[ $team_id ] ) ):
|
if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $value, $totals[ $team_id ] ) ):
|
||||||
$totals[ $team_id ]['eventsplayed']++;
|
$totals[ $team_id ]['eventsplayed']++;
|
||||||
$totals[ $team_id ][ $value ]++;
|
$totals[ $team_id ][ $value ]++;
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
if ( $value && $value != '-1' ):
|
if ( $value && $value != '-1' ):
|
||||||
if ( $streaks[ $team_id ]['name'] == $value ):
|
|
||||||
|
// Add to streak counter
|
||||||
|
if ( $streaks[ $team_id ]['fire'] && ( $streaks[ $team_id ]['name'] == '' || $streaks[ $team_id ]['name'] == $value ) ):
|
||||||
|
$streaks[ $team_id ]['name'] = $value;
|
||||||
$streaks[ $team_id ]['count'] ++;
|
$streaks[ $team_id ]['count'] ++;
|
||||||
else:
|
else:
|
||||||
$streaks[ $team_id ]['name'] = $value;
|
$streaks[ $team_id ]['fire'] = 0;
|
||||||
$streaks[ $team_id ]['count'] = 1;
|
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
|
// Add to last 10 counter if sum is less than 10
|
||||||
|
if ( array_key_exists( $team_id, $last10s ) && array_key_exists( $value, $last10s[ $team_id ] ) && array_sum( $last10s[ $team_id ] ) < 10 ):
|
||||||
|
$last10s[ $team_id ][ $value ] ++;
|
||||||
|
endif;
|
||||||
|
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ):
|
if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ):
|
||||||
$totals[ $team_id ][ $key . 'for' ] += $value;
|
$totals[ $team_id ][ $key . 'for' ] += $value;
|
||||||
@@ -1053,21 +1098,32 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) {
|
|||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
foreach ( $streaks as $team_id => $streak ):
|
foreach ( $streaks as $team_id => $streak ):
|
||||||
// Compile streaks counter and add to totals
|
// Compile streaks counter and add to totals
|
||||||
$args=array(
|
if ( $streak['name'] ):
|
||||||
'name' => $streak['name'],
|
$args = array(
|
||||||
'post_type' => 'sp_outcome',
|
'name' => $streak['name'],
|
||||||
'post_status' => 'publish',
|
'post_type' => 'sp_outcome',
|
||||||
'posts_per_page' => 1
|
'post_status' => 'publish',
|
||||||
);
|
'posts_per_page' => 1
|
||||||
$outcomes = get_posts( $args );
|
);
|
||||||
|
$outcomes = get_posts( $args );
|
||||||
|
|
||||||
if ( $outcomes ):
|
if ( $outcomes ):
|
||||||
$outcome = $outcomes[0];
|
$outcome = $outcomes[0];
|
||||||
$totals[ $team_id ]['streak'] = $outcome->post_title . $streak['count'];
|
$totals[ $team_id ]['streak'] = $outcome->post_title . $streak['count'];
|
||||||
|
else:
|
||||||
|
$totals[ $team_id ]['streak'] = '—';
|
||||||
|
endif;
|
||||||
|
else:
|
||||||
|
$totals[ $team_id ]['streak'] = '—';
|
||||||
endif;
|
endif;
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
|
foreach ( $last10s as $team_id => $last10 ):
|
||||||
|
// Add last 10 to totals
|
||||||
|
$totals[ $team_id ]['last10'] = $last10;
|
||||||
|
endforeach;
|
||||||
|
|
||||||
$args = array(
|
$args = array(
|
||||||
'post_type' => 'sp_column',
|
'post_type' => 'sp_column',
|
||||||
'numberposts' => -1,
|
'numberposts' => -1,
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ function sportspress_team_columns_meta( $post ) {
|
|||||||
|
|
||||||
foreach ( $div_ids as $div_id ):
|
foreach ( $div_ids as $div_id ):
|
||||||
|
|
||||||
$totals = array( 'eventsplayed' => 0, 'streak' => 0 );
|
$totals = array( 'eventsplayed' => 0, 'streak' => 0, 'last10' => null );
|
||||||
|
|
||||||
foreach ( $result_labels as $key => $value ):
|
foreach ( $result_labels as $key => $value ):
|
||||||
$totals[ $key . 'for' ] = 0;
|
$totals[ $key . 'for' ] = 0;
|
||||||
@@ -85,6 +85,18 @@ function sportspress_team_columns_meta( $post ) {
|
|||||||
$totals[ $key ] = 0;
|
$totals[ $key ] = 0;
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
|
// Initialize streaks counter
|
||||||
|
$streak = array( 'name' => '', 'count' => 0, 'fire' => 1 );
|
||||||
|
|
||||||
|
// Initialize last 10 counter
|
||||||
|
$last10 = array();
|
||||||
|
|
||||||
|
// Add outcome types to last 10 counter
|
||||||
|
foreach( $outcome_labels as $key => $value ):
|
||||||
|
$last10[ $key ] = 0;
|
||||||
|
endforeach;
|
||||||
|
|
||||||
|
// Get all events involving the team in current season
|
||||||
$args = array(
|
$args = array(
|
||||||
'post_type' => 'sp_event',
|
'post_type' => 'sp_event',
|
||||||
'numberposts' => -1,
|
'numberposts' => -1,
|
||||||
@@ -106,27 +118,36 @@ function sportspress_team_columns_meta( $post ) {
|
|||||||
);
|
);
|
||||||
$events = get_posts( $args );
|
$events = get_posts( $args );
|
||||||
|
|
||||||
// Initialize streaks counter
|
|
||||||
$streak = array( 'name' => '', 'count' => 0 );
|
|
||||||
|
|
||||||
foreach( $events as $event ):
|
foreach( $events as $event ):
|
||||||
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
|
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
|
||||||
foreach ( $results as $team_id => $team_result ):
|
foreach ( $results as $team_id => $team_result ):
|
||||||
foreach ( $team_result as $key => $value ):
|
foreach ( $team_result as $key => $value ):
|
||||||
if ( $team_id == $post->ID ):
|
if ( $team_id == $post->ID ):
|
||||||
if ( $key == 'outcome' ):
|
if ( $key == 'outcome' ):
|
||||||
|
|
||||||
|
// Increment events played and outcome count
|
||||||
if ( array_key_exists( $value, $totals ) ):
|
if ( array_key_exists( $value, $totals ) ):
|
||||||
$totals['eventsplayed']++;
|
$totals['eventsplayed']++;
|
||||||
$totals[ $value ]++;
|
$totals[ $value ]++;
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
if ( $value && $value != '-1' ):
|
if ( $value && $value != '-1' ):
|
||||||
if ( $streak['name'] == $value ):
|
|
||||||
|
// Add to streak counter
|
||||||
|
if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $value ) ):
|
||||||
|
$streak['name'] = $value;
|
||||||
$streak['count'] ++;
|
$streak['count'] ++;
|
||||||
else:
|
else:
|
||||||
$streak['name'] = $value;
|
$streak['fire'] = 0;
|
||||||
$streak['count'] = 1;
|
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
|
// Add to last 10 counter if sum is less than 10
|
||||||
|
if ( array_key_exists( $value, $last10 ) && array_sum( $last10 ) < 10 ):
|
||||||
|
$last10[ $value ] ++;
|
||||||
|
endif;
|
||||||
|
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if ( array_key_exists( $key . 'for', $totals ) ):
|
if ( array_key_exists( $key . 'for', $totals ) ):
|
||||||
$totals[ $key . 'for' ] += $value;
|
$totals[ $key . 'for' ] += $value;
|
||||||
@@ -157,6 +178,9 @@ function sportspress_team_columns_meta( $post ) {
|
|||||||
$totals['streak'] = $outcome->post_title . $streak['count'];
|
$totals['streak'] = $outcome->post_title . $streak['count'];
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
|
// Add last 10 to totals
|
||||||
|
$totals['last10'] = $last10;
|
||||||
|
|
||||||
// Generate array of placeholder values for each league
|
// Generate array of placeholder values for each league
|
||||||
$placeholders[ $div_id ] = array();
|
$placeholders[ $div_id ] = array();
|
||||||
foreach ( $equations as $key => $value ):
|
foreach ( $equations as $key => $value ):
|
||||||
|
|||||||
Reference in New Issue
Block a user