use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend;
use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
use Org\Mxchange\CoreFramework\Registry\Registerable;
+// Import SPL stuff
+use \InvalidArgumentException;
+
/**
* A database frontend for city sections. Sections are the smalles entity of a
* city. They can be connected with each other and form a lot. Therefore only
*
* @author Roland Haeder <webmaster@shipsimu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2015, 2016 City Developer Team
+ * @copyright Copyright (c) 2015 - 2023 City Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.shipsimu.org
*
*
* @param $cityId City id to check
* @return $isFound Whether the city id is found
+ * @throws InvalidArgumentException If a parameter as an invalid value
*/
public function ifCityHasSections (int $cityId) {
+ // Check parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: cityId=%d - CALLED!', $cityId));
+ if ($cityId < 1) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('cityId=%d is not a valid id', $cityId), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+ }
+
// Get search instance
$searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
$resultInstance = $this->doSelectByCriteria($searchInstance);
// Check if there is one entry
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: resultInstance=%s', $resultInstance->__toString()));
$isFound = $resultInstance->next();
// Return result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: isFound=%d - EXIT!', intval($isFound)));
return $isFound;
}
*
* @param $cityId City id to check
* @return $ids Sections ids from initial expansion
+ * @throws InvalidArgumentException If a parameter as an invalid value
+ * @throws BadMethodCallException If the city has already sections but this method was invoked
* @todo Add handling of water types to make a more cooler map
*/
public function doInitialCityExpansion (int $cityId) {
- // Make sure this city has no sections
- assert(!$this->ifCityHasSections($cityId));
+ // Check parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: cityId=%d - CALLED!', $cityId));
+ if ($cityId < 1) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('cityId=%d is not a valid id', $cityId), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+ } elseif ($this->ifCityHasSections($cityId)) {
+ // Already has sections
+ throw new BadMethodCallException(sprintf('cityId=%d has already sections', $cityId), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL);
+ }
/*
* "Cache" max "radius" for initial city expansion. It is not a real
// Calculate total sections
$totalSections = (($radius + 1) * ($radius + 1) * $maxUp * ($maxDown + 1));
- // Debug message:
- /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: radius=' . $radius . ',maxUp=' . $maxUp . ',maxDown=' . $maxDown . ',totalSections=' . $totalSections);
-
// Get data set instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: radius=%d,maxUp=%d,maxDown=%d,totalSections=%d', $radius, $maxUp, $maxDown, $totalSections));
$dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_SECTIONS));
// Add values for "zero point"
$dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_RESERVED , self::IS_NOT_RESERVED);
// Set primary key to 'city_id'/'section_id'
- $dataSetInstance->setPrimaryKeyCombined(array(self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID));
+ $dataSetInstance->setPrimaryKeyCombined([self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID]);
// Entry id for counting each entry
$dataSetInstance->setUniqueKey(self::DB_COLUMN_ENTRY_ID);
$this->queryInsertDataSet($dataSetInstance);
// Set section id to 2 as 1 is already initialized + init array
- $sections = array();
+ $sections = [];
// Output message to ask for user's patience ...
- self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Writing ' . $totalSections . ' sections for city ' . $cityId . ' ... (this may takes some time)');
+ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: Writing %d sections for city %d ... (this may takes some time)', $totalSections, $cityId));
// Expand half of it to north/south (north=positive, south=negative)
for ($north = 1; $north < round($radius / 2); $north++) {
for ($west = 1; $west < round($radius / 2); $west++) {
// Expand up/down (including "zero point")
for ($z = $maxDown; $z < ($maxUp + 1); $z++) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
-
// Fill array up with south/east/down ids
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
$sections[($north * -1)][($west * -1)][$z]['type'] = self::SECTION_TYPE_EMPTY_LAND;
$sections[($north * -1)][($west * -1)][$z]['sub'] = self::SECTION_SUB_TYPE_GRASS;
// Fill up array with north/west/up ids (only air)
$sections[$north][$west][$z]['type'] = self::SECTION_TYPE_AIR;
$sections[$north][$west][$z]['sub'] = self::SECTION_SUB_TYPE_AIR;
- } // END - for
- } // END - for
- } // END - for
+ }
+ }
+ }
// Init section id with 2 as 1 is the "zero point"
$sectionId = 2;
foreach ($sectionX as $y => $sectionY) {
// Loop through up/down values
foreach ($sectionY as $z => $sectionData) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
-
// Set all coordinates for positive directions
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
$dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_ID , $sectionId);
$dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_X, $x);
$dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_Y, $y);
// Count id up
$sectionId++;
- } // END - foreach
+ }
// Debug message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ' has been written.');
- } // END - foreach
+ }
// Debug message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ' has been written.');
- } // END - foreach
+ }
// Thank you for waiting! :-)
self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: A total of ' . $totalSections . ' sections has been written for city id ' . $cityId . '.');