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