3 * The general simulator personell class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class SimulatorPersonell extends BasePersonell {
26 private $personellList = null;
29 private $cacheList = null;
31 // A string for cached conditions
32 private $cacheCond = null;
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Magic wake-up method called when unserialize() is called. This is
46 * neccessary because in this case a personell does not need to know the
47 * min/max ages range and system classes. This would anyway use more RAM
48 * what is not required.
52 public function __wakeup () {
54 $this->removePersonellList();
55 $this->removeMinMaxAge();
57 $this->removeSystemArray();
61 * Generate a specified amount of personell and return the prepared instance
63 * @param $amountPersonell Number of personell we shall
65 * @return $personellInstance An instance of this object with a
68 public final static function createSimulatorPersonell ($amountPersonell) {
69 // Make sure only integer can pass
70 $amountPersonell = (int) $amountPersonell;
73 $personellInstance = new SimulatorPersonell();
76 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.",
82 // Initialize the personell list
83 $personellInstance->createPersonellList();
85 // Create requested amount of personell
86 for ($idx = 0; $idx < $amountPersonell; $idx++) {
87 $personellInstance->addRandomPersonell();
91 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.",
98 $personellInstance->removeGender();
99 $personellInstance->removeNames();
100 $personellInstance->removeBirthday();
101 $personellInstance->removeSalary();
102 $personellInstance->removeEmployed();
103 $personellInstance->removeMarried();
104 $personellInstance->removeNumberFormaters();
105 //$personellInstance->removeCache();
106 $personellInstance->removeSystemArray();
108 // Instanz zurueckgeben
109 return $personellInstance;
113 * Create a SimulatorPersonell object by loading the specified personell
114 * list from an existing database backend
116 * @param $idNumber The ID number (only right part) of the list
117 * @return $personellInstance An instance of this class
118 * @throws InvalidIDFormatException If the given id number
119 * $idNumber is invalid
120 * @throws MissingSimulatorIdException If an ID number was not found
123 public final static function createSimulatorPersonellByID ($idNumber) {
125 $personellInstance = new SimulatorPersonell(false);
126 $personellInstance->makeDeprecated();
129 // Create personell list
130 public function createPersonellList () {
131 // Is the list already created?
132 if ($this->personelllList instanceof FrameworkArrayObject) {
133 // Throw an exception
134 throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
137 // Initialize the array
138 $this->personellList = new FrameworkArrayObject("FakedPersonellList");
141 // Remove the personell list
142 private final function removePersonellList () {
143 unset($this->personellList);
146 // Add new personell object to our list
147 public function addRandomPersonell () {
149 $genders = array("M", "F");
151 // Create new personell members
152 $personellInstance = new SimulatorPersonell();
154 // Set a randomized gender
155 $personellInstance->setGender($genders[mt_rand(0, 1)]);
157 // Set a randomized birthday (maximum age required, see const MAX_AGE)
158 $personellInstance->createBirthday();
160 // Married? Same values means: married
161 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
164 $personellInstance->removePersonellList();
165 $personellInstance->removeMinMaxAge();
166 $personellInstance->removeCache();
167 $personellInstance->removeSystemArray();
169 // Add new member to the list
170 $this->personellList->append($personellInstance);
174 * Get a specifyable list of our people, null or empty string will be ignored!
176 * @return $cacheList A list of cached personells
178 function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
179 // Serialize the conditions for checking if we can take the cache
180 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
182 // The same (last) conditions?
183 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
184 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
189 // Return cached list
190 return $this->cacheList;
193 // Output debug message
194 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
199 // Remember the conditions
200 $this->setCacheCond($serialized);
202 // Create cached list
203 $this->setAllCacheList(new FrameworkArrayObject('FakedCacheList'));
205 // Search all unemployed personells
206 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
208 $el = $idx->current();
210 // Check currenylt all single conditions (combined conditions are not yet supported)
211 if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
212 // Add this one (employed status asked)
213 $this->cacheList->append($el);
214 } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
215 // Add this one (marrital status asked)
216 $this->cacheList->append($el);
217 } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
218 // Add this one (specified gender)
219 $this->cacheList->append($el);
223 // Return the completed list
224 return $this->cacheList;
228 * Get amount of unemployed personell
230 * @return $count Amount of unemployed personell
232 public final function getAllUnemployed () {
233 // Get a temporary list
234 $list = $this->getSpecialPersonellList(false);
236 // Anzahl zurueckliefern
237 return $list->count();
241 * Remove cache things
245 private function removeCache () {
247 unset($this->cacheList);
248 unset($this->cacheCond);
252 * Setter for cache list
254 * @param $cacheList The new cache list to set or null for initialization/reset
257 private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
258 $this->cacheList = $cacheList;
262 * Setter for cache conditions
264 * @param $cacheCond The new cache conditions to set
267 private final function setCacheCond ($cacheCond) {
268 $this->cacheCond = (string) $cacheCond;
276 public function resetCache () {
277 $this->setAllCacheList(null);
278 $this->setCacheCond("");
282 * Getter for surname. If no surname is set then default surnames are set
283 * for male and female personells.
285 * @return $surname The personell' surname
287 public final function getSurname () {
288 $surname = parent::getSurname();
290 // Make sure every one has a surname...
291 if (empty($surname)) {
292 if ($this->isMale()) {
296 // Typical female name
297 $surname = "Jennifer";
300 // Set typical family name
301 parent::setFamily("Smith");
309 * Getter for personell list
311 * @return $personellList The list of all personells
313 public final function getPersonellList () {
314 return $this->personellList;
318 * Loads the mostly pre-cached personell list
320 * @param $idNumber The ID number we shall use for looking up
325 public function loadPersonellList ($idNumber) {
326 $this->makeDeprecated();