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