* @version 0.0.0 * @copyright Copyright (c) 2015, 2016 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 . */ class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria { /** * 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 ($className) { // Call parent constructor parent::__construct($className); // Get a wrapper instance $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class'); // Set it here $this->setWrapperInstance($wrapperInstance); // Get a crypto instance $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class'); // Set it here $this->setCryptoInstance($cryptoInstance); // Add own instance to registry Registry::getRegistry()->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) $app = $this->getApplicationInstance(); // Output all lines self::createDebugInstance(__CLASS__)->debugOutput(' '); self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' daemon starting'); self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015, 2016 City Developer Team'); self::createDebugInstance(__CLASS__)->debugOutput(' '); self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.'); self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain'); self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.'); self::createDebugInstance(__CLASS__)->debugOutput(' '); } /** * 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(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1); $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode')); // Add the City id $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::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 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue); return; // Get a critieria instance $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Add search criteria $searchInstance->addCriteria(UserDatabaseWrapper::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 wrapper class name $updateInstance->setWrapperConfigEntry('user_db_wrapper_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 = Registry::getRegistry()->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__)->debugOutput('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 ($isActive = TRUE) { $this->isActive = (bool) $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 $this->getConfigInstance()->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 (CityInformationDatabaseWrapper::getAllElements() as $element) { // Is the element there? if (isset($cityData[$element])) { // Add it $dataSetInstance->addCriteria($element, $cityData[$element]); } else { // Output warning message /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.'); } } // END - foreac } /** * Initializes the city daemon * * @return void * @todo 0% done */ public function bootstrapInitCityDaemon () { $this->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->isMapPendingExpansion(); } /** * 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(); } }