This repository was archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathioaccount.cpp
More file actions
126 lines (103 loc) · 4.13 KB
/
Copy pathioaccount.cpp
File metadata and controls
126 lines (103 loc) · 4.13 KB
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
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
// Base class for the Account Loader/Saver
//////////////////////////////////////////////////////////////////////
// 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 "otpch.h"
#include "ioaccount.h"
#include "database_driver.h"
#include "game.h"
#include "configmanager.h"
extern Game g_game;
extern ConfigManager g_config;
bool predicateAccountCharactersByName(const AccountCharacter& a, const AccountCharacter& b)
{
return a.name < b.name;
}
Account IOAccount::loadAccount(const std::string& accountName, bool preLoad /* = false*/)
{
Account acc;
acc.name = accountName;
if(g_game.onAccountLogin(
acc.name, acc.number, acc.password,
acc.premiumEnd, acc.warnings, acc.charList)){
//handled by script
return acc;
}
DatabaseDriver* db = DatabaseDriver::instance();
DBQuery query;
DBResult_ptr result;
query << "SELECT `id`, `name`, `password`, `premend`, `warnings` FROM `accounts` WHERE `name` = " << db->escapeString(accountName);
if(!(result = db->storeQuery(query))){
return acc;
}
acc.number = result->getDataInt("id");
acc.password = result->getDataString("password");
acc.premiumEnd = result->getDataInt("premend");
acc.name = result->getDataString("name");
acc.warnings = result->getDataInt("warnings");
if(preLoad)
return acc;
query.reset();
query << "SELECT " <<
"`players`.`name` AS `name`, `worlds`.`name` AS `world`, " <<
"`worlds`.`port` AS `port`, `worlds`.`ip` AS `ip`, `worlds`.`id` AS `world_id`" <<
"FROM `players` " <<
"LEFT JOIN `worlds` ON `worlds`.`id` = `players`.`world_id` " <<
"WHERE `account_id` = " << acc.number;
for(result = db->storeQuery(query); result; result = result->advance()) {
AccountCharacter c;
c.name = result->getDataString("name");
c.world_name = result->getDataString("world");
c.world_id = (uint16_t)result->getDataInt("world_id");
c.port = (uint16_t)result->getDataInt("port");
c.ip = (uint32_t)result->getDataLong("ip");
acc.charList.push_back(c);
}
acc.charList.sort(predicateAccountCharactersByName);
return acc;
}
bool IOAccount::saveAccount(const Account& acc)
{
DatabaseDriver* db = DatabaseDriver::instance();
DBQuery query;
query << "UPDATE `accounts` SET `premend` = " << acc.premiumEnd << ", `warnings` = " << acc.warnings << " WHERE `id` = " << acc.number;
return db->executeQuery(query);
}
bool IOAccount::getPassword(const std::string& accountName, const std::string& playerName, std::string& password)
{
DatabaseDriver* db = DatabaseDriver::instance();
DBQuery query;
DBResult_ptr result;
query << "SELECT `accounts`.`password` AS `password` FROM `accounts`, `players` " <<
"WHERE `accounts`.`name` = " << db->escapeString(accountName) <<
" AND `accounts`.`id` = `players`.`account_id` AND `players`.`name` = " << db->escapeString(playerName);
if((result = db->storeQuery(query.str()))){
password = result->getDataString("password");
return true;
}
return false;
}
uint16_t IOAccount::getPremiumDaysLeft(uint32_t time)
{
uint32_t now = (uint32_t)std::time(NULL) / 86400;
if(uint32_t(time / 86400) < now)
return 0;
if(uint32_t(time / 86400) - now >= 0xFFFF)
return 0xFFFF;
return uint16_t(uint32_t(time / 86400) - now);
}