Hi,
I’m working on a score system for a game. The piece of code underneath checks if a player stopped playing while the game was not yet finished (after 1 hour of no progress) and the exit button in the game wasn’t used to quit. This is to splice the player from the datafile. The code underneath is working but I’m sure it must be possible to make it much smoother and quicker. For instance, is it possible to integrate the splice code into the code that checks if a game is aborted (I couldn’t get this working)?
sub play_stop {
# create array of aborted games
my $count=0;
$match_count=0;
@stopped;
open (data, "<$data_file"
or &error("Unable to open the data file"
;
while (<data>) {
chomp();
(my $one, my $new, my $hour, my $game, my $view) = split(/\|/, $_);
if ($game ne "yes" and $view eq "show"
{
if ($hour>$hr){$check_hr=$hr+24;}else{$check_hr=$hr;}
if ($check_hr - $hour > 1) {
push @stopped, $count;
$match_count++;
}
}
$count++;
}
close(data);
# splice aborted games from data file
open (SCORES_FILE, "< $data_file"
or &error("Could not open check data file at line ", __LINE__);
@scores_data = <SCORES_FILE>;
for my $i (0..$match_count-1) {
splice (@scores_data, @stopped[$i]-$i, 1);
}
chomp @scores_data;
close SCORES_FILE;
# save file
open (SCORES_FILE, "> $data_file"
or &error("Could not open data file at line ", __LINE__);
flock SCORES_FILE, 2 or &error("Could not lock data file at line ", __LINE__);
if (-w SCORES_FILE) {
# save only top scores to data file (default = 100)
for my $i (0..$scores_to_store-1) {
if (defined($scores_data[$i])) {
print SCORES_FILE $scores_data[$i] . "\n";
}
else {
last;
}
}
}
else {
&error("Could not write to data file at line ", __LINE__);
}
close SCORES_FILE;
flock SCORES_FILE, 8;
}
Regards,
Raoul
I’m working on a score system for a game. The piece of code underneath checks if a player stopped playing while the game was not yet finished (after 1 hour of no progress) and the exit button in the game wasn’t used to quit. This is to splice the player from the datafile. The code underneath is working but I’m sure it must be possible to make it much smoother and quicker. For instance, is it possible to integrate the splice code into the code that checks if a game is aborted (I couldn’t get this working)?
sub play_stop {
# create array of aborted games
my $count=0;
$match_count=0;
@stopped;
open (data, "<$data_file"
while (<data>) {
chomp();
(my $one, my $new, my $hour, my $game, my $view) = split(/\|/, $_);
if ($game ne "yes" and $view eq "show"
if ($hour>$hr){$check_hr=$hr+24;}else{$check_hr=$hr;}
if ($check_hr - $hour > 1) {
push @stopped, $count;
$match_count++;
}
}
$count++;
}
close(data);
# splice aborted games from data file
open (SCORES_FILE, "< $data_file"
@scores_data = <SCORES_FILE>;
for my $i (0..$match_count-1) {
splice (@scores_data, @stopped[$i]-$i, 1);
}
chomp @scores_data;
close SCORES_FILE;
# save file
open (SCORES_FILE, "> $data_file"
flock SCORES_FILE, 2 or &error("Could not lock data file at line ", __LINE__);
if (-w SCORES_FILE) {
# save only top scores to data file (default = 100)
for my $i (0..$scores_to_store-1) {
if (defined($scores_data[$i])) {
print SCORES_FILE $scores_data[$i] . "\n";
}
else {
last;
}
}
}
else {
&error("Could not write to data file at line ", __LINE__);
}
close SCORES_FILE;
flock SCORES_FILE, 8;
}
Regards,
Raoul