Remade Highscores: Added multi-page support, built-in score type navigation, using GET instead of POST.

Tested on TFS 1.0 and 0.3/4, but should work on 0.2 as well.
This commit is contained in:
Stefan Brannfjell 2014-02-16 11:07:04 +01:00
parent a236ec2db8
commit b6cf2a97e0
4 changed files with 135 additions and 205 deletions

View File

@ -4,7 +4,7 @@
// TFS 0.3 = TFS_03 (If ur using 0.3.6, set $config['salt'] to false)!
// TFS 0.4 = TFS_03
// TFS 1.0 = TFS_10 (Under developement)
$config['TFSVersion'] = 'TFS_10';
$config['TFSVersion'] = 'TFS_04';
$config['site_title'] = 'Znote AAC';
$config['site_title_context'] = 'Because open communities are good communities. :3';
@ -14,13 +14,13 @@
// ------------------------ \\
// phpmyadmin username for OT server: (DONT USE "root" if ur hosting to public.).
$config['sqlUser'] = 'dev';
$config['sqlUser'] = 'gremlee';
// phpmyadmin password for OT server:
$config['sqlPassword'] = 'dev';
$config['sqlPassword'] = 'gremlee';
// The database name to connect to. (This is usually same as username).
$config['sqlDatabase'] = 'dev';
$config['sqlDatabase'] = 'gremlee';
// Hostname is usually localhost or 127.0.0.1.
$config['sqlHost'] = 'localhost';
@ -140,6 +140,13 @@
// WEBSITE STUFF \\
// -------------- \\
// Highscore configuration
$config['highscore'] = array(
'rows' => 100,
'rowsPerPage' => 20,
'ignoreGroupId' => 2, // Ignore group id higher than this (staff)
);
// ONLY FOR TFS 0.2 (TFS 0.3/4 users don't need to care about this, as its fully loaded from db)
$config['house'] = array(
'house_file' => 'C:\test\Mystic Spirit_0.2.5\data\world\forgotten-house.xml',

View File

@ -809,103 +809,35 @@ function user_character_list_count($account_id) {
// END MY ACCOUNT RELATED
// HIGHSCORE FUNCTIONS \\(I think I will move this to an own file later)
function highscore_getAll() {
$result = array();
for ($i = 0; $i <= 6; $i++) {
$result[$i] = highscore_skills($i);
}
$result[7] = highscore_experience();
$result[8] = highscore_maglevel();
return $result;
}
// TFS 1.0 highscore
function highscore_getAll_10($from = 0, $to = 30) {
$result = array();
for ($i = 0; $i <= 8; $i++) {
$result[$i] = highscore_getSkill_10($i, $from, $to);
}
return $result;
}
function highscore_getSkill_10($id = 8, $from = 0, $to = 30) {
$skills = array(
0 => 'skill_fist',
1 => 'skill_club',
2 => 'skill_sword',
3 => 'skill_axe',
4 => 'skill_dist',
5 => 'skill_shielding',
6 => 'skill_fishing',
8 => 'maglevel',
7 => 'level',
);
if ($id < 7 || $id > 7) $scores = mysql_select_multi("SELECT `". $skills[$id] ."` AS `value`, `name`, `vocation` FROM `players` WHERE `group_id`<'2' ORDER BY `". $skills[$id] ."` DESC LIMIT {$from}, {$to};");
else $scores = mysql_select_multi("SELECT `". $skills[$id] ."` AS `level`, `experience` AS `value`, `name`, `vocation` FROM `players` WHERE `group_id`<'2' ORDER BY `experience` DESC LIMIT {$from}, {$to};");
for ($i = 0; $i < count($scores); $i++) $scores[$i]['vocation'] = vocation_id_to_name($scores[$i]['vocation']);
return $scores;
}
// Returns an array containing up to 30 best players in terms of (selected skillid). Returns player ID and skill value.
function highscore_skills($skillid) {
$skillid = (int)$skillid;
$data = mysql_select_multi("SELECT `player_id`, `value` FROM `player_skills` WHERE `skillid`='$skillid' ORDER BY `value` DESC LIMIT 0, 30");
if ($data !== false) {
for ($i = 0; $i < count($data); $i++) {
// Fetch extra data from SQL players table
if ($skillid == 6 || $skillid == 5) $vd = mysql_select_single("SELECT `vocation`, `group_id`, `name` FROM `players` WHERE `id` = '". $data[$i]['player_id'] ."';");
else $vd = mysql_select_single("SELECT `group_id`, `name` FROM `players` WHERE `id` = '". $data[$i]['player_id'] ."';");
// If skillid is fish fighting, lets display vocation name instead of id.
if ($skillid == 6 || $skillid == 5) {
if ($vd !== false) $data[$i]['vocation'] = vocation_id_to_name($vd['vocation']);
else $data[$i]['vocation'] = 'Unknown';
}
// Happen to every skill group
$data[$i]['group_id'] = $vd['group_id'];
$data[$i]['name'] = $vd['name'];
unset($data[$i]['player_id']);
}
}
return $data;
}
// Returns an array containing up to 30 best players in terms of experience. Returns name, experience, vocation and level.
function highscore_experience() {
$data = mysql_select_multi("SELECT `name`, `experience` as `value`, `vocation`, `level`, `group_id` FROM `players` WHERE `experience`>500 ORDER BY `experience` DESC LIMIT 0, 30");
if ($data !== false) {
for ($i = 0; $i < count($data); $i++) {
$data[$i]['vocation'] = vocation_id_to_name($data[$i]['vocation']);
}
// HIGHSCORE FUNCTIONS \\
function fetchAllScores($rows, $tfs, $g) {
// Return scores ordered by type
$data = array();
if ($tfs == 'TFS_10') {
$data[1] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `skill_club` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `skill_club` DESC LIMIT 0, $rows;");
$data[2] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `skill_sword` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `skill_sword` DESC LIMIT 0, $rows;");
$data[3] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `skill_axe` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `skill_axe` DESC LIMIT 0, $rows;");
$data[4] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `skill_dist` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `skill_dist` DESC LIMIT 0, $rows;");
$data[5] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `skill_shielding` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `skill_shielding` DESC LIMIT 0, $rows;");
$data[6] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `skill_fishing` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `skill_fishing` DESC LIMIT 0, $rows;");
$data[7] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `experience`, `level` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `experience` DESC LIMIT 0, $rows;");
$data[8] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `maglevel` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `maglevel` DESC LIMIT 0, $rows;");
$data[9] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `skill_fist` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `skill_fist` DESC LIMIT 0, $rows;");
} else {
$data[9] = mysql_select_multi("SELECT `s`.`player_id` AS `id`, `s`.`value` AS `value`, `p`.`name` AS `name`, `p`.`vocation` AS `vocation` FROM `player_skills` AS `s` LEFT JOIN `players` AS `p` ON `s`.`player_id`=`p`.`id` WHERE `s`.`skillid` = 0 AND `p`.`group_id` < $g ORDER BY `s`.`value` DESC LIMIT 0, $rows;");
$data[1] = mysql_select_multi("SELECT `s`.`player_id` AS `id`, `s`.`value` AS `value`, `p`.`name` AS `name`, `p`.`vocation` AS `vocation` FROM `player_skills` AS `s` LEFT JOIN `players` AS `p` ON `s`.`player_id`=`p`.`id` WHERE `s`.`skillid` = 1 AND `p`.`group_id` < $g ORDER BY `s`.`value` DESC LIMIT 0, $rows;");
$data[2] = mysql_select_multi("SELECT `s`.`player_id` AS `id`, `s`.`value` AS `value`, `p`.`name` AS `name`, `p`.`vocation` AS `vocation` FROM `player_skills` AS `s` LEFT JOIN `players` AS `p` ON `s`.`player_id`=`p`.`id` WHERE `s`.`skillid` = 2 AND `p`.`group_id` < $g ORDER BY `s`.`value` DESC LIMIT 0, $rows;");
$data[3] = mysql_select_multi("SELECT `s`.`player_id` AS `id`, `s`.`value` AS `value`, `p`.`name` AS `name`, `p`.`vocation` AS `vocation` FROM `player_skills` AS `s` LEFT JOIN `players` AS `p` ON `s`.`player_id`=`p`.`id` WHERE `s`.`skillid` = 3 AND `p`.`group_id` < $g ORDER BY `s`.`value` DESC LIMIT 0, $rows;");
$data[4] = mysql_select_multi("SELECT `s`.`player_id` AS `id`, `s`.`value` AS `value`, `p`.`name` AS `name`, `p`.`vocation` AS `vocation` FROM `player_skills` AS `s` LEFT JOIN `players` AS `p` ON `s`.`player_id`=`p`.`id` WHERE `s`.`skillid` = 4 AND `p`.`group_id` < $g ORDER BY `s`.`value` DESC LIMIT 0, $rows;");
$data[5] = mysql_select_multi("SELECT `s`.`player_id` AS `id`, `s`.`value` AS `value`, `p`.`name` AS `name`, `p`.`vocation` AS `vocation` FROM `player_skills` AS `s` LEFT JOIN `players` AS `p` ON `s`.`player_id`=`p`.`id` WHERE `s`.`skillid` = 5 AND `p`.`group_id` < $g ORDER BY `s`.`value` DESC LIMIT 0, $rows;");
$data[6] = mysql_select_multi("SELECT `s`.`player_id` AS `id`, `s`.`value` AS `value`, `p`.`name` AS `name`, `p`.`vocation` AS `vocation` FROM `player_skills` AS `s` LEFT JOIN `players` AS `p` ON `s`.`player_id`=`p`.`id` WHERE `s`.`skillid` = 6 AND `p`.`group_id` < $g ORDER BY `s`.`value` DESC LIMIT 0, $rows;");
$data[7] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `experience`, `level` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `experience` DESC limit 0, $rows;");
$data[8] = mysql_select_multi("SELECT `id`, `name`, `vocation`, `maglevel` AS `value` FROM `players` WHERE `group_id` < $g ORDER BY `maglevel` DESC limit 0, $rows;");
}
return $data;
}
// Returns an array containing up to 30 best players with high magic level (returns their name and magic level)
function highscore_maglevel() {
return mysql_select_multi("SELECT `name`, `maglevel` as `value`, `group_id` FROM `players` WHERE `experience`>500 ORDER BY `maglevel` DESC LIMIT 0, 30");
}
// Count how many skill entries are in the db for a certain skillid (this can relate to how many players exist).
function highscore_count($skillid) {
$data = mysql_select_single("SELECT COUNT(`player_id`) AS `count` FROM `player_skills` WHERE `skillid`='$skillid' LIMIT 0, 30");
return ($data !== false) ? $data['count'] : 0;
}
// Count how many players have higher exp than 500
function highscore_experience_count() {
$data = mysql_select_single("SELECT COUNT(`id`) AS `count` FROM `players` WHERE `experience`>'500' LIMIT 0, 30");
return ($data !== false) ? $data['count'] : 0;
}
// END HIGHSCORE FUNCTIONS
function user_recover($mode, $edom, $email, $character, $ip) {
/* -- Lost account function - user_recovery --

View File

@ -1,110 +1,105 @@
<?php require_once 'engine/init.php'; include 'layout/overall/header.php';
if ($config['log_ip']) {
znote_visitor_insert_detailed_data(3);
}
if (empty($_POST) === false) {
#if ($_POST['token'] == $_SESSION['token']) {
/* Token used for cross site scripting security */
if (isset($_POST['token']) && Token::isValid($_POST['token'])) {
$skillid = (int)$_POST['selected'];
$cache = new Cache('engine/cache/highscores');
if ($cache->hasExpired()) {
if ($config['TFSVersion'] != 'TFS_10') $tmp = highscore_getAll();
else $tmp = highscore_getAll_10(0, 30);
// Fetch highscore type
$type = getValue($_GET['type']);
if (!$type) $type = 7;
else $type = (int)$type;
if ($type > 9) $type = 7;
$cache->setContent($tmp);
$cache->save();
// Fetch highscore page
$page = getValue($_GET['page']);
if (!$page || $page == 0) $page = 1;
else $page = (int)$page;
$array = isset($tmp[$skillid]) ? $tmp[$skillid] : $tmp[7];
} else {
$tmp = $cache->load();
$array = $tmp[$skillid];
}
if ($skillid < 9) {
// Design and present the list
if ($array) {
?>
<h2>
<?php echo ucfirst(skillid_to_name($skillid)); ?> scoreboard. Next update:
<?php
if ($cache->remainingTime() > 0) {
$hours = seconds_to_hours($cache->remainingTime());
$minutes = ($hours - (int)$hours) * 60;
$seconds = ($minutes - (int)$minutes) * 60;
if ($hours >= 1) {
echo (int)$hours .'h';
}
if ($minutes >= 1) {
echo ' '. (int)$minutes .'m';
}
if ($seconds >= 1) {
echo ' '. (int)$seconds .'s';
}
} else {
echo '0s';
}
?>. <?php echo remaining_seconds_to_clock($cache->remainingTime());?>
</h2>
<table id="highscoresTable" class="table table-striped table-hover">
<tr class="yellow">
<th>Name:</th>
<?php
if ($skillid == 7) echo '<th>Level:</th><th>Experience:</th>';
else {
?>
<th>Value:</th>
<?php
}
if ($skillid == 7 || $skillid == 6 || $skillid == 5) {
echo '<th>Vocation:</th>';
}
?>
</tr>
<?php
foreach ($array as $value) {
// start foreach
if ($config['TFSVersion'] == 'TFS_10' || $value['group_id'] < 2) {
echo '<tr>';
echo '<td><a href="characterprofile.php?name='. $value['name'] .'">'. $value['name'] .'</a></td>';
if ($skillid == 7) echo '<td>'. $value['level'] .'</td>';
echo '<td>'. $value['value'] .'</td>';
if ($skillid == 7 || $skillid == 6 || $skillid == 5) {
echo '<td>'. $value['vocation'] .'</td>';
}
echo '</tr>';
}
// end foreach
}
?>
</table>
<?php
} else {
echo 'Empty list, it appears all players have less than 500 experience points.';
}
//Done.
}
} else {
echo 'Token appears to be incorrect.<br><br>';
//Token::debug($_POST['token']);
echo 'Please clear your web cache/cookies <b>OR</b> use another web browser<br>';
}
$highscore = $config['highscore'];
$rows = $highscore['rows'];
$rowsPerPage = $highscore['rowsPerPage'];
function skillName($type) {
$types = array(
1 => "Club",
2 => "Sword",
3 => "Axe",
4 => "Distance",
5 => "Shield",
6 => "Fish",
7 => "Experience", // Hardcoded
8 => "Magic Level", // Hardcoded
9 => "Fist", // Since 0 returns false I will make 9 = 0. :)
);
return $types[(int)$type];
}
/*
0 fist: SELECT (SELECT `name` from `players` WHERE `player_id`=`id`) AS `name`, `value` FROM `player_skills` WHERE `skillid`=0
1 club:
2 sword:
3 axe:
4 dist:
5 Shield:
6 Fish
7 Hardcoded experience
8 Hardcoded maglevel
*/
function pageCheck($index, $page, $rowPerPage) {
return ($index < ($page * $rowPerPage) && $index >= ($page * $rowPerPage) - $rowPerPage) ? true : false;
}
$cache = new Cache('engine/cache/highscores');
if ($cache->hasExpired()) {
$scores = fetchAllScores($rows, $config['TFSVersion'], $highscore['ignoreGroupId']);
$cache->setContent($scores);
$cache->save();
} else {
$scores = $cache->load();
}
if ($scores) {
?>
<h1>Ranking for <?php echo skillName($type); ?>.</h1>
<form action="" method="GET">
<select name="type">
<option value="7" <?php if ($type == 7) echo "selected"; ?>>Experience</option>
<option value="8" <?php if ($type == 8) echo "selected"; ?>>Magic</option>
<option value="5" <?php if ($type == 5) echo "selected"; ?>>Shield</option>
<option value="2" <?php if ($type == 2) echo "selected"; ?>>Sword</option>
<option value="1" <?php if ($type == 1) echo "selected"; ?>>Club</option>
<option value="3" <?php if ($type == 3) echo "selected"; ?>>Axe</option>
<option value="4" <?php if ($type == 4) echo "selected"; ?>>Distance</option>
<option value="6" <?php if ($type == 6) echo "selected"; ?>>Fish</option>
<option value="9" <?php if ($type == 9) echo "selected"; ?>>Fist</option>
</select>
<select name="page">
<?php
$pages = (int)($highscore['rows'] / $highscore['rowsPerPage']) + 1;
for ($i = 0; $i < $pages; $i++) {
$x = $i + 1;
if ($x == $page) echo "<option value='".$x."' selected>Page: ".$x."</option>";
else echo "<option value='".$x."'>Page: ".$x."</option>";
}
?>
</select>
<input type="submit" value=" View " class="btn btn-info">
</form>
<table id="highscoresTable" class="table table-striped table-hover">
<tr class="yellow">
<td>Rank</td>
<td>Name</td>
<td>Vocation</td>
<td>Level</td>
<?php if ($type === 7) echo "<td>Points</td>"; ?>
</tr>
<?php
for ($i = 0; $i < count($scores[$type]); $i++) {
if (pageCheck($i, $page, $rowsPerPage)) {
?>
<tr>
<td><?php echo $i+1; ?></td>
<td><a href="characterprofile.php?name=<?php echo $scores[$type][$i]['name']; ?>"><?php echo $scores[$type][$i]['name']; ?></a></td>
<td><?php echo vocation_id_to_name($scores[$type][$i]['vocation']); ?></td>
<td><?php echo $scores[$type][$i]['value']; ?></td>
<?php if ($type === 7) echo "<td>". $scores[$type][$i]['experience'] ."</td>"; ?>
</tr>
<?php
}
}
?>
</table>
<?php
}
include 'layout/overall/footer.php'; ?>

View File

@ -1,24 +1,20 @@
<div class="sidebar">
<h2>Search highscores</h2>
<div class="inner">
<form action="highscores.php" method="post">
<form action="highscores.php" method="get">
Select skill type to view:<br>
<select name="selected">
<select name="type">
<option value="7">Experience</option>
<option value="5">Shielding</option>
<option value="3">Axe</option>
<option value="2">Sword</option>
<option value="1">Club</option>
<option value="4">Distance</option>
<option value="0">Fist</option>
<option value="9">Fist</option>
<option value="6">Fish</option>
<option value="8">Magic</option>
</select>
<?php
/* Form file */
Token::create();
?>
</select>
<input type="submit" value="Fetch scoreboard">
</form>
</div>