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