WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Predictions and Groups

[Resolved] Predictions and Groups

  • latinosamorir
    Member

    @latinosamorir

    Hi! Awesome plugin!

    Quick question. As you make your predictions, is there a shortcode that can show the user the team points as they enter their prediction?

    Also, is there a way that the “Winner of Group A” gets filled as the predictions get entered from the Group Stage?

    Thank you!

    https://wordpress.org/plugins/football-pool/

Viewing 15 replies - 1 through 15 (of 33 total)
  • Plugin Author AntoineH
    Participant

    @antoineh

    Hi,
    it is not possible to automatically enter the winners from the group phase in the final rounds. Every sport has its own rules on what to do in case two teams end the group phase with the same points. And even the FIFA rules use information that is not from the tournament after goals and the result between teams are taken into account.

    I don’t understand the question about the shortcode. But the available shortcodes are mentioned in the help files.

    latinosamorir
    Member

    @latinosamorir

    Hi Antoine.

    Very true, but at least to get an idea of how, based on your predictions, the groups will end up.

    We know the basics: win 3 points, tie 1 point, goal difference, etc.

    You already create the groups based on the Match data from the backend, if I wanted to use the same template but use different data to calculate group standings, where do you have that code and how can I go about it?

    My php level is intermediate.

    Thanks!

    Plugin Author AntoineH
    Participant

    @antoineh

    The class Football_Pool_Groups contains the functions you are looking for. Function compare_teams contains the sorting method to determine the standing in the group. The function get_standing_for_team calculates the points.

    latinosamorir
    Member

    @latinosamorir

    Thank you.

    How would I use the private functions of your class in my theme functions.php file?

    Also, which function pulls the data from the matches? I would need to modify the SQL query to pull data from the user’s predictions.

    Thank you!

    Giovanni

    latinosamorir
    Member

    @latinosamorir

    I take that back, I guess I’m a beginner. I am not familiar with classes and objects.

    Any quick tip?

    Plugin Author AntoineH
    Participant

    @antoineh

    A quick tip? Let it go and stick with the out-of-the-box functionality 😉
    If you are not at least an intermediate coder (I am one) then it will be very hard to do customizations in the PHP code yourself.

    To answer your question about the static functions: those can’t be used outside the class scope. So you would have to make them public first.

    latinosamorir
    Member

    @latinosamorir

    I did it!

    http://www.worldcup2014bracket.co/my-predictions

    I guess I am intermediate but lacking knowledge of classes and objects. 😉

    I created my own comparison function to use uasort and is not the cleanest code (I use a lot of queries to get the information (scores, team names) to calculate wins, points, losses, etc but it works!

    Very useful for when a user makes predictions and can see the group standings based on their predictions! =)

    Not for this project but I’d love to learn AJAX to automatically save the predictions and update the group standing table as users enter information.

    Thanks again and awesome plugin!

    Giovanni

    Plugin Author AntoineH
    Participant

    @antoineh

    Well done 🙂

    latinosamorir or anyone else who can help me, i am also looking for the exact same function. can you please share how you did it? i appreciate it.

    another thing I liked in your site, how did you separate prediction per group ? like you could do all Group A then B and so on. thanks again buddy.

    Plugin Author AntoineH
    Participant

    @antoineh

    There is a prediction form shortcode that displays the prediction form for a subset of matches. See the help page for details.

    latinosamorir
    Member

    @latinosamorir

    @raul77 sorry for the delay. i’m traveling at the moment.

    i get back next monday and will share the code.

    Thanks for both replies. looking forward to the code. I really like that feature.

    latinosamorir
    Member

    @latinosamorir

    I must say, this is not the cleanest code, but it gets the job done. Any feedback appreciated.

    /*** Add Group Predictions ***/
    
    function gd_prediction_group_results( $atts ){
    	global $wpdb;
    	$user_id = get_current_user_id();
    	$prefix = FOOTBALLPOOL_DB_PREFIX;
    	$output = '';
    
    	extract( shortcode_atts( array(
    				'group_letter' => null,
    				'matches' => null,
    			), $atts ) );
    
    	if ($group_letter == "A"){
    		$match_ids = array(1, 2, 16, 20,  35, 36);
    	}
    	elseif ($group_letter =="B"){
    		$match_ids = array(3, 4, 18, 19,  33, 34);
    	}
    	elseif ($group_letter =="C"){
    		$match_ids = array(5,8,21,23,39,40);
    	}
    	elseif ($group_letter == "D"){
    		$match_ids = array(6,7,22,24,37,38);
    	}
    	elseif ($group_letter == "E"){
    		$match_ids = array(9,10,25,26,43,44);
    	}
    	elseif ($group_letter == "F"){
    		$match_ids = array(11,13,27,29,41,42);
    	}
    	elseif ($group_letter == "G"){
    		$match_ids = array(12,14,28,32,45,46);
    	}
    	elseif ($group_letter == "H"){
    		$match_ids = array(15,17,30,31,47,48);
    	}
    	else{
    		$output .="<br/>Forgot to specify which group!";
    	}
    
    	$i=0;
    	$scores = array();
    	$points = array();
    	$group_teams = array();
    	$team_ids = array();
    	$goalsF = array();
    	$goalsA = array();
    	$goalD = array();
    	$wins = array();
    	$loss = array();
    	$draws = array();
    
    	foreach ($match_ids as $match_id) {
    
    		$sql_scores = "SELECT p.home_score AS home, p.away_score AS away
    						FROM {$prefix}predictions p
    						WHERE user_id = $user_id AND match_id = $match_id";
    		$scores = $wpdb->get_row( $sql_scores, ARRAY_A );
    
    		// Get teams' info
    
    		$sql_hometeam_id = "SELECT home_team_id FROM pool_cgq_matches WHERE id=$match_id";
    		$hometeam_id = $wpdb->get_var($sql_hometeam_id);
    		$sql_awayteam_id = "SELECT away_team_id FROM pool_cgq_matches WHERE id=$match_id";
    		$awayteam_id = $wpdb->get_var($sql_awayteam_id);
    
    		if(!in_array($hometeam_id, $group_teams)){
    
    			//Get names of teams in group
    
    			$sql_hometeam_name = "SELECT name
    						FROM {$prefix}teams
    						WHERE id = $hometeam_id";
    			$group_teams[$hometeam_id] = $wpdb->get_var( $sql_hometeam_name);
    			$team_ids[] = $hometeam_id;
    
    			// Get goals
    
    			$goalsF[$hometeam_id] = $scores['home'] + $goalsF[$hometeam_id];
    			$goalsA[$hometeam_id] = $scores['away'] + $goalsA[$hometeam_id];
    		}
    
    		if(!in_array($awayteam_id, $group_teams)){
    			$sql_awayteam_name = "SELECT name
    						FROM {$prefix}teams
    						WHERE id = $awayteam_id";
    			$group_teams[$awayteam_id] = $wpdb->get_var( $sql_awayteam_name);
    			$team_ids[] = $awayteam_id;
    			$goalsF[$awayteam_id] = $scores['away'] + $goalsF[$awayteam_id];
    			$goalsA[$awayteam_id] = $scores['home'] + $goalsA[$awayteam_id];
    		}
    
    		// Compare scores
    
    		$wins[$hometeam_id] = $wins[$hometeam_id] + 0;
    		$loss[$awayteam_id] = $loss[$awayteam_id] + 0;
    		$draws[$hometeam_id] = $draws[$hometeam_id] + 0;
    		$draws[$awayteam_id] = $draws[$awayteam_id] + 0;		
    
    		if($scores['home']>$scores['away']){
    			$points[$hometeam_id] = $points[$hometeam_id] + 3;
    			$points[$awayteam_id] = $points[$awayteam_id] + 0;
    			$wins[$hometeam_id] = $wins[$hometeam_id] + 1;
    			$loss[$awayteam_id] = $loss[$awayteam_id] + 1;
    		}
    		elseif($scores['home']==$scores['away'] && !empty($scores['home'])){
    			$points[$hometeam_id] = $points[$hometeam_id] + 1;
    			$points[$awayteam_id] = $points[$awayteam_id] + 1;
    			$draws[$hometeam_id] = $draws[$hometeam_id] + 1;
    			$draws[$awayteam_id] = $draws[$awayteam_id] + 1;
    		}
    		elseif($scores['away']>$scores['home']){
    			$points[$awayteam_id] = $points[$awayteam_id] + 3;
    			$points[$hometeam_id] = $points[$hometeam_id] + 0;
    			$wins[$awayteam_id] = $wins[$awayteam_id] + 1;
    			$loss[$hometeam_id] = $loss[$hometeam_id] + 1;
    		}
    
    		$i++;
    	} 
    
    		$goal_D[0] = $goalsF[$team_ids[0]] - $goalsA[$team_ids[0]];
    		$goal_D[1] = $goalsF[$team_ids[1]] - $goalsA[$team_ids[1]];
    		$goal_D[2] = $goalsF[$team_ids[2]] - $goalsA[$team_ids[2]];
    		$goal_D[3] = $goalsF[$team_ids[3]] - $goalsA[$team_ids[3]];
    
    		$groups = array(
        array('teamname' => $group_teams[$team_ids[0]], 'points' => $points[$team_ids[0]], 'goals_favor' => $goalsF[$team_ids[0]],'goals_against' => $goalsA[$team_ids[0]], 'goal_difference' => $goal_D[0], 'wins' => $wins[$team_ids[0]], 'loss' => $loss[$team_ids[0]], 'draws' => $draws [$team_ids[0]]),
        array('teamname' => $group_teams[$team_ids[1]], 'points' => $points[$team_ids[1]], 'goals_favor' => $goalsF[$team_ids[1]],'goals_against' => $goalsA[$team_ids[1]], 'goal_difference' => $goal_D[1], 'wins' => $wins[$team_ids[1]], 'loss' => $loss[$team_ids[1]], 'draws' => $draws [$team_ids[1]]),
        array('teamname' => $group_teams[$team_ids[2]], 'points' => $points[$team_ids[2]], 'goals_favor' => $goalsF[$team_ids[2]],'goals_against' => $goalsA[$team_ids[2]], 'goal_difference' => $goal_D[2], 'wins' => $wins[$team_ids[2]], 'loss' => $loss[$team_ids[2]], 'draws' => $draws [$team_ids[2]]),
        array('teamname' => $group_teams[$team_ids[3]], 'points' => $points[$team_ids[3]], 'goals_favor' => $goalsF[$team_ids[3]],'goals_against' => $goalsA[$team_ids[3]], 'goal_difference' => $goal_D[3], 'wins' => $wins[$team_ids[3]], 'loss' => $loss[$team_ids[3]], 'draws' => $draws [$team_ids[3]]),
    	);
    
    $groups2 = sort_by_key($groups, 'points');
    
    $output .= "<table class='ranking'><thead><tr>
    <th class='team'>Team</th>
    <th class='wins'><span title='wins'>w</span></th>
    <th class='draws'><span title='draws'>d</span></th>
    <th class='losses'><span title='losses'>l</span></th>
    <th class='points'><span title='points'>Pts</span></th>
    <th class='losses'>Goals</th>
    </tr></thead><tbody>";
    
    foreach ($groups2 as $group) {
    $output .="<tr>
    <td class='team' style='text-align:center;'>" . $group['teamname'] . "</td>
    <td class='wins' style='text-align:center;'>".$group['wins']."</td>
    <td class='draws' style='text-align:center;'>".$group['draws']."</td>
    <td class='losses' style='text-align:center;'>" . $group['loss'] . "</td>
    <td class='points' style='text-align:center;'>".$group['points']."</td>
    <td class='goals' style='text-align:center;'>(".$group['goals_favor']."-".$group['goals_against']."),  ".$group['goal_difference']."</td>
    </tr>";
    
    }
    $output .= "</tbody></table>";
    
    	return $output;
    }
    
    add_shortcode( 'prediction_group_results', 'gd_prediction_group_results' );
    latinosamorir
    Member

    @latinosamorir

    I forgot to add these two other functions:

    function sort_by_key ($arr,$key) {
        global $key2sort;
        $key2sort = $key;
        uasort($arr, 'sbk');
        return ($arr);
    } 
    
    function sbk ($a, $b) {
    	global $key2sort;
    	if ($a[$key2sort]==$b[$key2sort])
    	{
    		if($a['goal_difference'] == $b['goal_difference'])
    		{
    			return ($a['goals_favor'] > $b['goals_favor'])?-1:1;
    		}
    		else{
    			return ($a['goal_difference'] > $b['goal_difference'])?-1:1;
    		}
    	}
    	else{
    		return ($a[$key2sort]>$b[$key2sort])?-1:1;
    	}
    }

    You can call the table by using this format of shortcode:

    [prediction_group_results group_letter=”A”]

Viewing 15 replies - 1 through 15 (of 33 total)
  • The topic ‘[Resolved] Predictions and Groups’ is closed to new replies.