EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SovereigntyDataMgr.cpp
Go to the documentation of this file.
1 
11 #include <boost/range/iterator_range.hpp>
13 #include "database/EVEDBUtils.h"
14 #include "packets/Sovereignty.h"
15 #include "StaticDataMgr.h"
16 #include "Client.h"
17 
19 {
20  m_sovData.clear();
21 }
22 
24 {
25  //Clear();
26 }
27 
29 {
30  Populate();
31  sLog.Blue(" SovereigntyDataMgr", "Sovereignty Data Manager Initialized.");
32  return 1;
33 }
34 
36 {
38  //for (auto cur : m_sovPyData)
39  // PySafeDecRef(cur.second);
40 
41  sLog.Warning(" SovereigntyDataMgr", "Sovereignty Data Manager has been closed.");
42 }
43 
45 {
46  //for (auto cur : m_sovPyData)
47  // PySafeDecRef(cur.second);
48  m_sovData.clear();
49  //m_sovPyData.clear();
50 }
51 
53 {
54  double start = GetTimeMSeconds();
55  DBQueryResult *res = new DBQueryResult();
56  DBResultRow row;
57 
58  std::map<int8, int32>::iterator itr;
59 
60  //This data is added by solar system, so we need to use that tag here
61  auto &bySolar = m_sovData.get<SovDataBySolarSystem>();
62 
64  while (res->GetRow(row))
65  {
67  sData.solarSystemID = row.GetUInt(0);
68  sData.constellationID = row.GetUInt(1);
69  sData.regionID = row.GetUInt(2);
70  sData.corporationID = row.GetUInt(3);
71  sData.allianceID = row.GetUInt(4);
72  sData.claimStructureID = row.GetUInt(5);
73  sData.claimTime = row.GetInt64(6);
74  sData.hubID = row.GetUInt(7);
75  sData.contested = row.GetUInt(8);
76  sData.stationCount = row.GetUInt(9);
77  sData.militaryPoints = row.GetUInt(10);
78  sData.industrialPoints = row.GetUInt(11);
79  sData.claimID = row.GetUInt(12);
80  sData.beaconID = row.GetUInt(13);
81  sData.jammerID = row.GetUInt(14);
82  bySolar.insert(sData);
83  }
84 
85  sLog.Cyan(" SovereigntyDataMgr", "%u Sovereignty data sets loaded in %.3fms.",
86  bySolar.size(), (GetTimeMSeconds() - start));
87 
88  //cleanup
89  SafeDelete(res);
90 }
91 
93 {
94  /* return info about loaded items? */
95  /*
96  * m_sovData;
97  *
98  */
99 }
100 
102 {
103  auto it = m_sovData.get<SovDataBySolarSystem>().find(systemID);
104  if (it != m_sovData.get<SovDataBySolarSystem>().end())
105  {
106  SovereigntyData sData = *it;
107  return sData.allianceID;
108  }
109  return 0;
110 }
111 
113 {
114  auto it = m_sovData.get<SovDataBySolarSystem>().find(systemID);
115  if (it != m_sovData.get<SovDataBySolarSystem>().end())
116  {
117  SovereigntyData sData = *it;
118  return sData;
119  } else
120  {
121  //Return empty object in case we don't find anything
122  SovereigntyData sData;
123  return sData;
124  }
125 }
126 
128 {
129  SystemData sysData = SystemData();
130  PyDict *args = new PyDict();
131 
132  //Figure out if this system is in empire space
133  sDataMgr.GetSystemData(systemID, sysData);
134 
135  if (sysData.factionID)
136  { //If we have a factionID, we can set the system's sov data to it
137  args->SetItemString("contested", PyStatic.NewZero());
138  args->SetItemString("corporationID", PyStatic.NewZero());
139  args->SetItemString("claimTime", new PyLong(0));
140  args->SetItemString("claimStructureID", PyStatic.NewZero());
141  args->SetItemString("hubID", PyStatic.NewZero());
142  args->SetItemString("allianceID", new PyInt(sysData.factionID));
143  args->SetItemString("solarSystemID", new PyInt(systemID));
144  _log(SOV__INFO, "SovereigntyDataMgr::GetSystemSovereignty(): Faction system %u found, assigning factionID as allianceID.", systemID);
145  }
146  else
147  {
148  auto it = m_sovData.get<SovDataBySolarSystem>().find(systemID);
149  if (it != m_sovData.get<SovDataBySolarSystem>().end())
150  {
151  SovereigntyData sData = *it;
152  _log(SOV__INFO, "SovereigntyDataMgr::GetSystemSovereignty(): Found sov data for solarSystemID %u.", systemID);
153  _log(SOV__DEBUG, "== data (GetSystemSovereignty()) ==");
154  _log(SOV__DEBUG, "claimID: %u", sData.claimID);
155  _log(SOV__DEBUG, "solarSystemID: %u", sData.solarSystemID);
156  _log(SOV__DEBUG, "constellationID: %u", sData.constellationID);
157  _log(SOV__DEBUG, "corporationID: %u", sData.corporationID);
158  _log(SOV__DEBUG, "allianceID: %u", sData.allianceID);
159  _log(SOV__DEBUG, "claimStructureID: %u", sData.claimStructureID);
160  _log(SOV__DEBUG, "==========");
161  args->SetItemString("contested", new PyInt(sData.contested));
162  args->SetItemString("corporationID", new PyInt(sData.corporationID));
163  args->SetItemString("claimTime", new PyLong(sData.claimTime));
164  args->SetItemString("claimStructureID", new PyInt(sData.claimStructureID));
165  args->SetItemString("hubID", new PyInt(sData.hubID));
166  args->SetItemString("allianceID", new PyInt(sData.allianceID));
167  args->SetItemString("solarSystemID", new PyInt(sData.solarSystemID));
168  }
169  else
170  {
171  _log(SOV__INFO, "SovereigntyDataMgr::GetSystemSovereignty(): No data for solarSystemID %u. Sending blank SovereigntyData object.", systemID);
172  args->SetItemString("contested", PyStatic.NewNone());
173  args->SetItemString("corporationID", PyStatic.NewNone());
174  args->SetItemString("claimTime", PyStatic.NewNone());
175  args->SetItemString("claimStructureID", PyStatic.NewNone());
176  args->SetItemString("hubID", PyStatic.NewNone());
177  args->SetItemString("allianceID", PyStatic.NewNone());
178  args->SetItemString("solarSystemID", new PyInt(systemID));
179  }
180  }
181  return new PyObject("util.KeyVal", args);
182 }
183 
184 PyRep *SovereigntyDataMgr::GetAllianceSystems() //Get all systems associated with all alliances?
185 {
186  PyList *list = new PyList();
187  for (auto &it : m_sovData.get<SovDataBySolarSystem>())
188  {
189  _log(SOV__DEBUG, "== data (GetAllianceSystems()) ==");
190  _log(SOV__DEBUG, "claimID: %u", it.claimID);
191  _log(SOV__DEBUG, "solarSystemID: %u", it.solarSystemID);
192  _log(SOV__DEBUG, "allianceID: %u", it.allianceID);
193  _log(SOV__DEBUG, "==========");
194 
195  PyDict *dict = new PyDict();
196  dict->SetItemString("allianceID", new PyInt(it.allianceID));
197  dict->SetItemString("solarSystemID", new PyInt(it.solarSystemID));
198  list->AddItem(new PyObject("util.KeyVal", dict));
199  }
200  return list;
201 }
202 
203 PyRep *SovereigntyDataMgr::GetAllianceBeacons(uint32 allianceID) //Get all beacons associated with current alliance
204 {
205  PyList* list = new PyList();
206  for (SovereigntyData const &sData : boost::make_iterator_range(
207  m_sovData.get<SovDataByAlliance>().equal_range(allianceID)))
208  {
209  // Don't add systems where no beacon exists
210  if (sData.beaconID == 0) {
211  continue;
212  }
213  _log(SOV__DEBUG, "== data (GetAllianceBeacons()) ==");
214  _log(SOV__DEBUG, "solarSystemID: %u", sData.solarSystemID);
215  _log(SOV__DEBUG, "beaconID: %u", sData.beaconID);
216  _log(SOV__DEBUG, "==========");
217 
218  PyTuple* tuple = new PyTuple(3);
219  tuple->SetItem(0, new PyInt(sData.solarSystemID));
220  tuple->SetItem(1, new PyInt(sData.beaconID));
222  list->AddItem(tuple);
223  }
224  return list;
225 }
226 
228 {
229  DBRowDescriptor *header = new DBRowDescriptor();
230  header->AddColumn("locationID", DBTYPE_I4);
231  header->AddColumn("allianceID", DBTYPE_I4);
232  header->AddColumn("stationCount", DBTYPE_I2);
233  header->AddColumn("militaryPoints", DBTYPE_I2);
234  header->AddColumn("industrialPoints", DBTYPE_I2);
235  header->AddColumn("claimedFor", DBTYPE_I4);
236  CRowSet *rowset = new CRowSet(&header);
237 
238  if (IsConstellationID(locationID))
239  {
240  for (SovereigntyData const &sData : boost::make_iterator_range(
241  m_sovData.get<SovDataByConstellation>().equal_range(locationID)))
242  {
243  PyPackedRow *row = rowset->NewRow();
244  row->SetField("locationID", new PyInt(sData.solarSystemID));
245  row->SetField("allianceID", new PyInt(sData.allianceID));
246  row->SetField("stationCount", new PyInt(sData.stationCount));
247  row->SetField("militaryPoints", new PyInt(sData.militaryPoints));
248  row->SetField("industrialPoints", new PyInt(sData.industrialPoints));
249  row->SetField("claimedFor", new PyInt(sData.allianceID));
250  }
251  }
252  //Get all unique alliances in the region who hold sovereignty
253  else if (IsRegionID(locationID))
254  {
255  std::vector<uint32> cv;
256  for (SovereigntyData const &sData : boost::make_iterator_range(
257  m_sovData.get<SovDataByRegion>().equal_range(locationID)))
258  {
259  if (!(std::find(cv.begin(), cv.end(), sData.constellationID) != cv.end()))
260  {
261  PyPackedRow *row = rowset->NewRow();
262  row->SetField("locationID", new PyInt(sData.constellationID));
263  row->SetField("allianceID", new PyInt(sData.allianceID));
264  row->SetField("stationCount", new PyInt(sData.stationCount));
265  row->SetField("militaryPoints", new PyInt(sData.militaryPoints));
266  row->SetField("industrialPoints", new PyInt(sData.industrialPoints));
267  row->SetField("claimedFor", new PyInt(sData.allianceID));
268  cv.push_back(sData.constellationID);
269  }
270  }
271  }
272  else if (sDataMgr.IsSolarSystem(locationID))
273  {
274  for (SovereigntyData const &sData : boost::make_iterator_range(
275  m_sovData.get<SovDataByRegion>().equal_range(locationID)))
276  {
277  PyPackedRow *row = rowset->NewRow();
278  row->SetField("locationID", new PyInt(sData.solarSystemID));
279  row->SetField("allianceID", new PyInt(sData.allianceID));
280  row->SetField("stationCount", new PyInt(sData.stationCount));
281  row->SetField("militaryPoints", new PyInt(sData.militaryPoints));
282  row->SetField("industrialPoints", new PyInt(sData.industrialPoints));
283  row->SetField("claimedFor", new PyInt(sData.allianceID));
284  }
285  }
286 
287  return rowset;
288 }
289 
291 {
292  _log(SOV__INFO, "AddSovClaim() - Adding claim for %u to DataMgr...", data.solarSystemID);
293 
294  //Add a new sovereignty claim to DB
295  uint32 claimID(0);
296  SovereigntyDB::AddSovereigntyData(data, claimID);
297  _log(SOV__DEBUG, "AddSovClaim() - ClaimID %u added to DB...", claimID);
298 
299  //Define our view from container
300  auto &bySolar = m_sovData.get<SovDataBySolarSystem>();
301 
302  //Delete existing records in container which match the systemID
303  auto it = m_sovData.get<SovDataBySolarSystem>().find(data.solarSystemID);
304  if (it != m_sovData.get<SovDataBySolarSystem>().end())
305  {
306  bySolar.erase(data.solarSystemID);
307  }
308 
310 }
311 
312 void SovereigntyDataMgr::MarkContested(uint32 systemID, bool contested)
313 {
314  _log(SOV__INFO, "MarkContested() - Marking system %u as state %u", systemID, int(contested));
315 
316  //Update state in DB
317  SovereigntyDB::SetContested(systemID, contested);
318 
319  //Re-sync the claim data
320  UpdateClaim(systemID);
321 }
322 
324 {
325  _log(SOV__INFO, "RemoveSovClaim() - Removing claim for %u from DataMgr...", systemID);
326 
327  //Delete sovereignty claim from DB
329  _log(SOV__DEBUG, "RemoveSovClaim() - Claim for %u removed from DB...", systemID);
330 
331  //Remove claim from container
332  auto &bySolar = m_sovData.get<SovDataBySolarSystem>();
333  bySolar.erase(systemID);
334 }
335 
337 {
338  _log(SOV__INFO, "UpdateSystemHubID() - Updating hubID for system %u with hub %u", systemID, hubID);
339 
340  //Update state in DB
341  SovereigntyDB::SetHubID(systemID, hubID);
342 
343  //Re-sync the claim data
344  UpdateClaim(systemID);
345 }
346 
348 {
349  _log(SOV__INFO, "UpdateSystemJammerID() - Updating jammerID for system %u with jammer %u", systemID, jammerID);
350 
351  //Update state in DB
352  SovereigntyDB::SetJammerID(systemID, jammerID);
353 
354  //Re-sync the claim data
355  UpdateClaim(systemID);
356 }
357 
359 {
360  _log(SOV__INFO, "UpdateSystemBeaconID() - Updating beaconID for system %u with beacon %u", systemID, beaconID);
361 
362  //Update state in DB
363  SovereigntyDB::SetBeaconID(systemID, beaconID);
364 
365  //Re-sync the claim data
366  UpdateClaim(systemID);
367 }
368 
369 // Update managed claim data from database, effectively a re-sync
371 {
372  //Define our view from container
373  auto &bySolar = m_sovData.get<SovDataBySolarSystem>();
374 
375  //Remove old from container
376  bySolar.erase(systemID);
377 
378  //Get the data from the DB, this will avoid inconsistencies
379  DBQueryResult *res = new DBQueryResult();
380  DBResultRow row;
381 
383  while (res->GetRow(row))
384  {
386  sData.solarSystemID = row.GetUInt(0);
387  sData.constellationID = row.GetUInt(1);
388  sData.regionID = row.GetUInt(2);
389  sData.corporationID = row.GetUInt(3);
390  sData.allianceID = row.GetUInt(4);
391  sData.claimStructureID = row.GetUInt(5);
392  sData.claimTime = row.GetInt64(6);
393  sData.hubID = row.GetUInt(7);
394  sData.contested = row.GetUInt(8);
395  sData.stationCount = row.GetUInt(9);
396  sData.militaryPoints = row.GetUInt(10);
397  sData.industrialPoints = row.GetUInt(11);
398  sData.claimID = row.GetUInt(12);
399  sData.beaconID = row.GetUInt(13);
400  sData.jammerID = row.GetUInt(14);
401  bySolar.insert(sData);
402  }
403  SafeDelete(res);
404 }
Base Python wire object.
Definition: PyRep.h:66
static void RemoveSovereigntyData(uint32 systemID)
#define IsConstellationID(itemID)
Definition: EVE_Defines.h:276
void UpdateClaim(uint32 systemID)
#define _log(type, fmt,...)
Definition: logsys.h:124
Python's dictionary.
Definition: PyRep.h:719
uint32 factionID
uint32 GetUInt(uint32 index) const
Definition: dbcore.cpp:658
void AddColumn(const char *name, DBTYPE type)
Definition: PyDatabase.cpp:96
void UpdateSystemJammerID(uint32 systemID, uint32 jammerID)
SovereigntyData GetSovereigntyData(uint32 systemID)
Python tuple.
Definition: PyRep.h:567
void RemoveSovClaim(uint32 systemID)
static void SetBeaconID(uint32 systemID, uint32 beaconID)
void AddItem(PyRep *i)
Definition: PyRep.h:701
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
* args
bool GetRow(DBResultRow &into)
Definition: dbcore.cpp:552
static void SetJammerID(uint32 systemID, uint32 jammerID)
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
static void AddSovereigntyData(SovereigntyData data, uint32 &claimID)
double GetTimeMSeconds()
Definition: utils_time.cpp:104
Python object.
Definition: PyRep.h:826
Python object "blue.DBRowDescriptor".
Definition: PyDatabase.h:41
PyRep * GetAllianceBeacons(uint32 allianceID)
void SetItem(size_t index, PyRep *object)
Stores Python object.
Definition: PyRep.h:610
Python object "dbutil.CRowset".
Definition: PyDatabase.h:124
uint32 locationID() const
Python integer.
Definition: PyRep.h:231
PyRep * GetSystemSovereignty(uint32 systemID)
#define PyStatic
Definition: PyRep.h:1209
void AddSovClaim(SovereigntyData data)
void UpdateSystemHubID(uint32 systemID, uint32 hubID)
uint32 GetSystemAllianceID(uint32 systemID)
unsigned __int32 uint32
Definition: eve-compat.h:50
PyRep * GetCurrentSovData(uint32 locationID)
bool SetField(uint32 index, PyRep *value)
Definition: PyRep.cpp:1031
PyPackedRow * NewRow()
Definition: PyDatabase.cpp:132
Packed row.
Definition: PyRep.h:961
static void SetContested(uint32 systemID, bool contested)
static void SetHubID(uint32 systemID, uint32 hubID)
#define IsRegionID(itemID)
Definition: EVE_Defines.h:273
void MarkContested(uint32 systemID, bool contested)
int64 GetInt64(uint32 index) const
Definition: dbcore.cpp:670
void UpdateSystemBeaconID(uint32 systemID, uint32 beaconID)
SovDataContainer m_sovData
static void GetSovereigntyDataForSystem(DBQueryResult &res, uint32 systemID)
static void GetSovereigntyData(DBQueryResult &res)
Python list.
Definition: PyRep.h:639
void SetItemString(const char *key, PyRep *value)
SetItemString adds or sets a database entry.
Definition: PyRep.h:812
Python long integer.
Definition: PyRep.h:261
#define sDataMgr