File indexing completed on 2024-04-28 04:01:30

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 #ifndef LIBATLANTIC_TRADE_H
0018 #define LIBATLANTIC_TRADE_H
0019 
0020 #include <QObject>
0021 #include <QMap>
0022 #include <QList>
0023 
0024 #include "libatlantic_export.h"
0025 
0026 class Player;
0027 class Trade;
0028 class Estate;
0029 class Card;
0030 
0031 class LIBATLANTIC_EXPORT TradeItem : public QObject
0032 {
0033 Q_OBJECT
0034 
0035 public:
0036     TradeItem(Trade *trade, Player *from, Player *to);
0037     virtual ~TradeItem() { }
0038     
0039     Player *from() const { return mFrom; }
0040     Player *to() const { return mTo; }
0041     void setTo(Player *p) { mTo=p; }
0042     Trade *trade() const { return mTrade; }
0043 
0044     /**
0045      * how to visualize this
0046      **/
0047     virtual QString text() const=0;
0048 
0049 Q_SIGNALS:
0050     void changed(TradeItem *);
0051 
0052 private Q_SLOTS:
0053     void playerChanged();
0054 
0055 private:
0056     Player *mFrom, *mTo;
0057     Trade *mTrade;
0058 };
0059 
0060 class LIBATLANTIC_EXPORT TradeEstate : public TradeItem
0061 {
0062 Q_OBJECT
0063 
0064 public:
0065     TradeEstate(Estate *estate, Trade *trade, Player *to);
0066 
0067     Estate *estate() const { return mEstate; }
0068     
0069     QString text() const override;
0070 
0071 Q_SIGNALS:
0072     void updateEstate(Trade *trade, Estate *estate, Player *player);
0073     void updateMoney(Trade *trade, unsigned int money, Player *from, Player *to);
0074 
0075 private:
0076     Estate *mEstate;
0077 };
0078 
0079 class LIBATLANTIC_EXPORT TradeMoney : public TradeItem
0080 {
0081 Q_OBJECT
0082 
0083 public:
0084     TradeMoney(unsigned int money, Trade *trade, Player *from, Player *to);
0085 
0086     unsigned int money() const { return m_money; }
0087     void setMoney(unsigned int money);
0088     
0089     QString text() const override;
0090 
0091 Q_SIGNALS:
0092     void changed(TradeItem *tradeItem);
0093 
0094 private:
0095     unsigned int m_money;
0096 };
0097 
0098 class LIBATLANTIC_EXPORT TradeCard : public TradeItem
0099 {
0100 Q_OBJECT
0101 
0102 public:
0103     TradeCard(Card *card, Trade *trade, Player *to);
0104 
0105     Card *card() const { return mCard; }
0106     
0107     QString text() const override;
0108 
0109 Q_SIGNALS:
0110 
0111 private:
0112     Card *mCard;
0113 };
0114 
0115 
0116 class LIBATLANTIC_EXPORT Trade : public QObject
0117 {
0118 Q_OBJECT
0119 
0120 public:
0121     Trade(int tradeId);
0122     int tradeId() const { return m_tradeId; }
0123 
0124     void setRevision(int revision);
0125     int revision() const;
0126 
0127     void addPlayer(Player *player);
0128     void removePlayer(Player *player);
0129     
0130     unsigned int count( bool acceptOnly ) const;
0131 
0132     bool isRejected() const { return m_rejected; }
0133 
0134     QList<Player *> participants() const;
0135 
0136 private Q_SLOTS:
0137     /**
0138      * tell someone that this changed
0139      **/
0140 //  void changed(TradeItem *i) { Q_EMIT itemChanged(i); }
0141 
0142 public:
0143     void update(bool force = false);
0144     void updateEstate(Estate *estate, Player *player);
0145     void updateMoney(unsigned int money, Player *from, Player *to);
0146     void updateCard(Card *card, Player *to);
0147     void updateAccept(Player *player, bool accept);
0148     void reject(Player *player);
0149     
0150 Q_SIGNALS:
0151     void changed(Trade *);
0152     void rejected(Player *player);
0153 
0154     void itemAdded(TradeItem *);
0155     void itemRemoved(TradeItem *);
0156 
0157     void updateEstate(Trade *trade, Estate *estate, Player *to);
0158     void updateMoney(Trade *trade, unsigned int money, Player *from, Player *to);
0159     void updateCard(Trade *trade, Card *card, Player *to);
0160     void acceptChanged(Player *player, bool accept);
0161     void reject(Trade *trade);
0162     void accept(Trade *trade);
0163 
0164     void playerAdded(Player *player);
0165     void playerRemoved(Player *player);
0166 
0167 private:
0168     bool m_changed, m_rejected;
0169     int m_tradeId, m_revision;
0170 
0171     QMap<Player *, bool> m_playerAcceptMap;
0172     
0173     QList<TradeItem *> mTradeItems;
0174 };
0175 
0176 #endif