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