$sql = "SELECT TIMESTAMPDIFF(MINUTE, '{$prev_timestamp}', '{$curr_timestamp}')"; $stmt = $conn->prepare($sql); $stmt->execute(); $record = $stmt->fetch(); $minutes_elapsed = $record[0]; // determine number of blocks grown $prevGrowth = $cityInfo->sectorBlocks[$prevSector] + $minutes_elapsed; // cap value if it is larger than number of blocks $prevGrowth = min($prevGrowth, $nBlocks - $nUsedBlocks); // update blocks value $sql = "UPDATE CityBlocks SET nBlocks={$prevGrowth} WHERE cityID={$cityID} AND sector='{$prevSector}'"; $conn->exec($sql); } // update stuff $cityInfo = CityData::getCityInfo($cityName, $username); $nUsedBlocks = CityData::getUsedBlocksCount($cityInfo); // update current with growth if ($currentSector != SECTOR_NONE && $nBlocks > $nUsedBlocks) { // determine growth level $nBlocks = min($cityInfo->sectorBlocks[$currentSector] + $growth, $nBlocks - $nUsedBlocks); // update blocks value $sql = "UPDATE CityBlocks SET nBlocks={$nBlocks} WHERE cityID={$cityID} AND sector='{$currentSector}'"; $conn->exec($sql); } // make current sector name into mysql form if ($currentSector == SECTOR_NONE) { $currentSector = "NULL"; } else { $currentSector = "'{$currentSector}'"; } // update sector
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}"; }
var sectorBlocks = [<?php if ($currCityInfo != null && $currCityInfo->sectorBlocks != null) { echo sprintf("%d, %d, %d, %d", $currCityInfo->sectorBlocks[SECTOR_RESIDENTIAL], $currCityInfo->sectorBlocks[SECTOR_EDUCATIONAL], $currCityInfo->sectorBlocks[SECTOR_RECREATIONAL], $currCityInfo->sectorBlocks[SECTOR_BUSINESS]); } ?> ]; // currently selected city var selectedCity = <?php echo $currCity == null ? "\"\"" : $currCity; ?> ; // get description var description = <?php echo $currCityInfo ? "\"" . CityData::getDescription($currCityInfo) . "\"" : "\"\""; ?> ; // description updates largest sector value in session <?php // figure out largest sector $largestSector = SECTOR_NONE; if (array_key_exists("CityBuilder_largestSector", $_SESSION)) { $largestSector = $_SESSION["CityBuilder_largestSector"]; } ?> // currently selected sector var selectedSectorName = <?php echo $currCityInfo && $currCityInfo->currSector ? "\"{$currCityInfo->currSector}\"" : sprintf("\"%s\"", SECTOR_NONE);
?> <title>Create City in City Builder</title> </head> <body> <?php include "../scripts/CityData.php"; $username = $_SESSION["citybuilder_username"]; if (!$_SESSION["citybuilder_bLoggedIn"] || $username == null) { echo "ERROR: not logged in<br />"; } // form validation if ($_SERVER["REQUEST_METHOD"] == "POST") { // get form info $cityname = CityBuilder::validateInput($_POST["cityname"], false); // create city $message = CityData::addCity($cityname, $username, 2000, 0); if ($message != null) { echo "ERROR: {$message}<br />"; } else { echo "Successfully created '{$cityname}'<br />"; } } define("CURRENT_PAGE", "../pages/newcity.php"); include "../scripts/header.php"; ?> <article> <header>Create City</header> <content> Let's make a city! <form action = "newcity.php" method = "POST">