コード例 #1
0
ファイル: CityData.php プロジェクト: kitwistful/CityBuilder
 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}";
 }