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\Middleware\Debug\DebugMiddleware;
21 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;;
22 use Org\Mxchange\CoreFramework\Request\Requestable;
23 use Org\Mxchange\CoreFramework\Response\Responseable;
24 use Org\Mxchange\CoreFramework\Traits\Crypto\CryptoTrait;
25 use Org\Mxchange\CoreFramework\Traits\Database\Frontend\DatabaseFrontendTrait;
26 use Org\Mxchange\CoreFramework\Traits\State\StateableTrait;
29 * A general City class
31 * @author Roland Haeder <webmaster@shipsimu.org>
33 * @copyright Copyright (c) 2015 - 2023 City Developer Team
34 * @license GNU GPL 3.0 or any newer version
35 * @link http://www.shipsimu.org
37 * This program is free software: you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation, either version 3 of the License, or
40 * (at your option) any later version.
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
47 * You should have received a copy of the GNU General Public License
48 * along with this program. If not, see <http://www.gnu.org/licenses/>.
50 abstract class BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
53 use DatabaseFrontendTrait;
59 const CITY_TYPE_DEFAULT = 'default';
60 const CITY_TYPE_TESTING = 'testing';
63 * Whether this City is active (default: FALSE)
65 private $isActive = FALSE;
68 * Protected constructor
70 * @param $className Name of the class
73 protected function __construct (string $className) {
74 // Call parent constructor
75 parent::__construct($className);
77 // Get a frontend instance
78 $frontendInstance = DatabaseFrontendFactory::createFrontendByConfiguredName('city_info_db_frontend_class');
81 $this->setFrontendInstance($frontendInstance);
83 // Get a crypto instance
84 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
87 $this->setCryptoInstance($cryptoInstance);
89 // Add own instance to registry
90 GenericRegistry::getRegistry()->addInstance('city', $this);
92 // Init state which sets the state to 'init'
97 * Initializes the City's state which sets it to 'init'
101 private function initState() {
102 // Get the state factory and create the initial state.
103 CityStateFactory::createCityStateInstanceByName('init');
107 * Outputs the console teaser. This should only be executed on startup or
108 * full restarts. This method generates some space around the teaser.
112 public function outputConsoleTeaser () {
113 // Get the app instance (for shortening our code)
114 $applicationInstance = ApplicationHelper::getSelfInstance();
117 self::createDebugInstance(__CLASS__)->debugOutput(' ');
118 self::createDebugInstance(__CLASS__)->debugOutput($applicationInstance->getAppName() . ' v' . $applicationInstance->getAppVersion() . ' - ' . FrameworkBootstrap::getRequestInstance()->getRequestElement('mode') . ' daemon starting');
119 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015 - 2023 City Developer Team');
120 self::createDebugInstance(__CLASS__)->debugOutput(' ');
121 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
122 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
123 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
124 self::createDebugInstance(__CLASS__)->debugOutput(' ');
128 * Adds City data elements to a given dataset instance
130 * @param $criteriaInstance An instance of a storeable criteria
131 * @param $requestInstance An instance of a Requestable class
134 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
135 // Make sure the request instance is set as it is not optional.
136 assert($requestInstance instanceof Requestable);
138 // Add City number and type
139 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID , 1);
140 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
143 $criteriaInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID, $this->getCityId());
147 * Updates a given field with new value
149 * @param $fieldName Field to update
150 * @param $fieldValue New value to store
152 * @throws DatabaseUpdateSupportException If this class does not support database updates
153 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
155 public function updateDatabaseField ($fieldName, $fieldValue) {
157 DebugMiddleware::getSelfInstance()->partialStub('Unfinished: fieldName=' . $fieldName . ',fieldValue=' . $fieldValue);
160 // Get a critieria instance
161 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
163 // Add search criteria
164 $searchInstance->addCriteria(UserDatabaseFrontend::DB_COLUMN_USERNAME, $this->getUserName());
165 $searchInstance->setLimit(1);
167 // Now get another criteria
168 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
170 // Add criteria entry which we shall update
171 $updateInstance->addCriteria($fieldName, $fieldValue);
173 // Add the search criteria for searching for the right entry
174 $updateInstance->setSearchInstance($searchInstance);
176 // Set frontend class name
177 $updateInstance->setFrontendConfigEntry('user_db_frontend_class');
179 // Remember the update in database result
180 $this->getResultInstance()->add2UpdateQueue($updateInstance);
184 * Activates the City by doing some final preparation and setting
185 * $CityIsActive to TRUE.
187 * @param $requestInstance A Requestable class
188 * @param $responseInstance A Responseable class
191 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
192 // Get the controller here
193 $controllerInstance = GenericRegistry::getRegistry()->getInstance('controller');
195 // Run all filters for the City activation
196 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
198 // Make sure the city's state is 'init'
199 $this->getStateInstance()->validateCityStateIsInit();
201 // ----------------------- Last step from here ------------------------
202 // Activate the city daemon. This is ALWAYS the last step in this method
203 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
204 $this->getStateInstance()->citySimulationIsActivated();
205 // ---------------------- Last step until here ------------------------
209 * Getter for isActive attribute
211 * @return $isActive Whether the City is active
213 public final function isCityActive () {
214 return $this->isActive;
218 * Enables (default) or disables isActive flag
220 * @param $isActive Whether the City is active
223 public final function enableIsActive ($isActive = TRUE) {
224 $this->isActive = (bool) $isActive;
228 * Updates/refreshes City data (e.g. status).
231 * @todo Find more to do here
233 public function updateCityData () {
234 // Set some dummy configuration entries, e.g. city_status
235 FrameworkBootstrap::getConfigurationInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
239 * Adds all required elements from given array into data set instance
241 * @param $dataSetInstance An instance of a StoreableCriteria class
242 * @param $CityData An array with valid City data
245 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
246 // Add all data the array provides
247 foreach (CityInformationDatabaseFrontend::getAllElements() as $element) {
248 // Is the element there?
249 if (isset($cityData[$element])) {
251 $dataSetInstance->addCriteria($element, $cityData[$element]);
253 // Output warning message
254 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
260 * Initializes the city daemon
265 public function bootstrapInitCityDaemon () {
266 DebugMiddleware::getSelfInstance()->partialStub('Please add something here.');
270 * Checks whether at least one map requires expansion
272 * @return $requiresExpansion Whether a map requires expansion
274 public function isMapPendingExpansion () {
275 // @TODO Is the game paused by user?
277 // Get sections manager
278 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
280 // Call it's method and return value
281 return $sectionsInstance->isSectionPendingExpansion();
285 * Expands any found map that requires expansion
289 public function expandMaps () {
290 // Get sections manager
291 $sectionsInstance = ManagerFactory::createManagerByType('city_sections');
293 // Call it's method and return value
294 $sectionsInstance->expandMaps();