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\Frontend\User\UserDatabaseWrapper;
16 use Org\Mxchange\CoreFramework\Database\Updateable;
17 use Org\Mxchange\CoreFramework\Factory\Database\Wrapper\DatabaseWrapperFactory;
18 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
19 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;;
20 use Org\Mxchange\CoreFramework\Request\Requestable;
21 use Org\Mxchange\CoreFramework\Response\Responseable;
24 * A general City class
26 * @author Roland Haeder <webmaster@shipsimu.org>
28 * @copyright Copyright (c) 2015, 2016 City Developer Team
29 * @license GNU GPL 3.0 or any newer version
30 * @link http://www.shipsimu.org
32 * This program is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, either version 3 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program. If not, see <http://www.gnu.org/licenses/>.
45 abstract class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
49 const CITY_TYPE_DEFAULT = 'default';
50 const CITY_TYPE_TESTING = 'testing';
53 * Whether this City is active (default: FALSE)
55 private $isActive = FALSE;
58 * Protected constructor
60 * @param $className Name of the class
63 protected function __construct ($className) {
64 // Call parent constructor
65 parent::__construct($className);
67 // Get a wrapper instance
68 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
71 $this->setWrapperInstance($wrapperInstance);
73 // Get a crypto instance
74 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
77 $this->setCryptoInstance($cryptoInstance);
79 // Add own instance to registry
80 GenericRegistry::getRegistry()->addInstance('city', $this);
82 // Init state which sets the state to 'init'
87 * Initializes the City's state which sets it to 'init'
91 private function initState() {
92 // Get the state factory and create the initial state.
93 CityStateFactory::createCityStateInstanceByName('init');
97 * Outputs the console teaser. This should only be executed on startup or
98 * full restarts. This method generates some space around the teaser.
102 public function outputConsoleTeaser () {
103 // Get the app instance (for shortening our code)
104 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
107 self::createDebugInstance(__CLASS__)->debugOutput(' ');
108 self::createDebugInstance(__CLASS__)->debugOutput($applicationInstance->getAppName() . ' v' . $applicationInstance->getAppVersion() . ' - ' . FrameworkBootstrap::getRequestInstance()->getRequestElement('mode') . ' daemon starting');
109 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015, 2016 City Developer Team');
110 self::createDebugInstance(__CLASS__)->debugOutput(' ');
111 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
112 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
113 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
114 self::createDebugInstance(__CLASS__)->debugOutput(' ');
118 * Adds City data elements to a given dataset instance
120 * @param $criteriaInstance An instance of a storeable criteria
121 * @param $requestInstance An instance of a Requestable class
124 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
125 // Make sure the request instance is set as it is not optional.
126 assert($requestInstance instanceof Requestable);
128 // Add City number and type
129 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
130 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
133 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
137 * Updates a given field with new value
139 * @param $fieldName Field to update
140 * @param $fieldValue New value to store
142 * @throws DatabaseUpdateSupportException If this class does not support database updates
143 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
145 public function updateDatabaseField ($fieldName, $fieldValue) {
147 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
150 // Get a critieria instance
151 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
153 // Add search criteria
154 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
155 $searchInstance->setLimit(1);
157 // Now get another criteria
158 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
160 // Add criteria entry which we shall update
161 $updateInstance->addCriteria($fieldName, $fieldValue);
163 // Add the search criteria for searching for the right entry
164 $updateInstance->setSearchInstance($searchInstance);
166 // Set wrapper class name
167 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
169 // Remember the update in database result
170 $this->getResultInstance()->add2UpdateQueue($updateInstance);
174 * Activates the City by doing some final preparation and setting
175 * $CityIsActive to TRUE.
177 * @param $requestInstance A Requestable class
178 * @param $responseInstance A Responseable class
181 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
182 // Get the controller here
183 $controllerInstance = GenericRegistry::getRegistry()->getInstance('controller');
185 // Run all filters for the City activation
186 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
188 // Make sure the city's state is 'init'
189 $this->getStateInstance()->validateCityStateIsInit();
191 // ----------------------- Last step from here ------------------------
192 // Activate the city daemon. This is ALWAYS the last step in this method
193 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
194 $this->getStateInstance()->citySimulationIsActivated();
195 // ---------------------- Last step until here ------------------------
199 * Getter for isActive attribute
201 * @return $isActive Whether the City is active
203 public final function isCityActive () {
204 return $this->isActive;
208 * Enables (default) or disables isActive flag
210 * @param $isActive Whether the City is active
213 public final function enableIsActive ($isActive = TRUE) {
214 $this->isActive = (bool) $isActive;
218 * Updates/refreshes City data (e.g. status).
221 * @todo Find more to do here
223 public function updateCityData () {
224 // Set some dummy configuration entries, e.g. city_status
225 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
229 * Adds all required elements from given array into data set instance
231 * @param $dataSetInstance An instance of a StoreableCriteria class
232 * @param $CityData An array with valid City data
235 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
236 // Add all data the array provides
237 foreach (CityInformationDatabaseWrapper::getAllElements() as $element) {
238 // Is the element there?
239 if (isset($cityData[$element])) {
241 $dataSetInstance->addCriteria($element, $cityData[$element]);
243 // Output warning message
244 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
250 * Initializes the city daemon
255 public function bootstrapInitCityDaemon () {
256 $this->partialStub('Please add something here.');
260 * Checks whether at least one map requires expansion
262 * @return $requiresExpansion Whether a map requires expansion
264 public function isMapPendingExpansion () {
265 // @TODO Is the game paused by user?
267 // Get sections manager
268 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
270 // Call it's method and return value
271 return $sectionsInstance->isSectionPendingExpansion();
275 * Expands any found map that requires expansion
279 public function expandMaps () {
280 // Get sections manager
281 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
283 // Call it's method and return value
284 $sectionsInstance->expandMaps();