highscores

This commit is contained in:
Edwin Eefting 2014-08-09 15:45:27 +02:00
parent c14c335689
commit eb7f748737

View file

@ -8,12 +8,35 @@ use Time::HiRes qw( gettimeofday tv_interval);
HELP "game" => "Scan-fu game!";
my $fastest=1000;
my $fastest_name="nobody";
my $start_time;
my $step;
my $slowest;
my $elapsed=1000;
sub command :Tab(game) {
sub print_highscores()
{
$slowest=1000;
print "\n---- fastest times ----\n";
open my $in, "highscores.txt";
my $nr=0;
while (<$in>)
{
my ($name,$time)=split(" ");
printf "$nr: %f $name\n", $time;
$nr++;
$slowest=$time;
}
if ($nr<10)
{
$slowest=1000;
}
close($in);
print "-----------------------\n";
}
sub command :Tab(game)
{
my ($self, $cart, $command) = @_;
@ -21,7 +44,7 @@ sub command :Tab(game) {
print "\nWelcome to SCAN-FU \\o/\n";
print "\nRecord time is $fastest by $fastest_name\n";
print_highscores();
$start_time=[gettimeofday];
$step=1;
@ -34,7 +57,7 @@ sub command :Tab(game) {
sub step :Tab(whatevah) {
my ($self, $cart, $input) = @_;
if ($step<5)
if ($step<1)
{
if ($input ne "game_$step")
{
@ -50,20 +73,17 @@ sub step :Tab(whatevah) {
else
{
my $elapsed = tv_interval ( $start_time, [gettimeofday]);
$elapsed = tv_interval ( $start_time, [gettimeofday]);
print "\aCompleted in $elapsed seconds *tadaah*\n";
if ($elapsed < $fastest)
if ($elapsed < $slowest)
{
print " #####################\n";
print " #### NEW RECORD! ####\n";
print " #####################\n";
print " You did it in $elapsed !!!\n";
$fastest_name="(unknown)";
$fastest=$elapsed;
return "Enter your name for the hall of fame", \&name;
}
@ -72,10 +92,45 @@ sub step :Tab(whatevah) {
}
}
sub name :Tab(whatevah) {
sub name :Tab(whatevah)
{
my ($self, $cart, $input) = @_;
$fastest_name=$input;
#store new highscores:
#(ulgy as hell...but hey i'm sleepy :)
open my $in, "<highscores.txt";
open my $out, ">highscores.txt.tmp";
my $nr=0;
while (<$in>)
{
$nr++;
my ($name,$time)=split(" ");
if ($elapsed!=-1 && $elapsed<=$time)
{
print $out "$input $elapsed\n";
$elapsed=-1;
$nr++;
}
print $out "$name $time\n";
if ($nr>10)
{
last;
}
}
if ($nr<10 && $elapsed!=-1)
{
print $out "$input $elapsed\n";
}
close($in);
close($out);
rename("highscores.txt.tmp", "highscores.txt");
print_highscores();
return ACCEPT;
}