3 namespace Org\Mxchange\City\Daemon;
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;
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\Updateable;
16 use Org\Mxchange\CoreFramework\Factory\Database\Wrapper\DatabaseWrapperFactory;
17 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
18 use Org\Mxchange\CoreFramework\Registry\Registry;
19 use Org\Mxchange\CoreFramework\Request\Requestable;
20 use Org\Mxchange\CoreFramework\Response\Responseable;
23 * A general City class
25 * @author Roland Haeder <webmaster@shipsimu.org>
27 * @copyright Copyright (c) 2015, 2016 City Developer Team
28 * @license GNU GPL 3.0 or any newer version
29 * @link http://www.shipsimu.org
31 * This program is free software: you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation, either version 3 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with this program. If not, see <http://www.gnu.org/licenses/>.
44 abstract class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
48 const CITY_TYPE_DEFAULT = 'default';
49 const CITY_TYPE_TESTING = 'testing';
52 * Whether this City is active (default: FALSE)
54 private $isActive = FALSE;
57 * Protected constructor
59 * @param $className Name of the class
62 protected function __construct ($className) {
63 // Call parent constructor
64 parent::__construct($className);
66 // Get a wrapper instance
67 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
70 $this->setWrapperInstance($wrapperInstance);
72 // Get a crypto instance
73 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
76 $this->setCryptoInstance($cryptoInstance);
78 // Add own instance to registry
79 Registry::getRegistry()->addInstance('city', $this);
81 // Init state which sets the state to 'init'
86 * Initializes the City's state which sets it to 'init'
90 private function initState() {
91 // Get the state factory and create the initial state.
92 CityStateFactory::createCityStateInstanceByName('init');
96 * Outputs the console teaser. This should only be executed on startup or
97 * full restarts. This method generates some space around the teaser.
101 public function outputConsoleTeaser () {
102 // Get the app instance (for shortening our code)
103 $app = $this->getApplicationInstance();
106 self::createDebugInstance(__CLASS__)->debugOutput(' ');
107 self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . FrameworkBootstrap::getRequestInstance()->getRequestElement('mode') . ' daemon starting');
108 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015, 2016 City Developer Team');
109 self::createDebugInstance(__CLASS__)->debugOutput(' ');
110 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
111 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
112 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
113 self::createDebugInstance(__CLASS__)->debugOutput(' ');
117 * Adds City data elements to a given dataset instance
119 * @param $criteriaInstance An instance of a storeable criteria
120 * @param $requestInstance An instance of a Requestable class
123 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
124 // Make sure the request instance is set as it is not optional.
125 assert($requestInstance instanceof Requestable);
127 // Add City number and type
128 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
129 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
132 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
136 * Updates a given field with new value
138 * @param $fieldName Field to update
139 * @param $fieldValue New value to store
141 * @throws DatabaseUpdateSupportException If this class does not support database updates
142 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
144 public function updateDatabaseField ($fieldName, $fieldValue) {
146 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
149 // Get a critieria instance
150 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
152 // Add search criteria
153 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
154 $searchInstance->setLimit(1);
156 // Now get another criteria
157 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
159 // Add criteria entry which we shall update
160 $updateInstance->addCriteria($fieldName, $fieldValue);
162 // Add the search criteria for searching for the right entry
163 $updateInstance->setSearchInstance($searchInstance);
165 // Set wrapper class name
166 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
168 // Remember the update in database result
169 $this->getResultInstance()->add2UpdateQueue($updateInstance);
173 * Activates the City by doing some final preparation and setting
174 * $CityIsActive to TRUE.
176 * @param $requestInstance A Requestable class
177 * @param $responseInstance A Responseable class
180 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
181 // Get the controller here
182 $controllerInstance = Registry::getRegistry()->getInstance('controller');
184 // Run all filters for the City activation
185 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
187 // Make sure the city's state is 'init'
188 $this->getStateInstance()->validateCityStateIsInit();
190 // ----------------------- Last step from here ------------------------
191 // Activate the city daemon. This is ALWAYS the last step in this method
192 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
193 $this->getStateInstance()->citySimulationIsActivated();
194 // ---------------------- Last step until here ------------------------
198 * Getter for isActive attribute
200 * @return $isActive Whether the City is active
202 public final function isCityActive () {
203 return $this->isActive;
207 * Enables (default) or disables isActive flag
209 * @param $isActive Whether the City is active
212 public final function enableIsActive ($isActive = TRUE) {
213 $this->isActive = (bool) $isActive;
217 * Updates/refreshes City data (e.g. status).
220 * @todo Find more to do here
222 public function updateCityData () {
223 // Set some dummy configuration entries, e.g. city_status
224 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
228 * Adds all required elements from given array into data set instance
230 * @param $dataSetInstance An instance of a StoreableCriteria class
231 * @param $CityData An array with valid City data
234 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
235 // Add all data the array provides
236 foreach (CityInformationDatabaseWrapper::getAllElements() as $element) {
237 // Is the element there?
238 if (isset($cityData[$element])) {
240 $dataSetInstance->addCriteria($element, $cityData[$element]);
242 // Output warning message
243 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
249 * Initializes the city daemon
254 public function bootstrapInitCityDaemon () {
255 $this->partialStub('Please add something here.');
259 * Checks whether at least one map requires expansion
261 * @return $requiresExpansion Whether a map requires expansion
263 public function isMapPendingExpansion () {
264 // @TODO Is the game paused by user?
266 // Get sections manager
267 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
269 // Call it's method and return value
270 return $sectionsInstance->isMapPendingExpansion();
274 * Expands any found map that requires expansion
278 public function expandMaps () {
279 // Get sections manager
280 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
282 // Call it's method and return value
283 $sectionsInstance->expandMaps();