function addUser($username, $password) { // make connection $conn = CityBuilder::getDatabaseConnection(); // message $message = "unknown error"; // statements $sql_check = "SELECT * FROM Users WHERE name='{$username}'"; $sql_add = "INSERT INTO Users(name, password) VALUES('{$username}', '{$password}')"; $sql = ""; // access database try { // search for username $sql = $sql_check; $stmt = $conn->prepare($sql); $stmt->execute(); // evaluate results $userExists = $stmt->rowCount() > 0; // try to add user if ($userExists) { $message = "username '{$username}' is taken"; } else { $sql = $sql_add; $conn->exec($sql); $message = null; } } catch (PDOException $e) { $message = $e->getMessage(); } // break connection $conn = null; // return success return $message; }
function validateLogin($username, $password) { // connect to database $conn = CityBuilder::getDatabaseConnection(); // search for pair $sql = "SELECT * FROM Users WHERE name='{$username}' AND password='******'"; $stmt = $conn->prepare($sql); $stmt->execute(); // inspect results $result = $stmt->rowCount() > 0; // retrieve row key if ($result) { //todo } // disconnect from database $conn = null; // return result return $result; }
// get username $username = $_SESSION["citybuilder_username"]; // update city index in session $_SESSION["CityBuilder_currCity"] = $cityIndex; // lookup city info $cityInfo = CityData::getCityInfo($cityName, $username); // don't change the current sector of this if the sector is null if ($currentSector == null) { $currentSector = $cityInfo->currSector; } // lookup cityID $cityID = $cityInfo->cityID; // record prev sector $prevSector = $cityInfo->currSector; // make database connection $conn = CityBuilder::getDatabaseConnection(); try { // get last timestamp $sql = "SELECT timestamp, created FROM Cities WHERE cityID = {$cityID}"; $stmt = $conn->prepare($sql); $stmt->execute(); $record = $stmt->fetch(); $prev_timestamp = $record["timestamp"]; // use created timestamp if prev_timestamp is strange if ($prev_timestamp == "0000-00-00 00:00:00") { $prev_timestamp = $record["created"]; } // get current timestamp $sql = "SELECT NOW()"; $stmt = $conn->prepare($sql); $stmt->execute();
function getDescription($cityInfo) { // find highest sector size $largest = CityData::getBiggestSectors($cityInfo); $largestSectorSize = $largest["size"]; // determine sector $largestSectorName = CityData::pickSector($cityInfo, $largest["sectors"]); // update session value. $_SESSION["CityBuilder_largestSector"] = $largestSectorName; // connect to database $conn = CityBuilder::getDatabaseConnection(); // description found $description = "error"; // do queries try { // query for block rank values $stmt = $conn->prepare("SELECT rankID, nBlocks FROM SectorBlockRanks"); $stmt->execute(); $records = $stmt->fetchAll(); // record block rank values $blockRanks = array(); foreach ($records as $i => $record) { // add to array $blockRanks[$record["rankID"]] = $record["nBlocks"]; } // array of already read values $alreadyRead = array(); // lookup values based on this id $currentDescriptionID = 1; // start at 1 and keep going "next" until it's null while ($currentDescriptionID != null) { // check id hasn't already been selected $stillOkay = true; foreach ($alreadyRead as $i => $id) { if ($currentDescriptionID == $id) { $stillOkay = false; break; } } // only continue if the above check worked out if (!$stillOkay) { break; } else { // add id to list $alreadyRead[count($alreadyRead)] = $currentDescriptionID; // get record $stmt = $conn->prepare("SELECT nextDescID, sector, blockRank, content FROM CityDescriptions WHERE descID={$currentDescriptionID}"); $stmt->execute(); $record = $stmt->fetch(); // don't continue if the sector doesn't match, unless this is the first run. if ($record["sector"] == $largestSectorName || $currentDescriptionID == 1) { // update description $description = $record["content"]; // get next id if ($largestSectorSize < $blockRanks[min($record["blockRank"] + 1, 4)]) { // we've hit the ceiling. break; } else { if ($currentDescriptionID == 1) { // query for first sector rank $stmt = $conn->prepare("SELECT descID FROM CityDescriptions WHERE sector='{$largestSectorName}' AND blockRank=2"); $stmt->execute(); $record = $stmt->fetch(); // set desc id to this one $currentDescriptionID = $record["descID"]; } else { // iterate to the next id $currentDescriptionID = $record["nextDescID"]; } } } else { // this is an error condition, by the way... $description = "Error: this is a really strange error."; break; } } } } catch (PDOException $e) { return $e->getLine() . ": " . $e->getMessage(); } // disconnect from database $conn = null; // return description return "{$description}"; }