Guest

Untitled

By: a guest on Mar 10th, 2010  |  syntax: PHP  |  size: 3.01 KB  |  hits: 134  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. class Highscore
  2. {
  3.         private $highscoreEntries;
  4.  
  5.         public function __construct($skill = SKILL_LEVEL, $entries, $pagination = 1)
  6.         {
  7.                 $pagination--;
  8.  
  9.                 $cache = Cache::getInstance();
  10.                 if ($cache->isEnabled() && $cache->fetch('highscore_cache_' . $skill . '_' . $entries . '_' . $pagination, $this->highscoreEntries))
  11.                         return;
  12.  
  13.                 $db = Database::getInstance();
  14.                 $this->highscoreEntries = array();
  15.                 switch ($skill) {
  16.                         case SKILL_LEVEL:
  17.                         {
  18.                                 $result = $db->query('SELECT `name`, `level`, `experience`, `redskull` FROM `players` ORDER BY `experience` DESC LIMIT ' . ($pagination * $entries) . ', ' . $entries . ';');
  19.                                 while (($row = $db->fetchRows($result)))
  20.                                         array_push($this->highscoreEntries, array('name' => $row['name'], 'skill' => $row['level'], 'tries' => $row['experience'], 'redskull' => $row['redskull']));
  21.  
  22.                                 $db->freeResult($result);
  23.                                 break;
  24.                         }
  25.  
  26.                         case SKILL_MAGIC:
  27.                         {
  28.                                 $result = $db->query('SELECT `name`, `maglevel`, `manaspent` FROM `players` ORDER BY `maglevel` DESC, `manaspent` DESC LIMIT ' . ($pagination * $entries) . ', ' . $entries . ';');
  29.                                 while (($row = $db->fetchRows($result)))
  30.                                         array_push($this->highscoreEntries, array('name' => $row['name'], 'skill' => $row['maglevel'], 'tries' => $row['manaspent']));
  31.  
  32.                                 $db->freeResult($result);
  33.                                 break;
  34.                         }
  35.  
  36.                         default:
  37.                         {
  38.                                 $result = $db->query('SELECT `player_id`, `value`, `count` FROM `player_skills` WHERE `skillid` = ' . $skill . ' ORDER BY `value` DESC, `count` DESC LIMIT ' . ($pagination * $entries) . ', ' . $entries . ';');
  39.                                 while (($row = $db->fetchRows($result)))
  40.                                         array_push($this->highscoreEntries, array('name' => Player::getNameById($row['player_id']), 'skill' => $row['value'], 'tries' => $row['count']));
  41.  
  42.                                 $db->freeResult($result);
  43.                                 break;
  44.                         }
  45.                 }
  46.  
  47.                 if ($cache->isEnabled())
  48.                         $cache->register('highscore_cache_' . $skill . '_' . $entries . '_' . $pagination, $this->highscoreEntries, 3600);
  49.         }
  50.  
  51.         public function __destruct()
  52.         {
  53.                 if (isset($this->highscoreEntries))
  54.                         unset($this->highscoreEntries);
  55.         }
  56.  
  57.         public function getEntries()
  58.         {
  59.                 return $this->highscoreEntries;
  60.         }
  61. }