1)
if (isset($_COOKIE['doLogin']) && $_COOKIE['doLogin'] == 'yes')
CookieLogin();
if (!isset($_SESSION['accepted']) || $_SESSION['accepted'] <> 1)
$accepted = false;
else
$accepted = true;
// encode array $json to JSON string
$debug = false;
//$debug = true;
if (isset($_GET['act']) && $_GET['act'] == "getmap") {
$map = GenerateMapByCode($_GET['mapcode']);
$solution = formSolution($_GET['solution']);
$solvedmap = MergeMapSolution($map, $solution);
if ($solvedmap == -1) {
$json['error'][] = "INVALID WALL POSITIONS";
}
if ($solvedmap == -2) {
$json['error'][] = "USED TOO MANY WALLS";
}
}
// TODO: Depreciate?
if (isset($_GET['checkachieve']) && $_GET['checkachieve'] == 'true' && $_SESSION['accepted'] == 1) {
$json['notification'] = true;
$userID = $_SESSION['userID'];
//Located in includes/datas.php
$note = getNotified($userID);
if ($note !== false)
$json['notificationtext'] = $note;
}
if ($_GET['r'] == 'reqMemberPage') {
$order = $_GET['order'];
$orderBy = $_GET['orderBy'];
$page = $_GET['reqPage'] + 0;
if (!is_int($page))
return;
$pageDivide = $_GET['membersPageDivide'] + 0;
if (!is_int($pageDivide))
return;
$json = getMembers($page, $pageDivide, $order, $orderBy);
$json['page'] = $page;
$encoded = json_encode($json);
die($encoded);
}
if ($_GET['r'] == 'reqScorePage') {
$mapID = $_GET['mapid'] + 0;
$page = $_GET['reqPage'] + 0;
if (!is_int($mapID) OR !is_int($page))
return;
//Include the notification text
$json = getScores($mapID, $page, 10);
$json['mapid'] = $mapID;
$json['page'] = $page;
$note = false;
if ($_SESSION['accepted'] == 1) {
$userID = $_SESSION['userID'];
$note = getNotified($userID);
} else {
if ($_SESSION['preCompletedTutorial'] == true && $_SESSION['preCompletedTutorialNotified'] == false) {
$_SESSION['preCompletedTutorialNotified'] = true;
$note = "Tutorial Completed!";
$note .= "
You've unlocked: Blue Wall Color!";
$note .= "";
$note .= '
Sign in to save your progress!
';
$note .= "";
}
}
if ($note !== false)
$json['notificationtext'] = $note;
$encoded = json_encode($json);
die($encoded);
}
if ($_GET['r'] == 'getsol') {
$mapID = $_GET['mapID'] + 0;
//echo "working...";
if (!is_int($mapID)) return;
$userID = $_SESSION['userID'];
$json['solution'] = '';
$json['moves'] = 0;
if ($accepted) {
$sol = getSolution($userID, $mapID);
$json['solution'] = $sol['solution'];
$json['moves'] = $sol['moves'];
}
if (isset($_SESSION[$mapID.'sol']) && $_SESSION[$mapID.'moves'] > $json['moves']) {
$json['solution'] = $_SESSION[$mapID.'sol'];
$json['moves'] = $_SESSION[$mapID.'moves'];
}
$json['mapid'] = $mapID;
$encoded = json_encode($json);
die($encoded);
}
if ($_GET['r'] == 'getChallengeSolution') {
$mapID = $_GET['mapID'] + 0;
$challengeID = $_GET['challengeID'] + 0;
//echo "working...";
if (!is_int($mapID) OR !is_int($challengeID)) return;
if (!$accepted) return;
$userID = $_SESSION['userID'];
$json = getChallengeSolution($userID, $challengeID);
$json['mapid'] = $mapID;
$encoded = json_encode($json);
die($encoded);
}
// ------------ MAIN; getpath.
if ($_GET['r'] == 'getpath') {
//Join the partial-solution from the map, and the solution sent.
//Could be used to validate maps in for challenges where
// getting the map code from the database is not an option
//$tmp = GenerateMapByCode($_GET['mapcode']);
//$tmp = seperateMapSolution($tmp);
//$solution = formSolution($_GET['solution'].$tmp);
// Enables the ability to discover an exact duplicate solution.
$solution = formSolution($_GET['solution']);
$userID = $_SESSION['userID'];
//valid mapID?
$mapID = $_GET[mapid] + 0;
if (!is_int($mapID)) return;
//$firephp->log($mapID, "mapID");
//the first 10 ID's reserved for challenges & tutorial.
if ($mapID > 10)
$mapcode = getMapCode($mapID);
else
$mapcode = $_GET['mapcode'];
//mygrid will be the map, with the solution applied.
$map = GenerateMapByCode($mapcode);
$mygrid = MergeMapSolution($map, $solution);
//Check both starting point groups for paths
$json = routeMultiPath($mygrid);
$moves = $json['totalMoves'];
$json['mapid'] = $mapID;
//What could go wrong?
if ($json['blocked']) {
//$json['error'][] = "blocked";
$encoded = json_encode($json);
die($encoded);
}
if ($mygrid == -1) {
$json['error'][] = "INVALID WALL POSITIONS";
$encoded = json_encode($json);
die($encoded);
}
if ($mygrid[0][4] < 0) {
$used = $mygrid[0][4] - $map[0][4];
$json['error'][] = "Too many walls used. ".$used." of ".$map[0][4]." walls used.";
$encoded = json_encode($json);
die($encoded);
}
if ($_GET['isChallenge'] == 'true' AND isChallengeMap($mapID)) {
// $firephp->log('Calling challenges');
$json['info'][] = 'Executed as a challenge';
$json['completedChallenges'] = getCompletedChallenges($userID, $mapID, $solution, $moves, $json['path']);
// $firephp->log('Challenges called!');
die(json_encode($json));
}
//Get current score data. - to see if a pertinent score was beat.
$sql = "SELECT `moves` as bestmoves, `displayName`, IFNULL(q1.mymoves, 0)
FROM `solutions`, `users`
LEFT JOIN (
SELECT `moves` as mymoves
FROM `solutions`
WHERE `userID` = '$userID' AND
`mapID` = '$mapID'
) as q1
ON 1
WHERE
`mapID` = '$mapID' AND
`userID` = users.ID
ORDER BY `moves` DESC, `dateModified` ASC, solutions.ID DESC
LIMIT 1";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
list($bestMoves, $byName, $myMoves) = mysql_fetch_row($result);
$json['best'] = $bestMoves;
$json['bestby'] = $byName;
$json['mybest'] = $myMoves;
} else {
$json['best'] = 0;
$json['bestby'] = 'no one';
}
// --------- ------------------------------ ---------
// --------- RUSH THE PATH BACK TO THE USER ---------
// --------- ------------------------------ ---------
if (!$debug) {
ignore_user_abort(true);
$encoded = json_encode($json);
header("Connection: close");
header("Content-Length: " . mb_strlen($encoded));
echo $encoded;
flush();
//The connection is now closed, no further communication to the client can be done!
}
// --------- CONTINUE EXECUTION ---------
//TODO: !! Close the session with session_write_close(); as soon as possible.
// This allows the session file to be opened by other requests.
// Now we don't have to worry about how long we're taking.
// All of this code will execute without the user waiting on the server.
//$json['error'][] = "bestMoves, byName, myMoves: $bestMoves, $byName, $myMoves;";
//TODO: Commented this out - temporarly.
// $firephp->log('Got this far');
// $firephp->log($_SESSION['accepted'], '$_SESSION["accepted"]');
////Challenge/Tutorial?
////TODO: Clean this up a bit
// if ($_GET['isChallenge'] == 'true')
// {
// return;
//} //x
//TODO: I changed this; note these changes before re-applying the above.
//Challenge/Tutorial?
if ($mapID <= 10 AND $_GET['isChallenge'] = 'true') {
$json['error'][] = 'executed as a challenge';
//This will allow me to give insentive to logging in.
// So that they don't have to do the tutorial twice.
if ($mapID == 5 && $moves == 75 && $accepted == false) {
$_SESSION['preCompletedTutorial'] = true;
$_SESSION['preCompletedTutorialNotified'] = false;
}
if ($accepted == false) die(json_encode($json));
//TODO: Where's $paths coming from?
getCompletedChallenges($userID, $mapID, $solution, $moves, $paths);
die(json_encode($json));
}
// --------- USER NOT LOGGED IN?
if ($_SESSION['accepted'] !== 1) {
if ($moves >= ($_SESSION[$mapID.'moves'] + 0)) {
$_SESSION[$mapID.'moves'] = $moves;
$_SESSION[$mapID.'sol'] = $solution;
}
return;
}
// ---------- CLOSING THE SESSION $_SESSION WILL NO LONGER WORK FOR WRITING
session_write_close();
//Now that is done, we can easily do whatever we want - Like, send any emails that there might be.
include_once('./includes/emails.php');
SendQueuedEmail();
$json['error'][] = 'user is logged in';
// --------- USER LOGGED IN
//Is the map still valid to score on?
if (!isCurrentMap($mapID))
return;
// --------- UPDATE SCORES
$json['error'][] = 'map is current';
$checkcp = false;
$checkcm = false;
//Is there an existing record?
if ($myMoves > 0) {
if ($myMoves < $moves) {
$sql = "UPDATE `solutions`
SET `moves` = '$moves' ,
`dateModified` = NOW() ,
`solution` = '$solution'
WHERE `userID` = '$userID' AND
`mapID` = '$mapID'";
mysql_query($sql);
$checkcp = true;
$json['error'][] = 'Score was improved';
//Update the solution only, if it's the same score.
} elseif ($myMoves == $moves) {
$sql = "UPDATE `solutions`
SET `moves` = '$moves' ,
`solution` = '$solution'
WHERE `userID` = '$userID' AND
`mapID` = '$mapID'";
mysql_query($sql);
$json['error'][] = 'Score unchanged, but solution updated';
}
//Create a new record.
} else {
$sql = "INSERT INTO `solutions` (`userID`, `mapID`, `solution`, `moves`)
VALUES ('$userID', '$mapID', '$solution', '$moves')";
mysql_query($sql);
$checkcm = true;
$checkcp = true;
$json['error'][] = 'A new record was created';
}
$json['error'][] = "Involved data: ('$userID', '$mapID', '$solution', '$moves')";
// --------- APPLY ACHIEVEMENTS
if ($checkcp)
applyAchievements($userID, 1);
if ($checkcm) {
applyAchievements($userID, 2);
applyAchievements($userID, 3);
applyAchievements($userID, 4);
}
// --------- END
}
$json['error'][] = 'Rush-send failed';
//$encoded = json_encode($json);
die(json_encode($json));
function isCurrentMap($mapID) {
include_once('./includes/sqlEmbedded.php');
$sql = "SELECT mapID
FROM `mapOfTheDay`
WHERE `mapID` = '$mapID'
AND mapExpireTime > NOW()
LIMIT 1
";
$result = mysql_query($sql) or die(mysql_error());
return (mysql_num_rows($result) != 0);
}
//Very simple, confirm that all targets are reachable.
function ValidateMap($mygrid) {
$start = "0,1.";
$target[] = 'a';
$target[] = 'b';
$target[] = 'c';
$target[] = 'f';
$blocked = false;
foreach($target as $t) {
$p = Findpath ($mygrid, $start, $t);
if ($p['blocked']) return false;
}
return true;
}
?>