#!/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 CGI::Fast; use webshots::MDocs; use webshots::WebshotsUtility; use webshots::SuperLogEvent; use webshots::SuperMail; #------------------------------------------------------------------------- # Get or create the fileList and startTime! #------------------------------------------------------------------------- my $BOXPATH = 'http://daily.webshots.com/content/puzzles/block/'; my $URLPATH = '/content/puzzles/block/'; my $FILEPATH = '/home/dailyphoto/htdocs/content/puzzles/block/'; 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 $EXIT = 0; #------------------------------------------------------------------------- #------------------------------------------------------------------------- LOOP: while (my $cgi = new CGI::Fast) { my $fileList; my $imgUrl; my $html; my $html_t = $HTML; my $d_html_t = $D_HTML; $fileList = $cgi->param('order'); my $game_date; if ($cgi->param('game_date')) { $game_date = $cgi->param('game_date'); } else { $game_date = $cgi->cookie(-name=>'game_date'); } $imgUrl = $URLPATH . 'img/' . $game_date . '/'; my $imgFolder = $FILEPATH . 'img/' . $game_date . '/'; my $startTime = $cgi->cookie(-name=>'blockGameStart'); my $numMoves = $cgi->cookie(-name=>'blockGameMoves'); if ($fileList eq '') { $fileList = 'abcdefghijkl'; # create a new cookie for this game $startTime = time(); $numMoves = 0; print "Set-Cookie: ".$cgi->cookie(-name=>'blockGameStart', -value=>$startTime, -expires=>'+12h', domain=>'.webshots.com')."\n"; print "Set-Cookie: ".$cgi->cookie(-name=>'blockGameMoves', -value=>$numMoves, -expires=>'+12h', domain=>'.webshots.com')."\n"; print "Set-Cookie: ".$cgi->cookie(-name=>'game_date', -value=>$game_date, -expires=>'+12h', domain=>'.webshots.com')."\n"; SuperLogEvent::Log("games", 1, { TYPE => "start_game", GAME => "block" } ); } print "Set-Cookie: ".$cgi->cookie(-name=>'blockGameMoves', -value=>$numMoves+1, -expires=>'+12h', domain=>'.webshots.com')."\n"; my $minutes = int((time()-$startTime)/60); my $seconds = time()-$startTime-$minutes*60; my $timeString = sprintf("%d:%02s
", $minutes,$seconds); #------------------------------------------------------------------------- # Read in the solution file #------------------------------------------------------------------------- my $path = $imgFolder.'solution.txt'; open (FILE, "<$path"); my $solution = ; close (FILE); #------------------------------------------------------------------------- # Look for a winner! #------------------------------------------------------------------------- if ($fileList eq $solution) { SuperLogEvent::Log("games", 1, { TYPE => "end_game", MOVES => $numMoves, GAME => "block" } ); my $name = WebshotsUtility::PeekFirstName($cgi); my $start_url = $URLPATH . 'create_page.fcgi?game_date=' . $game_date; my $tryagain = 'Try Again?'; $timeString =~ s/
//; my $send_url = $BOXPATH . 'create_page.fcgi?game_date=' . $game_date; my $send_link = SuperMail::GetFormHTML ( -MYSUBJECT => "Try this!", -MYTEXT => "Can you complete this puzzle?\n\n$send_url\n", -MYGREETING => "Hey.", -MYSALUTATION => "See ya", -MYLINKSIZE => "2", -MYLINKTEXT => "Challenge A Friend To Complete This Puzzle!", -MYCUSTOM => "Challenge A Friend", -MYTARGET => "_parent", -MYREF => "puzzles_block" ); $d_html_t =~ s/_NAME_/ $name/; $d_html_t =~ s/_TIME_/$timeString/; $d_html_t =~ s/_MOVES_/$numMoves/; $d_html_t =~ s/_TRYAGAIN_/$tryagain/; $d_html_t =~ s/_SEND_/$send_link/; my $r_var = \$d_html_t; MDocs::InsertSitewideHeaderFooter($r_var,"/home/dailyphoto/htdocs/mdocs/", { -PAGEZONE => 'daily', -LOGJAM => 'puzzle_block'}); print "Content-type: text/html\n\n"; print $d_html_t; $EXIT ? exit() : next LOOP; } #------------------------------------------------------------------------- # Show the preview and make the table #------------------------------------------------------------------------- my $width = 4; my $height = 3; my $path = $imgUrl .'preview.gif'; #------------------------------------------------------------------------- # Get the blank cell and position (it's always the last letter in the solution #------------------------------------------------------------------------- my $blankCell = substr($solution, $width*$height-1, 1); my $blankPos = index($fileList, $blankCell); $html .= "\n"; for (my $y=0;$y<$height;$y++) { $html .= "\n"; for (my $x=0;$x<$width;$x++) { $html .= "\n"; } $html .= "\n"; } $html .= "
"; my $filename = substr($fileList, $y*$width+$x, 1).'.gif'; my $pos = $y*$width + $x; if ($pos == $blankPos-1) #left { if ($pos % $width == $width-1) { $html .= PrintNormalImage($filename,$imgUrl); } else { $html .= PrintLinkedImage($filename, SwapOrder($pos, $pos+1, $fileList),$imgUrl); } } elsif ($pos == $blankPos-$width) #top { $html .= PrintLinkedImage($filename, SwapOrder($pos, $pos+$width, $fileList),$imgUrl); } elsif ($pos == $blankPos+1) # right of blank { if ($pos % $width == 0) { $html .= PrintNormalImage($filename,$imgUrl); } else { $html .= PrintLinkedImage($filename, SwapOrder($pos-1, $pos, $fileList),$imgUrl); } } elsif ($pos == $blankPos+$width) #bottom { $html .= PrintLinkedImage($filename, SwapOrder($pos-$width, $pos, $fileList),$imgUrl); } else # not a special square - just an image { $html .= PrintNormalImage($filename,$imgUrl); } $html .= "
"; my $photo_info_file = $imgFolder . 'info.txt'; my %data = DPGlobals::ReadTabbedHashFile($photo_info_file); my $title = $data{"TITLE"}; my $credit = $data{"CREDIT"}; my $credit_html = '' . $title . ' - Copyright ' . $credit . ''; my $preview = ''; my $restart_url = $URLPATH . 'create_page.fcgi?game_date=' . $game_date; my $send_url = $BOXPATH . 'create_page.fcgi?game_date=' . $game_date; my $send_link = SuperMail::GetFormHTML ( -MYSUBJECT => "Try this!", -MYTEXT => "Can you complete this puzzle?\n\n$send_url\n", -MYGREETING => "Hey.", -MYSALUTATION => "See ya", -MYLINKSIZE => "2", -MYLINKTEXT => "Challenge A Friend To Complete This Puzzle!", -MYCUSTOM => "Challenge A Friend", -MYREF => "puzzles_block" ); $html_t =~ s/_PUZZLE_/$html/; $html_t =~ s/_TIME_/$timeString/; $html_t =~ s/_MOVES_/$numMoves/; $html_t =~ s/_PREVIEW_/$preview/; $html_t =~ s/_CREDITLINE_/$credit_html/; $html_t =~ s/_RESTART_/$restart_url/; $html_t =~ s/_SEND_/$send_link/; my $r_var = \$html_t; MDocs::InsertSitewideHeaderFooter($r_var,"/home/dailyphoto/htdocs/mdocs/", { -PAGEZONE => 'daily', -LOGJAM => 'puzzle_block'}); print "Content-type: text/html\n\n"; print $html_t; $EXIT ? exit() : next LOOP; } #------------------------------------------------------------------------- # FUNCTIONS #------------------------------------------------------------------------- sub PrintNormalImage { my $imgUrl = $_[1]; my $filename = $imgUrl . $_[0]; my $return_html = ""; return $return_html; } #------------------------------------------------------------------------- #------------------------------------------------------------------------- sub PrintLinkedImage { my $imgUrl = $_[2]; my $filename = $imgUrl . $_[0]; my $newFileList = $_[1]; my $return_html = ""; $return_html .= ""; $return_html .= ""; return $return_html; } #------------------------------------------------------------------------- #------------------------------------------------------------------------- sub SwapOrder { my $pos1 = $_[0]; my $pos2 = $_[1]; my $fileList = $_[2]; # always make pos1 less than pos2 if ($pos2 < $pos1) { my $temp = $pos1; $pos1 = $pos2; $pos2 = $temp; } # make the new swapped list my $newList = substr($fileList, 0, $pos1); $newList .= substr($fileList, $pos2, 1); $newList .= substr($fileList, $pos1+1, $pos2-$pos1-1); $newList .= substr($fileList, $pos1, 1); $newList .= substr($fileList, $pos2+1, length($fileList)-$pos2); return $newList; } #------------------------------------------------------------------------- # END #-------------------------------------------------------------------------