class HT_ENTRY
{
function HT_ENTRY (){}
var $name;
var $score;
var $length;
var $bananas;
var $cherries;
var $hemps;
var $multi;
}
function HT_GetHotd ()
{
$file = file( "hotd.txt" );
if (count($file)===0) {
echo 'Unknown';
}
else {
printf( $file[0] );
}
}
function LoadHighscore ()
{
}
function HT_RateAllow($score)
{
$score = trim(intval($score));
$filename = 'trixx.txt';
if (false === ($fh = fopen($filename, 'a'))) {
return;
}
fwrite($fh, sprintf('%d:%d', time(), $score).PHP_EOL);
fclose($fh);
}
function HT_InsertAllowed($score)
{
$score = intval($score, 10);
if ($score <= 0) { return false; }
$filename = 'trixx.txt';
if (file_exists($filename)) {
$file = file($filename);
} else {
return false;
}
$allowed = false;
$file_content = '';
$keep = array();
foreach ($file as $id => $line)
{
list($t, $s) = $line;
if ($t > (time() + 7)) {
continue;
}
if ($score == $s) {
$allowed = true;
continue;
}
$file_content .= sprintf('%s:%s', $t, $s).PHP_EOL;
}
file_put_contents($filename, $file_content);
return $allowed;
}
function HT_Rate ()
{
$rank = 1;
$file = file( "highscore.txt" );
foreach ($file as $line) {
if ($line !== "\n") {
$entry = explode( "::", $line );
if ($entry[1] < $_POST['score']) {
break;
}
$rank++;
}
if ($rank > 100) {
break;
}
}
if ($rank > 100) { printf("0"); return; }
else { HT_RateAllow($_POST['score']); }
printf( $rank );
}
function HT_Insert()
{
if (!HT_InsertAllowed($_POST["score"])) { return; }
$_POST['name'] = str_replace( ":", ".", $_POST['name'] );
$written = false;
$rank = 1;
$file = file( "highscore.txt" );
$out = fopen( "highscore.txt", "w" );
foreach ($file as $line) {
if ('' === ($line = trim($line))) {
}
if ( ($line != "\n") && ($line != "") ) {
$entry = explode( "::", $line );
if ( ($entry[1] < $_POST["score"]) && ($written === false) ) {
fprintf( $out, "%s::%s::%s::%s::%s::%s::%s\n",
$_POST['name'], $_POST['score'],
$_POST['length'], $_POST['bananas'],
$_POST['cherries'], $_POST['hemps'],
$_POST['multi'] );
$written = true;
$rank++;
}
if ($rank < 101) {
fprintf( $out, "%s", $line );
echo( "$rank:$line
" );
$rank++;
}
else {
break;
}
}
}
if ( ($written === false) && ($rank < 101) ) {
// echo "Hallo";
fprintf( $out, "%s::%s::%s::%s::%s::%s::%s\n",
$_POST['name'], $_POST['score'],
$_POST['length'], $_POST['bananas'],
$_POST['cherries'], $_POST['hemps'],
$_POST['multi'] );
}
fclose( $out );
}
if (isset( $_POST['cmd'] )) {
switch ($_POST["cmd"]){
case "HT_INSERT": HT_Insert(); break;
case "HT_HOTD": HT_GetHotd(); break;
case "HT_RATE": HT_Rate(); break;
default:
// echo "LIST";
foreach ($_POST as $name => $value) {
echo "
Rank | "; echo "Name | "; echo "Score | "; echo "Length | "; echo "Cherries | "; echo "Hemps | "; echo "
---|---|---|---|---|---|
".$rank++." | "; echo "".htmlspecialchars($entry[0])." | "; echo "".htmlspecialchars($entry[1])." | "; echo "".htmlspecialchars($entry[2])." | "; echo "".htmlspecialchars($entry[3])." | "; echo "".htmlspecialchars($entry[5])." | "; echo "