#!/usr/bin/perl
require "../cgi-lib.pl";
ReadParse(*in);
if($in{func} eq "report") {
    print "Status: 302 Moved Temporarily\n";
    print "Location: /~dave/images/spacer.gif\n\n";
    open($RF,">> /home/dave/tmp/csscore.txt");
    print $RF scalar(localtime())."\^$in{score}\^$ENV{REMOTE_ADDR}\^$ENV{HTTP_USER_AGENT}\n";
    close $RF;
    exit(0);
}
print "Content-Type: text/html\n\n";
@themes = (
    { name => 'Classic Space', year => '1978', logo => 'classic_space.png', colors => {
	19 => { name => 'Tr. Yellow', rgb => '#EEEE70', val => '437.62', },
	10 => { name => 'Dark Gray', rgb => '#6d6e6c', val => '0.66', },
	5 => { name => 'Red', rgb => '#FF281b', val => '1.2', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '1838.34', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '3719.87', },
	20 => { name => 'Tr. Green', rgb => '#50DF50', val => '85.14', },
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '163.07', },
	3 => { name => 'Yellow', rgb => '#f5dd2f', val => '37.93', },
	12 => { name => 'Tr. Clear', rgb => '#EEEEEE', val => '13.03', },
	11 => { name => 'Black', rgb => '#010101', val => '1862.28', },
	14 => { name => 'Tr. Dark Blue', rgb => '#6060FF', val => '167.36', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '3248.04000000001', },
      }, },
    { name => 'Blacktron I', year => '1987', logo => 'blacktron1.png', colors => {
	1 => { name => 'White', rgb => '#f2f3f2', val => '4.97', },
	19 => { name => 'Tr. Yellow', rgb => '#EEEE70', val => '89.66', },
	11 => { name => 'Black', rgb => '#010101', val => '1339.66', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '6.34', },
	3 => { name => 'Yellow', rgb => '#f5dd2f', val => '134.82', },
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '31.38', },
	20 => { name => 'Tr. Green', rgb => '#50DF50', val => '0.4', },
      }, },
    { name => 'Futuron', year => '1987', logo => 'futuron.png', colors => {
	20 => { name => 'Tr. Green', rgb => '#50DF50', val => '0.43', },
	14 => { name => 'Tr. Dark Blue', rgb => '#6060FF', val => '250.25', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '28.16', },
	19 => { name => 'Tr. Yellow', rgb => '#EEEE70', val => '0.48', },
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '27.8', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '1356.74', },
	11 => { name => 'Black', rgb => '#010101', val => '852.13', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '4.39', },
      }, },
    { name => 'Space Police I', year => '1989', logo => 'space_police1.png', colors => {
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '0.64', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '13.05', },
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '113.02', },
	11 => { name => 'Black', rgb => '#010101', val => '862.19', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '380.79', },
      }, },
    { name => 'M-Tron', year => '1990', logo => 'mtron.png', colors => {
	11 => { name => 'Black', rgb => '#010101', val => '1072.61', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '6.95', },
	5 => { name => 'Red', rgb => '#FF281b', val => '646.63', },
	16 => { name => 'Tr. Neon Green', rgb => '#C0FF60', val => '97.24', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '79.91', },
      }, },
    { name => 'Blacktron II', year => '1991', logo => 'blacktron2.png', colors => {
	16 => { name => 'Tr. Neon Green', rgb => '#C0FF60', val => '223.58', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '304.54', },
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '3.9', },
	11 => { name => 'Black', rgb => '#010101', val => '1215.24', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '6.15', },
	20 => { name => 'Tr. Green', rgb => '#50DF50', val => '1.04', },
      }, },
    { name => 'Space Police II', year => '1992', logo => 'space_police2.png', colors => {
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '363.95', },
	5 => { name => 'Red', rgb => '#FF281b', val => '75.92', },
	11 => { name => 'Black', rgb => '#010101', val => '746.83', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '3.32', },
	20 => { name => 'Tr. Green', rgb => '#50DF50', val => '103.08', },
      }, },
    { name => 'Ice Planet 2002', year => '1993', logo => 'ice_planet.png', colors => {
	18 => { name => 'Tr. Neon Orange', rgb => '#FF800D', val => '222.4', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '459.43', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '7.8', },
	11 => { name => 'Black', rgb => '#010101', val => '399.84', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '494.46', },
      }, },
    { name => 'Spyrius', year => '1994', logo => 'spyrius.png', colors => {
	5 => { name => 'Red', rgb => '#FF281b', val => '241.87', },
	16 => { name => 'Tr. Neon Green', rgb => '#C0FF60', val => '5.98', },
	10 => { name => 'Dark Gray', rgb => '#6d6e6c', val => '90.76', },
	14 => { name => 'Tr. Dark Blue', rgb => '#6060FF', val => '74.23', },
	11 => { name => 'Black', rgb => '#010101', val => '767.52', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '9.38', },
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '9.05', },
      }, },
    { name => 'Unitron', year => '1995', logo => 'unitron.png', colors => {
	14 => { name => 'Tr. Dark Blue', rgb => '#6060FF', val => '159.35', },
	16 => { name => 'Tr. Neon Green', rgb => '#C0FF60', val => '26.08', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '395.35', },
	12 => { name => 'Tr. Clear', rgb => '#EEEEEE', val => '0.36', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '180.27', },
	11 => { name => 'Black', rgb => '#010101', val => '1140.27', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '73.26', },
	10 => { name => 'Dark Gray', rgb => '#6d6e6c', val => '57.92', },
      }, },
    { name => 'Exploriens', year => '1996', logo => 'exploriens.png', colors => {
	14 => { name => 'Tr. Dark Blue', rgb => '#6060FF', val => '179.93', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '997.509999999999', },
	19 => { name => 'Tr. Yellow', rgb => '#EEEE70', val => '0.24', },
	10 => { name => 'Dark Gray', rgb => '#6d6e6c', val => '1.36', },
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '8.98', },
	16 => { name => 'Tr. Neon Green', rgb => '#C0FF60', val => '48.3', },
	5 => { name => 'Red', rgb => '#FF281b', val => '1.28', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '22.13', },
	11 => { name => 'Black', rgb => '#010101', val => '733.87', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '0.3', },
      }, },
    { name => 'Robo Force', year => '1997', logo => 'roboforce.png', colors => {
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '0.48', },
	5 => { name => 'Red', rgb => '#FF281b', val => '6.33', },
	14 => { name => 'Tr. Dark Blue', rgb => '#6060FF', val => '1.84', },
	10 => { name => 'Dark Gray', rgb => '#6d6e6c', val => '10.86', },
	11 => { name => 'Black', rgb => '#010101', val => '567.47', },
	16 => { name => 'Tr. Neon Green', rgb => '#C0FF60', val => '37.21', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '328.56', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '4.14', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '44.42', },
	18 => { name => 'Tr. Neon Orange', rgb => '#FF800D', val => '19.16', },
	3 => { name => 'Yellow', rgb => '#f5dd2f', val => '21.6', },
      }, },
    { name => 'UFO', year => '1997', logo => 'ufo.png', colors => {
	16 => { name => 'Tr. Neon Green', rgb => '#C0FF60', val => '276.98', },
	3 => { name => 'Yellow', rgb => '#f5dd2f', val => '1.43', },
	12 => { name => 'Tr. Clear', rgb => '#EEEEEE', val => '2', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '5.85', },
	17 => { name => 'Tr. Red', rgb => '#FF6060', val => '0.22', },
	14 => { name => 'Tr. Dark Blue', rgb => '#6060FF', val => '0.48', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '972.27', },
	18 => { name => 'Tr. Neon Orange', rgb => '#FF800D', val => '2.39', },
	11 => { name => 'Black', rgb => '#010101', val => '919.59', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '8.21', },
	10 => { name => 'Dark Gray', rgb => '#6d6e6c', val => '6.58', },
	5 => { name => 'Red', rgb => '#FF281b', val => '194.93', },
      }, },
    { name => 'Insectoids', year => '1998', logo => 'insectoids.png', colors => {
	5 => { name => 'Red', rgb => '#FF281b', val => '0.36', },
	18 => { name => 'Tr. Neon Orange', rgb => '#FF800D', val => '1.04', },
	3 => { name => 'Yellow', rgb => '#f5dd2f', val => '2.5', },
	1 => { name => 'White', rgb => '#f2f3f2', val => '0.72', },
	14 => { name => 'Tr. Dark Blue', rgb => '#6060FF', val => '70.74', },
	16 => { name => 'Tr. Neon Green', rgb => '#C0FF60', val => '130.72', },
	7 => { name => 'Blue', rgb => '#1030FF', val => '665.36', },
	11 => { name => 'Black', rgb => '#010101', val => '642.59', },
	9 => { name => 'Light Gray', rgb => '#a1a5a2', val => '2.04', },
	10 => { name => 'Dark Gray', rgb => '#6d6e6c', val => '900.109999999999', },
	20 => { name => 'Tr. Green', rgb => '#50DF50', val => '78.36', },
      }, },
    );

use Data::Dumper;

my($tid) = 1 + (int rand(10));
foreach my $t (sort { (rand() > 0.5) ? 1 : -1 } @themes) {
    $t->{tid} = $tid;
    $t->{cid} = 100-$tid;
    $tid += 1 + (int rand(6));
    #my $url = "http://www.suave.net/~dave/cgi/pie.cgi?width=131&height=131&bgcolor=FFFFFF&border=1&saveme=1&name=".minify($t->{name});
    my $url = "https://baseplate.com/davee123/pie.cgi?antialias=1&random=1&width=131&height=131&bgcolor=FFFFFF&border=1&saveme=1&name=".minify($t->{name});
    my($id) = 0;
    my($total,$alt);
    foreach my $color (sort { (rand() > 0.5) ? 1 : -1 } (keys %{$t->{colors}})) {
	$total += $t->{colors}{$color}{val};
	$url .= "&s$id"."color=$t->{colors}{$color}{rgb}&s$id"."val=$t->{colors}{$color}{val}";
	if($t->{colors}{$color}{name} =~ /^Tr\./) {
	    $url .= "&s$id"."dither=1";
	}
	$colors{$color}{name} = $t->{colors}{$color}{name};
	$colors{$color}{rgb} = $t->{colors}{$color}{rgb};
	$id++;
    }
    foreach my $color (sort { $t->{colors}{$b}{val} <=> $t->{colors}{$a}{val}; } (keys %{$t->{colors}})) {
	my($perc) = 100*$t->{colors}{$color}{val}/$total;
	if($perc < 0.1) { $alt .= "<0.1"; }
	else { $alt .= sprintf("%.1f",(100*$t->{colors}{$color}{val}/$total)); }
	$alt .= "% ".$t->{colors}{$color}{name}."\n";
    }
    $url =~ s/\#//gs;
    $t->{color_img} = $url;
    $t->{alt} = $alt;
    $t->{logo_img} = "http://www.suave.net/~dave/images/logos/$t->{logo}";
}
print qq`<!DOCTYPE html>
<html>
    <head>
       <title>LEGOLAND Space Color Quiz</title>
       <link rel="stylesheet" type="text/css" href="/~dave/ext/ext-4.2.1.883/resources/ext-theme-neptune/ext-theme-neptune-all.css">

       <script type="text/javascript" src="/~dave/ext/ext-4.2.1.883/ext-all.js"></script> 
       <script type="text/javascript" src="/~dave/ext/ext-4.2.1.883/ext-theme-neptune.js"></script>

       <script type ="text/javascript" src="csquiz.js"></script>

    </head>
<body>
<style>
#content \{
    width:1024px;
    height:75px;
    padding:2px;
    border:1px solid #FFF;
\}
    
#logospace \{
    float:left;
    width:100%;
    text-align: center;
    height:100%;
    border:1px solid #AAA;
     background-color:rgba(222, 222, 222, 1.0);
\}

#keyspace \{
    float: left;
    width: 1024px;
    height: 200px;
    border: 1px solid black;
\}

#mainRoom \{
    float:left;
    width:55%;
    height:100%;
    margin-left:15px;
    border:1px solid #AAA;
     background-color:rgba(222, 222, 222, 1.0);
\}
#colorspace \{
    width:1024px;
    height:450px;
    display: inline-block;
    border:1px solid #AAA;
     background-color: #FFF;
\}
\.themelogo \{
  width:50px;
  height:50px;
    color:#FFFFFF;
    background-color:rgba(254, 108, 98, 1.0);
  border:1px solid rgba(127, 127, 191, 1.0);    
  display: inline-block;
  margin:8px;
  cursor:pointer;
\}

\.colorkey \{
  width: 150px;
  height: 30px;
  border: none;
  margin: 8px;
  vertical-align: middle;
  display: inline-block;
\}

\.colorkeybox \{
  width:25px;
  height:25px;
  border:1px solid black;
  display: inline-block;
  vertical-align: middle;
  margin:8px;
\}

\.themecolor \{
  width:60px;
  height:60px;
    color:#FFFFFF;
    background-color:#FFFFFF;
  border:1px dotted black;
  display: inline-block;
  margin:10px;
\}
\.colorcell \{
  text-align: center;
\}
\.valid-zone \{
    background-color:rgba(128, 128, 255, 1.0) !important;
\}
\.selected \{
  opacity:0.5;
\}
\.scorebox \{
  background-color: #CCCCCC;
  display: inline-block;
  border: 3px solid black;
  text-align: center;
\}
\.rank \{
  font-size: 20pt;
  color: #2060A0;
  margin-top: 20px;
  margin-left: 20px;
  margin-right: 20px;
  text-align: center;
\}
\.score \{
  margin-left: 20px;
  margin-right: 20px;
  text-align: center;
  margin-bottom: 20px;
\}
\.pinfo \{
  width: 1024px;
  text-align: center;
\}
</style>`;
print "<div id=\"content\">\n";
print "<div style=\"display: inline-block; text-align: center; width: 1024px; padding: 2px;\">Match the LEGOLAND Space subtheme logo to its corresponding colors!</div>\n";
print "<div id=\"logospace\">\n";
foreach my $t (sort randorder (@themes)) {
    print "<div id=\"logodiv_$t->{tid}\" class=\"themelogo\"><img title=\"$t->{name}\" id=\"logoimg_$t->{tid}\" width=\"50\" height=\"50\" src=\"$t->{logo_img}\"></div>\n";
}
print "</div>\n";
print "<div id=\"colorspace\">\n";
print "<table>\n<tr>";
my($i) = 0;
foreach my $t (sort randorder (@themes)) {
    $i++;
    print "<td id=\"colortd_$t->{cid}\" class=\"colorcell\">";
    print "<img id=\"colorimg_$t->{cid}\" src=\"$t->{color_img}\" title=\"$t->{alt}\">";
    print "<div id=\"colordiv_$t->{cid}\" class=\"themecolor\">";
    print "</td>\n";
    unless($i % 7) {
	print "</tr><tr>\n";
    }
}
print "</tr></table>\n";
print "</div>\n";

print "<div id=\"pinfo\" class=\"pinfo\">\n";
print "</div>\n";
#print "<img style=\"float: left;\" src=\"/~dave/images/tmp/cs_colorkey.png\">\n";

#print "<div id=\"keyspace\">\n";
#foreach my $c (sort { if($colors{$a}{name} =~ /Tr\./ && $colors{$b}{name} !~ /Tr\./) { return 1; }
#		      elsif($colors{$a}{name} !~ /Tr\./ && $colors{$b}{name} !~ /Tr\./) { return -1; }
#		      else { return $colors{$a}{name} cmp $colors{$b}{name} } } (keys %colors)) {
#    print "<div class=\"colorkey\"><div class=\"colorkeybox\" style=\"background-color: $colors{$c}{rgb};\"></div>$colors{$c}{name}</div>\n";
#}
#print "</div>\n";

sub minify {
    my($v) = @_;
    $v = lc $v;
    $v =~ s/\W+//gs;
    return $v;
}

sub randorder {
    return (rand() > 0.5) ? 1 : -1;
}
