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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2000,2001 Alistair Riddoch
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// $Id: Persistance.cpp,v 1.53 2007-12-07 17:42:59 alriddoch Exp $
#include "Persistance.h"
#include "Admin.h"
#include "Player.h"
#include "rulesets/Entity.h"
#include "common/id.h"
#include "common/log.h"
#include "common/const.h"
#include "common/debug.h"
#include "common/Database.h"
#include "common/compose.hpp"
using Atlas::Message::MapType;
using Atlas::Objects::Root;
static const bool debug_flag = false;
Persistance * Persistance::m_instance = NULL;
Persistance::Persistance() : m_connection(*Database::instance())
{
}
Persistance * Persistance::instance()
{
if (m_instance == NULL) {
m_instance = new Persistance();
}
return m_instance;
}
int Persistance::init()
{
assert(this != 0);
if (m_connection.initConnection() != 0) {
if (::instance == CYPHESIS) {
return DATABASE_CONERR;
}
if (m_connection.createInstanceDatabase() != 0) {
log(ERROR, "Database creation failed.");
return DATABASE_CONERR;
}
if (m_connection.initConnection() != 0) {
log(ERROR, "Still couldn't connect.");
return DATABASE_CONERR;
}
log(INFO, String::compose("Auto created database for new instance "
"\"%1\".", ::instance));
}
if (!m_connection.registerEntityIdGenerator()) {
log(ERROR, "Faled to register Id generator in database.");
return DATABASE_TABERR;
}
bool i = m_connection.initRule(true);
MapType tableDesc;
tableDesc["username"] = " ";
tableDesc["password"] = " ";
tableDesc["type"] = " ";
bool j = m_connection.registerSimpleTable("accounts", tableDesc);
bool k = m_connection.registerRelation(m_characterRelation,
"accounts",
"entity_ent");
if (!findAccount("admin")) {
debug(std::cout << "Bootstraping admin account."
<< std::endl << std::flush;);
std::string adminAccountId;
long adminAccountIntId = m_connection.newId(adminAccountId);
if (adminAccountIntId < 0) {
log(CRITICAL, "Unable to get admin account ID from Database");
return -2;
}
Admin dummyAdminAccount(0, "admin", consts::defaultAdminPasswordHash,
adminAccountId, adminAccountIntId);
putAccount(dummyAdminAccount);
}
return (i && j && k) ? 0 : -2;
}
void Persistance::shutdown()
{
m_connection.shutdownConnection();
delete &m_connection;
assert(this == m_instance);
delete m_instance;
m_instance = NULL;
}
bool Persistance::findAccount(const std::string & name)
{
std::string namestr = "'" + name + "'";
DatabaseResult dr = m_connection.selectSimpleRowBy("accounts", "username", namestr);
if (dr.error()) {
log(ERROR, "Failure while find account.");
return false;
}
if (dr.empty()) {
dr.clear();
return false;
}
if (dr.size() > 1) {
log(ERROR, "Duplicate username in accounts database.");
}
dr.clear();
return true;
}
Account * Persistance::getAccount(const std::string & name)
{
std::string namestr = "'" + name + "'";
DatabaseResult dr = m_connection.selectSimpleRowBy("accounts", "username", namestr);
if (dr.error()) {
log(ERROR, "Failure while find account.");
return 0;
}
if (dr.empty()) {
dr.clear();
return 0;
}
if (dr.size() > 1) {
log(ERROR, "Duplicate username in accounts database.");
}
const char * c = dr.field("id");
if (c == 0) {
dr.clear();
log(ERROR, "Unable to find id field in accounts database.");
return 0;
}
std::string id = c;
long intId = integerId(id);
if (intId == -1) {
dr.clear();
log(ERROR, String::compose("Invalid ID \"%1\" for account from database.", id));
return 0;
}
c = dr.field("password");
if (c == 0) {
dr.clear();
log(ERROR, "Unable to find password field in accounts database.");
return 0;
}
std::string passwd = c;
c = dr.field("type");
if (c == 0) {
dr.clear();
log(ERROR, "Unable to find type field in accounts database.");
return 0;
}
std::string type = c;
dr.clear();
if (type == "admin") {
return new Admin(0, name, passwd, id, intId);
} else {
return new Player(0, name, passwd, id, intId);
}
}
void Persistance::putAccount(const Account & ac)
{
std::string columns = "username, type, password";
std::string values = "'";
values += ac.m_username;
values += "', '";
values += ac.getType();
values += "', '";
values += ac.m_password;
values += "'";
m_connection.createSimpleRow("accounts", ac.getId(), columns, values);
}
void Persistance::registerCharacters(Account & ac,
const EntityDict & worldObjects)
{
DatabaseResult dr = m_connection.selectRelation(m_characterRelation,
ac.getId());
if (dr.error()) {
log(ERROR, "Database query failed while looking for characters for account.");
}
DatabaseResult::const_iterator Iend = dr.end();
for (DatabaseResult::const_iterator I = dr.begin(); I != Iend; ++I) {
const char * id = I.column(0);
if (id == 0) {
log(ERROR, "No ID data in relation when examing characters");
continue;
}
long intId = integerId(id);
EntityDict::const_iterator J = worldObjects.find(intId);
if (J == worldObjects.end()) {
log(WARNING, String::compose("Persistance: Got character id \"%1\" "
"from database which does not exist "
"in world.", id));
continue;
}
ac.addCharacter(J->second);
}
dr.clear();
}
void Persistance::addCharacter(const Account & ac, const Entity & e)
{
m_connection.createRelationRow(m_characterRelation, ac.getId(), e.getId());
}
void Persistance::delCharacter(const std::string & id)
{
m_connection.removeRelationRowByOther(m_characterRelation, id);
}
bool Persistance::getRules(std::map<std::string, Root> & t)
{
return m_connection.getTable(m_connection.rule(), t);
}
bool Persistance::clearRules()
{
return m_connection.clearTable(m_connection.rule());
}
|