getter, setter are all final; several code clean-ups
[shipsimu.git] / application / ship-simu / main / ships / class_BaseShip.php
1 <?php
2 class BaseShip extends BaseSimulator {
3         // Name des Shipes
4         private $shipName   = "Unbekanntes Schiff";
5
6         // Anzahl Anker
7         private $numAnchor  = 0;
8
9         // Tiefgang in Meter
10         private $draught    = 0;
11
12         // Besatzung-Objekte
13         private $crewList   = null;
14
15         // Aufbauten-Objekte
16         private $structures = null;
17
18         // Namenloses Ship generieren
19         private function __construct($class) {
20                 // Call parent constructor
21                 parent::constructor($class);
22
23                 // Beim Schiff angelangt
24                 if (((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT')))
25                         $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
26                                 __CLASS__,
27                                 __LINE__
28                         ));
29
30                 // Bezeichnung setzen
31                 $this->setPartDescr("Schiff");
32
33                 // Array-Objekt generieren
34                 $this->createStructuresArray();
35
36                 // Instanz entfernen
37                 $this->removePartInstance();
38                 $this->removeNumberFormaters();
39         }
40
41         /**
42          * Calls the private constructor
43          *
44          * @param       $class  The class' name
45          * @return      void
46          */
47         public function constructor ($class) {
48                 $this->__construct($class);
49         }
50
51         // Array-Objekt anlegen
52         private function createStructuresArray () {
53                 $this->structures = new FrameworkArrayObject();
54         }
55
56         // Schiffsteil generieren (kann alles sein)
57         // buildInstance = Das was in das Schiffsteil evtl. eingebaut werden soll (null = kein besonderes Teil einbauen!)
58         // partClass = Das zu konstruierende Schiffsteil
59         public function createShipPart (ConstructableShipPart $buildInstance, $partClass) {
60                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> erh&auml;lt ein neues Schiffsteil (%s).<br />\n",
61                         __CLASS__,
62                         __LINE__,
63                         $this->getShipName(),
64                         $partClass
65                 ));
66
67                 // Ist die gewuenschte Klasse vorhanden?
68                 if (class_exists($partClass)) {
69                         // Befehl zusammenbauen
70                         $eval = sprintf("\$partInstance = %s::create%s();",
71                                 $partClass, $partClass
72                         );
73
74                         // Debug-Meldung ausgeben
75                         if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
76                                 __CLASS__,
77                                 __LINE__,
78                                 htmlentities($eval)
79                         ));
80
81                         // ... und ausfuehren
82                         eval($eval);
83                 } else {
84                         // Nicht vorhanden, dann Ausnahme werfen!
85                         throw new ClassNotFoundException($partClass, 0);
86                 }
87
88                 // Das Einbauen versuchen...
89                 try {
90                         $partInstance->addShipPartToShip($this, $buildInstance);
91                 } catch (MotorShipMismatchException $e) {
92                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat keinen Motor erhalten! Grund: <strong>%s</strong><br />\n",
93                                 __CLASS__,
94                                 __LINE__,
95                                 $this->getShipName(),
96                                 $e->getMessage()
97                         ));
98                         return false;
99                 } catch (RoomShipMismatchException $e) {
100                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat keinen Maschinenraum erhalten! Grund: <strong>%s</strong><br />\n",
101                                 __CLASS__,
102                                 __LINE__,
103                                 $this->getShipName(),
104                                 $e->getMessage()
105                         ));
106                         return false;
107
108                 } catch (StructureShipMismatchException $e) {
109                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat keine Aufbauten erhalten! Grund: <strong>%s</strong><br />\n",
110                                 __CLASS__,
111                                 __LINE__,
112                                 $this->getShipName(),
113                                 $e->getMessage()
114                         ));
115                         return false;
116                 } catch (CabinShipMismatchException $e) {
117                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat keine Kabine erhalten! Grund: <strong>%s</strong><br />\n",
118                                 __CLASS__,
119                                 __LINE__,
120                                 $this->getShipName(),
121                                 $e->getMessage()
122                         ));
123                         return false;
124                 } catch (DeckShipMismatchException $e) {
125                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat kein Deck erhalten! Grund: <strong>%s</strong><br />\n",
126                                 __CLASS__,
127                                 __LINE__,
128                                 $this->getShipName(),
129                                 $e->getMessage()
130                         ));
131                         return false;
132                 } catch (ExceptionNotChangedException $e) {
133                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Eine Exception wurde nicht ge&auml;ndert. Details: <strong>%s</strong><br />\n",
134                                 __CLASS__,
135                                 __LINE__,
136                                 $e->getMessage()
137                         ));
138                         return false;
139                 } catch (ExceptionNotFoundException $e) {
140                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Eine Exception wurde nicht gefunden. Details: <strong>%s</strong><br />\n",
141                                 __CLASS__,
142                                 __LINE__,
143                                 $e->getMessage()
144                         ));
145                         return false;
146                 }
147
148                 // Instanz im Aufbauten-Array vermerken
149                 $this->structures->append($partInstance);
150
151                 // Debug-Meldung ausgeben
152                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat das Schiffsteil <strong>%s</strong> eingebaut bekommen.<br />\n",
153                         __CLASS__,
154                         __LINE__,
155                         $this->getShipName(),
156                         $partInstance->getPartDescr()
157                 ));
158
159                 // Alles klar!
160                 return true;
161         }
162
163         // Getter-Methode fuer Strukturen-Array
164         public final function getStructuresArray () {
165                 return $this->structures;
166         }
167
168         // STUB: Getter-Methode Anzahl Betten
169         public function calcTotalBeds () {
170                 $this->getDebugInstance()->output("[%s:%d] Stub! Anzahl Betten erreicht.<br />\n");
171                 return 0;
172         }
173
174         // Setter-Methode fuer Schiffsnamen
175         public final function setShipName ($shipName) {
176                 // Cast the string
177                 $shipName = (string) $shipName;
178
179                 // Debug message
180                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das neue Schiff vom Typ <strong>%s</strong> wird auf den Namen <strong>%s</strong> getauft.<br />\n",
181                         __CLASS__,
182                         __LINE__,
183                         $this->__toString(),
184                         $shipName
185                 ));
186
187                 // Set ship name
188                 $this->shipName = $shipName;
189         }
190
191         // Getter-Methode fuer Schiffsnamen
192         public final function getShipName () {
193                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das <strong>%s</strong> ist auf den Namen <strong>%s</strong> getauft worden.<br />\n",
194                         __CLASS__,
195                         __LINE__,
196                         $this->__toString(),
197                         $this->shipName
198                 ));
199                 return $this->shipName;
200         }
201
202         // Setter-Methode fuer Tiefgang
203         public final function setDraught ($draught) {
204                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das <strong>%s</strong> mit dem Namen <strong>%s</strong> hat einen Tiefgang von <strong>%sm</strong>.<br />\n",
205                         __CLASS__,
206                         __LINE__,
207                         $this->__toString(),
208                         $this->shipName,
209                         $draught
210                 ));
211                 $this->draught = (int) $draught;
212         }
213
214         // Getter-Methode fuer Tiefgang
215         public final function getDraught() {
216                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Tiefgang des Schiffes <strong>%s</strong> wurde angefordert.<br />\n",
217                         __CLASS__,
218                         __LINE__,
219                         $this->shipName
220                 ));
221                 return $this->draught;
222         }
223
224         // Setter-Methode fuer Anzahl Anker
225         public final function setNumAnchor ($numAnchor) {
226                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das <strong>%s</strong> mit dem Namen <strong>%s</strong> hat <strong>%s</strong> Anker.<br />\n",
227                         __CLASS__,
228                         __LINE__,
229                         $this->__toString(),
230                         $this->shipName,
231                         $numAnchor
232                 ));
233                 $this->numAnchor = (int) $numAnchor;
234         }
235 }
236
237 // [EOF]
238 ?>