/*
1. save stats per user
a. MPS - moves per second
b. best score on image based on time/moves
2. high scores on images, etc
a. fastest times
b. least moves
3. challenge a user. two users get the same puzzle (saved to a file) and play it to see who wins.
*/
require('security.php');
$intParts = verifyInput();
handleUpload();
handleWinner();
// intro and selection screen
if (($intParts == '') && ($_POST['PARTS'] == '') | (($_POST['IMAGE'] == '') && ($_FILES['UPLOAD']['tmp_name'] == '')))
{
?>
LANGDON X DOT COM - Image Puzzle
exit;
}
//
// bad input
//
if ($_POST['PARTS'] != '' && $_POST['IMAGE'] != '')
{
header('location: ' . $_SERVER['PHP_SELF'] . '?p=' . $_POST['PARTS'] . '&f=' . $_POST['IMAGE']);
}
//
// add a record to show that they started the game
//
if (isLoggedIn())
{
/*
//1. write a get_puzzle_file_id() or just mysql_query_scalarit
//2. create some sort of hash
//3. add this hash + $puzzle_user_statistic_id to the form (hidden fields)
4. on game completion submit some form somewhere that checks the verifies the hash and updates the record with the # of moves and time amount
*/
require('../include/database.php');
$puzzle_file_id = mysql_query_scalar('SELECT pkPUZZLE_FILE_ID FROM PUZZLE_FILE WHERE FILENAME = \'' . $_GET['f'] . '\'');
$query = "INSERT INTO PUZZLE_USER_STATISTIC (fkPUZZLE_FILE_ID, fkPUZZLE_USER_ID, MOVES, PARTS, SECONDS, DATE_INSERTED, DATE_LAST_UPDATED) VALUES ({$puzzle_file_id}, {$_COOKIE['USER_ID']}, NULL, {$_GET['p']}, NULL, NOW(), NOW())";
mysql_query($query) or die('stat insert failed.');
$puzzle_user_statistic_id = mysql_insert_id($connection);
mysql_close($connection);
}
//
// define the width and height
//
$source = imagecreatefromjpeg($_GET['f']);
$intWidth = makeFit(imagesx($source), sqrt($_GET['p']));
$intHeight = makeFit(imagesy($source), sqrt($_GET['p']));
$intPieceWidth = round($intWidth / sqrt($_GET['p']));
$intPieceHeight = round($intHeight / sqrt($_GET['p']));
//
// create the parts
//
$a_strParts = array();
$a_strPartsOriginal = array();
for ($i = 0; $i < $_GET['p']; $i++)
{
$a_strPartsOriginal[$i] = $i;
}
//
// jumble up the parts
//
$a_strParts = jumbleArray($a_strPartsOriginal);//$a_strPartsOriginal;
//
// variables for the image parser
//
$_SESSION['PARTS'] = $_GET['p'];
$_SESSION['IMAGE'] = $_GET['f'];
$_SESSION['WIDTH'] = $intWidth;
$_SESSION['HEIGHT'] = $intHeight;
?>
LANGDON X DOT COM - Image Puzzle
for ($i = 0; $i < $_SESSION['PARTS'] - 1; $i++)
{
$intTop = 0;
$intLeft = $intPieceWidth * $i;
while (($intLeft+intPieceWidth) >= $intWidth)
{
$intTop = $intTop + $intPieceHeight;
$intLeft = $intLeft - $intWidth;
}
?>

}
?>
function handleUpload()
{
$strTempFile = $_FILES['UPLOAD']['tmp_name'];
// copy the file from the temp php folder
if (is_uploaded_file($strTempFile))
{
$strPath = $_SERVER['DOCUMENT_ROOT'] . '/puzzle/';
@$a_image_size = GetImageSize($strTempFile);
if ($a_image_size == '')
throw_exception('You must upload a JPEG file.');
// if it's not a jpeg
if ($a_image_size[2] != 2)
{
unlink($strTempFile);
throw_exception('You must upload a JPEG file.');
}
// if the file size is too large
if (filesize($strTempFile) > 153600)
{
unlink($strTempFile);
throw_exception('The file must be smaller than 150 kilobytes.');
}
// if it doesn't meet size requirements
if (!((($a_image_size[0] <= 640) && ($a_image_size[0] >= 160)) && (($a_image_size[1] <= 640) && ($a_image_size[1] >= 160))))
{
unlink($strTempFile);
throw_exception('The image must be no greater than 640 pixels and no smaller than 100 pixels on any side.');
}
$i = 0;
$strNewFile = $strPath . $_FILES['UPLOAD']['name'];
$strNewFileName = $_FILES['UPLOAD']['name'];
// if a file w/ the same name exists
while (file_exists($strNewFile))
{
$i++;
$strNewFile = $strPath . $i . $_FILES['UPLOAD']['name'];
$strNewFileName = $i . $_FILES['UPLOAD']['name'];
}
// move file to intended destination
move_uploaded_file($strTempFile, $strNewFile) or die('failed to move file.');
chmod($strNewFile, 7660);
require('../include/database.php');
// insert a new record for this file
$query = 'INSERT INTO PUZZLE_FILE (fkPUZZLE_USER_ID, FILENAME) VALUES (' . ((isLoggedIn()) ? $_SESSION['USER_ID'] : 'NULL') . ', \'' . $strNewFileName . '\')';
mysql_query($query) or die('puzzle insert failed');
mysql_close($connection);
// redirect
header('location: ' . $_SERVER['PHP_SELF'] . '?p=' . $_POST['PARTS'] . '&f=' . $strNewFileName);
}
}
function handleWinner()
{
if ($_SERVER['QUERY_STRING'] == 'winner')
{
print 'Congratulations. Try another?';
if (isLoggedIn())
{
if ($_POST['hash'] == md5($_SESSION['USER_ID'] . ' plays dis puzzle ' . $_POST['id'] . ' @ dis parts ' . $_POST['parts']))
{
require('../include/database.php');
$query = "UPDATE PUZZLE_USER_STATISTIC SET DATE_LAST_UPDATED = NOW(), MOVES = {$_POST['txtAttempt']}, SECONDS = {$_POST['txtTime']} WHERE pkPUZZLE_USER_STATISTIC_ID = {$_POST['id']}";
mysql_query($query) or die('stat update failed..');
mysql_close($connection);
// say if they made a new record or something
}
}
else
{
print '
If you were logged in, you might have been able to obtain a high score.';
}
exit;
}
}
function jumbleArray($p_array)
{
$a_array = $p_array;
$intIndex = count($p_array) - 1;
$intMaxMoves = count($p_array) * count($p_array) * 10;
$intWidth = sqrt(count($p_array));
for ($i = 0; $i < ($intMaxMoves + 6); $i++)
{
$a_intIndexes = array();
// can move up
if (($intIndex - $intWidth) >= 0)
$a_intIndexes[] = ($intIndex - $intWidth);
// can move down
if (($intIndex + $intWidth) < count($p_array))
$a_intIndexes[] = ($intIndex + $intWidth);
// can move left
if ( (($intIndex - 1) > 0) && ((($intIndex) % $intWidth) != 0))
$a_intIndexes[] = ($intIndex - 1);
// can move right
if ( (($intIndex + 1) < count($p_array)) && ((($intIndex + 1) % $intWidth) != 0))
$a_intIndexes[] = ($intIndex + 1);
if ($intMaxMoves < $i)
{
sort($a_intIndexes);
$intNewIndex = $a_intIndexes[count($a_intIndexes) - 1];
if ((count($a_intIndexes) - 1) == $intNewIndex)
return $a_array;
}
else
{
// pick a direction at random
$intNewIndex = $a_intIndexes[rand(0, (count($a_intIndexes) - 1))];
}
// swap the pieces
$temp = $a_array[$intIndex];
$a_array[$intIndex] = $a_array[$intNewIndex];
$a_array[$intNewIndex] = $temp;
$intIndex = $intNewIndex;
}
return $a_array;
}
function makeFit($p_size, $p_divider)
{
while ($p_size % $p_divider != 0)
$p_size--;
return $p_size;
}
function throw_exception($p_text)
{
$_SESSION['exception'] = $p_text;
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
function verifyInput()
{
// verify that the parts input is valid
if (isset($_GET['p']))
if (($_GET['p'] == 9) | ($_GET['p'] == 16) | ($_GET['p'] == 25) | ($_GET['p'] == 36) | ($_GET['p'] == 49))
$intParts = $_GET['p'];
else
$intParts = '';
// verify that the filename specified is valid
if (isset($_GET['f']))
if (!file_exists($_GET['f']))
{
header('location: ' . $_GET['PHP_SELF']);
exit;
}
return $intParts;
}
?>