]> git.mxchange.org Git - city.git/blob - application/city/classes/city_daemon/class_BaseCityDaemon.php
Removed code from importing it from 'hub' project (also a daemon).
[city.git] / application / city / classes / city_daemon / class_BaseCityDaemon.php
1 <?php
2 /**
3  * A general City class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2015 City Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
25         /**
26          * City types
27          */
28         const CITY_TYPE_DEFAULT = 'default';
29         const CITY_TYPE_TESTING = 'testing';
30
31         /**
32          * Whether this City is active (default: FALSE)
33          */
34         private $isActive = FALSE;
35
36         /**
37          * Protected constructor
38          *
39          * @param       $className      Name of the class
40          * @return      void
41          */
42         protected function __construct ($className) {
43                 // Call parent constructor
44                 parent::__construct($className);
45
46                 // Get a wrapper instance
47                 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
48
49                 // Set it here
50                 $this->setWrapperInstance($wrapperInstance);
51
52                 // Get a crypto instance
53                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
54
55                 // Set it here
56                 $this->setCryptoInstance($cryptoInstance);
57
58                 // Add own instance to registry
59                 Registry::getRegistry()->addInstance('city', $this);
60
61                 // Init state which sets the state to 'init'
62                 $this->initState();
63         }
64
65         /**
66          * Initializes the City's state which sets it to 'init'
67          *
68          * @return      void
69          */
70         private function initState() {
71                 // Get the state factory and create the initial state.
72                 CityStateFactory::createCityStateInstanceByName('init');
73         }
74
75         /**
76          * Outputs the console teaser. This should only be executed on startup or
77          * full restarts. This method generates some space around the teaser.
78          *
79          * @return      void
80          */
81         public function outputConsoleTeaser () {
82                 // Get the app instance (for shortening our code)
83                 $app = $this->getApplicationInstance();
84
85                 // Output all lines
86                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
87                 self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' daemon starting');
88                 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015 City Developer Team');
89                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
90                 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
91                 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
92                 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
93                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
94         }
95
96         /**
97          * Adds City data elements to a given dataset instance
98          *
99          * @param       $criteriaInstance       An instance of a storeable criteria
100          * @param       $requestInstance        An instance of a Requestable class
101          * @return      void
102          */
103         public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
104                 // Make sure the request instance is set as it is not optional.
105                 assert($requestInstance instanceof Requestable);
106
107                 // Add City number and type
108                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID  , 1);
109                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
110
111                 // Add the City id
112                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
113         }
114
115         /**
116          * Updates a given field with new value
117          *
118          * @param       $fieldName              Field to update
119          * @param       $fieldValue             New value to store
120          * @return      void
121          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
122          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
123          */
124         public function updateDatabaseField ($fieldName, $fieldValue) {
125                 // Unfinished
126                 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
127                 return;
128
129                 // Get a critieria instance
130                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
131
132                 // Add search criteria
133                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
134                 $searchInstance->setLimit(1);
135
136                 // Now get another criteria
137                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
138
139                 // Add criteria entry which we shall update
140                 $updateInstance->addCriteria($fieldName, $fieldValue);
141
142                 // Add the search criteria for searching for the right entry
143                 $updateInstance->setSearchInstance($searchInstance);
144
145                 // Set wrapper class name
146                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
147
148                 // Remember the update in database result
149                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
150         }
151
152         /**
153          * Activates the City by doing some final preparation and setting
154          * $CityIsActive to TRUE.
155          *
156          * @param       $requestInstance        A Requestable class
157          * @param       $responseInstance       A Responseable class
158          * @return      void
159          */
160         public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
161                 // Get the controller here
162                 $controllerInstance = Registry::getRegistry()->getInstance('controller');
163
164                 // Run all filters for the City activation
165                 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
166
167                 // Make sure the city's state is 'init'
168                 $this->getStateInstance()->validateCityStateIsInit();
169
170                 // ----------------------- Last step from here ------------------------
171                 // Activate the city daemon. This is ALWAYS the last step in this method
172                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
173                 $this->getStateInstance()->citySimulationIsActivated();
174                 // ---------------------- Last step until here ------------------------
175         }
176
177         /**
178          * Getter for isActive attribute
179          *
180          * @return      $isActive       Whether the City is active
181          */
182         public final function isCityActive () {
183                 return $this->isActive;
184         }
185
186         /**
187          * Enables (default) or disables isActive flag
188          *
189          * @param       $isActive       Whether the City is active
190          * @return      void
191          */
192         public final function enableIsActive ($isActive = TRUE) {
193                 $this->isActive = (bool) $isActive;
194         }
195
196         /**
197          * Updates/refreshes City data (e.g. status).
198          *
199          * @return      void
200          * @todo        Find more to do here
201          */
202         public function updateCityData () {
203                 // Set some dummy configuration entries, e.g. city_status
204                 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
205         }
206
207         /**
208          * Adds all required elements from given array into data set instance
209          *
210          * @param       $dataSetInstance        An instance of a StoreableCriteria class
211          * @param       $CityData                       An array with valid City data
212          * @return      void
213          */
214         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
215                 // Add all data the array provides
216                 foreach (CityInformationDatabaseWrapper::getAllElements() as $element) {
217                         // Is the element there?
218                         if (isset($cityData[$element])) {
219                                 // Add it
220                                 $dataSetInstance->addCriteria($element, $cityData[$element]);
221                         } else {
222                                 // Output warning message
223                                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
224                         }
225                 } // END - foreac
226         }
227
228         /**
229          * Initializes the city daemon
230          *
231          * @return      void
232          * @todo        0% done
233          */
234         public function bootstrapInitCityDaemon () {
235                 $this->partialStub('Please add something here.');
236         }
237
238         /**
239          * Checks whether at least one map requires expansion
240          *
241          * @return      $requiresExpansion      Whether a map requires expansion
242          */
243         public function isMapPendingExpansion () {
244                 // Get sections manager
245                 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
246
247                 // Call it's method and return value
248                 return $sectionsInstance->isMapPendingExpansion();
249         }
250
251         /**
252          * Expands any found map that requires expansion
253          *
254          * @return      void
255          */
256         public function expandMaps () {
257                 // Get sections manager
258                 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
259
260                 // Call it's method and return value
261                 $sectionsInstance->expandMaps();
262         }
263 }
264
265 // [EOF]
266 ?>