]> git.mxchange.org Git - shipsimu.git/blob - application/ship-simu/main/class_Merchant.php
b0748e7fc890f15118506f73624758fb12523b4f
[shipsimu.git] / application / ship-simu / main / class_Merchant.php
1 <?php
2 /**
3  * A class for merchants which can trade items
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class Merchant extends BaseFrameworkSystem {
25         // Name des Haendlers
26         private $merchantName   = "Namenloser H&auml;ndler";
27
28         // Preislite (Objekte wiedermal!)
29         private $priceList      = null;
30
31         // Zugewiesener Hafen
32         private $harborInstance = null;
33
34         // Konstruktor
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38
39                 // Clean up a little
40                 $this->removeSystemArray();
41         }
42
43         // Haendler mit Namen erzeugen
44         public final static function createMerchant ($merchantName, Harbor $harborInstance) {
45                 // String absichern
46                 $merchantName = (string) $merchantName;
47
48                 // Get new instance
49                 $merchantInstance = new Merchant();
50
51                 // Debug message
52                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) {
53                         $merchantInstance->debugOutput(sprintf("[%s:%d] Ein H&auml;ndler <strong>%s</strong> wird angelegt und soll sich am <strong>%s</strong> niederlassen.",
54                                 __CLASS__,
55                                 __LINE__,
56                                 $merchantName,
57                                 $harborInstance->getHarborName()
58                         ));
59                 }
60
61                 // Haendlernamen setzen
62                 $merchantInstance->setMerchantName($merchantName);
63
64                 // In dem angegebenen Hafen den Haendler ansiedeln
65                 $merchantInstance->setHarborInstance($harborInstance);
66
67                 // Preisliste initialisieren
68                 $merchantInstance->createPriceList();
69
70                 // Instanz zurueckliefern
71                 return $merchantInstance;
72         }
73
74         // Initialize pricing list
75         private function createPriceList () {
76                 $this->priceList = new FrameworkArrayObject("FakedPriceList");
77         }
78
79         // Setter for merchant name
80         public final function setMerchantName ($merchantName) {
81                 // Debug message
82                 $this->merchantName = (string) $merchantName;
83         }
84
85         // Getter for merchant name
86         public final function getMerchantName () {
87                 return $this->merchantName;
88         }
89
90         // Setter for harbor instance
91         public final function setHarborInstance (Harbor $harborInstance) {
92                 $this->harborInstance = $harborInstance;
93         }
94
95         // Getter for harbor instance
96         public final function getHarborInstance () {
97                 return $this->harborInstance;
98         }
99
100         // Add new item to merchant's price list
101         public function addItemToPriceList (TradeableItem $itemInstance, $price) {
102                 // Secure pricing
103                 $price = (float) $price;
104
105                 // Construct pricing item and add it to the list
106                 $this->priceList->append(array(
107                         'item'  => $itemInstance,
108                         'price' => $price
109                 ));
110
111                 // Remove price attribute
112                 $itemInstance->removePrice();
113         }
114
115         // Get a price from the merchant's list
116         public final function getPriceFromList (TradeableItem $itemInstance) {
117                 $price = 0;
118
119                 // Iterate throw whole list
120                 for ($iter = $this->priceList->getIterator(); $iter->valid(); $iter->next()) {
121                         // Get current item
122                         $item = $iter->current();
123
124                         // Does this item match? The unique ID may not work...
125                         if ($item['item']->itemMatches($itemInstance)) {
126                                 // Extract price and stop searching
127                                 $price = $item['price'];
128                                 break;
129                         }
130                 }
131
132                 // Was the item found?
133                 if ($price === 0) {
134                         // Throw exception
135                         throw new ItemNotInPriceListException($itemInstance, self::EXCEPTION_ITEM_NOT_IN_PRICE_LIST);
136                 }
137
138                 // Return price
139                 return $price;
140         }
141 }
142
143 // [EOF]
144 ?>