diff options
-rw-r--r-- | css/challenge.css | 12 | ||||
-rw-r--r-- | do.php | 5 | ||||
-rw-r--r-- | includes/datas.php | 201 | ||||
-rw-r--r-- | includes/maps.php | 2 | ||||
-rw-r--r-- | js/mapspecs.js | 7 | ||||
-rw-r--r-- | pages/challenge.php | 34 | ||||
-rw-r--r-- | pages/tutorial.php | 4 |
7 files changed, 208 insertions, 57 deletions
diff --git a/css/challenge.css b/css/challenge.css new file mode 100644 index 0000000..f12cec4 --- /dev/null +++ b/css/challenge.css @@ -0,0 +1,12 @@ +#challenge_wrapper #challenges { } +#challenge_wrapper #challenges_title { + font-size: 1.3em; + font-weight: bold; +} +#challenge_wrapper #challenges_listing { } +#challenge_wrapper #challenges_listing .challenge_complete { + text-decoration: line-through; +} +#challenge_wrapper #challenges_listing .challenge_incomplete { + font-weight: bold; +}
\ No newline at end of file @@ -215,9 +215,8 @@ if ($_GET['r'] == 'getpath') { // --------- USER LOGGED IN
//Challenge/Tutorial?
- if ($mapID <= 10 AND $_GET['isChallenge'] = 'true') {
- //echo "running function".$_GET['challengeID'];
- applyChallengeAchievements($userID, $_GET['challengeID'], $mapID, $solution, $moves);
+ if ($_GET['isChallenge'] == 'true') {
+ checkForCompletedChallenges($userID, $mapID, $solution, $moves, $json['path']);
return;
}
diff --git a/includes/datas.php b/includes/datas.php index 170f74e..676e68b 100644 --- a/includes/datas.php +++ b/includes/datas.php @@ -289,41 +289,173 @@ function applyCareerMazesAchievements($userID) { return true; } -//Challenges including the Tutorial. -function applyChallengeAchievements($userID, $challengeID, $mapID, $solution, $moves) { - if ($challengeID == '1') { - //echo 'challengeID'.$mapID; - //echo 'moves'.$moves; - if ($moves == 75 AND $mapID == 5) { - //echo 'true story'; - // -------- Completed the tutorial! - - //have they already completed he tutorial? - $sql = "SELECT `level` - FROM `achievements` - WHERE `userID` = '$userID' - AND `type` = 32 - ORDER BY `level` DESC - LIMIT 1"; - $result = mysql_query($sql); - if (mysql_num_rows($result) == 1) +/** + * Called when a user completes a challenge + * @param type $userID User who did the challenge + * @param type $mapID Map to check for challenges + * @param type $solution The final placement of towers, as a string of .-separated x,y pairs. + * Begins and ends with "." as well + * @param type $moves The total number of moves in the solution, for all paths + * @param type $paths An array of path-objects, each as returned by routePath(). + */ +function checkForCompletedChallenges($userID, $mapID, $solution, $moves, $paths) { + //mapID 5 is the tutorial, and is treated specially + if($mapID == 5) { + if ($moves == 75) { + onCompletedTutorial($userID); + } + return; + } + + $challengeResultset = loadChallengesForMap($mapID, $userID); + while($challenge = mysql_fetch_array($challengeResultset)) + { + //Skip checking challenges which have already been completed + if($challenge["dateCompleted"] !== NULL) + continue; + else if(hasChallengeBeenCompleted($challenge, $solution, $moves, $paths)) + setChallengeCompleted($challenge, $solution, $userID, $moves); + } +} + +/** + * Called when a user completes the tutorial + */ +function onCompletedTutorial($userID) { + //have they previously completed the tutorial? + if(hasCompletedTutorial($userID)) + return false; + + // -------- This is the first time they've completed the tutorial! + $sql = "INSERT INTO `achievements` + (`userID`, `type`, `level`) + VALUES ('$userID', 32, '1')"; + mysql_query($sql); + $aID = mysql_insert_id(); + + // ------- User gets unlock. (unlocks plural is possible here) + $sql = "INSERT INTO `unlocks` + (`userID`, `achievementID`, `type`, `subtype`, `name`, `value`) + VALUES ('$userID', '$aID', '1', 0, 'Blue', '#4444ff')"; + mysql_query($sql); + + return true; +} + +/** + * Checks if the given solution meets the requirements for the given challenge + */ +function hasChallengeBeenCompleted($challenge, $solution, $moves, $paths) { + //Check the maze-length + if($challenge['goal'] != 0) { + //Er, "greater than" should really be called "greater than or equal to" :\ + if(($challenge['inequality'] == "greater than" && $moves < $challenge['goal']) + || ($challenge['inequality'] == "less than" && $moves >= $challenge['goal']) + || ($challenge['inequality'] == "equal" && $moves != $challenge['goal'])) + { + return false; + } + } + + //Check wall-count restriction + if($challenge['restrictWallCount'] !== NULL) + { + //The number of walls is equal to the number of "," characters in the $solution string + $temp = count_chars($solution, 1); //$temp needed due to extreme PHP-stupidity + $numWalls = $temp[ord(",")]; + if($numWalls > $challenge['restrictWallCount']) { + return false; + } + } + + //Check wall-placement restriction + if($challenge['restrictWallPlacement'] !== NULL) + { + $invalidWalls = explode(".", $challenge['restrictWallPlacement']); + foreach($invalidWalls as $invalidWall) + { + if(strpos($solution,$invalidWall) !== false) + { return false; - - // -------- This is the first time they've completed the tutorial! - $sql = "INSERT INTO `achievements` - (`userID`, `type`, `level`) - VALUES ('$userID', 32, '1')"; - mysql_query($sql); - $aID = mysql_insert_id(); - - // ------- User gets unlock. (unlocks plural is possible here) - $sql = "INSERT INTO `unlocks` - (`userID`, `achievementID`, `type`, `subtype`, `name`, `value`) - VALUES ('$userID', '$aID', '1', 0, 'Blue', '#4444ff')"; - mysql_query($sql); - return true; + } + } + } + + //TODO: This portion probably needs to be rewritten due to the weird way paths are saved + if($challenge['restrictTeleportCount'] !== NULL || $challenge['restrictTeleportsUsed'] !== NULL) + { + //Check teleport count + $usedTeleports = explode(".", getUsedTeleports($paths)); + if($challenge['restrictTeleportCount'] !== NULL) + { + if(count($usedTeleports) !== $challenge['restrictTeleportCount'] && + ($challenge['restrictTeleportCount'] != 0 || $usedTeleports[0] != "")) + { + return false; + } + } + + //Check teleports used + if($challenge['restrictTeleportsUsed'] !== NULL) + { + foreach($usedTeleports as $usedTeleport) + { + foreach($paths as $path) + { + //Check each path to see if it walks over that teleport + if(strpos($path['path'], $usedTeleport) !== false) + { + return false; + } + } + } } } + + //Check start points + if($challenge['restrictStartPoint'] !== NULL) + { + $usesCorrectStartPoint = false; + foreach($paths as $path) + { + if($path['start'] == $challenge['restrictStartPoint']) + $usesCorrectStartPoint = true; + } + + if(!$usesCorrectStartPoint) + return false; + } + + //Check end points + if($challenge['restrictEndPoint'] !== NULL) + { + $usesCorrectEndPoint = false; + foreach($paths as $path) + { + if($path['end'] == $challenge['restrictEndPoint']) + $usesCorrectEndPoint = true; + } + + if(!$usesCorrectEndPoint) + return false; + } + + //All of the restrictions were met, meaning the challenge was completed! + return true; +} + +/** + * Sets the given challenge as completed by the user in the database + */ +function setChallengeCompleted($challenge, $solution, $userID, $moves) { + $solution = mysql_escape_string($solution); + $userID = mysql_escape_string($userID); + $challengeID = $challenge["challengeID"]; + + $sql = "INSERT INTO `challengeSolutions` + (`userID`, `challengeID`, `solution`, `moves`, `dateSolved`) + VALUES ('$userID', '$challengeID', '$solution', $moves, NOW())"; + mysql_query($sql); } function applyAchievements($userID, $aType) { @@ -727,8 +859,9 @@ function getScores($mapid, $pageNumber = 1, $pageDivide = 10) { * Returns true if the given user has completed the tutorial, false otherwise */ function hasCompletedTutorial($userID) { - $sql = "SELECT * FROM `achievements` - WHERE `type` = 32 AND `userID` = '$userID'"; + $sql = "SELECT `level` FROM `achievements` + WHERE `type` = 32 AND `userID` = '$userID' + LIMIT 1"; $result = mysql_query($sql); return (mysql_num_rows($result) >= 1); } diff --git a/includes/maps.php b/includes/maps.php index 05559b4..2b20eac 100644 --- a/includes/maps.php +++ b/includes/maps.php @@ -965,6 +965,8 @@ function routePath($mygrid, $validate = false, $traverseBackwards = false) { //Mark where we ended up starting from.
if ($r['start'] == '')
$r['start'] = $p['start'];
+ $r['end'] = $p['end'];
+
//Could be blocked right already.
$blocked = $p['blocked'];
do {
diff --git a/js/mapspecs.js b/js/mapspecs.js index ded7bfc..30a8ec7 100644 --- a/js/mapspecs.js +++ b/js/mapspecs.js @@ -20,7 +20,6 @@ else if (document.attachEvent) { // IE 8- }
var isChallenge = false;
-var challengeID = false;
var solution = new Array();
var blocks = new Array();
@@ -188,11 +187,13 @@ function doSend(mapid) { pressedGoTime = new Date().getTime();
- reqstr = "&mapcode="+mapdata[mapid].code;
+ reqstr = "isChallenge="+isChallenge
+ reqstr += "&r=getpath"
+ reqstr += "&mapcode="+mapdata[mapid].code;
reqstr += "&mapid="+mapid;
reqstr += "&solution="+solution[mapid];
- ajax.requestFile = "do.php?challengeID="+challengeID+"&isChallenge="+isChallenge+"&r=getpath"+reqstr; //prepare strdata
+ ajax.requestFile = "do.php?"+reqstr; //prepare strdata
ajax.onCompletion = request_path_done; // specify function to be executed on response
ajax.runAJAX();
}
diff --git a/pages/challenge.php b/pages/challenge.php index 1fa420a..6990644 100644 --- a/pages/challenge.php +++ b/pages/challenge.php @@ -1,7 +1,7 @@ <?php
ob_start("ob_gzhandler");
htmlHeader(
- array('tutorial'), 'Pathery Challenges',
+ array('tutorial', 'challenge'), 'Pathery Challenges',
'Challenges', array('scores', 'dateformat')
);
@@ -50,6 +50,7 @@ if (getCookie('pref_mute') != 'true') { <body>
<script type="text/javascript">
+var isChallenge = true;
playerWallColor = '<?PHP echo isset($wallColor) ? $wallColor : ''; ?>';
playerWallEmblem = '<?PHP echo isset($wallEmblem) ? $wallEmblem : ''; ?>';
</script>
@@ -58,7 +59,7 @@ playerWallEmblem = '<?PHP echo isset($wallEmblem) ? $wallEmblem : ''; ?>'; topbar($Links);
?>
-<div class="wrapper">
+<div id="challenge_wrapper" class="wrapper">
<?
@@ -92,18 +93,18 @@ $mapContent = displayMap(GenerateMapByCode($mapCode), 1); $challengeResultset = loadChallengesForMap($_GET["mapID"], $userID);
if($challengeResultset === NULL)
{
-
//TODO: Redirect to challenge listing also
return;
}
-displayChallenges($challengeResultset);
-
?>
<noscript>Sorry, this game requires scripts to run. Please enable javascript and <a href='home'>Reload this site</a>
<br />This game is best viewed in <a href='http://www.google.com/chrome'>Google Chrome</a>
</noscript>
- <? echo $mapContent; ?>
+ <?
+ echo $mapContent;
+ displayChallenges($challengeResultset);
+ ?>
</div>
<div id="copy" style='width:100%;clear: both'>
@@ -119,18 +120,25 @@ htmlFooter(); ?>
<?php
+/**
+ * Outputs the given list of challenges on the page
+ */
function displayChallenges($challengeResultset)
{
- echo "<div>";
+ echo '<div id="challenges">';
+ echo '<div id="challenges_title">Challenges</div>';
+ echo '<div id="challenges_listing"><ol>';
while($challenge = mysql_fetch_array($challengeResultset))
{
+ echo "<li>";
if($challenge["dateCompleted"] !== NULL)
- echo "<strike>" . getChallengeDisplayString($challenge) . "</strike>";
+ $cssClass = "challenge_complete";
else
- echo getChallengeDisplayString($challenge);
- echo "<br />";
+ $cssClass = "challenge_incomplete";
+ echo "<span class='$cssClass'>" . getChallengeDisplayString($challenge) . "</span>";
+ echo "</li>";
}
- echo "</div>";
+ echo "</ol></div></div>";
}
/**
@@ -140,7 +148,7 @@ function displayChallenges($challengeResultset) function getChallengeDisplayString($challenge)
{
$returnMe = "";
- if($challenge['goal'] == 0)
+ if($challenge["goal"] == 0)
$returnMe .= "Complete the maze";
else if ($challenge['inequality'] == "greater than")
$returnMe .= "Get " . $challenge['goal'];
@@ -220,4 +228,4 @@ function getChallengeDisplayString($challenge) $returnMe .= ".";
return $returnMe;
}
-?>
+?>
\ No newline at end of file diff --git a/pages/tutorial.php b/pages/tutorial.php index d09480c..f6517c4 100644 --- a/pages/tutorial.php +++ b/pages/tutorial.php @@ -36,19 +36,15 @@ playerWallEmblem = '<?PHP echo isset($wallEmblem) ? $wallEmblem : ''; ?>'; <script>
isChallenge = true;
-challengeID = 1;
-
function challengeLoad() {}
-
challenge1 = new Object();
challenge2 = new Object();
challenge3 = new Object();
challenge4 = new Object();
challenge5 = new Object();
-
challenge1.start = "The objective in this game is to create the longest <i>path</i> between the start and the finish.<br>Below is a demonstration.<br>Press <b>Go!</b>";
challenge1.complete = "Now that you see what is going on, let's move on to some puzzles.<br/>Press <b>Next</b>";
|