3 namespace Org\Mxchange\City\Daemon;
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;
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;
22 * A general City class
24 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
43 abstract class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
47 const CITY_TYPE_DEFAULT = 'default';
48 const CITY_TYPE_TESTING = 'testing';
51 * Whether this City is active (default: FALSE)
53 private $isActive = FALSE;
56 * Protected constructor
58 * @param $className Name of the class
61 protected function __construct ($className) {
62 // Call parent constructor
63 parent::__construct($className);
65 // Get a wrapper instance
66 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
69 $this->setWrapperInstance($wrapperInstance);
71 // Get a crypto instance
72 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
75 $this->setCryptoInstance($cryptoInstance);
77 // Add own instance to registry
78 Registry::getRegistry()->addInstance('city', $this);
80 // Init state which sets the state to 'init'
85 * Initializes the City's state which sets it to 'init'
89 private function initState() {
90 // Get the state factory and create the initial state.
91 CityStateFactory::createCityStateInstanceByName('init');
95 * Outputs the console teaser. This should only be executed on startup or
96 * full restarts. This method generates some space around the teaser.
100 public function outputConsoleTeaser () {
101 // Get the app instance (for shortening our code)
102 $app = $this->getApplicationInstance();
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(' ');
116 * Adds City data elements to a given dataset instance
118 * @param $criteriaInstance An instance of a storeable criteria
119 * @param $requestInstance An instance of a Requestable class
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);
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'));
131 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
135 * Updates a given field with new value
137 * @param $fieldName Field to update
138 * @param $fieldValue New value to store
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
143 public function updateDatabaseField ($fieldName, $fieldValue) {
145 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
148 // Get a critieria instance
149 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
151 // Add search criteria
152 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
153 $searchInstance->setLimit(1);
155 // Now get another criteria
156 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
158 // Add criteria entry which we shall update
159 $updateInstance->addCriteria($fieldName, $fieldValue);
161 // Add the search criteria for searching for the right entry
162 $updateInstance->setSearchInstance($searchInstance);
164 // Set wrapper class name
165 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
167 // Remember the update in database result
168 $this->getResultInstance()->add2UpdateQueue($updateInstance);
172 * Activates the City by doing some final preparation and setting
173 * $CityIsActive to TRUE.
175 * @param $requestInstance A Requestable class
176 * @param $responseInstance A Responseable class
179 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
180 // Get the controller here
181 $controllerInstance = Registry::getRegistry()->getInstance('controller');
183 // Run all filters for the City activation
184 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
186 // Make sure the city's state is 'init'
187 $this->getStateInstance()->validateCityStateIsInit();
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 ------------------------
197 * Getter for isActive attribute
199 * @return $isActive Whether the City is active
201 public final function isCityActive () {
202 return $this->isActive;
206 * Enables (default) or disables isActive flag
208 * @param $isActive Whether the City is active
211 public final function enableIsActive ($isActive = TRUE) {
212 $this->isActive = (bool) $isActive;
216 * Updates/refreshes City data (e.g. status).
219 * @todo Find more to do here
221 public function updateCityData () {
222 // Set some dummy configuration entries, e.g. city_status
223 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
227 * Adds all required elements from given array into data set instance
229 * @param $dataSetInstance An instance of a StoreableCriteria class
230 * @param $CityData An array with valid City data
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])) {
239 $dataSetInstance->addCriteria($element, $cityData[$element]);
241 // Output warning message
242 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
248 * Initializes the city daemon
253 public function bootstrapInitCityDaemon () {
254 $this->partialStub('Please add something here.');
258 * Checks whether at least one map requires expansion
260 * @return $requiresExpansion Whether a map requires expansion
262 public function isMapPendingExpansion () {
263 // @TODO Is the game paused by user?
265 // Get sections manager
266 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
268 // Call it's method and return value
269 return $sectionsInstance->isMapPendingExpansion();
273 * Expands any found map that requires expansion
277 public function expandMaps () {
278 // Get sections manager
279 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
281 // Call it's method and return value
282 $sectionsInstance->expandMaps();