-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamemanager.cpp
192 lines (145 loc) · 5.43 KB
/
gamemanager.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "gamemanager.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
GameManager::GameManager(QObject *parent) : QObject(parent), m_gameData(),
m_accountManager(m_gameData),m_campaignManager(m_gameData), m_positionLogger(m_gameData), m_playerModel(m_gameData), m_messageModel(m_gameData), m_geoLocalItemsModel(m_gameData)
{
m_gameTimer = new QTimer(this);
connect( m_gameTimer, SIGNAL( timeout()), this, SLOT( play()));
connect( &m_positionLogger, SIGNAL(directionChanged()), this, SIGNAL(directionChanged()) );
connect( &m_positionLogger, SIGNAL(positionChanged()), this, SIGNAL(lastPositionChanged()) );
}
GameManager::~GameManager()
{
}
bool GameManager::startGame(uint idCampaign)
{
m_campaignManager.joinCampaign( idCampaign, m_accountManager.accountId());
m_campaignManager.setCurrentCampaign( idCampaign);
//Remove unprocessed events
//Read events
QString str = QString("DELETE FROM `simplisim_dejavu`.`geo_events` WHERE ( \
`EMITTER` =%1 OR `RECIPIENT` =%1 ) AND `PROCESSED` =0").arg( m_gameData.accountId() );
QSqlQuery query(str);
query.exec();
m_positionLogger.start();
//Notify the others players using the intercom
m_messageModel.sendSystemMessage( m_gameData.accountName() + " joined the campaign");
m_gameTimer->start(3000);
m_playerModel.resetModel();
m_messageModel.resetModel();
m_geoLocalItemsModel.resetModel(); //QTimer::singleShot(1000, &m_geoLocalItemsModel, SLOT(resetModel()) );
emit campaignNameChanged();
emit gameStarted();
return true;
}
bool GameManager::stopGame()
{
//Notify the others players using the intercom
m_messageModel.sendSystemMessage( m_gameData.accountName() + " left the campaign");
m_campaignManager.leaveCampaign(m_gameData.campaignId(), m_accountManager.accountId());
m_campaignManager.setCurrentCampaign( 0);
m_positionLogger.stop();
m_gameTimer->stop();
emit campaignNameChanged();
emit gameStopped();
return true;
}
void GameManager::play()
{
static long nbTic = 0;
nbTic++;
//update players model
m_playerModel.resetModel();//m_playerModel.updateModel(); //bug Qt with dataSignal for MapViewItem
if (nbTic % 2 == 0)
m_messageModel.updateModel();
//if (nbTic > 10)
m_geoLocalItemsModel.checkForEnergyAround();
//Read events
QString str = QString("SELECT * FROM `geo_events` WHERE `RECIPIENT` IN (%1,0) AND `PROCESSED` = 0")
.arg( m_gameData.accountId() );
QSqlQuery query(str);
std::vector<int> lstEventId;
while (query.next())
{
int id = query.value(0).toInt();
lstEventId.push_back( id);
int type = query.value(1).toInt();
int emitter = query.value(2).toInt();
QString options = query.value(4).toString();
processEvent(type, emitter, options);
}
//Set the processed flag to the read event
aknowledgeEvent( lstEventId);
//TODO : handle the case of the broadcast event (timeout)
}
void GameManager::processEvent(int type, int emitter, QString option )
{
Q_UNUSED(option)
//retrieve emitter data
PlayerData emitterData = m_playerModel.getPlayerData( emitter);
if (emitterData.ID != 0)
{
switch (type)
{
case 1: //SHOOT (by someone)
shoot( emitter, false);
emit killedBy( emitterData.NamePlayer );
break;
case 2: //(someone has been) KILLED (by us)
emit killed( emitterData.NamePlayer );
break;
}
}
}
void GameManager::aknowledgeEvent(const std::vector<int>& lstEventId)
{
QString sLstEventId ="";
int i=0;
for (auto id:lstEventId)
{
if ( i>0 )
sLstEventId += QString(",%1").arg(id);
else sLstEventId += QString("%1").arg(id);
i++;
}
//Read events
QString str = QString("UPDATE `simplisim_dejavu`.`geo_events` SET `PROCESSED` = '1',`ProcessedTime` = NOW( ) WHERE `geo_events`.`ID` IN(%1);")
.arg( sLstEventId);
QSqlQuery query(str);
}
bool GameManager::saveStuffItem(int idStuff, int idQty, bool isSelected)
{
QString str = QString("replace INTO `simplisim_dejavu`.`geo_stuff` ( `ID_PLAYER` ,`ID_STUFF` , `QTY` , `SELECTED`) \
VALUES ('%1', '%2', '%3', '%4' );")
.arg( m_gameData.accountId() ).arg(idStuff).arg( idQty ).arg(isSelected ? 1 : 0);
QSqlQuery query;
return query.exec( str );
}
QList<int> GameManager::getStuffItemData( int IdStuff){
QList<int> stuffData = QList<int>();
int IsSelected = -1;
int Qty = -1;
//Retrieve stuffitem from db
QString str = QString("SELECT * FROM `geo_stuff` WHERE `ID_PLAYER` = %1 AND `ID_STUFF` = %2")
.arg( m_gameData.accountId() ).arg( IdStuff);
QSqlQuery query(str);
if (query.next())
{
Qty= query.value(2).toInt();
IsSelected = query.value(3).toInt();
}
stuffData.append(Qty);
stuffData.append(IsSelected);
return stuffData;
}
bool GameManager::shoot(int idTarget, bool shoot)
{
int action = 1;//SHOOT
if (!shoot) action = 2;//"KILLED";
QString str = QString("INSERT INTO `simplisim_dejavu`.`geo_events` (`ID` ,`TYPE`, `EMITTER` ,`RECIPIENT` ,`OPTIONS` ,`PROCESSED`) VALUES ( NULL , '%1', '%2', '%3', '', '0')")
.arg( action).arg( m_gameData.accountId() ).arg( idTarget);
QSqlQuery query;
return query.exec( str );
}