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