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\CityInformationDatabaseFrontend;
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\UserDatabaseFrontend;
16 use Org\Mxchange\CoreFramework\Database\Updateable;
17 use Org\Mxchange\CoreFramework\Factory\Database\Frontend\DatabaseFrontendFactory;
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;
22 use Org\Mxchange\CoreFramework\Traits\Crypto\CryptoTrait;
23 use Org\Mxchange\CoreFramework\Traits\Database\Frontend\DatabaseFrontendTrait;
26 * A general City class
28 * @author Roland Haeder <webmaster@shipsimu.org>
30 * @copyright Copyright (c) 2015, 2016 City Developer Team
31 * @license GNU GPL 3.0 or any newer version
32 * @link http://www.shipsimu.org
34 * This program is free software: you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation, either version 3 of the License, or
37 * (at your option) any later version.
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with this program. If not, see <http://www.gnu.org/licenses/>.
47 abstract class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
50 use DatabaseFrontendTrait;
55 const CITY_TYPE_DEFAULT = 'default';
56 const CITY_TYPE_TESTING = 'testing';
59 * Whether this City is active (default: FALSE)
61 private $isActive = FALSE;
64 * Protected constructor
66 * @param $className Name of the class
69 protected function __construct (string $className) {
70 // Call parent constructor
71 parent::__construct($className);
73 // Get a frontend instance
74 $frontendInstance = DatabaseFrontendFactory::createFrontendByConfiguredName('city_info_db_frontend_class');
77 $this->setFrontendInstance($frontendInstance);
79 // Get a crypto instance
80 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
83 $this->setCryptoInstance($cryptoInstance);
85 // Add own instance to registry
86 GenericRegistry::getRegistry()->addInstance('city', $this);
88 // Init state which sets the state to 'init'
93 * Initializes the City's state which sets it to 'init'
97 private function initState() {
98 // Get the state factory and create the initial state.
99 CityStateFactory::createCityStateInstanceByName('init');
103 * Outputs the console teaser. This should only be executed on startup or
104 * full restarts. This method generates some space around the teaser.
108 public function outputConsoleTeaser () {
109 // Get the app instance (for shortening our code)
110 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
113 self::createDebugInstance(__CLASS__)->debugOutput(' ');
114 self::createDebugInstance(__CLASS__)->debugOutput($applicationInstance->getAppName() . ' v' . $applicationInstance->getAppVersion() . ' - ' . FrameworkBootstrap::getRequestInstance()->getRequestElement('mode') . ' daemon starting');
115 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015, 2016 City Developer Team');
116 self::createDebugInstance(__CLASS__)->debugOutput(' ');
117 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
118 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
119 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
120 self::createDebugInstance(__CLASS__)->debugOutput(' ');
124 * Adds City data elements to a given dataset instance
126 * @param $criteriaInstance An instance of a storeable criteria
127 * @param $requestInstance An instance of a Requestable class
130 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
131 // Make sure the request instance is set as it is not optional.
132 assert($requestInstance instanceof Requestable);
134 // Add City number and type
135 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID , 1);
136 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
139 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID, $this->getCityId());
143 * Updates a given field with new value
145 * @param $fieldName Field to update
146 * @param $fieldValue New value to store
148 * @throws DatabaseUpdateSupportException If this class does not support database updates
149 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
151 public function updateDatabaseField ($fieldName, $fieldValue) {
153 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
156 // Get a critieria instance
157 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
159 // Add search criteria
160 $searchInstance->addCriteria(UserDatabaseFrontend::DB_COLUMN_USERNAME, $this->getUserName());
161 $searchInstance->setLimit(1);
163 // Now get another criteria
164 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
166 // Add criteria entry which we shall update
167 $updateInstance->addCriteria($fieldName, $fieldValue);
169 // Add the search criteria for searching for the right entry
170 $updateInstance->setSearchInstance($searchInstance);
172 // Set frontend class name
173 $updateInstance->setFrontendConfigEntry('user_db_frontend_class');
175 // Remember the update in database result
176 $this->getResultInstance()->add2UpdateQueue($updateInstance);
180 * Activates the City by doing some final preparation and setting
181 * $CityIsActive to TRUE.
183 * @param $requestInstance A Requestable class
184 * @param $responseInstance A Responseable class
187 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
188 // Get the controller here
189 $controllerInstance = GenericRegistry::getRegistry()->getInstance('controller');
191 // Run all filters for the City activation
192 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
194 // Make sure the city's state is 'init'
195 $this->getStateInstance()->validateCityStateIsInit();
197 // ----------------------- Last step from here ------------------------
198 // Activate the city daemon. This is ALWAYS the last step in this method
199 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
200 $this->getStateInstance()->citySimulationIsActivated();
201 // ---------------------- Last step until here ------------------------
205 * Getter for isActive attribute
207 * @return $isActive Whether the City is active
209 public final function isCityActive () {
210 return $this->isActive;
214 * Enables (default) or disables isActive flag
216 * @param $isActive Whether the City is active
219 public final function enableIsActive ($isActive = TRUE) {
220 $this->isActive = (bool) $isActive;
224 * Updates/refreshes City data (e.g. status).
227 * @todo Find more to do here
229 public function updateCityData () {
230 // Set some dummy configuration entries, e.g. city_status
231 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
235 * Adds all required elements from given array into data set instance
237 * @param $dataSetInstance An instance of a StoreableCriteria class
238 * @param $CityData An array with valid City data
241 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
242 // Add all data the array provides
243 foreach (CityInformationDatabaseFrontend::getAllElements() as $element) {
244 // Is the element there?
245 if (isset($cityData[$element])) {
247 $dataSetInstance->addCriteria($element, $cityData[$element]);
249 // Output warning message
250 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
256 * Initializes the city daemon
261 public function bootstrapInitCityDaemon () {
262 $this->partialStub('Please add something here.');
266 * Checks whether at least one map requires expansion
268 * @return $requiresExpansion Whether a map requires expansion
270 public function isMapPendingExpansion () {
271 // @TODO Is the game paused by user?
273 // Get sections manager
274 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
276 // Call it's method and return value
277 return $sectionsInstance->isSectionPendingExpansion();
281 * Expands any found map that requires expansion
285 public function expandMaps () {
286 // Get sections manager
287 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
289 // Call it's method and return value
290 $sectionsInstance->expandMaps();