3 namespace Org\Mxchange\City\Daemon;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Request\Requestable;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
10 * A general City class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2015, 2016 City Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
35 const CITY_TYPE_DEFAULT = 'default';
36 const CITY_TYPE_TESTING = 'testing';
39 * Whether this City is active (default: FALSE)
41 private $isActive = FALSE;
44 * Protected constructor
46 * @param $className Name of the class
49 protected function __construct ($className) {
50 // Call parent constructor
51 parent::__construct($className);
53 // Get a wrapper instance
54 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
57 $this->setWrapperInstance($wrapperInstance);
59 // Get a crypto instance
60 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
63 $this->setCryptoInstance($cryptoInstance);
65 // Add own instance to registry
66 Registry::getRegistry()->addInstance('city', $this);
68 // Init state which sets the state to 'init'
73 * Initializes the City's state which sets it to 'init'
77 private function initState() {
78 // Get the state factory and create the initial state.
79 CityStateFactory::createCityStateInstanceByName('init');
83 * Outputs the console teaser. This should only be executed on startup or
84 * full restarts. This method generates some space around the teaser.
88 public function outputConsoleTeaser () {
89 // Get the app instance (for shortening our code)
90 $app = $this->getApplicationInstance();
93 self::createDebugInstance(__CLASS__)->debugOutput(' ');
94 self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' daemon starting');
95 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015, 2016 City Developer Team');
96 self::createDebugInstance(__CLASS__)->debugOutput(' ');
97 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
98 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
99 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
100 self::createDebugInstance(__CLASS__)->debugOutput(' ');
104 * Adds City data elements to a given dataset instance
106 * @param $criteriaInstance An instance of a storeable criteria
107 * @param $requestInstance An instance of a Requestable class
110 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
111 // Make sure the request instance is set as it is not optional.
112 assert($requestInstance instanceof Requestable);
114 // Add City number and type
115 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
116 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
119 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
123 * Updates a given field with new value
125 * @param $fieldName Field to update
126 * @param $fieldValue New value to store
128 * @throws DatabaseUpdateSupportException If this class does not support database updates
129 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
131 public function updateDatabaseField ($fieldName, $fieldValue) {
133 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
136 // Get a critieria instance
137 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
139 // Add search criteria
140 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
141 $searchInstance->setLimit(1);
143 // Now get another criteria
144 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
146 // Add criteria entry which we shall update
147 $updateInstance->addCriteria($fieldName, $fieldValue);
149 // Add the search criteria for searching for the right entry
150 $updateInstance->setSearchInstance($searchInstance);
152 // Set wrapper class name
153 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
155 // Remember the update in database result
156 $this->getResultInstance()->add2UpdateQueue($updateInstance);
160 * Activates the City by doing some final preparation and setting
161 * $CityIsActive to TRUE.
163 * @param $requestInstance A Requestable class
164 * @param $responseInstance A Responseable class
167 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
168 // Get the controller here
169 $controllerInstance = Registry::getRegistry()->getInstance('controller');
171 // Run all filters for the City activation
172 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
174 // Make sure the city's state is 'init'
175 $this->getStateInstance()->validateCityStateIsInit();
177 // ----------------------- Last step from here ------------------------
178 // Activate the city daemon. This is ALWAYS the last step in this method
179 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
180 $this->getStateInstance()->citySimulationIsActivated();
181 // ---------------------- Last step until here ------------------------
185 * Getter for isActive attribute
187 * @return $isActive Whether the City is active
189 public final function isCityActive () {
190 return $this->isActive;
194 * Enables (default) or disables isActive flag
196 * @param $isActive Whether the City is active
199 public final function enableIsActive ($isActive = TRUE) {
200 $this->isActive = (bool) $isActive;
204 * Updates/refreshes City data (e.g. status).
207 * @todo Find more to do here
209 public function updateCityData () {
210 // Set some dummy configuration entries, e.g. city_status
211 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
215 * Adds all required elements from given array into data set instance
217 * @param $dataSetInstance An instance of a StoreableCriteria class
218 * @param $CityData An array with valid City data
221 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
222 // Add all data the array provides
223 foreach (CityInformationDatabaseWrapper::getAllElements() as $element) {
224 // Is the element there?
225 if (isset($cityData[$element])) {
227 $dataSetInstance->addCriteria($element, $cityData[$element]);
229 // Output warning message
230 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
236 * Initializes the city daemon
241 public function bootstrapInitCityDaemon () {
242 $this->partialStub('Please add something here.');
246 * Checks whether at least one map requires expansion
248 * @return $requiresExpansion Whether a map requires expansion
250 public function isMapPendingExpansion () {
251 // @TODO Is the game paused by user?
253 // Get sections manager
254 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
256 // Call it's method and return value
257 return $sectionsInstance->isMapPendingExpansion();
261 * Expands any found map that requires expansion
265 public function expandMaps () {
266 // Get sections manager
267 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
269 // Call it's method and return value
270 $sectionsInstance->expandMaps();