File indexing completed on 2024-04-28 11:49:39
0001 // Copyright (c) 2002-2003 Rob Kaper <cap@capsi.com> 0002 // 0003 // This library is free software; you can redistribute it and/or 0004 // modify it under the terms of the GNU Lesser General Public 0005 // License version 2.1 as published by the Free Software Foundation. 0006 // 0007 // This library is distributed in the hope that it will be useful, 0008 // but WITHOUT ANY WARRANTY; without even the implied warranty of 0009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0010 // Lesser General Public License for more details. 0011 // 0012 // You should have received a copy of the GNU Lesser General Public License 0013 // along with this library; see the file COPYING.LIB. If not, write to 0014 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0015 // Boston, MA 02110-1301, USA. 0016 0017 #include "trade.h" 0018 #include "player.h" 0019 #include "estate.h" 0020 #include "card.h" 0021 0022 #include <algorithm> 0023 0024 Trade::Trade(int tradeId) 0025 : QObject() 0026 , m_changed(false) 0027 , m_rejected(false) 0028 , m_tradeId(tradeId) 0029 , m_revision(0) 0030 { 0031 } 0032 0033 void Trade::setRevision(int revision) 0034 { 0035 m_revision = revision; 0036 } 0037 0038 int Trade::revision() const 0039 { 0040 return m_revision; 0041 } 0042 0043 void Trade::addPlayer(Player *player) 0044 { 0045 m_playerAcceptMap[player] = false; 0046 Q_EMIT playerAdded(player); 0047 } 0048 0049 void Trade::removePlayer(Player *player) 0050 { 0051 m_playerAcceptMap[player] = false; 0052 Q_EMIT playerRemoved(player); 0053 } 0054 0055 unsigned int Trade::count( bool acceptOnly ) const 0056 { 0057 return acceptOnly 0058 ? std::count(m_playerAcceptMap.constBegin(), m_playerAcceptMap.constEnd(), true) 0059 : m_playerAcceptMap.count(); 0060 } 0061 0062 QList<Player *> Trade::participants() const 0063 { 0064 return m_playerAcceptMap.keys(); 0065 } 0066 0067 void Trade::updateEstate(Estate *estate, Player *to) 0068 { 0069 TradeEstate *t=nullptr; 0070 0071 foreach (TradeItem *i, mTradeItems) 0072 { 0073 t=dynamic_cast<TradeEstate*>(i); 0074 0075 if (!t) 0076 continue; 0077 0078 if (t->estate()==estate) 0079 break; 0080 0081 t=nullptr; 0082 } 0083 if (t) 0084 { 0085 if (to) 0086 { 0087 if (t->to() == to) 0088 return; 0089 t->setTo(to); 0090 } 0091 else 0092 { 0093 mTradeItems.removeOne(t); 0094 Q_EMIT itemRemoved(t); 0095 t->deleteLater(); 0096 } 0097 } 0098 else if (estate && to) 0099 { 0100 // new trade 0101 t = new TradeEstate(estate, this, to); 0102 0103 mTradeItems.append(t); 0104 Q_EMIT itemAdded(t); 0105 } 0106 } 0107 0108 void Trade::updateMoney(unsigned int money, Player *from, Player *to) 0109 { 0110 TradeMoney *t=nullptr; 0111 0112 foreach (TradeItem *i, mTradeItems) 0113 { 0114 t=dynamic_cast<TradeMoney*>(i); 0115 0116 if (!t) 0117 continue; 0118 0119 if (t->from() == from && t->to() == to && t->money()) 0120 break; 0121 0122 t=nullptr; 0123 } 0124 if (t) 0125 { 0126 if (from && to && money) 0127 { 0128 if (t->money() == money) 0129 return; 0130 t->setMoney(money); 0131 } 0132 else 0133 { 0134 mTradeItems.removeOne(t); 0135 Q_EMIT itemRemoved(t); 0136 t->deleteLater(); 0137 } 0138 } 0139 else if (from && to && money) 0140 { 0141 // new trade 0142 t = new TradeMoney(money, this, from, to); 0143 0144 mTradeItems.append(t); 0145 Q_EMIT itemAdded(t); 0146 } 0147 } 0148 0149 void Trade::updateCard(Card *card, Player *to) 0150 { 0151 TradeCard *t=nullptr; 0152 0153 foreach (TradeItem *i, mTradeItems) 0154 { 0155 t=dynamic_cast<TradeCard*>(i); 0156 0157 if (!t) 0158 continue; 0159 0160 if (t->card()==card) 0161 break; 0162 0163 t=nullptr; 0164 } 0165 if (t) 0166 { 0167 if (to) 0168 { 0169 if (t->to() == to) 0170 return; 0171 t->setTo(to); 0172 } 0173 else 0174 { 0175 mTradeItems.removeOne(t); 0176 Q_EMIT itemRemoved(t); 0177 t->deleteLater(); 0178 } 0179 } 0180 else if (card && to) 0181 { 0182 // new trade 0183 t = new TradeCard(card, this, to); 0184 0185 mTradeItems.append(t); 0186 Q_EMIT itemAdded(t); 0187 } 0188 } 0189 0190 void Trade::updateAccept(Player *player, bool accept) 0191 { 0192 const bool hadItem = m_playerAcceptMap.contains(player); 0193 if (m_playerAcceptMap[player] != accept) 0194 { 0195 m_playerAcceptMap[player] = accept; 0196 Q_EMIT acceptChanged(player, accept); 0197 m_changed = true; 0198 } else if (!hadItem) 0199 Q_EMIT acceptChanged(player, accept); 0200 } 0201 0202 void Trade::reject(Player *player) 0203 { 0204 m_rejected = true; 0205 Q_EMIT rejected(player); 0206 } 0207 0208 void Trade::update(bool force) 0209 { 0210 if (m_changed || force) 0211 { 0212 Q_EMIT changed(this); 0213 m_changed = false; 0214 } 0215 } 0216 0217 TradeItem::TradeItem(Trade *trade, Player *from, Player *to) : mFrom(from), mTo(to), mTrade(trade) 0218 { 0219 connect(from, SIGNAL(changed(Player *)), this, SLOT(playerChanged())); 0220 connect(to, SIGNAL(changed(Player *)), this, SLOT(playerChanged())); 0221 } 0222 0223 void TradeItem::playerChanged() 0224 { 0225 Q_EMIT changed(this); 0226 } 0227 0228 TradeEstate::TradeEstate(Estate *estate, Trade *trade, Player *to) : TradeItem(trade, estate->owner(), to), mEstate(estate) 0229 { 0230 } 0231 0232 QString TradeEstate::text() const 0233 { 0234 return mEstate->name(); 0235 } 0236 0237 TradeMoney::TradeMoney(unsigned int money, Trade *trade, Player *from, Player *to) : TradeItem(trade, from, to), m_money(money) 0238 { 0239 } 0240 0241 void TradeMoney::setMoney(unsigned int money) 0242 { 0243 if (m_money != money) 0244 { 0245 m_money = money; 0246 Q_EMIT changed(this); 0247 } 0248 } 0249 0250 QString TradeMoney::text() const 0251 { 0252 return QStringLiteral("$%1").arg(m_money); 0253 } 0254 0255 TradeCard::TradeCard(Card *card, Trade *trade, Player *to) : TradeItem(trade, card->owner(), to), mCard(card) 0256 { 0257 } 0258 0259 QString TradeCard::text() const 0260 { 0261 return mCard->title(); 0262 } 0263 0264 #include "moc_trade.cpp"