File indexing completed on 2024-05-12 05:39:51

0001 /*************************************************************************
0002  *     Copyright (C) 2011 by Joseph Boudou                               *
0003  *     Copyright (C) 2014 by Renaud Guezennec                            *
0004  *                                                                       *
0005  *     https://rolisteam.org/                                         *
0006  *                                                                       *
0007  *   Rolisteam is free software; you can redistribute it and/or modify   *
0008  *   it under the terms of the GNU General Public License as published   *
0009  *   by the Free Software Foundation; either version 2 of the License,   *
0010  *   or (at your option) any later version.                              *
0011  *                                                                       *
0012  *   This program is distributed in the hope that it will be useful,     *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0015  *   GNU General Public License for more details.                        *
0016  *                                                                       *
0017  *   You should have received a copy of the GNU General Public License   *
0018  *   along with this program; if not, write to the                       *
0019  *   Free Software Foundation, Inc.,                                     *
0020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0021  *************************************************************************/
0022 
0023 #ifndef PLAYERS_LIST_H
0024 #define PLAYERS_LIST_H
0025 
0026 //#include "data/player.h"
0027 #include <QAbstractItemModel>
0028 #include <QPointer>
0029 #include <core_global.h>
0030 #include <memory>
0031 class Character;
0032 class Player;
0033 class Person;
0034 /**
0035  * @brief PlayersList is a model of players and character. List of connected players and theyr characters
0036  * @note This class is NOT thread-safe.
0037  */
0038 class CORE_EXPORT PlayerModel : public QAbstractItemModel
0039 {
0040     Q_OBJECT
0041     Q_PROPERTY(QString gameMasterId READ gameMasterId NOTIFY gameMasterIdChanged)
0042     Q_PROPERTY(QString localPlayerId READ localPlayerId WRITE setLocalPlayerId NOTIFY localPlayerIdChanged)
0043 public:
0044     enum ItemDataRole
0045     {
0046         IdentifierRole= Qt::UserRole + 1,
0047         PersonPtrRole,
0048         NameRole,
0049         ColorRole,
0050         LocalRole,
0051         GmRole,
0052         CharacterRole,
0053         CharacterStateIdRole,
0054         NpcRole,
0055         AvatarRole
0056     };
0057 
0058     PlayerModel(QObject* parent= nullptr);
0059     virtual ~PlayerModel() override;
0060 
0061     ////////////////////////////////////
0062     // implements QAbstractItemModel
0063     ///////////////////////////////////
0064     QVariant data(const QModelIndex& index, int role) const override;
0065     Qt::ItemFlags flags(const QModelIndex& index) const override;
0066     QVariant headerData(int section, Qt::Orientation orientation, int role= Qt::DisplayRole) const override;
0067     QModelIndex index(int row, int column, const QModelIndex& parent= QModelIndex()) const override;
0068     QModelIndex parent(const QModelIndex& index) const override;
0069     int rowCount(const QModelIndex& parent= QModelIndex()) const override;
0070     int columnCount(const QModelIndex& parent= QModelIndex()) const override;
0071     bool setData(const QModelIndex& index, const QVariant& value, int role) override;
0072 
0073     Player* playerById(const QString& id) const;
0074     Person* personById(const QString& id) const;
0075     Character* characterById(const QString& id)const;
0076 
0077     QModelIndex personToIndex(Person* person) const;
0078 
0079     QString gameMasterId() const;
0080     QString localPlayerId() const;
0081     QHash<int, QByteArray> roleNames() const override;
0082 
0083 public slots:
0084     void clear();
0085     void addPlayer(Player* player);
0086     void removePlayer(Player* player);
0087     void addCharacter(const QModelIndex& parent, Character *character, int pos= -1);
0088     void removeCharacter(Character* character);
0089     void setLocalPlayerId(const QString& uuid);
0090 
0091 signals:
0092     void playerJoin(Player* player);
0093     void playerLeft(Player* player);
0094     void gameMasterIdChanged(const QString& gameMasterId);
0095     void localPlayerIdChanged(const QString& localId);
0096 
0097 private:
0098     void setGameMasterId(const QString& id);
0099 
0100 private:
0101     std::vector<std::unique_ptr<Player>> m_players;
0102     QString m_gameMasterId;
0103     QString m_localPlayerId;
0104 };
0105 
0106 #endif