/
SqlObjDataSource.cpp
330 lines (280 loc) · 11.1 KB
/
SqlObjDataSource.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (C) 2009-2012 Rajko Stojadinovic <http://github.com/rajkosto/hive>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "SqlObjDataSource.h"
#include "Database/Database.h"
#include <inttypes.h>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;
using boost::bad_lexical_cast;
namespace
{
typedef boost::optional<Sqf::Value> PositionInfo;
class PositionFixerVisitor : public boost::static_visitor<PositionInfo>
{
public:
PositionInfo operator()(Sqf::Parameters& pos) const
{
if (pos.size() != 3)
return PositionInfo();
try
{
double x = Sqf::GetDouble(pos[0]);
double y = Sqf::GetDouble(pos[1]);
double z = Sqf::GetDouble(pos[2]);
if (x < 0 || y > 15360)
{
PositionInfo fixed(pos);
pos.clear();
return fixed;
}
}
catch(const boost::bad_get&) {}
return PositionInfo();
}
template<typename T> PositionInfo operator()(const T& other) const { return PositionInfo(); }
};
class WorldspaceFixerVisitor : public boost::static_visitor<PositionInfo>
{
public:
PositionInfo operator()(Sqf::Parameters& ws) const
{
if (ws.size() != 2)
return PositionInfo();
return boost::apply_visitor(PositionFixerVisitor(),ws[1]);
}
template<typename T> PositionInfo operator()(const T& other) const { return PositionInfo(); }
};
PositionInfo FixOOBWorldspace(Sqf::Value& v) { return boost::apply_visitor(WorldspaceFixerVisitor(),v); }
};
#include <Poco/Util/AbstractConfiguration.h>
SqlObjDataSource::SqlObjDataSource( Poco::Logger& logger, shared_ptr<Database> db, const Poco::Util::AbstractConfiguration* conf ) : SqlDataSource(logger,db)
{
static const string defaultTable = "Object_DATA";
if (conf != NULL)
{
_objTableName = getDB()->escape(conf->getString("Table",defaultTable));
_cleanupPlacedDays = conf->getInt("CleanupPlacedAfterDays",6);
_vehicleOOBReset = conf->getBool("ResetOOBVehicles",false);
}
else
{
_objTableName = defaultTable;
_cleanupPlacedDays = -1;
_vehicleOOBReset = false;
}
}
void SqlObjDataSource::populateObjects( int serverId, ServerObjectsQueue& queue )
{
if (_cleanupPlacedDays >= 0)
{
string commonSql = "FROM `"+_objTableName+"` WHERE `Instance` = " + lexical_cast<string>(serverId) +
" AND `ObjectUID` <> 0 AND `CharacterID` <> 0" +
" AND `Datestamp` < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL "+lexical_cast<string>(_cleanupPlacedDays)+" DAY)" +
" AND ( (`Inventory` IS NULL) OR (`Inventory` = '[]') )";
int numCleaned = 0;
{
auto numObjsToClean = getDB()->query(("SELECT COUNT(*) "+commonSql).c_str());
if (numObjsToClean && numObjsToClean->fetchRow())
numCleaned = numObjsToClean->at(0).getInt32();
}
if (numCleaned > 0)
{
_logger.information("Removing " + lexical_cast<string>(numCleaned) + " empty placed objects older than " + lexical_cast<string>(_cleanupPlacedDays) + " days");
auto stmt = getDB()->makeStatement(_stmtDeleteOldObject, "DELETE "+commonSql);
if (!stmt->directExecute())
_logger.error("Error executing placed objects cleanup statement");
}
}
auto worldObjsRes = getDB()->queryParams("SELECT `ObjectID`, `Classname`, `CharacterID`, `Worldspace`, `Inventory`, `Hitpoints`, `Fuel`, `Damage` FROM `%s` WHERE `Instance`=%d AND `Classname` IS NOT NULL", _objTableName.c_str(), serverId);
if (!worldObjsRes)
{
_logger.error("Failed to fetch objects from database");
return;
}
while (worldObjsRes->fetchRow())
{
auto row = worldObjsRes->fields();
Sqf::Parameters objParams;
objParams.push_back(string("OBJ"));
int objectId = row[0].getInt32();
objParams.push_back(lexical_cast<string>(objectId)); //objectId should be stringified
try
{
objParams.push_back(row[1].getString()); //classname
objParams.push_back(lexical_cast<string>(row[2].getInt32())); //ownerId should be stringified
Sqf::Value worldSpace = lexical_cast<Sqf::Value>(row[3].getString());
if (_vehicleOOBReset && row[2].getInt32() == 0) // no owner = vehicle
{
PositionInfo posInfo = FixOOBWorldspace(worldSpace);
if (posInfo.is_initialized())
_logger.information("Reset ObjectID " + lexical_cast<string>(objectId) + " (" + row[1].getString() + ") from position " + lexical_cast<string>(*posInfo));
}
objParams.push_back(worldSpace);
//Inventory can be NULL
{
string invStr = "[]";
if (!row[4].isNull())
invStr = row[4].getString();
objParams.push_back(lexical_cast<Sqf::Value>(invStr));
}
objParams.push_back(lexical_cast<Sqf::Value>(row[5].getCStr()));
objParams.push_back(row[6].getDouble());
objParams.push_back(row[7].getDouble());
}
catch (const bad_lexical_cast&)
{
_logger.error("Skipping ObjectID " + lexical_cast<string>(objectId) + " load because of invalid data in db");
continue;
}
queue.push(objParams);
}
}
void SqlObjDataSource::populateTraderObjects( int characterId, ServerObjectsQueue& queue )
{
auto worldObjsRes = getDB()->queryParams("SELECT `id`, `item`, `qty`, `buy`, `sell`, `order`, `tid`, `afile` FROM `Traders_DATA` WHERE `tid`=%d", characterId);
while (worldObjsRes->fetchRow())
{
auto row = worldObjsRes->fields();
Sqf::Parameters objParams;
objParams.push_back(row[0].getInt32());
// `item` varchar(255) NOT NULL COMMENT '[Class Name,1 = CfgMagazines | 2 = Vehicle | 3 = Weapon]',
// `qty` int(8) NOT NULL COMMENT 'amount in stock available to buy',
// `buy` varchar(255) NOT NULL COMMENT '[[Qty,Class,Type],]',
// `sell` varchar(255) NOT NULL COMMENT '[[Qty,Class,Type],]',
// `order` int(2) NOT NULL DEFAULT '0' COMMENT '# sort order for addAction menu',
// `tid` int(8) NOT NULL COMMENT 'Trader Menu ID',
// `afile` varchar(64) NOT NULL DEFAULT 'trade_items',
//get stuff from row
objParams.push_back(lexical_cast<Sqf::Value>(row[1].getString())); // item
objParams.push_back(row[2].getInt32()); // qty
objParams.push_back(lexical_cast<Sqf::Value>(row[3].getString())); // buy
objParams.push_back(lexical_cast<Sqf::Value>(row[4].getString())); // sell
objParams.push_back(row[5].getInt32()); // order
objParams.push_back(row[6].getInt32()); // tid
objParams.push_back(row[7].getString()); // afile
queue.push(objParams);
}
}
bool SqlObjDataSource::updateObjectInventory( int serverId, Int64 objectIdent, bool byUID, const Sqf::Value& inventory )
{
unique_ptr<SqlStatement> stmt;
if (byUID)
stmt = getDB()->makeStatement(_stmtUpdateObjectbyUID, "UPDATE `"+_objTableName+"` SET `Inventory` = ? WHERE `ObjectUID` = ? AND `Instance` = ?");
else
stmt = getDB()->makeStatement(_stmtUpdateObjectByID, "UPDATE `"+_objTableName+"` SET `Inventory` = ? WHERE `ObjectID` = ? AND `Instance` = ?");
stmt->addString(lexical_cast<string>(inventory));
stmt->addInt64(objectIdent);
stmt->addInt32(serverId);
bool exRes = stmt->execute();
poco_assert(exRes == true);
return exRes;
}
bool SqlObjDataSource::deleteObject( int serverId, Int64 objectIdent, bool byUID )
{
unique_ptr<SqlStatement> stmt;
if (byUID)
stmt = getDB()->makeStatement(_stmtDeleteObjectByUID, "DELETE FROM `"+_objTableName+"` WHERE `ObjectUID` = ? AND `Instance` = ?");
else
stmt = getDB()->makeStatement(_stmtDeleteObjectByID, "DELETE FROM `"+_objTableName+"` WHERE `ObjectID` = ? AND `Instance` = ?");
stmt->addInt64(objectIdent);
stmt->addInt32(serverId);
bool exRes = stmt->execute();
poco_assert(exRes == true);
return exRes;
}
bool SqlObjDataSource::updateDatestampObject( int serverId, Int64 objectIdent, bool byUID )
{
unique_ptr<SqlStatement> stmt;
if (byUID)
stmt = getDB()->makeStatement(_stmtUpdateDatestampObjectByUID, "UPDATE `" + _objTableName + "` SET `Datestamp` = CURRENT_TIMESTAMP, `Damage` = '0' WHERE `ObjectUID` = ? AND `Instance` = ?");
else
stmt = getDB()->makeStatement(_stmtUpdateDatestampObjectByID, "UPDATE `" + _objTableName + "` SET `Datestamp` = CURRENT_TIMESTAMP, `Damage` = '0' WHERE `ObjectID` = ? AND `Instance` = ?");
stmt->addInt64(objectIdent);
stmt->addInt32(serverId);
bool exRes = stmt->execute();
poco_assert(exRes == true);
return exRes;
}
bool SqlObjDataSource::updateVehicleMovement( int serverId, Int64 objectIdent, const Sqf::Value& worldspace, double fuel )
{
auto stmt = getDB()->makeStatement(_stmtUpdateVehicleMovement, "UPDATE `"+_objTableName+"` SET `Worldspace` = ? , `Fuel` = ? WHERE `ObjectID` = ? AND `Instance` = ?");
stmt->addString(lexical_cast<string>(worldspace));
stmt->addDouble(fuel);
stmt->addInt64(objectIdent);
stmt->addInt32(serverId);
bool exRes = stmt->execute();
poco_assert(exRes == true);
return exRes;
}
bool SqlObjDataSource::updateVehicleStatus( int serverId, Int64 objectIdent, const Sqf::Value& hitPoints, double damage )
{
auto stmt = getDB()->makeStatement(_stmtUpdateVehicleStatus, "UPDATE `"+_objTableName+"` SET `Hitpoints` = ? , `Damage` = ? WHERE `ObjectID` = ? AND `Instance` = ?");
stmt->addString(lexical_cast<string>(hitPoints));
stmt->addDouble(damage);
stmt->addInt64(objectIdent);
stmt->addInt32(serverId);
bool exRes = stmt->execute();
poco_assert(exRes == true);
return exRes;
}
bool SqlObjDataSource::createObject( int serverId, const string& className, double damage, int characterId,
const Sqf::Value& worldSpace, const Sqf::Value& inventory, const Sqf::Value& hitPoints, double fuel, Int64 uniqueId )
{
auto stmt = getDB()->makeStatement(_stmtCreateObject,
"INSERT INTO `"+_objTableName+"` (`ObjectUID`, `Instance`, `Classname`, `Damage`, `CharacterID`, `Worldspace`, `Inventory`, `Hitpoints`, `Fuel`, `Datestamp`) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)");
stmt->addInt64(uniqueId);
stmt->addInt32(serverId);
stmt->addString(className);
stmt->addDouble(damage);
stmt->addInt32(characterId);
stmt->addString(lexical_cast<string>(worldSpace));
stmt->addString(lexical_cast<string>(inventory));
stmt->addString(lexical_cast<string>(hitPoints));
stmt->addDouble(fuel);
bool exRes = stmt->execute();
poco_assert(exRes == true);
return exRes;
}
Sqf::Value SqlObjDataSource::fetchObjectId( int serverId, Int64 objectIdent )
{
Sqf::Parameters retVal;
//get details from db
auto worldObjsRes = getDB()->queryParams("SELECT `ObjectID` FROM `%s` WHERE `Instance` = %d AND `ObjectUID` = %"PRIu64"", _objTableName.c_str(), serverId, objectIdent);
if (worldObjsRes && worldObjsRes->fetchRow())
{
int objectid = 0;
//get stuff from row
objectid = worldObjsRes->at(0).getInt32();
if (objectid != 0)
{
retVal.push_back(string("PASS"));
retVal.push_back(lexical_cast<string>(objectid));
}
else
{
retVal.push_back(string("ERROR"));
}
}
else
{
retVal.push_back(string("ERROR"));
}
return retVal;
}