#!/usr/bin/perl
#-------------------------------------------------------------------------
# Webshots Puzzler web version. Copyright 1999 The Webshots Corporation
#-------------------------------------------------------------------------
use CNET::lib;
use lib '/home/lib';
use lib '/home/dailyphoto/htdocs/content';
use lib '/home/dailyphoto/htdocs/content/puzzles/';
use strict;
use wsys::CalDate;
use wsys::DPGlobals;
use webshots::MDocs;
use webshots::WebshotsUtility;
use webshots::SuperLogEvent;
use CGI::Fast;
my $URLPATH = '/content/puzzles/concentration/';
my $FILEPATH = '/home/dailyphoto/htdocs/content/puzzles/concentration/';
my $TEMPLATE = $FILEPATH . 'base_template.html';
my $HTML = DPGlobals::ReadFile($TEMPLATE);
my $D_TEMPLATE = $FILEPATH . 'done_template.html';
my $D_HTML = DPGlobals::ReadFile($D_TEMPLATE);
my $E_TEMPLATE = $FILEPATH . 'error_template.html';
my $E_HTML = DPGlobals::ReadFile($E_TEMPLATE);
my $GAME_DATE;
my $IMG_FOLDER;
my $ERROR;
my $BLANK_PATH;
my $DONE_PATH;
my $QUESTION_PATH;
my $BODY;
my $SIZE_Y;
my $SIZE_X;
my $COMPLETE_TEXT;
my @TILE_ORDER;
my @DONE_TILES;
my $IMAGE_LIST;
LOOP: while (my $cgi = new CGI::Fast)
{
my $html_t = $HTML;
my $d_html_t = $D_HTML;
my $e_html_t = $E_HTML;
my $TODAY = new CalDate;
$TODAY->SetAsToday();
$GAME_DATE = $cgi->param('td');
$IMG_FOLDER = '';
$ERROR = '';
if ($GAME_DATE ne '') { $IMG_FOLDER = 'img/' . $GAME_DATE . '/'; }
else { $ERROR = 'You must specify a td to the cgi...'; }
$BLANK_PATH = $IMG_FOLDER . 'blank.gif';
$DONE_PATH = $IMG_FOLDER . 'done.gif';
$QUESTION_PATH = $IMG_FOLDER . 'question.gif';
$BODY = '';
$SIZE_Y = '';
$SIZE_X = '';
$COMPLETE_TEXT = '';
@TILE_ORDER = ();
@DONE_TILES = ();
$IMAGE_LIST = '';
my $flip_html = '';
my $guide_html = '';
my $foundTiles = '';
ReadOrderFile(); # Sets @TILE_ORDER
#-------------------------------------------------------------------------
# Check to see if the game is starting
#-------------------------------------------------------------------------
my $init = 0;
if ($cgi->param('s')) { $init = $cgi->param('s'); }
if ($init == 1 )
{
my $foundTilescookie = $cgi->cookie
(
-NAME => "foundTiles",
-VALUE => "0",
-EXPIRES => "+2h",
-DOMAIN => ".webshots.com",
-PATH => "/",
);
my $movescookie = $cgi->cookie
(
-NAME => "moves",
-VALUE => "0",
-EXPIRES => "+2h",
-DOMAIN => ".webshots.com",
-PATH => "/",
);
my $timecookie = $cgi->cookie
(
-NAME => "time",
-VALUE => "0",
-EXPIRES => "+2h",
-DOMAIN => ".webshots.com",
-PATH => "/",
);
print "Set-Cookie: $foundTilescookie\n";
print "Set-Cookie: $movescookie\n";
print "Set-Cookie: $timecookie\n";
# print "Set-Cookie: " . $cgi->cookie(-name=>'foundTiles', -value=>'0', domain=>'.webshots.com') . "\n";
# print "Set-Cookie: " . $cgi->cookie(-name=>'moves', -value=>'0', domain=>'.webshots.com') . "\n";
# print "Set-Cookie: " . $cgi->cookie(-name=>'time', -value=>'0', domain=>'.webshots.com') . "\n";
SuperLogEvent::Log("games", 1, { TYPE => "start_game", GAME => "concentration" } );
$foundTiles = 'reset';
}
#-------------------------------------------------------------------------
# Make sure a date is specified
#-------------------------------------------------------------------------
if ($ERROR ne '') { print "Content-type: text/html\n\n"; print $ERROR; exit;}
#-------------------------------------------------------------------------
# Make sure we have a good browser!
#-------------------------------------------------------------------------
my $browser = $ENV{"HTTP_USER_AGENT"};
my $version = 1;
if (($browser =~ /Netscape/i) || ($browser !~ /Mozilla.4|Mozilla.5/i) || ($browser !~ /MSIE|Internet Explorer/i))
{
my $msg = "This game uses javascript and requires Microsoft Internet Explorer version 4.0 or higher.";
$e_html_t =~ s/_ERROR_/$msg/;
my $r_var = \$e_html_t;
MDocs::InsertSitewideHeaderFooter($r_var,"/home/dailyphoto/htdocs/mdocs/", { -PAGEZONE => 'daily', -LOGJAM => 'puzzle_concentration'});
print "Content-type: text/html\n\n";
print $e_html_t;
next LOOP;
}
#-------------------------------------------------------------------------
# Look for a winner!
#-------------------------------------------------------------------------
if ($cgi->param('d') == 1)
{
my $stats = $cgi->param('t');
my $numMoves = $cgi->cookie(-NAME=>'moves');
SuperLogEvent::Log("games", 1, { TYPE => "end_game", MOVES => $stats, GAME => "concentration" } );
my $name = WebshotsUtility::PeekFirstName($cgi);
my $start_url = $URLPATH . 'create_page.cgi?td=' . $GAME_DATE;
my $tryagain = 'Try Again?';
my $game_date = $cgi->param('td');
$d_html_t =~ s/_GAMEDATE_/$game_date/g;
$stats =~ s/Elapsed time:|Guesses.*//g;
$stats =~ s/s/ seconds/g;
chop $stats;
$d_html_t =~ s/_NAME_/ $name/;
$d_html_t =~ s/_MOVES_/$numMoves/;
$d_html_t =~ s/_TIME_/$stats/;
$d_html_t =~ s/_TRYAGAIN_/$tryagain/;
my $r_var = \$d_html_t;
MDocs::InsertSitewideHeaderFooter($r_var,"/home/dailyphoto/htdocs/mdocs/", { -PAGEZONE => 'daily', -LOGJAM => 'puzzle_concentration'});
print "Content-type: text/html\n\n";
print $d_html_t;
next LOOP;
}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
if ($foundTiles eq "reset") { $foundTiles ='0'; }
else { $foundTiles = $cgi->cookie(-NAME=>'foundTiles'); }
@DONE_TILES = split(/\|/,$foundTiles);
while (@DONE_TILES < $SIZE_X*$SIZE_Y) { push(@DONE_TILES, 0); }
$guide_html = CreateDoneImage();
$flip_html = CreateFlipTable(\@TILE_ORDER);
CreateImageList(); # Sets $IMAGE_LIST
my $game_date = $cgi->param('td');
$html_t =~ s/_GAMEDATE_/$game_date/g;
$html_t =~ s/_BODY_/$flip_html/;
$html_t =~ s/_GUIDE_/$guide_html/;
$html_t =~ s/_BLANK_PATH_/$BLANK_PATH/g;
$html_t =~ s/_DONE_PATH_/$DONE_PATH/g;
my $temp = join('|', @DONE_TILES);
$html_t =~ s/_FOUND_TILE_STRING_/'$temp'/;
$html_t =~ s/_PRELOAD_IMAGE_LIST_/$IMAGE_LIST/;
my $r_var = \$html_t;
MDocs::InsertSitewideHeaderFooter($r_var,"/home/dailyphoto/htdocs/mdocs/", { -PAGEZONE => 'daily', -LOGJAM => 'puzzle_concentration'});
print "Content-type: text/html\n\n";
print $html_t;
next LOOP;
}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
sub CreateImageList
{
for (my $y=0;$y<$SIZE_Y;$y++)
{
$BODY.="
\n";
for (my $x=0;$x<$SIZE_X;$x++)
{
my $index = $y*$SIZE_X + $x;
my $tileCode = $TILE_ORDER[$index];
my $imgPath = $URLPATH . $IMG_FOLDER . 'img' . $index . '.gif';
$IMAGE_LIST.="imageList[$index]=new Image();\n";
$IMAGE_LIST.="imageList[$index].src=\"$imgPath\";\n";
}
}
}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
sub ReadOrderFile
{
my $path = $IMG_FOLDER . 'order.txt';
open (FILE, "<$path");
$SIZE_X = ;
$SIZE_Y = ;
my $temp = ;
close (FILE);
@TILE_ORDER = split(/\|/,$temp);
}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
sub CreateDoneImage
{
my $return_html;
$return_html .="\n";
for (my $y=0;$y<$SIZE_Y;$y++)
{
$return_html .="\n";
for (my $x=0;$x<$SIZE_X;$x++)
{
my $index = $y*$SIZE_X + $x;
$return_html .=" ";
if ($DONE_TILES[$index] == 1)
{
my $tileCode = $TILE_ORDER[$index];
my $imgPath = $URLPATH . $IMG_FOLDER . 'img'. $index . '.gif';
$return_html .=" ";
}
else
{
$return_html .=" ";
}
$return_html .=" | \n";
}
$return_html .="
\n";
}
$return_html .="
";
return $return_html;
}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
sub CreateFlipTable
{
my $r_TO = $_[0];
my @TILE_ORDER = @$r_TO;
my $tableWidth = $SIZE_Y*2;
my $tableHeight = $SIZE_X;
my $return_html;
$return_html.="\n";
for (my $y=0;$y<$tableHeight;$y++)
{
$return_html.="\n";
for (my $x=0;$x<$tableWidth;$x++)
{
$return_html.=" | ";
my $flip_cell_html = CreateFlipCell($x, $y, $y*$tableWidth + $x,\@TILE_ORDER);
$return_html = $return_html . $flip_cell_html;
$return_html.=" | \n";
}
$return_html.="
\n";
}
$return_html.="
";
return $return_html;
}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
sub CreateFlipCell
{
my $x = $_[0];
my $y = $_[1];
my $index = $_[2];
my $r_TO = $_[3];
my @TILE_ORDER = @$r_TO;
my $return_html;
my $tileCode = $TILE_ORDER[$index];
if ($DONE_TILES[$tileCode] == 1)
{
$return_html = "
";
return $return_html;
}
else
{
my $imgLink = "imageList[$TILE_ORDER[$index]].src";
$return_html = "
";
}
}
#-------------------------------------------------------------------------
# END
#-------------------------------------------------------------------------