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