#!/usr/bin/perl
#
# Original script written by Joel Hahn, 2001.

#use CGI::Carp qw(fatalsToBrowser);

#set up the filename/header data
$rawdata="firsted1=1st ed.&firsted2=1st ed.&firsted3=1st ed.&seced1=2nd ed.&seced2=2nd ed.&thirded=3rd ed.&alq=Al Qadim&birth=Birthright&dlance=Dragonlance&dsun=Dark Sun&eberron=Eberron&forg1=Forgotten Realms&forg2=Forgotten Realms&forg3=Forgotten Realms&grey=Greyhawk&kara=Kara Tur&lankh=Lankhmar&myst=Mystara&plane=Planescape&rloft=Ravenloft&sjam=Spelljammer&dnd1=DnD&dnd2=DnD&dnd3=DnD&dnd4=DnD";

#split the raw data into filename=prodline info
@pairs = split(/&/, $rawdata);

#for each product line, split into filename & product line name, 
#then go on to print all data for that line
$k=0;
$j=0;
foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair);
  $file{$k} = $name;
  $prodline{$k} = $value;

 # Open the relevant data file, dump all the data into the @LINES array
 open(FILEDAT,"$file{$k}.dat") || die "Can't Open $file{$k}.dat: $!\n";
 @LINES=<FILEDAT>;
 close(FILEDAT);
 $SIZE=@LINES;

 # Manipulate the data, crunch the numbers, output the results
 for ($i=0;$i<=$SIZE-1;$i++) {
   $_=$LINES[$i];
   ($prodcode, $prodname, $votes, $rate, $ratenums)= split(/\|/, $_, 5);

   if ($rate==0) {
     $avg=0;
   }
   else {
     $rawavg=$rate/$votes;
     $exp=2;
     $avg= sprintf("\%.${exp}f", $rawavg);
   }

   if ($avg>=8.50 and $votes>=10) {
     $BESTDATA[$j]="$avg / $votes</td> <td>$prodname ($prodline{$k})<br>\n|$ratenums";
     $j++;
   }
   elsif ($avg<=3.00 and $votes>=10) {
     $WORSTDATA[$jj]="$avg / $votes</td> <td>$prodname ($prodline{$k})<br>\n|$ratenums";
     $jj++;
   }
 }

}

#open(COUNT,">$RATE{$k}.dat") || die "Could not open $RATE{$k}.dat: $!\n";
#print COUNT "$newline";

# Start printing the output to the screen
print "Content-Type: text/html\n\n";
print "<html><head><title>Best &amp; Worst *D&amp;D Products</title></head>\n";
print "<body text=#000000 bgcolor=#FFFFFF>\n";

print "<h1>Best &amp; Worst *D&amp;D Products, Sorted By Ratings</h1>\n";

#@date = localtime(time);
#$date[4]++;
#$modyear=$date[5]
#if (length($modyear)==3) {
#  $modyear=substr($modyear,1,2);
#}
#print "<i>As of: $date[4]\/$date[3]\/$modyear</i>\n";
($sec,$min,$hour,$modmday,$modmon,$modyear,$wday,$yday,$isdst) = localtime(time);
$modmon++;
if (length($modyear)==3) {
  $modyear=substr($modyear,1,2);
}
print "<i>(As of: $modmon\/$modmday\/$modyear)</i>\n";

print "<hr>\n";
print "<p><font size=+2><b><a name=\"best\">The Top Products</a></b></font><br>\n";
print "As measured by average ratings by respondents to the unofficial *D&D Product Survey.<br>\n";
print "<i>Only products with at least 10 votes and an average rating of 8.5 or higher were eligible.</i>\n";
print "<p>\n";
print "<p>Jump to the <a href=\"#worst\">Worst products list</a>\n";

print "<p>\n";
print "<table>\n";
print "<tr><th>Rank</th> <th>Rating / Votes</th> <th align=left>Product Name</th>\n";

@outdata = sort {$b <=> $a} @BESTDATA;
$j=1;

foreach $odata (@outdata) { ($main, $rnums) = split(/\|/, $odata);

   print "<tr><td align=center valign=center>#$j </td><td align=center valign=top>$main";
   $j++;

   #$_=$rnums;
   #($r0, $r1, $r2, $r3, $r4, $r5, $r6, $r7, $r8, $r9, $r10)= split(/\,/, $_, 11);
   #print "<font color=0000FF>0:$r0</font> &nbsp\;\n";
   #print "<font color=0000CC>1:$r1</font> &nbsp\;\n";
   #print "<font color=330099>2:$r2</font> &nbsp\;\n";
   #print "<font color=660099>3:$r3</font> &nbsp\;\n";
   #print "<font color=990099>4:$r4</font> &nbsp\;\n";
   #print "<font color=BB00BB>5:$r5</font> &nbsp\;\n";
   #print "<font color=990099>6:$r6</font> &nbsp\;\n";
   #print "<font color=990066>7:$r7</font> &nbsp\;\n";
   #print "<font color=990033>8:$r8</font> &nbsp\;\n";
   #print "<font color=CC0000>9:$r9</font> &nbsp\;\n";
   #print "<font color=FF0000>10:$r10</font> \n";
   print "</td></tr>\n";
}

# Finish up the best results table
print "</table>\n";
print "<p>\n";
print "<hr>\n";
print "View the <a href=\"display.html\">complete ratings</a>.<br>\n";
print "Go <a href=\"rating.html\">Rate the products</a>.<br>\n";
print "Return to the <a href=\"index.html\">ratings index page</a>.\n";
print "<p>\n";
print "<hr>\n";

print "<p><font size=+2><b><a name=\"worst\">Bottom-Feeders:</a> The Worst Products</b></font><br>\n";
print "As measured by average ratings by respondents to the unofficial TSR Product Survey<br>\n";
print "<i>Only products with at least 10 votes and an average rating of 3.0 or lower were eligible</i>\n";
print "<p>\n";
print "<p>Jump to the <a href=\"#best\">Best products list</a>\n";

print "<p>\n";
print "<table>\n";
print "<tr><th>Rank</th> <th>Rating / Votes</th> <th align=left>Product Name</th>\n";

@outdata = sort {$b <=> $a} @WORSTDATA;

foreach $odata (@outdata) { ($main, $rnums) = split(/\|/, $odata);

   print "<tr><td align=center valign=top>#$jj </td><td align=center valign=top>$main";
   $jj--;

   #$_=$rnums;
   #($r0, $r1, $r2, $r3, $r4, $r5, $r6, $r7, $r8, $r9, $r10)= split(/\,/, $_, 11);
   #print "<font color=0000FF>0:$r0</font> &nbsp\;\n";
   #print "<font color=0000CC>1:$r1</font> &nbsp\;\n";
   #print "<font color=330099>2:$r2</font> &nbsp\;\n";
   #print "<font color=660099>3:$r3</font> &nbsp\;\n";
   #print "<font color=990099>4:$r4</font> &nbsp\;\n";
   #print "<font color=BB00BB>5:$r5</font> &nbsp\;\n";
   #print "<font color=990099>6:$r6</font> &nbsp\;\n";
   #print "<font color=990066>7:$r7</font> &nbsp\;\n";
   #print "<font color=990033>8:$r8</font> &nbsp\;\n";
   #print "<font color=CC0000>9:$r9</font> &nbsp\;\n";
   #print "<font color=FF0000>10:$r10</font> \n";
   print "</td></tr>\n";
}

# Finish up the worst results table & the page
print "</table>\n";
print "<p>\n";
print "<hr>\n";
print "View the <a href=\"http://www.hahnlibrary.net/rpgs/rate/display.html\">complete ratings</a>.<br>\n";
print "Go <a href=\"http://www.hahnlibrary.net/rpgs/rate/rating.html\">Rate the products</a>.<br>\n";
print "Return to the <a href=\"http://www.hahnlibrary.net/rpgs/rate/index.html\">index page</a>\n";
print "</body></html>";
