PerlPowerTools
view release on metacpan or search on metacpan
{
$Score_File = undef;
warn "$Score_File: $!; no scores will be saved\n";
return;
}
unless(flock(SCORE, LOCK_EX))
{
close(SCORE);
$Score_File = undef;
warn "$Score_File: $!; no scores will be saved\n";
return;
}
seek(SCORE, 0, 0); # Rewind, just in case
my($record, $uid, $score, $name, $info, $num_scores);
%High_Scores = ();
$New_Score = 0;
$My_Score = 0;
$record = 0;
$num_scores = 0;
while(<SCORE>)
{
chop;
($uid, $score, $name) = split(/\t/, $_);
push(@{$High_Scores{$score}}, [ $uid, $score, $name ]);
$num_scores++;
}
if($num_scores < MAX_SCORES)
{
$record = 1;
}
else
{
for my $score (keys(%High_Scores))
{
if($Arena{'PLAYER'}{'SCORE'} >= $score)
{
$record = 1;
last;
}
}
}
unless($record)
{
flock(SCORE, LOCK_UN);
close(SCORE);
return;
}
my($i, $count);
$uid = $<;
$score = $Arena{'PLAYER'}{'SCORE'};
$name = getpwuid($uid) || '???';
unshift(@{$High_Scores{$score}}, [ $uid, $score, $name ]);
# Clear and rewind
truncate(SCORE, 0);
seek(SCORE, 0, 0);
$i = 1;
$count = 0;
for my $score (sort { $b <=> $a } keys(%High_Scores))
{
for my $info (@{$High_Scores{$score}})
{
# Only allow MAX_SCORES_PER_USER
unless($< == ${$info}[0] && ++$count >= MAX_SCORES_PER_USER)
{
print SCORE join("\t", @{$info}), "\n";
if($score == $Arena{'PLAYER'}{'SCORE'})
{
$My_Score = $info unless($New_Score);
$New_Score = 1;
}
last if($i++ > MAX_SCORES);
}
}
}
flock(SCORE, LOCK_UN);
close(SCORE);
}
sub Show_Scores
{
endwin();
return unless(defined($Score_File));
open(SCORE, '<', $Score_File) || die "$Score_File: $!\n";
my($uid, $score, $name, $i);
$i = 1;
while(<SCORE>)
{
chop;
($uid, $score, $name) = split(/\t/, $_);
printf("%d\t%d\t%s\n", $i, $score, $name);
last if(++$i > $LINES);
}
close(SCORE);
exit(0);
}
sub Show_Scores_In_Game
{
my($info, $line, $uid, $score, $name);
( run in 1.364 second using v1.01-cache-2.11-cpan-39bf76dae61 )