-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcampaignmanager.h
145 lines (102 loc) · 3.36 KB
/
campaignmanager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef CAMPAIGNMANAGER_H
#define CAMPAIGNMANAGER_H
#include "gamedata.h"
#include <QObject>
#include <QAbstractListModel>
class CampaignItem
{
public:
enum Type{
Highlander = 1,
RingOfDeath
} ;
CampaignItem(GameData& gameData) : m_gameData( gameData){}
CampaignItem( GameData& gameData, uint id, QString name, double centerLatitude, double centerLongitude,
uint radius, uint nbTeam, Type type, uint owner, uint nbPlayers, bool isConnected): m_gameData( gameData)
{
m_id = id;
m_name = name;
m_centerLatitude = centerLatitude;
m_centerLongitude = centerLongitude;
m_radius= radius;
m_nbTeam = nbTeam;
m_type = type;
m_owner = owner;
m_nbPlayers = nbPlayers;
m_isConnected = isConnected;
}
QVariant getRole(int role) const;
enum Roles {
RoleIdcampaign = Qt::UserRole + 1,
RoleName,
RoleType,
RoleNbTeam,
RoleNbPlayers,
RoleAmIConnected,
RoleIsMyCampaign
};
private:
GameData& m_gameData;
uint m_id = 0;
QString m_name = "";
double m_centerLatitude = 0.;
double m_centerLongitude = 0.;
uint m_radius= 1;
uint m_nbTeam = 0;
Type m_type;
uint m_owner = 0;
uint m_nbPlayers = 0;
bool m_isConnected = false;
};
class CampaignManager : public QAbstractListModel//: public QObject
{
Q_OBJECT
public:
explicit CampaignManager( GameData& gameData, QObject *parent = 0);
~CampaignManager();
CampaignManager& operator=( const CampaignManager& pm){
Q_UNUSED(pm);
return *this; //TODO remove this awful hack!
}
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
Q_INVOKABLE Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
Q_INVOKABLE int createCampaign(uint owner, CampaignItem::Type type, QString name, uint nbTeam);
Q_INVOKABLE bool modifyCampaign(uint id, uint owner, CampaignItem::Type type, QString name, uint nbTeam);
Q_INVOKABLE bool deleteCampaign(uint idcampaign);
Q_INVOKABLE uint joinCampaign(uint idcampaign, uint idPlayer);
Q_INVOKABLE uint leaveCampaign(uint idcampaign, uint idPlayer);
Q_INVOKABLE void resetModel();
void setCurrentCampaign( uint idCampaign);
//This is to be able to access a given CampaignItem from QML(editing a campaign)
Q_INVOKABLE QVariantMap get(int idCampaign) {
int row = rowFromId(idCampaign);
QHash<int,QByteArray> names = roleNames();
QHashIterator<int, QByteArray> i(names);
QVariantMap res;
const QModelIndex idx = index(row, 0);
while (i.hasNext()) {
i.next();
res[i.value()] = data( idx, i.key());
}
return res;
}
signals:
public slots:
protected:
QHash<int, QByteArray> roleNames() const;
private:
GameData& m_gameData;
QVariant findCampaignData(uint idCampaign, CampaignItem::Roles );
int rowFromId(uint id){
int row = 0;
for (auto cp :m_lstValues){
if (cp.getRole( CampaignItem::RoleIdcampaign) == id)
break;
row++;
}
return row;
}
std::vector<CampaignItem> m_lstValues = std::vector<CampaignItem>();
};
#endif // CAMPAIGNMANAGER_H