From c90c389a8a8d9d8661e9772ec4144c5cf2039f23 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdegames@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- atlantik/libatlantic/trade.cpp | 200 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 atlantik/libatlantic/trade.cpp (limited to 'atlantik/libatlantic/trade.cpp') diff --git a/atlantik/libatlantic/trade.cpp b/atlantik/libatlantic/trade.cpp new file mode 100644 index 00000000..b516dc70 --- /dev/null +++ b/atlantik/libatlantic/trade.cpp @@ -0,0 +1,200 @@ +// Copyright (c) 2002-2003 Rob Kaper +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License version 2.1 as published by the Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this library; see the file COPYING.LIB. If not, write to +// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +// Boston, MA 02110-1301, USA. + +#include "trade.h" +#include "trade.moc" +#include "player.h" +#include "estate.h" + +Trade::Trade(int tradeId) +{ + m_tradeId = tradeId; + m_revision = 0; + m_changed = m_rejected = false; +} + +void Trade::setRevision(int revision) +{ + m_revision = revision; +} + +int Trade::revision() const +{ + return m_revision; +} + +void Trade::addPlayer(Player *player) +{ + m_playerAcceptMap[player] = false; +} + +void Trade::removePlayer(Player *player) +{ + m_playerAcceptMap[player] = false; +} + +unsigned int Trade::count( bool acceptOnly ) +{ + unsigned int count = 0; + for (QMapIterator it = m_playerAcceptMap.begin() ; it != m_playerAcceptMap.end() ; ++it) + if ( !acceptOnly || it.data() ) + count++; + return count; +} + +void Trade::updateEstate(Estate *estate, Player *to) +{ + TradeEstate *t=0; + + for (QPtrListIterator i(mTradeItems); *i; ++i) + { + t=dynamic_cast(*i); + + if (!t) + continue; + + if (t->estate()==estate) + break; + + t=0; + } + if (t) + { + if (to) + { + if (t->to() == to) + return; + t->setTo(to); + } + else + { + mTradeItems.removeRef(t); + emit itemRemoved(t); + t->deleteLater(); + } + } + else if (estate && to) + { + // new trade + t = new TradeEstate(estate, this, to); + + mTradeItems.append(t); + emit itemAdded(t); + } +} + +void Trade::updateMoney(unsigned int money, Player *from, Player *to) +{ + TradeMoney *t=0; + + for (QPtrListIterator i(mTradeItems); *i; ++i) + { + t=dynamic_cast(*i); + + if (!t) + continue; + + if (t->from() == from && t->to() == to && t->money()) + break; + + t=0; + } + if (t) + { + if (from && to && money) + { + if (t->money() == money) + return; + t->setMoney(money); + } + else + { + mTradeItems.removeRef(t); + emit itemRemoved(t); + t->deleteLater(); + } + } + else if (from && to && money) + { + // new trade + t = new TradeMoney(money, this, from, to); + + mTradeItems.append(t); + emit itemAdded(t); + } +} + +void Trade::updateAccept(Player *player, bool accept) +{ + if (m_playerAcceptMap[player] != accept) + { + m_playerAcceptMap[player] = accept; + m_changed = true; + } +} + +void Trade::reject(Player *player) +{ + m_rejected = true; + emit rejected(player); +} + +void Trade::update(bool force) +{ + if (m_changed || force) + { + emit changed(this); + m_changed = false; + } +} + +TradeItem::TradeItem(Trade *trade, Player *from, Player *to) : mFrom(from), mTo(to), mTrade(trade) +{ + connect(from, SIGNAL(changed(Player *)), this, SLOT(playerChanged())); + connect(to, SIGNAL(changed(Player *)), this, SLOT(playerChanged())); +} + +void TradeItem::playerChanged() +{ + emit changed(this); +} + +TradeEstate::TradeEstate(Estate *estate, Trade *trade, Player *to) : TradeItem(trade, estate->owner(), to), mEstate(estate) +{ +} + +QString TradeEstate::text() const +{ + return mEstate->name(); +} + +TradeMoney::TradeMoney(unsigned int money, Trade *trade, Player *from, Player *to) : TradeItem(trade, from, to), m_money(money) +{ +} + +void TradeMoney::setMoney(unsigned int money) +{ + if (m_money != money) + { + m_money = money; + emit changed(this); + } +} + +QString TradeMoney::text() const +{ + return QString("$%1").arg(m_money); +} -- cgit v1.2.1