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