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();
60 * Generate a specified amount of personell and return the prepared instance
62 * @param $amountPersonell Number of personell we shall
64 * @return $personellInstance An instance of this object with a
67 public final static function createSimulatorPersonell ($amountPersonell) {
68 // Make sure only integer can pass
69 $amountPersonell = (int) $amountPersonell;
72 $personellInstance = new SimulatorPersonell();
75 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.",
81 // Initialize the personell list
82 $personellInstance->createPersonellList();
84 // Create requested amount of personell
85 for ($idx = 0; $idx < $amountPersonell; $idx++) {
86 $personellInstance->addRandomPersonell();
90 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.",
97 $personellInstance->removeGender();
98 $personellInstance->removeNames();
99 $personellInstance->removeBirthday();
100 $personellInstance->removeSalary();
101 $personellInstance->removeEmployed();
102 $personellInstance->removeMarried();
103 //$personellInstance->removeCache();
105 // Instanz zurueckgeben
106 return $personellInstance;
110 * Create a SimulatorPersonell object by loading the specified personell
111 * list from an existing database backend
113 * @param $idNumber The ID number (only right part) of the list
114 * @return $personellInstance An instance of this class
115 * @throws InvalidIDFormatException If the given id number
116 * $idNumber is invalid
117 * @throws MissingSimulatorIdException If an ID number was not found
120 public final static function createSimulatorPersonellByID ($idNumber) {
122 $personellInstance = new SimulatorPersonell(false);
123 $personellInstance->makeDeprecated();
126 // Create personell list
127 public function createPersonellList () {
128 // Is the list already created?
129 if ($this->personelllList instanceof FrameworkArrayObject) {
130 // Throw an exception
131 throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
134 // Initialize the array
135 $this->personellList = new FrameworkArrayObject("FakedPersonellList");
138 // Remove the personell list
139 private final function removePersonellList () {
140 unset($this->personellList);
143 // Add new personell object to our list
144 public function addRandomPersonell () {
146 $genders = array("M", "F");
148 // Create new personell members
149 $personellInstance = new SimulatorPersonell();
151 // Set a randomized gender
152 $personellInstance->setGender($genders[mt_rand(0, 1)]);
154 // Set a randomized birthday (maximum age required, see const MAX_AGE)
155 $personellInstance->createBirthday();
157 // Married? Same values means: married
158 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
161 $personellInstance->removePersonellList();
162 $personellInstance->removeMinMaxAge();
163 $personellInstance->removeCache();
165 // Add new member to the list
166 $this->personellList->append($personellInstance);
170 * Get a specifyable list of our people, null or empty string will be ignored!
172 * @return $cacheList A list of cached personells
174 function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
175 // Serialize the conditions for checking if we can take the cache
176 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
178 // The same (last) conditions?
179 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
180 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
185 // Return cached list
186 return $this->cacheList;
189 // Output debug message
190 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
195 // Remember the conditions
196 $this->setCacheCond($serialized);
198 // Create cached list
199 $this->setAllCacheList(new FrameworkArrayObject('FakedCacheList'));
201 // Search all unemployed personells
202 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
204 $el = $idx->current();
206 // Check currenylt all single conditions (combined conditions are not yet supported)
207 if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
208 // Add this one (employed status asked)
209 $this->cacheList->append($el);
210 } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
211 // Add this one (marrital status asked)
212 $this->cacheList->append($el);
213 } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
214 // Add this one (specified gender)
215 $this->cacheList->append($el);
219 // Return the completed list
220 return $this->cacheList;
224 * Get amount of unemployed personell
226 * @return $count Amount of unemployed personell
228 public final function getAllUnemployed () {
229 // Get a temporary list
230 $list = $this->getSpecialPersonellList(false);
232 // Anzahl zurueckliefern
233 return $list->count();
237 * Remove cache things
241 private function removeCache () {
243 unset($this->cacheList);
244 unset($this->cacheCond);
248 * Setter for cache list
250 * @param $cacheList The new cache list to set or null for initialization/reset
253 private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
254 $this->cacheList = $cacheList;
258 * Setter for cache conditions
260 * @param $cacheCond The new cache conditions to set
263 private final function setCacheCond ($cacheCond) {
264 $this->cacheCond = (string) $cacheCond;
272 public function resetCache () {
273 $this->setAllCacheList(null);
274 $this->setCacheCond("");
278 * Getter for surname. If no surname is set then default surnames are set
279 * for male and female personells.
281 * @return $surname The personell' surname
283 public final function getSurname () {
284 $surname = parent::getSurname();
286 // Make sure every one has a surname...
287 if (empty($surname)) {
288 if ($this->isMale()) {
292 // Typical female name
293 $surname = "Jennifer";
296 // Set typical family name
297 parent::setFamily("Smith");
305 * Getter for personell list
307 * @return $personellList The list of all personells
309 public final function getPersonellList () {
310 return $this->personellList;
314 * Loads the mostly pre-cached personell list
316 * @param $idNumber The ID number we shall use for looking up
321 public function loadPersonellList ($idNumber) {
322 $this->makeDeprecated();