barrett777
03-07-2006, 09:45 PM
Hi All,
I'm programming an online game, and player's data will be saved and loaded from my server using CGI. I have working CGI scripts, but, if my game becomes popular (hopefully), I think I will need to optimize my scripts some. I'm new to Perl, so if there are shortcuts to what I'm doing, please help. My code is posted below, with comments where I have ideas for help:
#!/usr/bin/perl
print "content-type: text/plain\n\n";
open(SAVE, "+< playerData.txt") or die "OPEN";
# All data is stored in this file. This .cgi file does the saving portion.
flock(SAVE, 2) or die "LOCK";
# Makes sure I get an exclusive lock on the file, since I will be writing.
my @playerData = <>;
@playerData = split(/\s+/, $playerData[0]);
# This is my player data from my game. It will always be one line.
# Can I do those two lines in one line of code?
@fileData = <SAVE>;
# This is the contents of my player data file. I've had problems reading
# through and writing over the parts I need (Because what I'm replacing
# might be longer/shorter than what I'm replacing with, if that makes any
# sense). So I have been reading the whole file into an array, and
# rewriting the file, replacing what I'm looking for.
seek SAVE, 0, 0;
# Again, starting with the beginning of the file, I will write over all of it
# If there is a way I can do this differently, please let me know. My
# playerData.txt file is in the format "username password playerData"
# Each entry is on a new line, but each entry is of different lengths.
# Is there a way I can replace the line I want without running over
# the next line, or leaving parts of previous data if new line is shorter?
foreach (@fileData)
{
@line = split(/\s+/, $_);
if ($playerData[0] eq $line[0])
{
if ($playerData[1] eq $line[1])
{
print SAVE $playerData[0] . " " . $playerData[1] . " " . $playerData[2] . "\n";
print "SAVE";
}
else
{
print SAVE $_
print "PASSWORD";
}
}
else
{
print SAVE $_;
}
}
# I go through each line, looking for a username + password match. If I find
# one, I print the new data instead of the old stuff. If I find a username
# match, but different passwords, then the player entered the wrong
# password and I keep the old data. If no match, I keep the old data.
# If I find a way to read through and replace only the one line I want to,
# is there a way to end the loop when I find what I'm looking for?
close (SAVE);
# Should I try to unlock the file before closing, or just close it?
exit 0;
All help is greatly appreciated!
Thanks!
Ben Barrett
I'm programming an online game, and player's data will be saved and loaded from my server using CGI. I have working CGI scripts, but, if my game becomes popular (hopefully), I think I will need to optimize my scripts some. I'm new to Perl, so if there are shortcuts to what I'm doing, please help. My code is posted below, with comments where I have ideas for help:
#!/usr/bin/perl
print "content-type: text/plain\n\n";
open(SAVE, "+< playerData.txt") or die "OPEN";
# All data is stored in this file. This .cgi file does the saving portion.
flock(SAVE, 2) or die "LOCK";
# Makes sure I get an exclusive lock on the file, since I will be writing.
my @playerData = <>;
@playerData = split(/\s+/, $playerData[0]);
# This is my player data from my game. It will always be one line.
# Can I do those two lines in one line of code?
@fileData = <SAVE>;
# This is the contents of my player data file. I've had problems reading
# through and writing over the parts I need (Because what I'm replacing
# might be longer/shorter than what I'm replacing with, if that makes any
# sense). So I have been reading the whole file into an array, and
# rewriting the file, replacing what I'm looking for.
seek SAVE, 0, 0;
# Again, starting with the beginning of the file, I will write over all of it
# If there is a way I can do this differently, please let me know. My
# playerData.txt file is in the format "username password playerData"
# Each entry is on a new line, but each entry is of different lengths.
# Is there a way I can replace the line I want without running over
# the next line, or leaving parts of previous data if new line is shorter?
foreach (@fileData)
{
@line = split(/\s+/, $_);
if ($playerData[0] eq $line[0])
{
if ($playerData[1] eq $line[1])
{
print SAVE $playerData[0] . " " . $playerData[1] . " " . $playerData[2] . "\n";
print "SAVE";
}
else
{
print SAVE $_
print "PASSWORD";
}
}
else
{
print SAVE $_;
}
}
# I go through each line, looking for a username + password match. If I find
# one, I print the new data instead of the old stuff. If I find a username
# match, but different passwords, then the player entered the wrong
# password and I keep the old data. If no match, I keep the old data.
# If I find a way to read through and replace only the one line I want to,
# is there a way to end the loop when I find what I'm looking for?
close (SAVE);
# Should I try to unlock the file before closing, or just close it?
exit 0;
All help is greatly appreciated!
Thanks!
Ben Barrett