EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SystemDB.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24  Updates: Allan
25 */
26 
27 #include "eve-server.h"
28 
29 #include "system/SystemDB.h"
30 
32  DBQueryResult res;
33  if(!sDatabase.RunQuery(res, "SELECT factionID FROM facFactions")) {
34  codelog(DATABASE__ERROR, "Error in ListFactions query: %s", res.error.c_str());
35  return nullptr;
36  }
37 
38  return DBResultToRowset(res);
39 }
40 
42  DBQueryResult res;
43  if(!sDatabase.RunQuery(res,
44  "SELECT "
45  " celestialID AS toCelestialID,"
46  " solarSystemID AS locationID"
47  " FROM mapJumps "
48  " LEFT JOIN mapDenormalize ON celestialID=itemID"
49  " WHERE stargateID=%u", stargateID))
50  {
51  codelog(DATABASE__ERROR, "Error in ListJumps query: %s", res.error.c_str());
52  return nullptr;
53  }
54 
55  return DBResultToRowset(res);
56 }
57 
59  DBQueryResult res;
60  if (!sDatabase.RunQuery(res,
61  "SELECT "
62  " mss.solarSystemID,"
63  " mss.solarSystemName,"
64  " mss.x, mss.y, mss.z,"
65  " mss.radius,"
66  " mss.security,"
67  " mss.constellationID,"
68  " mss.factionID,"
69  " mss.sunTypeID,"
70  " mss.regionID,"
71  " mlwc.wormholeClassID"
72  " FROM mapSolarSystems AS mss"
73  " LEFT JOIN mapLocationWormholeClasses AS mlwc ON mlwc.locationID = mss.regionID"
74  " WHERE solarSystemID=%u", ssid ))
75  {
76  codelog(DATABASE__ERROR, "Error in GetSolarSystem query: %s", res.error.c_str());
77  return nullptr;
78  }
79 
80  DBResultRow row;
81  if(!res.GetRow(row)) {
82  codelog(DATABASE__ERROR, "Error in GetSolarSystem query: no solarsystem for id %u", ssid);
83  return nullptr;
84  }
85 
86  return DBRowToPackedRow(row);
87 }
88 
90  DBQueryResult res;
91  if(!sDatabase.RunQuery(res,
92  "SELECT x,y,z"
93  " FROM mapSolarSystems WHERE solarSystemID=%u",
94  systemID))
95  {
96  codelog(DATABASE__ERROR, "Error in GetSolarSystemPosition query: %s", res.error.c_str());
97  return NULL_ORIGIN;
98  }
99 
100  _log(DATABASE__RESULTS, "GetSolarSystemPosition returned %u items", res.GetRowCount());
101 
102  DBResultRow row;
103  GPoint point(NULL_ORIGIN);
104  if (res.GetRow(row))
105  point = GPoint(row.GetDouble(0), row.GetDouble(1), row.GetDouble(2));
106 
107  return point;
108 }
109 
110 bool SystemDB::LoadSystemStaticEntities(uint32 systemID, std::vector<DBSystemEntity>& into) {
111  DBQueryResult res;
112  if(!sDatabase.RunQuery(res,
113  "SELECT itemID,typeID,groupID,radius"
114  " FROM mapDenormalize WHERE solarSystemID=%u ORDER BY itemID",
115  systemID))
116  {
117  codelog(DATABASE__ERROR, "Error in LoadSystemStaticEntities query: %s", res.error.c_str());
118  return false;
119  }
120 
121  _log(DATABASE__RESULTS, "LoadSystemStaticEntities returned %u items", res.GetRowCount());
122 
123  DBResultRow row;
124  while(res.GetRow(row)) {
125  DBSystemEntity entry = DBSystemEntity();
126  entry.itemID = row.GetUInt(0);
127  entry.typeID = row.GetInt(1);
128  entry.groupID = row.GetInt(2);
129  entry.radius = row.GetDouble(3);
130  into.push_back(entry);
131  }
132 
133  return true;
134 }
135 
136 /* load system dynamics owned by the EvE _System (1) and globals owned by Players or PlayerCorps */
137 bool SystemDB::LoadSystemDynamicEntities(uint32 systemID, std::vector<DBSystemDynamicEntity>& into) {
138  using namespace EVEDB::invCategories;
139  DBQueryResult res, res2;
140  if(!sDatabase.RunQuery(res,
141  "SELECT"
142  " e.itemID,"
143  " e.itemName,"
144  " e.ownerID,"
145  " e.typeID,"
146  " t.groupID,"
147  " g.categoryID,"
148  " e.x, e.y, e.z,"
149  " IFNULL(e.customInfo, '0')"
150  " FROM entity AS e"
151  " LEFT JOIN invTypes AS t USING (typeID)"
152  " LEFT JOIN invGroups AS g USING (groupID)"
153  " WHERE e.locationID = %u"
154  " AND g.categoryID NOT IN (%u,%u,%u,%u)" // not Characters, stations, or roids
155  " AND (e.ownerID = 1" // get dynamics owned by the system -include abandonded ships
156  " OR g.categoryID IN (%u,%u))" // or orbitals and SOV structs owned by pc corps
157  " ORDER BY e.itemID",
158  systemID,
159  //exclude categories not applicable for in-space system entities or owned by player/corp :
160  _System/*0*/, /*Character*/1, /*Station*/3, Asteroid/*25*/, //asteroids are owned/controlled by BeltMgr.
161  Orbitals/*46*/,SovereigntyStructure/*40*/ )) {
162  codelog(DATABASE__ERROR, "Error in LoadSystemDynamicEntities query: %s", res.error.c_str());
163  return false;
164  }
165 
166  _log(DATABASE__RESULTS, "LoadSystemDynamicEntities returned %u items", res.GetRowCount());
167 
168  DBResultRow row, row2;
169  while(res.GetRow(row)) {
171  entry.itemID = row.GetUInt(0);
172  entry.itemName = row.GetText(1);
173  entry.ownerID = row.GetInt(2);
174  entry.typeID = row.GetUInt(3);
175  entry.groupID = row.GetUInt(4);
176  entry.categoryID = row.GetUInt(5);
177  GPoint pos(row.GetDouble(6), row.GetDouble(7), row.GetDouble(8));
178  entry.position = pos;
179  entry.planetID = atoi(row.GetText(9));
180 
181  if (IsCorp(entry.ownerID)) {
182  entry.corporationID = entry.ownerID;
183  if (sDatabase.RunQuery(res2, "SELECT allianceID, warFactionID FROM crpCorporation WHERE corporationID = %u", entry.corporationID))
184  if (res2.GetRow(row2)) {
185  entry.allianceID = row2.GetUInt(0);
186  entry.factionID = row2.GetUInt(1);
187  }
188  } else if (IsCharacterID(entry.ownerID)) {
189  if (sDatabase.RunQuery(res2,
190  "SELECT c.corporationID, co.allianceID, co.warFactionID FROM chrCharacters AS c"
191  " LEFT JOIN crpCorporation AS co USING (corporationID)"
192  " WHERE c.characterID = %u", entry.ownerID))
193  {
194  if (res2.GetRow(row2)) {
195  entry.corporationID = row2.GetUInt(0);
196  entry.allianceID = row2.GetUInt(1);
197  entry.factionID = row2.GetUInt(2);
198  }
199  }
200  }
201  into.push_back(entry);
202  }
203 
204  return true;
205 }
206 
207 /* load system dynamics owned by players */
208 bool SystemDB::LoadPlayerDynamicEntities(uint32 systemID, std::vector<DBSystemDynamicEntity>& into)
209 {
210  using namespace EVEDB::invCategories;
211  DBQueryResult res, res2;
212  if (!sDatabase.RunQuery(res,
213  "SELECT"
214  " e.itemID,"
215  " e.itemName,"
216  " e.typeID,"
217  " e.ownerID,"
218  " t.groupID,"
219  " g.categoryID," //5
220  " e.x, e.y, e.z" //8
221  " FROM entity AS e"
222  " LEFT JOIN invTypes AS t USING (typeID)"
223  " LEFT JOIN invGroups AS g USING (groupID)"
224  " WHERE e.locationID = %u"
225  " AND g.categoryID IN (%u,%u,%u,%u,%u,%u,%u)"
226  " AND e.ownerID != 1" // get dynamics not owned by the system
227  " ORDER BY e.itemID", // should we order by category? or group?
228  systemID, Celestial/*2*/, // Celestial is for containers (wrecks, jetcans, lsc)
229  Charge /*8*/, // this is for probes and spheres launched from ship (abandonded)
230  Deployable/*22*/, // include deployed items owned by players or corps
231  Drone/*18*/, Entity/*11*/, // Entity also contains NPCs, sentrys, LCOs, and other destructible objects
232  /*Structure*/23, StructureUpgrade/*39*/)) { // this is for POS and POCO
233  codelog(DATABASE__ERROR, "Error in LoadPlayerDynamicEntities query: %s", res.error.c_str());
234  return false;
235  }
236 
237  _log(DATABASE__RESULTS, "LoadPlayerDynamicEntities returned %u items", res.GetRowCount());
238  DBResultRow row, row2;
239  while(res.GetRow(row)) {
241  entry.itemID = row.GetUInt(0);
242  entry.itemName = row.GetText(1);
243  entry.typeID = row.GetUInt(2);
244  entry.ownerID = row.GetInt(3);
245  entry.groupID = row.GetUInt(4);
246  entry.categoryID = row.GetUInt(5);
247  GPoint pos(row.GetDouble(6), row.GetDouble(7), row.GetDouble(8));
248  entry.position = pos;
249 
250  if (IsCorp(entry.ownerID)) {
251  entry.corporationID = entry.ownerID;
252  if (sDatabase.RunQuery(res2, "SELECT allianceID, warFactionID FROM crpCorporation WHERE corporationID = %u", entry.corporationID))
253  if (res2.GetRow(row2)) {
254  entry.allianceID = row2.GetUInt(0);
255  entry.factionID = row2.GetUInt(1);
256  }
257  } else if (IsCharacterID(entry.ownerID)) {
258  if (sDatabase.RunQuery(res2,
259  "SELECT c.corporationID, co.allianceID, co.warFactionID FROM chrCharacters AS c"
260  " LEFT JOIN crpCorporation AS co USING (corporationID)"
261  " WHERE c.characterID = %u", entry.ownerID))
262  {
263  if (res2.GetRow(row2)) {
264  entry.corporationID = row2.GetUInt(0);
265  entry.allianceID = row2.GetUInt(1);
266  entry.factionID = row2.GetUInt(2);
267  }
268  }
269  }
270  into.push_back(entry);
271  }
272  return true;
273 }
274 
276  DBQueryResult res;
277  if(!sDatabase.RunQuery(res, "SELECT locationID FROM entity WHERE itemID=%u", itemID )) {
278  codelog(DATABASE__ERROR, "Error in GetObjectLocationID query: %s", res.error.c_str());
279  return 0;
280  }
281 
282  DBResultRow row;
283  if (res.GetRow(row))
284  return (row.GetUInt(0));
285  return 0;
286 }
287 
289  DBQueryResult res;
290  if(!sDatabase.RunQuery(res, "SELECT radius FROM invTypes WHERE typeID=%u", typeID )) {
291  codelog(DATABASE__ERROR, "Error in GetItemTypeRadius query: %s", res.error.c_str());
292  return 0.0;
293  }
294 
295  DBResultRow row;
296  if (res.GetRow(row))
297  return (row.GetDouble(0));
298  return 0.0;
299 }
300 
302  DBQueryResult res;
303  if(!sDatabase.RunQuery(res, "SELECT radius FROM mapDenormalize WHERE itemID=%u", itemID )) {
304  codelog(DATABASE__ERROR, "Error in GetItemTypeRadius query: %s", res.error.c_str());
305  return 10.1;
306  }
307 
308  DBResultRow row;
309  if (res.GetRow(row))
310  return (row.GetDouble(0));
311  return 12.3;
312 }
313 
315  if(!sDatabase.RunQuery(res, "SELECT typeID, wreckTypeID FROM invTypesToWrecks")) {
316  codelog(DATABASE__ERROR, "Error in GetWrecksToTypes query: %s", res.error.c_str());
317  return false;
318  }
319  return true;
320 }
321 
323  //if(!sDatabase.RunQuery(res, "SELECT groupID, lootGroupID, dropChance FROM npcLootGroup")) {
324  if(!sDatabase.RunQuery(res, "SELECT npcGroupID, itemGroupID, groupDropChance FROM lootGroup")) {
325  codelog(DATABASE__ERROR, "Error in GetLootGroups query: %s", res.error.c_str());
326  return;
327  }
328 }
329 
331  //if(!sDatabase.RunQuery(res, "SELECT lootGroupID, typeID, chance, minQuantity, maxQuantity FROM npcLootGroupType")) {
332  if(!sDatabase.RunQuery(res, "SELECT itemGroupID, itemID, itemMetaLevel, minAmount, maxAmount FROM lootItemGroup")) {
333  codelog(DATABASE__ERROR, "Error in GetLootGroupTypes query: %s", res.error.c_str());
334  return;
335  }
336 }
337 
338 void SystemDB::GetPlanets(uint32 systemID, std::vector<DBGPointEntity> &planetIDs, uint8 &total) {
339 // groupID = 7
340  DBQueryResult res;
341  sDatabase.RunQuery(res, "SELECT itemID, x, y, z, radius FROM mapDenormalize WHERE solarSystemID = %u AND groupID = 7", systemID);
342 
343  DBResultRow row;
344  while(res.GetRow(row)) {
345  DBGPointEntity entry = DBGPointEntity();
346  entry.idx = total;
347  entry.itemID = row.GetUInt(0);
348  entry.position = GPoint (
349  row.GetDouble(1),
350  row.GetDouble(2),
351  row.GetDouble(3)
352  );
353  entry.radius = row.GetDouble(4);
354  planetIDs.push_back(entry);
355  ++total;
356  }
357 }
358 
359 void SystemDB::GetMoons(uint32 systemID, std::vector<DBGPointEntity> &moonIDs, uint8 &total) {
360 // groupID = 8
361  DBQueryResult res;
362  sDatabase.RunQuery(res, "SELECT itemID, x, y, z, radius FROM mapDenormalize WHERE solarSystemID = %u AND groupID = 8", systemID);
363 
364  DBResultRow row;
365  while(res.GetRow(row)) {
366  DBGPointEntity entry = DBGPointEntity();
367  entry.idx = total;
368  entry.itemID = row.GetUInt(0);
369  entry.position = GPoint (
370  row.GetDouble(1),
371  row.GetDouble(2),
372  row.GetDouble(3)
373  );
374  entry.radius = row.GetDouble(4);
375 
376  moonIDs.push_back(entry);
377  ++total;
378  }
379 }
380 
381 void SystemDB::GetBelts(uint32 systemID, std::vector< DBGPointEntity > &beltIDs, uint8 &total)
382 {
383  // groupID = 9
384  DBQueryResult res;
385  sDatabase.RunQuery(res, "SELECT itemID, x, y, z, radius FROM mapDenormalize WHERE solarSystemID = %u AND groupID = 9", systemID);
386 
387  DBResultRow row;
388  while(res.GetRow(row)) {
389  DBGPointEntity entry = DBGPointEntity();
390  entry.idx = total;
391  entry.itemID = row.GetUInt(0);
392  entry.position = GPoint (
393  row.GetDouble(1),
394  row.GetDouble(2),
395  row.GetDouble(3)
396  );
397  entry.radius = row.GetDouble(4);
398 
399  beltIDs.push_back(entry);
400  ++total;
401  }
402 }
403 
404 void SystemDB::GetGates(uint32 systemID, std::vector< DBGPointEntity > &gateIDs)
405 {
406  // groupID = 10
407  DBQueryResult res;
408  sDatabase.RunQuery(res, "SELECT itemID, x, y, z, radius FROM mapDenormalize WHERE solarSystemID = %u AND groupID = 10", systemID);
409 
410  uint8 total = 0;
411  DBResultRow row;
412  while(res.GetRow(row)) {
413  DBGPointEntity entry = DBGPointEntity();
414  entry.idx = total;
415  entry.itemID = row.GetUInt(0);
416  entry.position = GPoint (
417  row.GetDouble(1),
418  row.GetDouble(2),
419  row.GetDouble(3)
420  );
421  entry.radius = row.GetDouble(4);
422 
423  gateIDs.push_back(entry);
424  ++total;
425  }
426 }
unsigned __int8 uint8
Definition: eve-compat.h:46
#define sDatabase
Definition: dbcore.h:199
static bool LoadSystemDynamicEntities(uint32 systemID, std::vector< DBSystemDynamicEntity > &into)
Definition: SystemDB.cpp:137
#define _log(type, fmt,...)
Definition: logsys.h:124
double GetCelestialRadius(uint32 itemID)
Definition: SystemDB.cpp:301
void GetBelts(uint32 systemID, std::vector< DBGPointEntity > &beltIDs, uint8 &total)
Definition: SystemDB.cpp:381
uint32 GetUInt(uint32 index) const
Definition: dbcore.cpp:658
static void GetLootGroups(DBQueryResult &res)
Definition: SystemDB.cpp:322
double GetDouble(uint32 index) const
Definition: dbcore.cpp:693
static PyPackedRow * GetSolarSystem(uint32 ssid)
Definition: SystemDB.cpp:58
static bool LoadPlayerDynamicEntities(uint32 systemID, std::vector< DBSystemDynamicEntity > &into)
Definition: SystemDB.cpp:208
static void GetLootGroupTypes(DBQueryResult &res)
Definition: SystemDB.cpp:330
static PyObject * ListJumps(uint32)
Definition: SystemDB.cpp:41
double GetItemTypeRadius(uint32 typeID)
Definition: SystemDB.cpp:288
void GetMoons(uint32 systemID, std::vector< DBGPointEntity > &moonIDs, uint8 &total)
Definition: SystemDB.cpp:359
bool GetRow(DBResultRow &into)
Definition: dbcore.cpp:552
Definition: gpoint.h:33
const char * c_str() const
Definition: dbcore.h:48
Python object.
Definition: PyRep.h:826
#define codelog(type, fmt,...)
Definition: logsys.h:128
static const GPoint NULL_ORIGIN(0, 0, 0)
PyObject * DBResultToRowset(DBQueryResult &result)
Definition: EVEDBUtils.cpp:81
void GetPlanets(uint32 systemID, std::vector< DBGPointEntity > &planetIDs, uint8 &total)
Definition: SystemDB.cpp:338
static GPoint GetSolarSystemPosition(uint32 systemID)
Definition: SystemDB.cpp:89
#define IsCharacterID(itemID)
Definition: EVE_Defines.h:206
unsigned __int32 uint32
Definition: eve-compat.h:50
static uint32 GetObjectLocationID(uint32 itemID)
Definition: SystemDB.cpp:275
#define IsCorp(itemID)
Definition: EVE_Defines.h:234
static bool LoadSystemStaticEntities(uint32 systemID, std::vector< DBSystemEntity > &into)
Definition: SystemDB.cpp:110
DBerror error
Definition: dbcore.h:69
typeID Spawn an NPC with the specified type text Search for items matching the specified query() type()() itemID() copy() materialLevel()() itemID(attributeID)-Retrieves attribute value." ) COMMAND( setattr
static bool GetWrecksToTypes(DBQueryResult &res)
Definition: SystemDB.cpp:314
size_t GetRowCount()
Definition: dbcore.h:72
PyPackedRow * DBRowToPackedRow(DBResultRow &row)
Definition: EVEDBUtils.cpp:453
PyObject * ListFactions()
Definition: SystemDB.cpp:31
Packed row.
Definition: PyRep.h:961
static void GetGates(uint32 systemID, std::vector< DBGPointEntity > &gateIDs)
Definition: SystemDB.cpp:404