3 * A class for merchants which can trade items
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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 Merchant extends BaseFrameworkSystem {
26 private $merchantName = "Namenloser Händler";
28 // Preislite (Objekte wiedermal!)
29 private $priceList = null;
32 private $harborInstance = null;
35 private function __construct () {
36 // Call parent constructor
37 parent::constructor(__CLASS__);
40 if (((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) {
41 $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
48 $this->setPartDescr("Händler");
50 // Generate unique ID number
51 $this->createUniqueID();
54 $this->removeSystemArray();
57 // Haendler mit Namen erzeugen
58 public final static function createMerchant ($merchantName, Harbor $harborInstance) {
60 $merchantName = (string) $merchantName;
63 $merchantInstance = new Merchant();
66 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) {
67 $merchantInstance->getDebugInstance()->output(sprintf("[%s:%d] Ein Händler <strong>%s</strong> wird angelegt und soll sich am <strong>%s</strong> niederlassen.<br />\n",
71 $harborInstance->getHarborName()
75 // Haendlernamen setzen
76 $merchantInstance->setMerchantName($merchantName);
78 // In dem angegebenen Hafen den Haendler ansiedeln
79 $merchantInstance->setHarborInstance($harborInstance);
81 // Preisliste initialisieren
82 $merchantInstance->createPriceList();
84 // Instanz zurueckliefern
85 return $merchantInstance;
88 // Initialize pricing list
89 private function createPriceList () {
90 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händler <strong>%s</strong> erhält eine leere Preisliste.<br />\n",
93 $this->getMerchantName()
95 $this->priceList = new FrameworkArrayObject();
98 // Setter for merchant name
99 public final function setMerchantName ($merchantName) {
101 $merchantName = (string) $merchantName;
104 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händlername <strong>%s</strong> ist nun bekannt.<br />\n",
109 $this->merchantName = $merchantName;
112 // Getter for merchant name
113 public final function getMerchantName () {
114 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händlername <strong>%s</strong> wird verlangt.<br />\n",
119 return $this->merchantName;
122 // Setter for harbor instance
123 public final function setHarborInstance (Harbor $harborInstance) {
124 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händler <strong>%s</strong> lässt sich am <strong>%s</strong> nieder.<br />\n",
127 $this->getMerchantName(),
128 $harborInstance->getHarborName()
130 $this->harborInstance = $harborInstance;
133 // Getter for harbor instance
134 public final function getHarborInstance () {
135 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händler <strong>%s</strong> hat sich am <strong>%s</strong> niedergelassen.<br />\n",
138 $this->getMerchantName(),
139 $harborInstance->getHarborName()
141 return $this->harborInstance;
144 // Add new item to merchant's price list
145 public function addItemToPriceList (TradeableItem $itemInstance, $price) {
147 $price = (float) $price;
150 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händler <strong>%s</strong> kann nun das Schiffsteil <strong>%s</strong> "<strong>%s</strong>" zu <strong>%s</strong> verkaufen.<br />\n",
153 $this->getMerchantName(),
154 $itemInstance->__toString(),
155 $itemInstance->getPartDescr(),
156 $this->formatCurrency($price)
159 // Construct pricing item and add it to the list
160 $this->priceList->append(array(
161 'item' => $itemInstance,
165 // Remove price attribute
166 $itemInstance->removePrice();
169 // Get a price from the merchant's list
170 public final function getPriceFromList (TradeableItem $itemInstance) {
173 // Iterate throw whole list
174 for ($iter = $this->priceList->getIterator(); $iter->valid(); $iter->next()) {
176 $item = $iter->current();
178 // Does this item match? The unique ID may not work...
179 if ($item['item']->itemMatches($itemInstance)) {
180 // Extract price and stop searching
181 $price = $item['price'];
186 // Was the item found?
189 throw new ItemNotInPriceListException($itemInstance, self::EXCEPTION_ITEM_NOT_IN_PRICE_LIST);
199 public function saveObjectToDatabase () {
200 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",