Link to home
Start Free TrialLog in
Avatar of MAXcom
MAXcom

asked on

ozo, pcuser 321.. HELP!?

Hi,
Here is a script that is supposed to take your email,password and score and then write that your line of a txt file. Then it is supposed to check the high scores file and if your score is higher than any of them, put yours in and bump the rest down by one. Then it it supposed to write your alias and score to a lastpost file so i know who the last person that posted was. Note: on the lastpost file, i dont want to append, just overwrite the existing.

After all that is done, i just want the user to be redirected to my main page.

It seemed to be going ok but i am having A LOT of trouble trying to get the bugs out. Yes i know the script is extremely messy but im just starting. (if thats an excuse). Would you please look at the script and tell me why it wont execute?

#!/usr/bin/perl
use CGI;

$query= CGI->new();
$email = $query->param('email');
$passwd = $query->param('password');
$score = $query->param('score');
$email=lc($email);
$passwd=lc($passwd);

if ($email eq ""){&leave;}
if ($passwd eq ""){&leave;}

sub leave{
print "Content-type: text/html\n\n";
print "Something is wrong. Please hit your back button and try again.\n";
exit;
}

&checkpass;

sub checkpass{
$file = "reg_".substr($email,0,1).".txt";
open (FILE,"$file");
@people = <FILE>;

foreach $line (@people){
      chomp($line);
      ($mail,$pass,$score,$alias) = split /\|/, $line;
      if ($email eq $mail) {
         if ($passwd eq $pass) {&decryptscore;} else {&nomail;}
      }
      else {&nomail;}
}

close(FILE);
}

sub nomail{
print "Content-type: text/html\n\n";
print "Invalid login. Are you sure you are registered? Click <a href=\/directory/register\.htm>HERE<\/a> to register or your back button to try again.\n";
}

sub decryptscore{
$pattern="(2x)(9-E)(30X)(9-A)(10X)(9-D)(20X)(9-H)(9X)(9-J)(16X)(9-B)(6X)(9-G)(9-F)(36X)(9-C)(84X*)(9-I)(X)";

$pattern=~s/\((\d*)X\*\)/z$1/gi;
$pattern=~s/\((\d*)X\)/x$1/gi;
$pattern=~s/\(9-(\w)\)/$1/g;

@N = $pattern=~/([A-Z])/g;
($p84=$pattern) =~ tr/zxA-Z/axx/;
($px=$pattern) =~ tr/zxA-Z/xax/;
($pn=$pattern) =~ tr/zxA-Z/xxa/;

$px =~ s/a9/x9/;


$x84=unpack $p84,$score;

$x = substr((join'',unpack $px,$score),0,length $x84);
$x=~s/(\d)/9-$1/eg;

if ($x eq $x84){
@n{@N} = unpack $pn,$score;
$dscore=(9x@N)-join'',@n{'A'..'J'};
&writeit;
&checkhigh;
&writelast;
} else {&cheater;}
}

sub cheater{
print "Content-type: text/html\n\n";
print "The encrypted score is invalid.\n";
exit;
}

sub writeit{

$file = "reg_".substr($email,0,1).".txt";
open (FILE,"$file");
open (TMPFL,">>temp.tmp");
@ids = <FILE>;
foreach $line (@ids){
chomp($line);
($mail,$pass,$score,$alias) = split /\|/, $line;
  if ($mail ne $email) {print TMPFL "$line\n";} else {
$theline = $email."|".$passwd."|".$dscore."|".$alias;
print TMPFL "$theline\n";}
}
close(TMPFL);
close(FILE);
unlink($file);
rename "temp.tmp",$file || die("Unable to rename.");
}

sub checkhigh{
$file = "high.txt";
open (FILE,"$file");
@people = <FILE>;

foreach $line (@people){
      chomp($line);
      ($no,$alia,$scor) = split /\|/, $line;
      if ($score > $scor) {$l = 1}}
close(FILE);
if ($l == 1){&writehigh;}
}

sub writehigh{
$nom = 0;
$file = "high.txt";
open (FILE,"$file");
open (TMPFL,">>high.tmp");
@ids = <FILE>;
  foreach $line (@ids){
  chomp($line);
  $nom++;
  ($no,$ali,$sco) = split /\|/, $line;
  $line = $nom."|".$ali."|".$sco;
    if ($sco > $scor) {print TMPFL "$line\n";} else
        {
        $theline = $nom."|".$alias."|".$dscore;
        print TMPFL "$theline\n";
        }
 }
close(TMPFL);
close(FILE);
unlink($file);
rename "high.tmp",$file || die("Unable to rename.");
}

sub writelast{
$file = "lastpost.txt";
open (FILE,">$file");
$theline = $alias."|".$dscore;
print FILE "$theline\n";
close(FILE);
print "Location: http:\/\/www\.mydomain\.com\/directory\/index1\.shtml\n\n";
exit;
}
Avatar of MAXcom
MAXcom

ASKER

sorry, forgot to include an encrypted scring for testing:
32905634009932754887810741732407198258806730962982965574976507206624257560046060088651698145973700578074755623815890827210070269201614160967943659900672451121892582675928174119326937017034425023492793393991134830185426299451
ASKER CERTIFIED SOLUTION
Avatar of PC_User321
PC_User321

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MAXcom

ASKER

Adjusted points to 9
Avatar of MAXcom

ASKER

Hi,
Thanks a lot. That worked. I didnt realise that overwriting it would cause a problem.
The High Scores are kept as:

rank(1-10)|Alias|Score

this is the only part that apparently didnt work. How do you sort an array?
Avatar of MAXcom

ASKER

nevermind, i got it working the way i had it.
thanks
Thanks for the points.

This is what I was talking about
(lots simpler than yours):-


# All scores in scores high.txt are stored like the quoted text below.
# Here is the new score that we want to test
#
$NewScoreAndAlias = "0000123333|PC_User321";
($NewScore, $NotUsed) = split (/|/, $NewScoreAndAlias);

# Read the high scores and add the new one to the list
open (SCORES, "+< high.txt");
@Scores = <SCORES>;
push @Scores, "$NewScoreAndAlias\n";

# Sort the scores and dump the worst one
@Scores = sort @Scores;
shift @Scores;                  # Push out the lowest score

# Don't know why the program wants these
close (SCORES);
open (SCORES, "> high.txt");

# Write back to file
print (SCORES @Scores);
close (SCORES);


The ranking (1st, 2nd,..) need not be in the file because the line number in the file gives that info.
Avatar of MAXcom

ASKER

amazing.