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