#!/usr/bin/perl -w # sets a page's rank and returns a thank you message use strict; use Fcntl ':flock'; # import lock constants. # this should be tidied up by using CGI, Your query string should # take the form: ?vote=value&gametitle=value&begin=value&end=value&bypass=value # but I suspect that it takes a form more like: # ?10&gametitle&0&10&1 or whatever. :( my $myquery = $ENV{'QUERY_STRING'}; my ($myvote,$mygametitle,$mybegin,$myend,$bypasscache) = split(/\&/,$myquery); $mygametitle =~ tr/\+/ /; my $mylink =~ tr/\~/\'/; # Pick the database number we're dealing with. my $dbnumber = ($mybegin == 0 ? 1 : 2); # lock the lockfile (and hope everything plays by the rules) open(FORLOCK,"gmlock.pl") or dienice("Rank set script could not open lock file: $!"); flock(FORLOCK, LOCK_EX) or dienice("Rank set script could not gain exclusive ". "lock on database: $!"); # open our dbfile open(INF, "gmlist$dbnumber.pl") or dienice("Rank set script could not open proper game database for reading: $!"); # read it all in at once my @ary = ; close(INF); # if mybegin is greater than zero then we'll want to skip some records... my $skip = 0; if($mybegin > 0) { $skip = $mybegin - 2; } # why -2? $skip = $skip < 0 ? 0 : $skip; # sanity checking, skip should have minimum 0 my ($line, $gamenumber) = ("", -1); # find our game number. foreach (@ary) { $gamenumber++; next if $skip--; # skip this line if we haven't reached $mybegin. chomp; # $_ is the same as $ary[$gamenumber] (well, unchomped) $line = $_; my ($gmtitle) = split(/\|/,$line); if ($gmtitle eq $mygametitle) { last; # we've found out gamenumber so end. } } # Pull stuff out from our line my ($gmtitle,$gmdescription,$gmurl,$totalvalue,$totalvotes,$averagevote, $clicks,$visibility,$siteid,$timesince,$lastip) = split(/\|/,$line); # calculate new values my $myavg; if ($lastip ne $ENV{'REMOTE_ADDR'}) { $totalvalue = $totalvalue + $myvote; $totalvotes++; $averagevote = $totalvalue / $totalvotes; $myavg = $averagevote; } $lastip = $ENV{'REMOTE_ADDR'}; my $aflag = 0; # This flags whether we're switching databases # Delete the line for gamenumber from the first file if (($dbnumber == 1) && ($totalvotes > 2)) { $ary[$gamenumber] = ""; # This line position is now empty open(OUTF,">gmlist$dbnumber.pl") or dienice("Rank set script could not re-open proper database for writing: $!"); print OUTF @ary; # everything except line with gamenumber close(OUTF); $dbnumber = 2; open(INF2,"gmlist$dbnumber.pl") or dienice("Rank set script could not open proper game database for reading: $!");; @ary = ; close(INF2); # our new @ary. $gamenumber = @ary; # the last position $aflag = 1; } # create our new gamennumber line. $ary[$gamenumber] = "$gmtitle|$gmdescription|$gmurl|$totalvalue|$totalvotes|". "$averagevote|$clicks|$visibility|$siteid|$timesince|$lastip\n"; # if the game is switching databases and this isn't the first entry in this db # OR if we're not switching databases but our vote is greater than average and # this is not the first entry in this db then ... if(($aflag ==1 && $gamenumber != 0) || ($aflag != 1 && $myvote >= $myavg && $gamenumber != 0)) { my $i = $gamenumber; # find the correct place to insert this line. while ($i > 0) { $i--; $line = $ary[$i]; chomp($line); ($gmtitle,$gmdescription,$gmurl,$totalvalue,$totalvotes, $averagevote,$clicks,$visibility,$siteid,$timesince,$lastip) = split(/\|/,$line); # if our vote passed in through the GET string is >= the # calculated average vote for this game... if ($myavg >= $averagevote) { # swap these two. my $holder = $ary[$i]; $ary[$i] = $ary[$gamenumber]; $ary[$gamenumber] = $holder; $gamenumber--; } else { # It must be in the correct position now. last; } } } else { my $i = $gamenumber; # find the correct place to insert this line. while ($i < @ary) { $i++; $line = $ary[$i]; chomp($line); ($gmtitle,$gmdescription,$gmurl,$totalvalue,$totalvotes,$averagevote, $clicks,$visibility,$siteid,$timesince,$lastip) = split(/\|/,$line); # if our vote passed in through the GET string is < the # calculated average vote for this game... if ($myavg < $averagevote) { # swap these two. my $holder = $ary[$i]; $ary[$i] = $ary[$gamenumber]; $ary[$gamenumber] = $holder; $gamenumber++; } else { # it must be in the correct position now. last; } } } open(OUTF2,">gmlist$dbnumber.pl") or dienice("Rank set script could not re-open proper database for writing: $!"); print OUTF2 @ary; close(OUTF2); # release the lock close(FORLOCK); # and all the printing out code goes below here