×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Can this be done in a quicker way?

Can this be done in a quicker way?

Can this be done in a quicker way?

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close