Location: PHPKode > scripts > Scoreboard class > scoreboard-class/Scoreboard.php
<?

/*
 * Class: Scoreboard
 *
 * A class to handle the submission and retrieval of scores
 * in a number of high score tables.
 *
 * Use this class to fetch the scoreboard data from any number
 * of hi-score tables. This is ideally suited for code that needs
 * to submit and retrieve scores for online games.
 *
 * The various methods in here allow you to retrieve scoreboards
 * for this week and last week for each game, plus scoreboards
 * for this week so far and last week overall.
 *
 * End of week rankings are supported, as is joint/equal ranking
 * on a per game basis.
 *
 * Distributed under the LGPL license:
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Duncan Gough
 * 3rdSense.com
 *
 * Home  http://www.suttree.com
 * Work  http://www.3rdsense.com
 * Play! http://www.playaholics.com
 */

require_once( "SQL/SQL.php" );

class Scoreboard {

        var $scoreboard = array();

        function Scoreboard() {

            $this->q = new SQL();

        }

        // Returns a scoreboard object containing all the scores for game X, this week,
        function this_week( $game_id ) {
            
           $this->scoreboard = $this->q->prepareAndExecute( $this->q->getSQL( "scores_this_week" ), array( $game_id ) );
           
           return $this;

        }

        // Returns a scoreboard object containing all the scores for game X, last week.
        function last_week( $game_id ) {
            
           $this->scoreboard = $this->q->prepareAndExecute( $this->q->getSQL( "scores_last_week" ), array( $game_id ) );

           return $this;

        }

        // Submit a score to the database only if it is better than your best score so far this week
        function submit_score( $user_id, $game, $score ) {

           $previous_score = $this->q->prepareAndExecute( $this->q->getSQL( "get_best_score_this_week" ), array( $game, $user_id) );

           if( $previous_score[ "BestScore" ] == 0 ) {

               return( $row = $this->q->prepareAndExecute( $this->q->getSQL( "submit_score" ), array( $user_id, $score, $game ) ) );

           } elseif( $score >= $previous_score[ "BestScore" ] ) { 

                return( $row = $this->q->prepareAndExecute( $this->q->getSQL( "update_score" ), array( $score, $user_id, $game, $previous_score[ "ID" ] ) ) );

           } else {

               return;

           }

        }

        // Returns a scoreboard object containing only the scores submitted by a specific player this week
        function personal_scoreboard_this_week( $user_id ) {

            if( $user_id > 0 ) {

                $this->scoreboard = $this->q->prepareAndExecute( $this->q->getSQL( "your_scores_this_week" ), array( $user_id ) );

                if( !$this->scoreboard ) {

                    return NULL;

               } elseif( $this->scoreboard[ "GameName" ] != "" ) {

                    // You've only got once score so far..
                    $game_ranking = $this->q->prepareAndExecute( $this->q->getSQL( "your_ranking_this_week" ), array( $this->scoreboard[ "GameName" ], $this->scoreboard[ "Score" ]) );      

                    $equal_ranking = $this->q->prepareAndExecute( $this->q->getSQL( "equal_ranking_this_week"), array( $this->scoreboard[ "GameName" ], $this->scoreboard[ "Score" ]) );    

                    $ranking = $game_ranking[ "Ranking" ] - ( $equal_ranking[ "EqualRanking" ] - 1 );

                    $this->scoreboard[ "Ranking" ] = $ranking;

                } else {

                    // You've got multiple high scores..
                    foreach( $this->scoreboard as $key => $game ) {

                        $game_ranking = $this->q->prepareAndExecute( $this->q->getSQL( "your_ranking_this_week" ), array( $game[ "GameName" ], $game[ "Score" ]) );

                        $equal_ranking = $this->q->prepareAndExecute( $this->q->getSQL( "equal_ranking_this_week" ), array( $game[ "GameName" ], $game[ "Score" ]) );

                        // Calculates your ranking in the scoreboard, taking into account scores equal to yours
                        $ranking = $game_ranking[ "Ranking" ] - ( $equal_ranking[ "EqualRanking" ] - 1 );

                        $this->scoreboard[ $key ][ "Ranking" ] = $ranking;

                    }

                    usort( $this->scoreboard, array( "Scoreboard", "_sortRanking" ) );

                }

                return $this;

            } else {
                
                return FALSE;

            }

        }

        // Returns a scoreboard object containing only the scores submitted by a specific player last week
        function personal_scoreboard_last_week( $user_id ) {

            if( $user_id > 0 ) {

                $this->scoreboard = $this->q->prepareAndExecute( $this->q->getSQL( "your_scores_last_week" ), array( $user_id ) );

                if( !$this->scoreboard ) {

                    return NULL;

                } elseif( $this->scoreboard[ "GameName" ] != "" ) {

                    // You've only got once score so far..
                    $game_ranking = $this->q->prepareAndExecute( $this->q->getSQL( "your_ranking_last_week" ), array( $this->scoreboard[ "GameName" ], $this->scoreboard[ "Score" ]) );

                    $equal_ranking = $this->q->prepareAndExecute( $this->q->getSQL( "equal_ranking_last_week"), array( $this->scoreboard[ "GameName" ], $this->scoreboard[ "Score" ]) );

                    $ranking = $game_ranking[ "Ranking" ] - ( $equal_ranking[ "EqualRanking" ] - 1 );

                    $this->scoreboard[ "Ranking" ] = $ranking;

                } else {

                    foreach( $this->scoreboard as $key => $game ) {
    
                        $game_ranking = $this->q->prepareAndExecute( $this->q->getSQL( "your_ranking_last_week" ), array( $game[ "GameName" ], $game[ "Score" ]) );
    
                        $equal_ranking = $this->q->prepareAndExecute( $this->q->getSQL( "equal_ranking_last_week" ), array( $game[ "GameName" ], $game[ "Score" ]) );
    
                        // Calculates your ranking in the scoreboard, taking into account scores equal to yours
                        $ranking = $game_ranking[ "Ranking" ] - ( $equal_ranking[ "EqualRanking" ] - 1 );
    
                        $this->scoreboard[ $key ][ "Ranking" ] = $game_ranking[ "Ranking" ];
                                            
                    }   

                    usort( $this->scoreboard, array( "Scoreboard", "_sortRanking" ));

                }

                return $this;

            } else {
                                                
                    return FALSE;
                                                                
            }

        }

        // Sorts an array of scores on their points ranking
        function _sortRanking( $a, $b ) {

            return $a[ "Ranking" ] > $b[ "Ranking" ] ? +1 : -1;

        } 

}

?>
Return current item: Scoreboard class