-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayermodel.h
169 lines (141 loc) · 4.6 KB
/
playermodel.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef PLAYERMODEL_H
#define PLAYERMODEL_H
#include "gamedata.h"
#include <QObject>
#include <QAbstractListModel>
#include <QGeoPositionInfoSource>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
struct PlayerData
{
PlayerData(){ }
PlayerData(uint _ID, QString _Name, int _Distance, int _Azimuth, int heading, int type, double lat, double lng,
uint _Health, uint _Energy, uint _Xp, uint _Credits, uint _Level){
ID = _ID;
Distance = _Distance;
Azimuth = _Azimuth;
Bearing = heading;
Type = type;
if (ID == 5 || ID == 6 || ID == 7) //Aka Bot1, Bot2, Bot3
Type = 0;
Latitude = lat;
Longitude = lng;
NamePlayer = _Name;
Health = _Health;
Energy = _Energy;
Xp = _Xp;
Credits = _Credits;
Level = _Level;
}
uint ID = 0;
int Distance = -1;
int Bearing = -1;
int Type = 0;
int Azimuth = -1;
double Latitude = 0.;
double Longitude = 0.;
QString NamePlayer = "Invalid";
uint Health = 0.;
uint Energy = 0.;
uint Xp = 0.;
uint Credits = 0.;
uint Level = 0.;
QVariant getRole(int role) const;
enum Roles {
RoleId = Qt::UserRole + 1,
RoleDistance,
RoleBearing,
RoleType,
RoleAzimuth,
RoleLatitude,
RoleLongitude,
RoleNamePlayer,
RoleHealth,
RoleEnergy,
RoleXP,
RoleCredits,
RoleLevel
};
};
class PlayerModel : public QAbstractListModel
{
Q_OBJECT
public:
PlayerModel(GameData& gameData);
PlayerModel& operator=( const PlayerModel& pm){
Q_UNUSED(pm);
//FIXME
return *this;
}
~PlayerModel();
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 void changeCharbyAzimuth(float azim)
{
QModelIndex inCd = indexFromIdPlayer(1);
setData( inCd, azim, PlayerData::RoleAzimuth);
emit dataChanged(inCd, inCd);
}
virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
//bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());
void resetModel();
void updateModel();
//void setMyCurrentPos( QGeoPositionInfo pos);
//void setMyId( uint id);
int maxDistance(){ return m_maxDistance;}
Q_INVOKABLE QList<int> getMyData(){
QList<int> myData = QList<int>();
PlayerData pdata = getPlayerData( m_gameData.accountId());
myData.append(pdata.Health);
myData.append(pdata.Xp);
myData.append(pdata.Credits);
myData.append(pdata.Energy);
myData.append(pdata.Level);
return myData;
}
Q_INVOKABLE void updateMyData( uint health, uint xp, uint credits, uint energy, uint level ){
QString str = QString("UPDATE `simplisim_dejavu`.`geo_players` SET `HEALTH` = '%1',\
`XP` = '%2', \
`CREDITS` = '%3', \
`ENERGY` = '%4', \
`LEVEL` = '%5' WHERE `geo_players`.`ID_ACCOUNT` =%6;")
.arg( health)
.arg( xp)
.arg( credits)
.arg( energy)
.arg( level)
.arg( m_gameData.accountId());
QSqlQuery query;
if ( !query.exec( str ) )
{
qDebug() << "Error with query : "<< str;
qDebug() << "last error : " << query.lastError();
}
}
PlayerData getPlayerData(int PlayerID){
QModelIndex index = indexFromIdPlayer( PlayerID );
if (index.isValid() && index.row() < m_lstValues.size())
return m_lstValues.at( index.row());
else
{
return PlayerData();
}
}
signals:
void newPlayer(QString playerName);
void playerLeave(QString playerName);
//void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles = QVector<int> ());
protected:
QHash<int, QByteArray> roleNames() const;
private:
GameData& m_gameData;
QVector< PlayerData> m_lstValues ;
QModelIndex indexFromIdPlayer(int);
void purgePlayers(const std::vector<int>& lstPlayersIdToKeep);
QGeoPositionInfo m_myPosition = QGeoPositionInfo();
int m_maxDistance =-1;
};
#endif // PLAYERMODEL_H