* @version 0.0.0 * @copyright Copyright (c) 2015 - 2023 City Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ abstract class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria { // Load traits use CryptoTrait; use DatabaseFrontendTrait; use StateableTrait; /** * City types */ const CITY_TYPE_DEFAULT = 'default'; const CITY_TYPE_TESTING = 'testing'; /** * Whether this City is active (default: FALSE) */ private $isActive = FALSE; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct (string $className) { // Call parent constructor parent::__construct($className); // Get a frontend instance $frontendInstance = DatabaseFrontendFactory::createFrontendByConfiguredName('city_info_db_frontend_class'); // Set it here $this->setFrontendInstance($frontendInstance); // Get a crypto instance $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class'); // Set it here $this->setCryptoInstance($cryptoInstance); // Add own instance to registry ObjectRegistry::getRegistry('factory')->addInstance('city', $this); // Init state which sets the state to 'init' $this->initState(); } /** * Initializes the City's state which sets it to 'init' * * @return void */ private function initState() { // Get the state factory and create the initial state. CityStateFactory::createCityStateInstanceByName('init'); } /** * Outputs the console teaser. This should only be executed on startup or * full restarts. This method generates some space around the teaser. * * @return void */ public function outputConsoleTeaser () { // Get the app instance (for shortening our code) $applicationInstance = ApplicationHelper::getSelfInstance(); // Output all lines self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(' '); self::createDebugInstance(__CLASS__, __LINE__)->debugMessage($applicationInstance->getAppName() . ' v' . $applicationInstance->getAppVersion() . ' - ' . FrameworkBootstrap::getRequestInstance()->getRequestElement('mode') . ' daemon starting'); self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('Copyright (c) 2015 - 2023 City Developer Team'); self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(' '); self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.'); self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('This is free software, and you are welcome to redistribute it under certain'); self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('conditions; see docs/COPYING for details.'); self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(' '); } /** * Adds City data elements to a given dataset instance * * @param $criteriaInstance An instance of a storeable criteria * @param $requestInstance An instance of a Requestable class * @return void */ public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) { // Make sure the request instance is set as it is not optional. assert($requestInstance instanceof Requestable); // Add City number and type $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID , 1); $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode')); // Add the City id $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID, $this->getCityId()); } /** * Updates a given field with new value * * @param $fieldName Field to update * @param $fieldValue New value to store * @return void * @throws DatabaseUpdateSupportException If this class does not support database updates * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem */ public function updateDatabaseField ($fieldName, $fieldValue) { // Unfinished DebugMiddleware::getSelfInstance()->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue); return; // Get a critieria instance $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Add search criteria $searchInstance->addCriteria(UserDatabaseFrontend::DB_COLUMN_USERNAME, $this->getUserName()); $searchInstance->setLimit(1); // Now get another criteria $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class'); // Add criteria entry which we shall update $updateInstance->addCriteria($fieldName, $fieldValue); // Add the search criteria for searching for the right entry $updateInstance->setSearchInstance($searchInstance); // Set frontend class name $updateInstance->setFrontendConfigEntry('user_db_frontend_class'); // Remember the update in database result $this->getResultInstance()->add2UpdateQueue($updateInstance); } /** * Activates the City by doing some final preparation and setting * $CityIsActive to TRUE. * * @param $requestInstance A Requestable class * @param $responseInstance A Responseable class * @return void */ public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) { // Get the controller here $controllerInstance = ObjectRegistry::getRegistry('generic')->getInstance('controller'); // Run all filters for the City activation $controllerInstance->executeActivationFilters($requestInstance, $responseInstance); // Make sure the city's state is 'init' $this->getStateInstance()->validateCityStateIsInit(); // ----------------------- Last step from here ------------------------ // Activate the city daemon. This is ALWAYS the last step in this method /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...'); $this->getStateInstance()->citySimulationIsActivated(); // ---------------------- Last step until here ------------------------ } /** * Getter for isActive attribute * * @return $isActive Whether the City is active */ public final function isCityActive () { return $this->isActive; } /** * Enables (default) or disables isActive flag * * @param $isActive Whether the City is active * @return void */ public final function enableIsActive (bool $isActive = TRUE) { $this->isActive = $isActive; } /** * Updates/refreshes City data (e.g. status). * * @return void * @todo Find more to do here */ public function updateCityData () { // Set some dummy configuration entries, e.g. city_status FrameworkBootstrap::getConfigurationInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName()); } /** * Adds all required elements from given array into data set instance * * @param $dataSetInstance An instance of a StoreableCriteria class * @param $CityData An array with valid City data * @return void */ public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) { // Add all data the array provides foreach (CityInformationDatabaseFrontend::getAllElements() as $element) { // Is the element there? if (isset($cityData[$element])) { // Add it $dataSetInstance->addCriteria($element, $cityData[$element]); } else { // Output warning message self::createDebugInstance(__CLASS__, __LINE__)->warningMessage(sprintf('BASE-CITY-DAEMON: Element %s not found in CityData array.', $element)); } } } /** * Initializes the city daemon * * @return void * @todo 0% done */ public function bootstrapInitCityDaemon () { DebugMiddleware::getSelfInstance()->partialStub('Please add something here.'); } /** * Checks whether at least one map requires expansion * * @return $requiresExpansion Whether a map requires expansion */ public function isMapPendingExpansion () { // @TODO Is the game paused by user? // Get sections manager $sectionsInstance = ManagerFactory::createManagerByType('city_sections'); // Call it's method and return value return $sectionsInstance->isSectionPendingExpansion(); } /** * Expands any found map that requires expansion * * @return void */ public function expandMaps () { // Get sections manager $sectionsInstance = ManagerFactory::createManagerByType('city_sections'); // Call it's method and return value $sectionsInstance->expandMaps(); } }