Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can this be done in a quicker way?

Status
Not open for further replies.

rtb1

IS-IT--Management
Jul 17, 2000
73
ES
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, &quot;<$data_file&quot;) or &error(&quot;Unable to open the data file&quot;);
while (<data>) {
chomp();
(my $one, my $new, my $hour, my $game, my $view) = split(/\|/, $_);
if ($game ne &quot;yes&quot; and $view eq &quot;show&quot;){
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, &quot;< $data_file&quot;) or &error(&quot;Could not open check data file at line &quot;, __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, &quot;> $data_file&quot;) or &error(&quot;Could not open data file at line &quot;, __LINE__);
flock SCORES_FILE, 2 or &error(&quot;Could not lock data file at line &quot;, __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] . &quot;\n&quot;;
}
else {
last;
}
}
}
else {
&error(&quot;Could not write to data file at line &quot;, __LINE__);
}
close SCORES_FILE;
flock SCORES_FILE, 8;
}


Regards,

Raoul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top