3 namespace Org\Mxchange\City\Daemon;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Request\Requestable;
8 use Org\Mxchange\CoreFramework\Response\Responseable;
11 * A general City class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2015, 2016 City Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
36 const CITY_TYPE_DEFAULT = 'default';
37 const CITY_TYPE_TESTING = 'testing';
40 * Whether this City is active (default: FALSE)
42 private $isActive = FALSE;
45 * Protected constructor
47 * @param $className Name of the class
50 protected function __construct ($className) {
51 // Call parent constructor
52 parent::__construct($className);
54 // Get a wrapper instance
55 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
58 $this->setWrapperInstance($wrapperInstance);
60 // Get a crypto instance
61 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
64 $this->setCryptoInstance($cryptoInstance);
66 // Add own instance to registry
67 Registry::getRegistry()->addInstance('city', $this);
69 // Init state which sets the state to 'init'
74 * Initializes the City's state which sets it to 'init'
78 private function initState() {
79 // Get the state factory and create the initial state.
80 CityStateFactory::createCityStateInstanceByName('init');
84 * Outputs the console teaser. This should only be executed on startup or
85 * full restarts. This method generates some space around the teaser.
89 public function outputConsoleTeaser () {
90 // Get the app instance (for shortening our code)
91 $app = $this->getApplicationInstance();
94 self::createDebugInstance(__CLASS__)->debugOutput(' ');
95 self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' daemon starting');
96 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015, 2016 City Developer Team');
97 self::createDebugInstance(__CLASS__)->debugOutput(' ');
98 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
99 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
100 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
101 self::createDebugInstance(__CLASS__)->debugOutput(' ');
105 * Adds City data elements to a given dataset instance
107 * @param $criteriaInstance An instance of a storeable criteria
108 * @param $requestInstance An instance of a Requestable class
111 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
112 // Make sure the request instance is set as it is not optional.
113 assert($requestInstance instanceof Requestable);
115 // Add City number and type
116 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
117 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
120 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
124 * Updates a given field with new value
126 * @param $fieldName Field to update
127 * @param $fieldValue New value to store
129 * @throws DatabaseUpdateSupportException If this class does not support database updates
130 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
132 public function updateDatabaseField ($fieldName, $fieldValue) {
134 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
137 // Get a critieria instance
138 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
140 // Add search criteria
141 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
142 $searchInstance->setLimit(1);
144 // Now get another criteria
145 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
147 // Add criteria entry which we shall update
148 $updateInstance->addCriteria($fieldName, $fieldValue);
150 // Add the search criteria for searching for the right entry
151 $updateInstance->setSearchInstance($searchInstance);
153 // Set wrapper class name
154 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
156 // Remember the update in database result
157 $this->getResultInstance()->add2UpdateQueue($updateInstance);
161 * Activates the City by doing some final preparation and setting
162 * $CityIsActive to TRUE.
164 * @param $requestInstance A Requestable class
165 * @param $responseInstance A Responseable class
168 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
169 // Get the controller here
170 $controllerInstance = Registry::getRegistry()->getInstance('controller');
172 // Run all filters for the City activation
173 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
175 // Make sure the city's state is 'init'
176 $this->getStateInstance()->validateCityStateIsInit();
178 // ----------------------- Last step from here ------------------------
179 // Activate the city daemon. This is ALWAYS the last step in this method
180 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
181 $this->getStateInstance()->citySimulationIsActivated();
182 // ---------------------- Last step until here ------------------------
186 * Getter for isActive attribute
188 * @return $isActive Whether the City is active
190 public final function isCityActive () {
191 return $this->isActive;
195 * Enables (default) or disables isActive flag
197 * @param $isActive Whether the City is active
200 public final function enableIsActive ($isActive = TRUE) {
201 $this->isActive = (bool) $isActive;
205 * Updates/refreshes City data (e.g. status).
208 * @todo Find more to do here
210 public function updateCityData () {
211 // Set some dummy configuration entries, e.g. city_status
212 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
216 * Adds all required elements from given array into data set instance
218 * @param $dataSetInstance An instance of a StoreableCriteria class
219 * @param $CityData An array with valid City data
222 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
223 // Add all data the array provides
224 foreach (CityInformationDatabaseWrapper::getAllElements() as $element) {
225 // Is the element there?
226 if (isset($cityData[$element])) {
228 $dataSetInstance->addCriteria($element, $cityData[$element]);
230 // Output warning message
231 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
237 * Initializes the city daemon
242 public function bootstrapInitCityDaemon () {
243 $this->partialStub('Please add something here.');
247 * Checks whether at least one map requires expansion
249 * @return $requiresExpansion Whether a map requires expansion
251 public function isMapPendingExpansion () {
252 // @TODO Is the game paused by user?
254 // Get sections manager
255 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
257 // Call it's method and return value
258 return $sectionsInstance->isMapPendingExpansion();
262 * Expands any found map that requires expansion
266 public function expandMaps () {
267 // Get sections manager
268 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
270 // Call it's method and return value
271 $sectionsInstance->expandMaps();