Add home and away record to equation builder

This commit is contained in:
Brian Miyaji
2015-04-11 14:39:45 +10:00
parent 93aef64809
commit ac83d14fa3
4 changed files with 76 additions and 5 deletions

View File

@@ -42,6 +42,8 @@ class SP_Meta_Box_Equation {
case 'outcome': case 'outcome':
$options[ 'Outcomes' ] = self::optgroup( 'sp_outcome' ); $options[ 'Outcomes' ] = self::optgroup( 'sp_outcome' );
$options[ 'Outcomes' ]['$gamesback'] = __( 'Games Back', 'sportspress' ); $options[ 'Outcomes' ]['$gamesback'] = __( 'Games Back', 'sportspress' );
$options[ 'Outcomes' ]['$homerecord'] = __( 'Home Record', 'sportspress' );
$options[ 'Outcomes' ]['$awayrecord'] = __( 'Away Record', 'sportspress' );
$options[ 'Outcomes' ]['$streak'] = __( 'Streak', 'sportspress' ); $options[ 'Outcomes' ]['$streak'] = __( 'Streak', 'sportspress' );
$options[ 'Outcomes' ]['$last5'] = __( 'Last 5', 'sportspress' ); $options[ 'Outcomes' ]['$last5'] = __( 'Last 5', 'sportspress' );
$options[ 'Outcomes' ]['$last10'] = __( 'Last 10', 'sportspress' ); $options[ 'Outcomes' ]['$last10'] = __( 'Last 10', 'sportspress' );

View File

@@ -104,6 +104,10 @@ class SP_League_Table extends SP_Custom_Post{
$last5s = array(); $last5s = array();
$last10s = array(); $last10s = array();
// Initialize record counters
$homerecords = array();
$awayrecords = array();
foreach ( $team_ids as $team_id ): foreach ( $team_ids as $team_id ):
if ( ! $team_id ) if ( ! $team_id )
continue; continue;
@@ -115,10 +119,16 @@ class SP_League_Table extends SP_Custom_Post{
$last5s[ $team_id ] = array(); $last5s[ $team_id ] = array();
$last10s[ $team_id ] = array(); $last10s[ $team_id ] = array();
// Add outcome types to team last counters // Initialize team record counters
$homerecords[ $team_id ] = array();
$awayrecords[ $team_id ] = array();
// Add outcome types to team last and record counters
foreach( $outcome_labels as $key => $value ): foreach( $outcome_labels as $key => $value ):
$last5s[ $team_id ][ $key ] = 0; $last5s[ $team_id ][ $key ] = 0;
$last10s[ $team_id ][ $key ] = 0; $last10s[ $team_id ][ $key ] = 0;
$homerecords[ $team_id ][ $key ] = 0;
$awayrecords[ $team_id ][ $key ] = 0;
endforeach; endforeach;
// Initialize team totals // Initialize team totals
@@ -187,6 +197,8 @@ class SP_League_Table extends SP_Custom_Post{
$minutes = get_post_meta( $event->ID, 'sp_minutes', true ); $minutes = get_post_meta( $event->ID, 'sp_minutes', true );
if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 ); if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 );
$i = 0;
foreach ( $results as $team_id => $team_result ): foreach ( $results as $team_id => $team_result ):
if ( ! in_array( $team_id, $team_ids ) ) if ( ! in_array( $team_id, $team_ids ) )
@@ -229,6 +241,17 @@ class SP_League_Table extends SP_Custom_Post{
$last10s[ $team_id ][ $outcome ] ++; $last10s[ $team_id ][ $outcome ] ++;
endif; endif;
// Add to home or away record
if ( 0 === $i ) {
if ( array_key_exists( $team_id, $homerecords ) && array_key_exists( $outcome, $homerecords[ $team_id ] ) ) {
$homerecords[ $team_id ][ $outcome ] ++;
}
} else {
if ( array_key_exists( $team_id, $awayrecords ) && array_key_exists( $outcome, $awayrecords[ $team_id ] ) ) {
$awayrecords[ $team_id ][ $outcome ] ++;
}
}
endif; endif;
endforeach; endforeach;
@@ -246,6 +269,8 @@ class SP_League_Table extends SP_Custom_Post{
endforeach; endif; endforeach; endif;
$i++;
endforeach; endforeach;
endforeach; endforeach;
@@ -287,6 +312,16 @@ class SP_League_Table extends SP_Custom_Post{
$totals[ $team_id ]['last10'] = $last10; $totals[ $team_id ]['last10'] = $last10;
endforeach; endforeach;
foreach ( $homerecords as $team_id => $homerecord ):
// Add home record to totals
$totals[ $team_id ]['homerecord'] = $homerecord;
endforeach;
foreach ( $awayrecords as $team_id => $awayrecord ):
// Add away record to totals
$totals[ $team_id ]['awayrecord'] = $awayrecord;
endforeach;
$args = array( $args = array(
'post_type' => 'sp_column', 'post_type' => 'sp_column',
'numberposts' => -1, 'numberposts' => -1,
@@ -348,7 +383,7 @@ class SP_League_Table extends SP_Custom_Post{
if ( '$gamesback' == $stat->equation ) if ( '$gamesback' == $stat->equation )
$gb_column = $stat->post_name; $gb_column = $stat->post_name;
if ( ! in_array( $stat->equation, array( '$gamesback', '$streak', '$last5', '$last10' ) ) ): if ( ! in_array( $stat->equation, array( '$gamesback', '$streak', '$last5', '$last10', '$homerecord', '$awayrecord' ) ) ):
// Adjustments // Adjustments
$adjustment = sp_array_value( $adjustments, $team_id, array() ); $adjustment = sp_array_value( $adjustments, $team_id, array() );

View File

@@ -77,7 +77,7 @@ class SP_Team extends SP_Custom_Post {
foreach ( $div_ids as $div_id ): foreach ( $div_ids as $div_id ):
$totals = array( 'eventsplayed' => 0, 'eventminutes' => 0, 'streak' => 0, 'last5' => null, 'last10' => null ); $totals = array( 'eventsplayed' => 0, 'eventminutes' => 0, 'streak' => 0, 'last5' => null, 'last10' => null, 'homerecord' => null, 'awayrecord' => null );
foreach ( $result_labels as $key => $value ): foreach ( $result_labels as $key => $value ):
$totals[ $key . 'for' ] = 0; $totals[ $key . 'for' ] = 0;
@@ -95,10 +95,16 @@ class SP_Team extends SP_Custom_Post {
$last5 = array(); $last5 = array();
$last10 = array(); $last10 = array();
// Add outcome types to last counters // Initialize record counters
$homerecord = array();
$awayrecord = array();
// Add outcome types to last and record counters
foreach( $outcome_labels as $key => $value ): foreach( $outcome_labels as $key => $value ):
$last5[ $key ] = 0; $last5[ $key ] = 0;
$last10[ $key ] = 0; $last10[ $key ] = 0;
$homerecord[ $key ] = 0;
$awayrecord[ $key ] = 0;
endforeach; endforeach;
// Get all events involving the team in current season // Get all events involving the team in current season
@@ -150,6 +156,8 @@ class SP_Team extends SP_Custom_Post {
$minutes = get_post_meta( $event->ID, 'sp_minutes', true ); $minutes = get_post_meta( $event->ID, 'sp_minutes', true );
if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 ); if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 );
$i = 0;
foreach ( $results as $team_id => $team_result ): foreach ( $results as $team_id => $team_result ):
if ( is_array( $team_result ) ): foreach ( $team_result as $key => $value ): if ( is_array( $team_result ) ): foreach ( $team_result as $key => $value ):
if ( $team_id == $this->ID ): if ( $team_id == $this->ID ):
@@ -189,6 +197,17 @@ class SP_Team extends SP_Custom_Post {
$last10[ $outcome ] ++; $last10[ $outcome ] ++;
endif; endif;
// Add to home or away record
if ( 0 === $i ) {
if ( array_key_exists( $outcome, $homerecord ) ) {
$homerecord[ $outcome ] ++;
}
} else {
if ( array_key_exists( $outcome, $awayrecord ) ) {
$awayrecord[ $outcome ] ++;
}
}
endif; endif;
endforeach; endforeach;
@@ -206,6 +225,7 @@ class SP_Team extends SP_Custom_Post {
endif; endif;
endif; endif;
endforeach; endif; endforeach; endif;
$i++;
endforeach; endforeach;
endforeach; endforeach;
@@ -228,9 +248,11 @@ class SP_Team extends SP_Custom_Post {
$totals['streak'] = $abbreviation . $streak['count']; $totals['streak'] = $abbreviation . $streak['count'];
endif; endif;
// Add last counters to totals // Add last and record counters to totals
$totals['last5'] = $last5; $totals['last5'] = $last5;
$totals['last10'] = $last10; $totals['last10'] = $last10;
$totals['homerecord'] = $homerecord;
$totals['awayrecord'] = $awayrecord;
// Generate array of placeholder values for each league // Generate array of placeholder values for each league
$placeholders[ $div_id ] = array(); $placeholders[ $div_id ] = array();

View File

@@ -1059,6 +1059,18 @@ if ( !function_exists( 'sp_solve' ) ) {
return '-'; return '-';
endif; endif;
elseif ( strpos( $equation, '$homerecord' ) !== false ):
// Return imploded string
$homerecord = sp_array_value( $vars, 'homerecord', array( 0 ) );
return implode( '-', $homerecord );
elseif ( strpos( $equation, '$awayrecord' ) !== false ):
// Return imploded string
$awayrecord = sp_array_value( $vars, 'awayrecord', array( 0 ) );
return implode( '-', $awayrecord );
endif; endif;
// Remove unnecessary variables from vars before calculating // Remove unnecessary variables from vars before calculating