EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AccountDB.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 "account/AccountDB.h"
30 
31 /*
32  * ACCOUNT__ERROR
33  * ACCOUNT__WARNING
34  * ACCOUNT__INFO
35  * ACCOUNT__MESSAGE
36  * ACCOUNT__TRACE
37  * ACCOUNT__CALL
38  * ACCOUNT__CALL_DUMP
39  * ACCOUNT__RSP_DUMP
40  * ACCOUNT__DB_ERROR
41  * ACCOUNT__DB_WARNING
42  * ACCOUNT__DB_INFO
43  * ACCOUNT__DB_MESSAGE
44  */
45 
46 double AccountDB::OfflineFundXfer(uint32 charID, double amount, uint8 type)
47 {
48  std::string from = "";
49  switch (type) {
50  case Account::CreditType::ISK: from = "balance"; break;
51  case Account::CreditType::AURUM: from = "aurBalance"; break;
52  case Account::CreditType::MPLEX: return 0; //make error..this isnt used yet
53  }
54 
55  double balance = 0;
56  DBQueryResult res;
57  sDatabase.RunQuery(res, "SELECT %s FROM chrCharacters WHERE characterID = %u", from.c_str(), charID);
58  DBResultRow row;
59  if (res.GetRow(row))
60  balance = row.GetDouble(0);
61 
62  balance += amount;
63  DBerror err;
64  sDatabase.RunQuery(err, "UPDATE chrCharacters SET %s = %f WHERE characterID = %u", from.c_str(), balance, charID);
65  return balance;
66 }
67 
68 
69 double AccountDB::GetCorpBalance(uint32 corpID, uint16 accountKey)
70 {
71  std::string acctKey = "";
72  switch (accountKey) {
73  case Account::KeyType::Cash: acctKey = "balance1"; break;
74  case Account::KeyType::Cash2: acctKey = "balance2"; break;
75  case Account::KeyType::Cash3: acctKey = "balance3"; break;
76  case Account::KeyType::Cash4: acctKey = "balance4"; break;
77  case Account::KeyType::Cash5: acctKey = "balance5"; break;
78  case Account::KeyType::Cash6: acctKey = "balance6"; break;
79  case Account::KeyType::Cash7: acctKey = "balance7"; break;
80  }
81  DBQueryResult res;
82  if (!sDatabase.RunQuery(res, "SELECT %s FROM crpWalletDivisons WHERE corporationID = %u ", acctKey.c_str(), corpID)) {
83  codelog(DATABASE__ERROR, "Error in query: %s", res.error.c_str());
84  return 0;
85  }
86  DBResultRow row;
87  if (!res.GetRow(row))
88  return 0;
89  return row.GetDouble(0);
90 }
91 
92 void AccountDB::UpdateCorpBalance(uint32 corpID, uint16 accountKey, double amount)
93 {
94  std::string acctKey = "";
95  switch (accountKey) {
96  case Account::KeyType::Cash: acctKey = "balance1"; break;
97  case Account::KeyType::Cash2: acctKey = "balance2"; break;
98  case Account::KeyType::Cash3: acctKey = "balance3"; break;
99  case Account::KeyType::Cash4: acctKey = "balance4"; break;
100  case Account::KeyType::Cash5: acctKey = "balance5"; break;
101  case Account::KeyType::Cash6: acctKey = "balance6"; break;
102  case Account::KeyType::Cash7: acctKey = "balance7"; break;
103  }
104  DBerror err;
105  sDatabase.RunQuery(err, "UPDATE crpWalletDivisons SET %s = %.2f WHERE corporationID = %u ", acctKey.c_str(), amount, corpID);
106 }
107 
108 
110  DBQueryResult res;
111  if (!sDatabase.RunQuery(res,
112  "SELECT balance1, balance2, balance3, balance4, balance5, balance6, balance7"
113  " FROM crpWalletDivisons"
114  " WHERE corporationID = %u", corpID))
115  {
116  codelog(DATABASE__ERROR, "Error in query: %s", res.error.c_str());
117  return nullptr;
118  }
119 
120  DBResultRow row;
121  PyList *list = new PyList();
122  if (res.GetRow(row))
123  for (int8 i = 0; i < 7; ++i) {
124  PyDict *dict = new PyDict();
125  dict->SetItemString("key", new PyInt(1000 + i));
126  dict->SetItemString("balance", new PyFloat(row.GetDouble(i)));
127  list->AddItem(new PyObject("util.KeyVal", dict));
128  }
129 
130  if (is_log_enabled(ACCOUNT__RSP_DUMP))
131  list->Dump(ACCOUNT__RSP_DUMP, " ");
132  return list;
133 }
134 
135 PyRep* AccountDB::GetJournal(uint32 ownerID, int8 entryTypeID, uint16 accountKey, int64 fromDate, bool reverse/*false*/)
136 {
137  std::string tblName = "jnlCharacters";
138  if (IsCorp(ownerID))
139  tblName = "jnlCorporations";
140 
141  std::string entryType = "";
142  if (entryTypeID) {
143  entryType = " AND entryTypeID = ";
144  entryType += std::to_string(entryTypeID);
145  }
146 
147  std::string sort = "";
148  if (reverse)
149  sort = "";
150 
151  DBQueryResult res;
152  if (!sDatabase.RunQuery(res,
153  "SELECT transactionID, transactionDate, referenceID, entryTypeID, ownerID1, ownerID2, accountKey, amount,"
154  " balance, description, currency, 1 AS sortValue"
155  " FROM %s"
156  " WHERE transactionDate > %li AND accountKey = %u %s AND ownerID = %u %s",
157  tblName.c_str(), fromDate, accountKey, entryType.c_str(), ownerID, sort.c_str()))
158  {
159  codelog(DATABASE__ERROR, "Error in query: %s", res.error.c_str());
160  return nullptr;
161  }
162 
163  return DBResultToCRowset(res);
164 }
165 
166 void AccountDB::AddJournalEntry(uint32 ownerID, int8 entryTypeID, uint32 ownerFromID, uint32 ownerToID, int8 currency, \
167  uint16 accountKey, double amount, double newBalance, std::string description, uint32 referenceID/*0*/ )
168 {
169  if (entryTypeID == Journal::EntryType::SkipLog)
170  return;
171  // account key 0 is usually sent by the client, it should be the main cash account
172  if (accountKey == 0)
173  accountKey = Account::KeyType::Cash;
174 
175  std::string eDesc;
176  sDatabase.DoEscapeString(eDesc, description);
177 
178  std::string tblName = "jnlCharacters";
179  if (IsCorp(ownerID))
180  tblName = "jnlCorporations";
181 
182  // dont care if this fails...
183  DBerror err;
184  sDatabase.RunQuery(err,
185  "INSERT INTO %s (ownerID, entryTypeID, accountKey, transactionDate, ownerID1, ownerID2, referenceID, currency, amount, balance, description)"
186  " VALUES (%u,%u,%u,%f,%u,%u,%u,%i,%.2f,%.2f,'%s')",
187  tblName.c_str(), ownerID, entryTypeID, accountKey, GetFileTimeNow(), ownerFromID, ownerToID, referenceID, currency, amount, newBalance, eDesc.c_str());
188 }
Base Python wire object.
Definition: PyRep.h:66
unsigned __int8 uint8
Definition: eve-compat.h:46
#define sDatabase
Definition: dbcore.h:199
PyObjectEx * DBResultToCRowset(DBQueryResult &result)
Definition: EVEDBUtils.cpp:402
static void AddJournalEntry(uint32 ownerID, int8 entryTypeID, uint32 ownerFromID, uint32 ownerToID, int8 currency, uint16 accountKey, double amount, double newBalance, std::string description, uint32 referenceID=0)
Definition: AccountDB.cpp:166
Python's dictionary.
Definition: PyRep.h:719
double GetDouble(uint32 index) const
Definition: dbcore.cpp:693
Python floating point number.
Definition: PyRep.h:292
void Dump(FILE *into, const char *pfx) const
Dumps object to file.
Definition: PyRep.cpp:84
signed __int8 int8
Definition: eve-compat.h:45
void AddItem(PyRep *i)
Definition: PyRep.h:701
bool GetRow(DBResultRow &into)
Definition: dbcore.cpp:552
#define is_log_enabled(type)
Definition: logsys.h:78
const char * c_str() const
Definition: dbcore.h:48
Python object.
Definition: PyRep.h:826
#define codelog(type, fmt,...)
Definition: logsys.h:128
Python integer.
Definition: PyRep.h:231
PyRep * GetWalletDivisionsInfo(uint32 corpID)
Definition: AccountDB.cpp:109
unsigned __int32 uint32
Definition: eve-compat.h:50
#define IsCorp(itemID)
Definition: EVE_Defines.h:234
double GetFileTimeNow()
Definition: utils_time.cpp:84
DBerror error
Definition: dbcore.h:69
signed __int64 int64
Definition: eve-compat.h:51
static void UpdateCorpBalance(uint32 corpID, uint16 accountKey, double amount)
Definition: AccountDB.cpp:92
static double GetCorpBalance(uint32 corpID, uint16 accountKey)
Definition: AccountDB.cpp:69
unsigned __int16 uint16
Definition: eve-compat.h:48
Definition: dbcore.h:39
static double OfflineFundXfer(uint32 charID, double amount, uint8 type=Account::CreditType::ISK)
Definition: AccountDB.cpp:46
Python list.
Definition: PyRep.h:639
PyRep * GetJournal(uint32 ownerID, int8 entryTypeID, uint16 accountKey, int64 fromDate, bool reverse=false)
Definition: AccountDB.cpp:135
void SetItemString(const char *key, PyRep *value)
SetItemString adds or sets a database entry.
Definition: PyRep.h:812