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