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\Object\ObjectFactory;
19 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
20 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;;
21 use Org\Mxchange\CoreFramework\Request\Requestable;
22 use Org\Mxchange\CoreFramework\Response\Responseable;
23 use Org\Mxchange\CoreFramework\Traits\Crypto\CryptoTrait;
24 use Org\Mxchange\CoreFramework\Traits\Database\Frontend\DatabaseFrontendTrait;
25 use Org\Mxchange\CoreFramework\Traits\State\StateableTrait;
28 * A general City class
30 * @author Roland Haeder <webmaster@shipsimu.org>
32 * @copyright Copyright (c) 2015 - 2023 City Developer Team
33 * @license GNU GPL 3.0 or any newer version
34 * @link http://www.shipsimu.org
36 * This program is free software: you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation, either version 3 of the License, or
39 * (at your option) any later version.
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
46 * You should have received a copy of the GNU General Public License
47 * along with this program. If not, see <http://www.gnu.org/licenses/>.
49 abstract class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
52 use DatabaseFrontendTrait;
58 const CITY_TYPE_DEFAULT = 'default';
59 const CITY_TYPE_TESTING = 'testing';
62 * Whether this City is active (default: FALSE)
64 private $isActive = FALSE;
67 * Protected constructor
69 * @param $className Name of the class
72 protected function __construct (string $className) {
73 // Call parent constructor
74 parent::__construct($className);
76 // Get a frontend instance
77 $frontendInstance = DatabaseFrontendFactory::createFrontendByConfiguredName('city_info_db_frontend_class');
80 $this->setFrontendInstance($frontendInstance);
82 // Get a crypto instance
83 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
86 $this->setCryptoInstance($cryptoInstance);
88 // Add own instance to registry
89 GenericRegistry::getRegistry()->addInstance('city', $this);
91 // Init state which sets the state to 'init'
96 * Initializes the City's state which sets it to 'init'
100 private function initState() {
101 // Get the state factory and create the initial state.
102 CityStateFactory::createCityStateInstanceByName('init');
106 * Outputs the console teaser. This should only be executed on startup or
107 * full restarts. This method generates some space around the teaser.
111 public function outputConsoleTeaser () {
112 // Get the app instance (for shortening our code)
113 $applicationInstance = ApplicationHelper::getSelfInstance();
116 self::createDebugInstance(__CLASS__)->debugOutput(' ');
117 self::createDebugInstance(__CLASS__)->debugOutput($applicationInstance->getAppName() . ' v' . $applicationInstance->getAppVersion() . ' - ' . FrameworkBootstrap::getRequestInstance()->getRequestElement('mode') . ' daemon starting');
118 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015 - 2023 City Developer Team');
119 self::createDebugInstance(__CLASS__)->debugOutput(' ');
120 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
121 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
122 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
123 self::createDebugInstance(__CLASS__)->debugOutput(' ');
127 * Adds City data elements to a given dataset instance
129 * @param $criteriaInstance An instance of a storeable criteria
130 * @param $requestInstance An instance of a Requestable class
133 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
134 // Make sure the request instance is set as it is not optional.
135 assert($requestInstance instanceof Requestable);
137 // Add City number and type
138 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID , 1);
139 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
142 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID, $this->getCityId());
146 * Updates a given field with new value
148 * @param $fieldName Field to update
149 * @param $fieldValue New value to store
151 * @throws DatabaseUpdateSupportException If this class does not support database updates
152 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
154 public function updateDatabaseField ($fieldName, $fieldValue) {
156 $this->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
159 // Get a critieria instance
160 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
162 // Add search criteria
163 $searchInstance->addCriteria(UserDatabaseFrontend::DB_COLUMN_USERNAME, $this->getUserName());
164 $searchInstance->setLimit(1);
166 // Now get another criteria
167 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
169 // Add criteria entry which we shall update
170 $updateInstance->addCriteria($fieldName, $fieldValue);
172 // Add the search criteria for searching for the right entry
173 $updateInstance->setSearchInstance($searchInstance);
175 // Set frontend class name
176 $updateInstance->setFrontendConfigEntry('user_db_frontend_class');
178 // Remember the update in database result
179 $this->getResultInstance()->add2UpdateQueue($updateInstance);
183 * Activates the City by doing some final preparation and setting
184 * $CityIsActive to TRUE.
186 * @param $requestInstance A Requestable class
187 * @param $responseInstance A Responseable class
190 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
191 // Get the controller here
192 $controllerInstance = GenericRegistry::getRegistry()->getInstance('controller');
194 // Run all filters for the City activation
195 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
197 // Make sure the city's state is 'init'
198 $this->getStateInstance()->validateCityStateIsInit();
200 // ----------------------- Last step from here ------------------------
201 // Activate the city daemon. This is ALWAYS the last step in this method
202 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
203 $this->getStateInstance()->citySimulationIsActivated();
204 // ---------------------- Last step until here ------------------------
208 * Getter for isActive attribute
210 * @return $isActive Whether the City is active
212 public final function isCityActive () {
213 return $this->isActive;
217 * Enables (default) or disables isActive flag
219 * @param $isActive Whether the City is active
222 public final function enableIsActive ($isActive = TRUE) {
223 $this->isActive = (bool) $isActive;
227 * Updates/refreshes City data (e.g. status).
230 * @todo Find more to do here
232 public function updateCityData () {
233 // Set some dummy configuration entries, e.g. city_status
234 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
238 * Adds all required elements from given array into data set instance
240 * @param $dataSetInstance An instance of a StoreableCriteria class
241 * @param $CityData An array with valid City data
244 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
245 // Add all data the array provides
246 foreach (CityInformationDatabaseFrontend::getAllElements() as $element) {
247 // Is the element there?
248 if (isset($cityData[$element])) {
250 $dataSetInstance->addCriteria($element, $cityData[$element]);
252 // Output warning message
253 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
259 * Initializes the city daemon
264 public function bootstrapInitCityDaemon () {
265 $this->partialStub('Please add something here.');
269 * Checks whether at least one map requires expansion
271 * @return $requiresExpansion Whether a map requires expansion
273 public function isMapPendingExpansion () {
274 // @TODO Is the game paused by user?
276 // Get sections manager
277 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
279 // Call it's method and return value
280 return $sectionsInstance->isSectionPendingExpansion();
284 * Expands any found map that requires expansion
288 public function expandMaps () {
289 // Get sections manager
290 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
292 // Call it's method and return value
293 $sectionsInstance->expandMaps();