FunRoomSite, on 22 February 2012 - 01:50, said:
<?php
/*
Plugin Name: Leaderboard
Description: Dominion leaderboard
Author: Alex Jones
Version: 1.0.0
Author URI: mailto:alexjones@myneowin.net
*/
function widget_leaderboard()
{
echo '<table border="1" cellspacing="0"><th>#</th><th>Username</th><th><a href="' . $_SERVER['PHP_SELF'] . '?sort=elo">ELO</a></th><th><a href="' . $_SERVER['PHP_SELF'] . '?sort=wins">Wins</a></th><th><a href="' . $_SERVER['PHP_SELF'] . '?sort=losses">Losses</a></th><th><a href="' . $_SERVER['PHP_SELF'] . '?sort=ratio">Ratio</a></th>';
$i = 0;
$fh = fopen('results.txt', 'r');
while($line = fgets($fh))
{
// Remove any lines with no Dominion results
if(strpos($line, 'Dominion: ') !== FALSE)
{
preg_match('/Dominion: (.*)L]/', $line, $scores);
// For the username, sometimes the line has '(Now:Bronze, S1:Silver)', sometimes not
$score[$i]['username'] = (substr($line, 0, strpos($line, ' (Now:')) ?: substr($line, 0, strpos($line, ', ')));
$score[$i]['elo'] = substr($scores[1], 0, strpos($scores[1], ' ['));
$score[$i]['wins'] = substr($scores[1], strlen($score[$i]['elo'])+2, strpos($scores[1], 'W/')-strlen($score[$i]['elo'])-2);
$score[$i]['losses'] = substr($scores[1], strlen($score[$i]['elo'])+strlen($score[$i]['wins'])+4);
$score[$i]['ratio'] = round($score[$i]['wins']/$score[$i]['losses'], 2);
$i++;
}
}
fclose($fh);
function mysort($a, $b)
{
// Define the default sort here (currently 'elo')
$sort = ($_GET['sort'] != 'elo' && $_GET['sort'] != 'wins' && $_GET['sort'] != 'losses' && $_GET['sort'] != 'ratio') ? 'elo' : $_GET['sort'];
return strnatcmp($b[$sort], $a[$sort]);
}
usort($score, 'mysort');
$i = 1;
foreach($score as $score)
{
echo '<tr><td>' . $i . '</td><td>' . $score['username'] . '</td><td>' . $score['elo'] . '</td><td>' . $score['wins'] . '</td><td>' . $score['losses'] . '</td><td>' . $score['ratio'] . '</td></tr>';
$i++;
}
echo '</table>';
}
function leaderboard_init()
{
register_sidebar_widget(__('Leaderboard'), 'widget_leaderboard');
}
add_action("plugins_loaded", "leaderboard_init");
?>
Then upload it to /public_html/wp-content/plugins
Next go to your WP admin dashboard, click on Plugins, and click Activate on the new 'Leaderboard' plugin.
Now go to Appearance > Widgets, and drag the new widget into (e.g.) your sidebar.
And now you'll have something like this:
Font sizes can be fiddled with afterwards. If you get a load of errors like 'fopen failed to open results.txt/fgets expects 2 parameters/blah blah blah', then you just need to change the path to results.txt in: $fh = fopen('results.txt', 'r');
Finally, I haven't just put my 'plugin author info' there to be anal - it's required by WP, otherwise the plugin won't show up.
As usual, any issues and we're here to help!









