EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PlanetDB.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: Allan, based on original design by Comet0
24 */
25 
26 /*
27  * PLANET__ERROR
28  * PLANET__WARNING
29  * PLANET__MESSAGE
30  * PLANET__DEBUG
31  * PLANET__INFO
32  * PLANET__TRACE
33  * PLANET__DUMP
34  * PLANET__RES_DUMP
35  * PLANET__GC_DUMP
36  * PLANET__PKT_TRACE
37  * PLANET__DB_ERROR
38  * PLANET__DB_WARNING
39  */
40 
41 
42 #include "planet/Colony.h"
43 #include "planet/PlanetDB.h"
44 #include "planet/PlanetDataMgr.h"
45 
46 
48 {
49  // load info into PIDataMgr
50  if(!sDatabase.RunQuery(res, "SELECT schematicID, typeID, quantity, isInput FROM piTypeMap" ))
51  _log(DATABASE__ERROR, "Error in GetSchematicData Query: %s", res.error.c_str());
52 }
53 
55 {
56  // load info into PIDataMgr
57  if(!sDatabase.RunQuery(res, "SELECT schematicID, cycleTime FROM piSchematics"))
58  _log(DATABASE__ERROR, "Error in GetSchematicTimes Query: %s", res.error.c_str());
59 }
60 
62 {
63  // load info into PlanetDataMgr
64  if(!sDatabase.RunQuery(res,
65  "SELECT planet.typeID AS planetTypeID,"
66  " resource.typeID AS resourceID"
67  " FROM invTypes planet, invTypes resource, dgmTypeAttributes dgm1, dgmTypeAttributes dgm2 "
68  " WHERE dgm1.typeID = dgm2.typeID AND dgm1.attributeID = 1632 AND dgm1.valueFloat = planet.typeID AND dgm2.attributeID = 709"
69  " AND dgm2.valueFloat = resource.typeID ORDER BY planet.typeID ")) {
70  _log(DATABASE__ERROR, "Error in GetPlanetData Query: %s", res.error.c_str());
71  }
72 }
73 
74 
79  DBQueryResult res;
80  if(!sDatabase.RunQuery(res,
81  "SELECT solarSystemID, planetID, typeID, numberOfPins"
82  " FROM piPlanets WHERE charID = %u", charID)) {
83  _log(DATABASE__ERROR, "Error in GetPlanetsForChar query: %s", res.error.c_str());
84  return nullptr;
85  }
86  _log(DATABASE__RESULTS, "GetPlanetsForChar returned %u items", res.GetRowCount());
87  return DBResultToCRowset(res);
88 }
89 
90 void PlanetDB::AddPlanetForChar(uint32 solarSystemID, uint32 planetID, uint32 charID, uint32 ccPinID, uint16 typeID)
91 {
92  DBerror err;
93  if(!sDatabase.RunQuery(err,
94  "INSERT INTO piPlanets (solarSystemID, planetID, charID, typeID, ccPinID)"
95  " VALUES (%u, %u, %u, %u, %u)", solarSystemID, planetID, charID, typeID, ccPinID))
96  {
97  _log(DATABASE__ERROR, "AddPlanetForChar - Unable to add planet %u for char %u: %s", planetID, charID, err.GetError());
98  }
99 
100 }
102 {
103  DBerror err;
104  if(!sDatabase.RunQuery(err, "UPDATE piPlanets SET numberOfPins = %u WHERE ccPinID = %u ", pins, ccPinID))
105  {
106  _log(DATABASE__ERROR, "UpdatePlanetPins - Unable to update ccPinID %u : %s", ccPinID, err.GetError());
107  }
108 }
109 
110 // expired = not blue.os.GetWallclockTime() - launch.launchTime < const.piLaunchOrbitDecayTime (5d)
112  DBQueryResult res;
113  if(!sDatabase.RunQuery(res, "SELECT itemID AS launchID, status, itemID, solarSystemID, planetID, launchTime, x, y, z"
114  " FROM piLaunches WHERE charID = %u", charID)) {
115  _log(DATABASE__ERROR, "Error in GetMyLaunchesDetails Query: %s", res.error.c_str());
116  return nullptr;
117  }
118  return DBResultToRowset(res);
119 }
120 
122 {
123  DBQueryResult res;
124  if(!sDatabase.RunQuery(res, "SELECT x,y,z FROM piLaunches WHERE launchID = %u", launchID)) {
125  _log(DATABASE__ERROR, "Error in GetLaunchPos query: %s", res.error.c_str());
126  }
127  DBResultRow row;
128  if (!res.GetRow(row)) {
129  _log(DATABASE__ERROR, "Error in GetLaunchPos query: %s", res.error.c_str());
130  return NULL_ORIGIN;
131  }
132  GPoint pos(row.GetDouble(0), row.GetDouble(1), row.GetDouble(2));
133  return pos;
134 }
135 
137 {
138  DBQueryResult res;
139  if(!sDatabase.RunQuery(res, "SELECT planetID FROM piLaunches WHERE launchID = %u", launchID)) {
140  _log(DATABASE__ERROR, "Error in GetLaunchPlanet query: %s", res.error.c_str());
141  }
142  DBResultRow row;
143  if (!res.GetRow(row)) {
144  _log(DATABASE__ERROR, "Error in GetLaunchPlanet query: %s", res.error.c_str());
145  return 0;
146  }
147 
148  return row.GetInt(0);
149 }
150 
152 {
153  DBQueryResult res;
154  if(!sDatabase.RunQuery(res, "SELECT itemID, status, solarSystemID, planetID, launchTime, x, y, z FROM piLaunches WHERE launchID = %u", data.launchID)) {
155  _log(DATABASE__ERROR, "Error in GetLaunchDetails query: %s", res.error.c_str());
156  }
157  DBResultRow row;
158  if (!res.GetRow(row)) {
159  _log(DATABASE__ERROR, "Error in GetLaunchDetails query: %s", res.error.c_str());
160  return;
161  }
162 
163  data.itemID = row.GetInt(0);
164  data.status = row.GetInt(1);
165  data.solarSystemID = row.GetInt(2);
166  data.planetID = row.GetInt(3);
167  data.launchTime = row.GetInt64(4);
168  data.x = row.GetDouble(5);
169  data.y = row.GetDouble(6);
170  data.z = row.GetDouble(7);
171 }
173 {
174  DBQueryResult res;
175  if(!sDatabase.RunQuery(res, "SELECT itemID FROM piLaunches WHERE launchID = %u", launchID)) {
176  _log(DATABASE__ERROR, "Error in GetLaunchItemID query: %s", res.error.c_str());
177  }
178  DBResultRow row;
179  if (!res.GetRow(row)) {
180  _log(DATABASE__ERROR, "Error in GetLaunchItemID query: %s", res.error.c_str());
181  return 0;
182  }
183 
184  return row.GetInt(0);
185 }
186 
188 {
189  DBerror err;
190  sDatabase.RunQuery(err, "UPDATE piLaunches SET status=%u WHERE itemID = %u", status, itemID);
191 }
192 
193 
195 {
196  //SELECT ccPinID, ownerID, ecuID, headID, typeID, latitude, longitude FROM piECUHeads WHERE 1
197  if (!sDatabase.RunQuery(res,
198  "SELECT headID, typeID, ownerID, latitude, longitude"
199  " FROM piECUHeads"
200  " WHERE ccPinID IN"
201  " (SELECT pinID FROM piCCPin"
202  " WHERE planetID = %u)",
203  planetID))
204  {
205  _log(DATABASE__ERROR, "Error in LoadPins Query: %s", res.error.c_str());
206  return;
207  }
208 
209  _log(DATABASE__RESULTS, "GetExtractorsForPlanet returned %u items", res.GetRowCount());
210 }
211 
212 bool PlanetDB::LoadColony(uint32 charID, uint32 planetID, PI_CCPin* ccPin)
213 {
214  DBQueryResult res;
215  if (!sDatabase.RunQuery(res,
216  "SELECT pinID,level"
217  " FROM piCCPin"
218  " WHERE charID = %u"
219  " AND planetID = %u",
220  charID, planetID ))
221  {
222  _log(DATABASE__ERROR, "Error in LoadPins Query: %s", res.error.c_str());
223  return false;
224  }
225  DBResultRow row;
226  if (res.GetRow(row)) {
227  ccPin->level = row.GetInt(1);
228  ccPin->ccPinID = row.GetInt(0);
229  return true;
230  }
231  return false;
232 }
233 
234 void PlanetDB::LoadPins(uint32 ccPinID, std::map<uint32, PI_Pin>& pins)
235 {
236  DBQueryResult res;
237  if (!sDatabase.RunQuery(res,
238  "SELECT pinID, typeID, ownerID, state, level, latitude, longitude, "
239  " isCommandCenter, isLaunchable, isProcess, isStorage, isECU,"
240  " schematicID, programType, headRadius,"
241  " launchTime, cycleTime, expiryTime, installTime, lastRunTime,"
242  " hasReceivedInputs, receivedInputsLastCycle, qtyPerCycle"
243  " FROM piPins"
244  " WHERE ccPinID = %u", ccPinID))
245  {
246  _log(DATABASE__ERROR, "Error in LoadPins Query: %s", res.error.c_str());
247  return;
248  }
249 
250  _log(DATABASE__RESULTS, "LoadPins returned %u items", res.GetRowCount());
251 
252  DBResultRow row;
253  while (res.GetRow(row)) {
254  PI_Pin pin = PI_Pin();
255  pin.typeID = row.GetInt(1);
256  pin.ownerID = row.GetInt(2);
257  pin.state = row.GetInt(3);
258  pin.level = row.GetInt(4);
259  pin.latitude = row.GetFloat(5);
260  pin.longitude = row.GetFloat(6);
261  pin.isCommandCenter = row.GetBool(7);
262  pin.isLaunchable = row.GetBool(8);
263  pin.isProcess = row.GetBool(9);
264  pin.isStorage = row.GetBool(10);
265  pin.isECU = row.GetBool(11);
266  pin.schematicID = row.GetInt(12);
267  pin.programType = row.GetInt(13);
268  pin.headRadius = row.GetFloat(14);
269  pin.lastLaunchTime = row.GetInt64(15);
270  pin.cycleTime = row.GetInt64(16);
271  if (pin.isECU) // ecu time is stored as filetime, but used as hours
272  pin.cycleTime /= EvE::Time::Hour;
273  pin.expiryTime = row.GetInt64(17);
274  pin.installTime = row.GetInt64(18);
275  pin.lastRunTime = row.GetInt64(19);
276  pin.hasReceivedInputs = row.GetBool(20);
277  pin.receivedInputsLastCycle = row.GetBool(21);
278  pin.qtyPerCycle = row.GetInt(22);
279 
280  if (pin.isStorage or pin.isProcess)
281  LoadContents(row.GetInt(0), pin.contents);
282 
283  if (pin.isECU)
284  LoadHeads(row.GetInt(0), pin.heads);
285 
286  pins[row.GetInt(0)] = pin;
287  }
288 }
289 
290 void PlanetDB::LoadLinks(uint32 ccPinID, std::map<uint32, PI_Link >& links)
291 {
292  DBQueryResult res;
293  if (!sDatabase.RunQuery(res,
294  "SELECT linkID, level, state, endpoint1, endpoint2"
295  " FROM piLinks WHERE ccPinID = %u",
296  ccPinID ))
297  {
298  _log(DATABASE__ERROR, "Error in LoadPins Query: %s", res.error.c_str());
299  return;
300  }
301 
302  _log(DATABASE__RESULTS, "LoadLinks returned %u items", res.GetRowCount());
303 
304  DBResultRow row;
305  while (res.GetRow(row)) {
306  PI_Link link = PI_Link();
307  link.level = row.GetInt(1);
308  link.state = row.GetInt(2);
309  link.typeID = 2280; // only link type in game
310  link.endpoint1 = row.GetInt(3);
311  link.endpoint2 = row.GetInt(4);
312  links[row.GetInt(0)] = link;
313  }
314 }
315 
316 void PlanetDB::LoadRoutes(uint32 ccPinID, std::map<uint16, PI_Route >& routes)
317 {
318  DBQueryResult res;
319  if (!sDatabase.RunQuery(res,
320  "SELECT routeID, srcPinID, destPinID, state, priority, path, typeID, itemQty"
321  " FROM piRoutes WHERE ccPinID = %u",
322  ccPinID ))
323  {
324  _log(DATABASE__ERROR, "Error in LoadPins Query: %s", res.error.c_str());
325  return;
326  }
327 
328  _log(DATABASE__RESULTS, "LoadRoutes returned %u items", res.GetRowCount());
329 
330  DBResultRow row;
331  std::string tempPath;
332  while (res.GetRow(row)) {
333  tempPath.clear();
334  PI_Route route = PI_Route();
335  route.srcPinID = row.GetInt(1);
336  route.destPinID = row.GetInt(2);
337  route.state = row.GetInt(3);
338  route.priority = row.GetInt(4);
339  route.commodityTypeID = row.GetInt(6);
340  route.commodityQuantity = row.GetInt(7);
341 
342  tempPath = row.GetText(5);
343  if (!tempPath.empty()) {
344  // route string is not empty, so extract one number at a time until it is
345  int pos = 0;
346  std::string tempString = "";
347 
348  while ((pos = tempPath.find_first_of(':')) > 0 ) {
349  tempString = tempPath.substr(0,pos);
350  tempPath = tempPath.substr(pos+1,tempPath.length()-1);
351  route.path.insert(route.path.end(), (atoi(tempString.c_str())));
352  }
353  // insert last pinID
354  route.path.insert(route.path.end(), (atoi(tempPath.c_str())));
355  }
356 
357  routes[row.GetInt(0)] = route;
358  }
359 }
360 
361 void PlanetDB::LoadContents(uint32 pinID, std::map<uint16, uint32>& contents)
362 {
363  //SELECT ccPinID, pinID, typeID, itemQty FROM piPinContents
364  DBQueryResult res;
365  if (!sDatabase.RunQuery(res,
366  "SELECT typeID, itemQty"
367  " FROM piPinContents WHERE pinID = %u",
368  pinID ))
369  {
370  _log(DATABASE__ERROR, "Error in LoadContents Query: %s", res.error.c_str());
371  return;
372  }
373 
374  _log(DATABASE__RESULTS, "LoadContents returned %u items", res.GetRowCount());
375 
376  DBResultRow row;
377  while (res.GetRow(row)) {
378  contents[row.GetInt(0)] = row.GetInt(1);
379  }
380 }
381 
382 void PlanetDB::LoadHeads(uint32 ecuID, std::map< uint16, PI_Heads >& heads)
383 {
384  //SELECT ecuID, headID, typeID, latitude, longitude FROM piECUHeads
385  DBQueryResult res;
386  if (!sDatabase.RunQuery(res,
387  "SELECT headID, typeID, latitude, longitude"
388  " FROM piECUHeads WHERE ecuID = %u",
389  ecuID ))
390  {
391  _log(DATABASE__ERROR, "Error in LoadHeads Query: %s", res.error.c_str());
392  return;
393  }
394 
395  _log(DATABASE__RESULTS, "LoadHeads returned %u items", res.GetRowCount());
396 
397  DBResultRow row;
398  while (res.GetRow(row)) {
399  PI_Heads head = PI_Heads();
400  head.typeID = row.GetInt(1);
401  head.ecuPinID = ecuID;
402  head.latitude = row.GetDouble(2);
403  head.longitude = row.GetDouble(3);
404  heads[(uint16)row.GetInt(0)] = head;
405  }
406 }
407 
408 void PlanetDB::SaveLaunch(uint32 contID, uint32 charID, uint32 systemID, uint32 planetID, GPoint& pos)
409 {
410  DBerror err;
411  if(!sDatabase.RunQuery(err,
412  "INSERT INTO piLaunches (itemID, status, charID, solarSystemID, planetID, launchTime, x, y, z) "
413  " VALUES (%u, 0, %u, %u, %u, %f, %f, %f, %f)",
414  contID, charID, systemID, planetID, GetFileTimeNow(), pos.x, pos.y, pos.z))
415  {
416  _log(DATABASE__ERROR, "SaveLaunch - Unable to save Launch: %s", err.GetError());
417  }
418 }
419 
420 void PlanetDB::SaveCommandCenter(uint32 pinID, uint32 charID, uint32 planetID, uint32 typeID, double latitude, double longitude)
421 {
422  DBerror err;
423  if(!sDatabase.RunQuery(err,
424  "INSERT INTO piCCPin (pinID, charID, planetID, typeID, latitude, longitude) "
425  " VALUES (%u, %u, %u, %u, %f, %f)",
426  pinID, charID, planetID, typeID, latitude, longitude))
427  {
428  _log(DATABASE__ERROR, "SaveCommandCenter - Unable to save CommandCenter: %s", err.GetError());
429  }
430 }
431 
433 {
434  DBerror err;
435  if(!sDatabase.RunQuery(err, "UPDATE piCCPin SET level = %u WHERE pinID = %u", level, pinID))
436  _log(DATABASE__ERROR, "SaveCCLevel - Unable to save CCLevel: %s", err.GetError());
437  if(!sDatabase.RunQuery(err, "UPDATE piPins SET level = %u WHERE pinID = %u", level, pinID))
438  _log(DATABASE__ERROR, "SaveCCLevel - Unable to save CCLevel: %s", err.GetError());
439 
440 }
441 
443 {
444  std::ostringstream Inserts;
445  // start the insert into command.
446  Inserts << "INSERT INTO piPins";
447  Inserts << " (ccPinID, pinID, typeID, ownerID, level, latitude, longitude,";
448  Inserts << " isCommandCenter, isLaunchable, isProcess, isStorage, isECU,";
449  Inserts << " schematicID, programType, headRadius, launchTime,";
450  Inserts << " cycleTime, expiryTime, installTime, lastRunTime)";
451 
452  bool first = true;
453  uint32 ccPinID = ccPin->ccPinID;
454  for (auto cur : ccPin->pins) {
455  if (first) {
456  Inserts << " VALUES ";
457  first = false;
458  } else {
459  Inserts << ", ";
460  }
461  Inserts << "(" << ccPinID << ", " << cur.first << ", " << cur.second.typeID << ", " << cur.second.ownerID << ", " << cur.second.level << ", " << cur.second.latitude << ", " << cur.second.longitude << ", ";
462  Inserts << cur.second.isCommandCenter << ", " << cur.second.isLaunchable << ", " << cur.second.isProcess << ", " << cur.second.isStorage <<", " << cur.second.isECU << ", ";
463  Inserts << cur.second.schematicID << ", " << cur.second.programType << ", " << cur.second.headRadius << ", " << cur.second.lastLaunchTime;
464  Inserts << ", " << cur.second.cycleTime << ", " << cur.second.expiryTime << ", " << cur.second.installTime << ", " << cur.second.lastRunTime << ")";
465  }
466 
467  if (!first) {
468  // finish creating the command.
469  Inserts << " ON DUPLICATE KEY UPDATE ";
470  Inserts << " schematicID=VALUES(schematicID), ";
471  Inserts << " programType=VALUES(programType), ";
472  Inserts << " headRadius=VALUES(headRadius),";
473  Inserts << " launchTime=VALUES(launchTime), ";
474  Inserts << " cycleTime=VALUES(cycleTime),";
475  Inserts << " expiryTime=VALUES(expiryTime), ";
476  Inserts << " installTime=VALUES(installTime),";
477  Inserts << " lastRunTime=VALUES(lastRunTime);";
478  // execute the command.
479  DBerror err;
480  if (!sDatabase.RunQuery(err, Inserts.str().c_str()))
481  _log(DATABASE__ERROR, "SavePins - unable to save pins: %s", err.c_str());
482  }
483 }
484 
486 {
487  std::ostringstream Inserts;
488  // start the insert into command.
489  Inserts << "INSERT INTO piPins";
490  Inserts << " (ccPinID, pinID, schematicID, programType, launchTime,";
491  Inserts << " cycleTime, installTime, lastRunTime, receivedInputsLastCycle, hasReceivedInputs)";
492 
493  bool first = true;
494  if (pinID) {
495  std::map<uint32, PI_Pin>::iterator itr;
496  itr = ccPin->pins.find(pinID);
497  if (itr != ccPin->pins.end()) {
498  first = false;
499  Inserts << " VALUES ";
500  Inserts << "(" << ccPin->ccPinID << ", " << itr->first << ", "<< itr->second.schematicID << ", " << itr->second.programType;
501  Inserts << ", " << itr->second.lastLaunchTime << ", " << itr->second.cycleTime << ", " << itr->second.installTime;
502  Inserts << ", " << itr->second.lastRunTime << ", " << itr->second.receivedInputsLastCycle << ", " << itr->second.hasReceivedInputs << ")";
503  }
504  } else {
505  for (auto cur : ccPin->pins) {
506  if (first) {
507  Inserts << " VALUES ";
508  first = false;
509  } else {
510  Inserts << ", ";
511  }
512  Inserts << "(" << ccPin->ccPinID << ", " << cur.first << ", "<< cur.second.schematicID << ", " << cur.second.programType;
513  Inserts << ", " << cur.second.lastLaunchTime << ", " << cur.second.cycleTime << ", " << cur.second.installTime;
514  Inserts << ", " << cur.second.lastRunTime << ", " << cur.second.receivedInputsLastCycle << ", " << cur.second.hasReceivedInputs << ")";
515  }
516  }
517 
518  if (!first) {
519  // finish creating the command.
520  Inserts << " ON DUPLICATE KEY UPDATE ";
521  Inserts << " schematicID=VALUES(schematicID), ";
522  Inserts << " programType=VALUES(programType), ";
523  Inserts << " launchTime=VALUES(launchTime), ";
524  Inserts << " cycleTime=VALUES(cycleTime),";
525  Inserts << " installTime=VALUES(installTime),";
526  Inserts << " lastRunTime=VALUES(lastRunTime),";
527  Inserts << " hasReceivedInputs=VALUES(hasReceivedInputs),";
528  Inserts << " receivedInputsLastCycle=VALUES(receivedInputsLastCycle);";
529  // execute the command.
530  DBerror err;
531  if (!sDatabase.RunQuery(err, Inserts.str().c_str()))
532  _log(DATABASE__ERROR, "SavePins - unable to save pins: %s", err.c_str());
533  }
534 }
535 
537 {
538  std::map<uint32, PI_Pin>::iterator itr = ccPin->pins.find(pinID);
539  if (itr == ccPin->pins.end()) {
540  _log(PLANET__ERROR, "PlanetDB::UpdateECUPin() - pinID %u not found in ccPin.pins map", pinID);
541  return;
542  }
543 
544  DBerror err;
545  if(!sDatabase.RunQuery(err,
546  "UPDATE piPins SET"
547  " programType = %u,"
548  " headRadius = %f,"
549  " qtyPerCycle = %u,"
550  " schematicID = %u,"
551  " expiryTime = %li,"
552  " cycleTime = %li,"
553  " installTime = %li,"
554  " lastRunTime = %li"
555  " WHERE pinID = %u",
556  itr->second.programType, itr->second.headRadius, itr->second.qtyPerCycle, itr->second.schematicID, itr->second.expiryTime,
557  itr->second.cycleTime, itr->second.installTime, itr->second.lastRunTime, pinID))
558  {
559  _log(DATABASE__ERROR, "UpdateECUPin - Unable to update pin: %s", err.GetError());
560  }
561 }
562 
564 {
565  std::ostringstream Inserts;
566  // start the insert into command.
567  Inserts << "INSERT INTO piPins";
568  Inserts << " (pinID, launchTime, installTime, lastRunTime)";
569 
570  bool first = true;
571  for (auto cur : ccPin->pins) {
572  if (first) {
573  Inserts << " VALUES ";
574  first = false;
575  } else {
576  Inserts << ", ";
577  }
578  Inserts << "(" << cur.first << ", " << cur.second.lastLaunchTime << ", ";
579  Inserts << cur.second.installTime << ", " << cur.second.lastRunTime << ")";
580  }
581 
582  if (!first) {
583  // finish creating the command.
584  Inserts << " ON DUPLICATE KEY UPDATE ";
585  Inserts << " launchTime=VALUES(launchTime),";
586  Inserts << " installTime=VALUES(installTime),";
587  Inserts << " lastRunTime=VALUES(lastRunTime);";
588  // execute the command.
589  DBerror err;
590  if (!sDatabase.RunQuery(err, Inserts.str().c_str()))
591  _log(DATABASE__ERROR, "SavePins - unable to save pins: %s", err.c_str());
592  }
593 }
594 
595 void PlanetDB::SaveHeads(uint32 ccPinID, uint32 ownerID, uint32 ecuID, std::map< uint16, PI_Heads >& heads)
596 {
597  if (heads.empty())
598  return;
599 
600  DBerror err;
601  for (auto cur : heads) {
602  // save the head data separately
603  if (!sDatabase.RunQuery(err,
604  "INSERT INTO piECUHeads (ccPinID, ownerID, ecuID, headID, typeID, latitude, longitude)"
605  " VALUES (%u, %u, %u, %u, %u, %f, %f)"
606  " ON DUPLICATE KEY UPDATE "
607  " typeID=VALUES(typeID),"
608  " latitude=VALUES(latitude),"
609  " longitude=VALUES(longitude)",
610  ccPinID, ownerID, ecuID, cur.first, cur.second.typeID, cur.second.latitude, cur.second.longitude))
611  {
612  _log(DATABASE__ERROR, "SaveHeads - Unable to save heads: %s", err.GetError());
613  }
614  }
615 }
616 
618 {
619  DBerror err;
620  if(!sDatabase.RunQuery(err, "UPDATE piPins SET level = %u WHERE pinID = %u", level, pinID))
621  {
622  _log(DATABASE__ERROR, "SavePinLevel - Unable to save PinLevel: %s", err.GetError());
623  }
624 }
625 
627 {
628  std::ostringstream Inserts;
629  // start the insert into command.
630  Inserts << "INSERT INTO piLinks";
631  Inserts << " (ccPinID, linkID, level, endpoint1, endpoint2)";
632 
633  bool first = true;
634  uint32 ccPinID = ccPin->ccPinID;
635  for (auto cur : ccPin->links) {
636  if (first) {
637  Inserts << " VALUES ";
638  first = false;
639  } else {
640  Inserts << ", ";
641  }
642  Inserts << "(" << ccPinID << ", " << cur.first << ", " << cur.second.level << ", " << cur.second.endpoint1 << ", " << cur.second.endpoint2 << ")";
643  }
644 
645  if (!first) {
646  // finish creating the command.
647  Inserts << " ON DUPLICATE KEY UPDATE";
648  Inserts << " level=VALUES(level), ";
649  Inserts << " endpoint1=VALUES(endpoint1), ";
650  Inserts << " endpoint2=VALUES(endpoint2)";
651  // execute the command.
652  DBerror err;
653  if (!sDatabase.RunQuery(err, Inserts.str().c_str()))
654  _log(DATABASE__ERROR, "SaveLinks - unable to save links - %s", err.c_str());
655  }
656 }
657 
659 {
660  DBerror err;
661  if(!sDatabase.RunQuery(err, "UPDATE piLinks SET level = %u WHERE linkID = %u", level, linkID))
662  {
663  _log(DATABASE__ERROR, "SaveLinkLevel - Unable to save LinkLevel: %s", err.GetError());
664  }
665 }
666 
668 {
669  DBerror err;
670  uint32 routeID = 0;
671  std::string path;
672  path.clear();
673  std::list<uint32>::iterator itr = route.path.begin();
674  while (itr != route.path.end()) {
675  path += std::to_string(*itr);
676  if (++itr != route.path.end())
677  path += ":";
678  }
679  if (!sDatabase.RunQueryLID(err, routeID,
680  "INSERT INTO `piRoutes`(`ccPinID`, `srcPinID`, `destPinID`, `state`, `priority`, `path`, `typeID`, `itemQty`) "
681  " VALUES (%u, %u, %u, %u, %u, '%s', %u, %u)",
682  ccPinID, route.srcPinID, route.destPinID, route.state, route.priority, path.c_str(), route.commodityTypeID, route.commodityQuantity))
683  {
684  _log(DATABASE__ERROR, "SaveHeads - Unable to save heads: %s", err.GetError());
685  }
686  return (uint16)routeID;
687 }
688 
690 {
691  std::ostringstream Inserts;
692  // start the insert into command.
693  Inserts << "INSERT INTO piRoutes";
694  Inserts << " (ccPinID, routeID, srcPinID, destPinID, path, typeID, itemQty)";
695 
696  bool first = true;
697  std::string path;
698  std::list<uint32>::iterator itr;
699  uint32 ccPinID = ccPin->ccPinID;
700  for (auto cur : ccPin->routes) {
701  path.clear();
702  if (first) {
703  Inserts << " VALUES ";
704  first = false;
705  } else {
706  Inserts << ", ";
707  }
708  itr = cur.second.path.begin();
709  while (itr != cur.second.path.end()) {
710  path += std::to_string(*itr);
711  if (++itr != cur.second.path.end())
712  path += ":";
713  }
714  Inserts << "(" << ccPinID << ", " << cur.first << ", " << cur.second.srcPinID << ", " << cur.second.destPinID << ", '" << path << "', ";
715  Inserts << cur.second.commodityTypeID << ", " << cur.second.commodityQuantity << ")";
716  }
717 
718  if (!first) {
719  // finish creating the command.
720  Inserts << " ON DUPLICATE KEY UPDATE";
721  Inserts << " path=VALUES(path),";
722  Inserts << " srcPinID=VALUES(srcPinID),";
723  Inserts << " destPinID=VALUES(destPinID),";
724  Inserts << " typeID=VALUES(typeID),";
725  Inserts << " itemQty=VALUES(itemQty);";
726  // execute the command.
727  DBerror err;
728  if (!sDatabase.RunQuery(err, Inserts.str().c_str()))
729  _log(DATABASE__ERROR, "SaveRoutes - unable to save route - %s", err.c_str());
730  }
731 }
732 
734 {
735  std::ostringstream Inserts;
736  // start the insert into command.
737  Inserts << "INSERT INTO piPinContents";
738  Inserts << " (ccPinID, pinID, typeID, itemQty)";
739 
740  bool first = true;
741  uint32 ccPinID = ccPin->ccPinID;
742  std::map<uint16, uint32>::iterator itr;
743  for (auto cur : ccPin->pins) {
744  if (cur.second.isStorage) {
745  for (itr = cur.second.contents.begin(); itr != cur.second.contents.end(); ++itr) {
746  if (first) {
747  Inserts << " VALUES ";
748  first = false;
749  } else {
750  Inserts << ", ";
751  }
752  Inserts << "(" << ccPinID << ", " << cur.first << ", " << itr->first << ", " << itr->second << ")";
753  }
754  }
755  }
756  if (!first) {
757  // finish creating the command.
758  Inserts << " ON DUPLICATE KEY UPDATE";
759  Inserts << " itemQty=VALUES(itemQty);";
760  // execute the command.
761  DBerror err;
762  if (!sDatabase.RunQuery(err, Inserts.str().c_str()))
763  _log(DATABASE__ERROR, "SaveContents - unable to save contents - %s", err.c_str());
764  }
765 }
766 
767 void PlanetDB::SavePinContents(uint32 ccPinID, uint32 pinID, std::map< uint16, uint32 >& contents)
768 {
769  std::ostringstream Inserts;
770  // start the insert into command.
771  Inserts << "INSERT INTO piPinContents";
772  Inserts << " (ccPinID, pinID, typeID, itemQty)";
773 
774  bool first = true;
775  std::map<uint16, uint32>::iterator itr;
776  for (itr = contents.begin(); itr != contents.end(); ++itr) {
777  if (first) {
778  Inserts << " VALUES ";
779  first = false;
780  } else {
781  Inserts << ", ";
782  }
783  Inserts << "(" << ccPinID << ", " << pinID << ", " << itr->first << ", " << itr->second << ")";
784  }
785  if (!first) {
786  // execute the command.
787  DBerror err;
788  if (!sDatabase.RunQuery(err, Inserts.str().c_str()))
789  _log(DATABASE__ERROR, "SavePinContents - unable to save contents - %s", err.c_str());
790  }
791 }
792 
794 {
795  DBerror err;
796  sDatabase.RunQuery(err, "DELETE FROM piPins WHERE pinID = %u", pinID);
797  sDatabase.RunQuery(err, "DELETE FROM entity WHERE itemID = %u", pinID);
798  sDatabase.RunQuery(err, "DELETE FROM entity_attributes WHERE itemID = %u", pinID);
799 }
800 
801 void PlanetDB::RemoveHead(uint32 ecuID, uint32 headID)
802 {
803  DBerror err;
804  sDatabase.RunQuery(err, "DELETE FROM piECUHeads WHERE ecuID = %u AND headID = %u", ecuID, headID);
805 }
806 
808 {
809  DBerror err;
810  sDatabase.RunQuery(err, "DELETE FROM piLinks WHERE linkID = %u", linkID);
811 }
812 
814 {
815  DBerror err;
816  sDatabase.RunQuery(err, "DELETE FROM piRoutes WHERE routeID = %u", routeID);
817 }
818 
820 {
821  DBerror err;
822  sDatabase.RunQuery(err, "DELETE FROM piPinContents WHERE pinID = %u", pinID);
823 }
824 
826 {
827  DBerror err;
828  sDatabase.RunQuery(err, "DELETE FROM piLaunches WHERE itemID = %u", contID);
829 }
830 
831 void PlanetDB::DeleteColony(uint32 ccPinID, uint32 planetID, uint32 charID)
832 {
834  DBerror err;
835  sDatabase.RunQuery(err, "DELETE FROM entity WHERE locationID = %u AND ownerID = %u", planetID, charID);
836  sDatabase.RunQuery(err, "DELETE FROM piCCPin WHERE pinID = %u", ccPinID);
837  sDatabase.RunQuery(err, "DELETE FROM piPins WHERE ccPinID = %u", ccPinID);
838  sDatabase.RunQuery(err, "DELETE FROM piLinks WHERE ccPinID = %u", ccPinID);
839  sDatabase.RunQuery(err, "DELETE FROM piRoutes WHERE ccPinID = %u", ccPinID);
840  sDatabase.RunQuery(err, "DELETE FROM piPlanets WHERE ccPinID = %u", ccPinID);
841  sDatabase.RunQuery(err, "DELETE FROM piECUHeads WHERE ccPinID = %u", ccPinID);
842  sDatabase.RunQuery(err, "DELETE FROM piPinContents WHERE ccPinID = %u", ccPinID);
843 }
double z
Definition: EVE_Planet.h:93
float longitude
Definition: EVE_Planet.h:197
Base Python wire object.
Definition: PyRep.h:66
std::map< uint32, PI_Pin > pins
Definition: EVE_Planet.h:235
std::map< uint16, PI_Route > routes
Definition: EVE_Planet.h:239
void UpdatePlanetPins(uint32 ccPinID, uint8 pins=1)
Definition: PlanetDB.cpp:101
std::list< uint32 > path
Definition: EVE_Planet.h:132
unsigned __int8 uint8
Definition: eve-compat.h:46
int8 priority
Definition: EVE_Planet.h:127
std::map< uint16, uint32 > contents
Definition: EVE_Planet.h:204
void SavePins(PI_CCPin *ccPin)
Definition: PlanetDB.cpp:442
#define sDatabase
Definition: dbcore.h:199
void UpdatePins(uint32 pinID, PI_CCPin *ccPin)
Definition: PlanetDB.cpp:485
bool isLaunchable
Definition: EVE_Planet.h:176
#define _log(type, fmt,...)
Definition: logsys.h:124
void SaveCommandCenter(uint32 pinID, uint32 charID, uint32 planetID, uint32 typeID, double latitude, double longitude)
Definition: PlanetDB.cpp:420
uint16 commodityQuantity
Definition: EVE_Planet.h:129
PyObjectEx * DBResultToCRowset(DBQueryResult &result)
Definition: EVEDBUtils.cpp:402
uint16 programType
Definition: EVE_Planet.h:187
bool isCommandCenter
Definition: EVE_Planet.h:177
void LoadHeads(uint32 ecuID, std::map< uint16, PI_Heads > &heads)
Definition: PlanetDB.cpp:382
int32 GetInt(uint32 index) const
Definition: dbcore.cpp:635
uint32 itemID
Definition: EVE_Planet.h:87
void SavePinLevel(uint32 pinID, uint8 level)
Definition: PlanetDB.cpp:617
uint8 level
Definition: EVE_Planet.h:231
void SaveLaunch(uint32 contID, uint32 charID, uint32 systemID, uint32 planetID, GPoint &pos)
Definition: PlanetDB.cpp:408
void GetSchematicData(DBQueryResult &res)
Definition: PlanetDB.cpp:47
void SaveHeads(uint32 ccPinID, uint32 ownerID, uint32 ecuID, std::map< uint16, PI_Heads > &heads)
Definition: PlanetDB.cpp:595
static uint32 GetLaunchItemID(uint32 launchID)
Definition: PlanetDB.cpp:172
PyRep * GetMyLaunchesDetails(uint32 charID)
Definition: PlanetDB.cpp:111
int64 launchTime
Definition: EVE_Planet.h:90
double GetDouble(uint32 index) const
Definition: dbcore.cpp:693
uint8 status
Definition: EVE_Planet.h:85
uint32 solarSystemID
Definition: EVE_Planet.h:88
uint32 ownerID
Definition: EVE_Planet.h:189
double x
Definition: EVE_Planet.h:91
const char * GetError() const
Definition: dbcore.h:46
uint32 qtyPerCycle
Definition: EVE_Planet.h:188
uint16 level
Definition: EVE_Planet.h:184
void LoadContents(uint32 pinID, std::map< uint16, uint32 > &contents)
Definition: PlanetDB.cpp:361
uint16 SaveRoute(uint32 ccPinID, PI_Route &route)
Definition: PlanetDB.cpp:667
void UpdateECUPin(uint32 pinID, PI_CCPin *ccPin)
Definition: PlanetDB.cpp:536
static uint32 GetLaunchPlanet(uint32 launchID)
Definition: PlanetDB.cpp:136
static void DeleteLaunch(uint32 contID)
Definition: PlanetDB.cpp:825
uint16 typeID
Definition: EVE_Planet.h:136
GaFloat x
Definition: GaTypes.h:207
std::map< uint16, PI_Heads > heads
Definition: EVE_Planet.h:203
bool LoadColony(uint32 charID, uint32 planetID, PI_CCPin *ccPin)
Definition: PlanetDB.cpp:212
void RemoveContents(uint32 pinID)
Definition: PlanetDB.cpp:819
void LoadRoutes(uint32 ccPinID, std::map< uint16, PI_Route > &routes)
Definition: PlanetDB.cpp:316
bool GetRow(DBResultRow &into)
Definition: dbcore.cpp:552
uint16 commodityTypeID
Definition: EVE_Planet.h:128
uint32 srcPinID
Definition: EVE_Planet.h:130
uint32 ecuPinID
Definition: EVE_Planet.h:137
int8 state
Definition: EVE_Planet.h:126
uint32 planetID
Definition: EVE_Planet.h:89
uint32 launchID
Definition: EVE_Planet.h:86
Definition: gpoint.h:33
int64 lastRunTime
Definition: EVE_Planet.h:190
bool isECU
Definition: EVE_Planet.h:171
float latitude
Definition: EVE_Planet.h:196
const char * c_str() const
Definition: dbcore.h:48
void SaveRoutes(PI_CCPin *ccPin)
Definition: PlanetDB.cpp:689
bool isStorage
Definition: EVE_Planet.h:173
static const GPoint NULL_ORIGIN(0, 0, 0)
uint16 typeID
Definition: EVE_Planet.h:185
PyObject * DBResultToRowset(DBQueryResult &result)
Definition: EVEDBUtils.cpp:81
void RemoveHead(uint32 ecuID, uint32 headID)
Definition: PlanetDB.cpp:801
bool hasReceivedInputs
Definition: EVE_Planet.h:180
float headRadius
Definition: EVE_Planet.h:201
int64 lastLaunchTime
Definition: EVE_Planet.h:194
bool isProcess
Definition: EVE_Planet.h:174
void LoadPins(uint32 ccPinID, std::map< uint32, PI_Pin > &pins)
Definition: PlanetDB.cpp:234
static void GetLaunchDetails(Launch::Data &data)
Definition: PlanetDB.cpp:151
double latitude
Definition: EVE_Planet.h:138
void SaveContents(PI_CCPin *ccPin)
Definition: PlanetDB.cpp:733
double longitude
Definition: EVE_Planet.h:139
void SaveLinks(PI_CCPin *ccPin)
Definition: PlanetDB.cpp:626
uint32 ccPinID
Definition: EVE_Planet.h:232
std::map< uint32, PI_Link > links
Definition: EVE_Planet.h:237
unsigned __int32 uint32
Definition: eve-compat.h:50
void RemovePin(uint32 pinID)
Definition: PlanetDB.cpp:793
double y
Definition: EVE_Planet.h:92
void DeleteColony(uint32 ccPinID, uint32 planetID, uint32 charID)
Definition: PlanetDB.cpp:831
bool receivedInputsLastCycle
Definition: EVE_Planet.h:181
GaFloat y
Definition: GaTypes.h:207
double GetFileTimeNow()
Definition: utils_time.cpp:84
void RemoveRoute(uint16 routeID)
Definition: PlanetDB.cpp:813
int64 installTime
Definition: EVE_Planet.h:193
DBerror error
Definition: dbcore.h:69
int64 cycleTime
Definition: EVE_Planet.h:191
void AddPlanetForChar(uint32 solarSystemID, uint32 planetID, uint32 charID, uint32 ccPinID, uint16 typeID)
Definition: PlanetDB.cpp:90
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
void SaveLinkLevel(uint32 linkID, uint8 level)
Definition: PlanetDB.cpp:658
void RemoveLink(uint32 linkID)
Definition: PlanetDB.cpp:807
PyRep * GetPlanetsForChar(uint32 charID)
Definition: PlanetDB.cpp:75
void SavePinContents(uint32 ccPinID, uint32 pinID, std::map< uint16, uint32 > &contents)
Definition: PlanetDB.cpp:767
int8 state
Definition: EVE_Planet.h:183
size_t GetRowCount()
Definition: dbcore.h:72
void SaveCCLevel(uint32 pinID, uint8 level)
Definition: PlanetDB.cpp:432
static void GetExtractorsForPlanet(uint32 planetID, DBQueryResult &res)
Definition: PlanetDB.cpp:194
void GetPlanetData(DBQueryResult &res)
Definition: PlanetDB.cpp:61
int64 GetInt64(uint32 index) const
Definition: dbcore.cpp:670
uint32 destPinID
Definition: EVE_Planet.h:131
static void UpdateLaunchStatus(uint32 itemID, uint8 status)
Definition: PlanetDB.cpp:187
unsigned __int16 uint16
Definition: eve-compat.h:48
uint16 schematicID
Definition: EVE_Planet.h:186
void UpdatePinTimes(PI_CCPin *ccPin)
Definition: PlanetDB.cpp:563
Definition: dbcore.h:39
GaFloat z
Definition: GaTypes.h:207
void GetSchematicTimes(DBQueryResult &res)
Definition: PlanetDB.cpp:54
int64 expiryTime
Definition: EVE_Planet.h:192
void LoadLinks(uint32 ccPinID, std::map< uint32, PI_Link > &links)
Definition: PlanetDB.cpp:290
static GPoint GetLaunchPos(uint32 launchID)
Definition: PlanetDB.cpp:121