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 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
40 $this->setObjectDescription("Händler");
42 // Generate unique ID number
43 $this->generateUniqueId();
46 $this->removeSystemArray();
49 // Haendler mit Namen erzeugen
50 public final static function createMerchant ($merchantName, Harbor $harborInstance) {
52 $merchantName = (string) $merchantName;
55 $merchantInstance = new Merchant();
58 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) {
59 $merchantInstance->getDebugInstance()->output(sprintf("[%s:%d] Ein Händler <strong>%s</strong> wird angelegt und soll sich am <strong>%s</strong> niederlassen.",
63 $harborInstance->getHarborName()
67 // Haendlernamen setzen
68 $merchantInstance->setMerchantName($merchantName);
70 // In dem angegebenen Hafen den Haendler ansiedeln
71 $merchantInstance->setHarborInstance($harborInstance);
73 // Preisliste initialisieren
74 $merchantInstance->createPriceList();
76 // Instanz zurueckliefern
77 return $merchantInstance;
80 // Initialize pricing list
81 private function createPriceList () {
82 $this->priceList = new FrameworkArrayObject("FakedPriceList");
85 // Setter for merchant name
86 public final function setMerchantName ($merchantName) {
88 $this->merchantName = (string) $merchantName;
91 // Getter for merchant name
92 public final function getMerchantName () {
93 return $this->merchantName;
96 // Setter for harbor instance
97 public final function setHarborInstance (Harbor $harborInstance) {
98 $this->harborInstance = $harborInstance;
101 // Getter for harbor instance
102 public final function getHarborInstance () {
103 return $this->harborInstance;
106 // Add new item to merchant's price list
107 public function addItemToPriceList (TradeableItem $itemInstance, $price) {
109 $price = (float) $price;
112 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.",
115 $this->getMerchantName(),
116 $itemInstance->__toString(),
117 $itemInstance->getObjectDescription(),
118 $this->formatCurrency($price)
121 // Construct pricing item and add it to the list
122 $this->priceList->append(array(
123 'item' => $itemInstance,
127 // Remove price attribute
128 $itemInstance->removePrice();
131 // Get a price from the merchant's list
132 public final function getPriceFromList (TradeableItem $itemInstance) {
135 // Iterate throw whole list
136 for ($iter = $this->priceList->getIterator(); $iter->valid(); $iter->next()) {
138 $item = $iter->current();
140 // Does this item match? The unique ID may not work...
141 if ($item['item']->itemMatches($itemInstance)) {
142 // Extract price and stop searching
143 $price = $item['price'];
148 // Was the item found?
151 throw new ItemNotInPriceListException($itemInstance, self::EXCEPTION_ITEM_NOT_IN_PRICE_LIST);