diff options
Diffstat (limited to 'src/storage.cpp')
-rw-r--r-- | src/storage.cpp | 199 |
1 files changed, 11 insertions, 188 deletions
diff --git a/src/storage.cpp b/src/storage.cpp index 0467cb84..53384424 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -20,211 +20,34 @@ * * $Id$ */ -#include "storage.h" -#include <iostream> -#include <sstream> -const char sqlAccountTable[] = -"create table tmw_accounts (" - //username - "user varchar(32) unique primary key not null," - //password hash - "password varchar(32) not null," - //email address - "email varchar(128) not null" - //account type (should we have "admin" table? or should we have account - //type here?) -// "type int not null" -")"; +#include "storage.h" +#include "sqlitestorage.h" -const char sqlCharacterTable[] = -"create table tmw_characters (" - //character name - "name varchar(32) unique primary key not null," - //user name - "user varchar(32) not null," - //player information - "gender int not null," - "level int not null," - "money int not null," - //coordinates - "x int not null," - "y int not null," - //map name - "map text not null," - //statistics - "strength int not null," - "agility int not null," - "vitality int not null," - "intelligence int not null," - "dexterity int not null," - "luck int not null," - //player equipment -// "inventory blob not null," // TODO: blob bad -// "equipment blob not null," // TODO: blob bad - //table relationship - "foreign key(user) references tmw_accounts(user)" -")"; +Storage *Storage::instance = NULL; Storage::Storage() { -#ifdef SQLITE_SUPPORT - // Open database - if (sqlite.Open("tmw.db")) { - std::cout << "Database: tmw.db created or opened" << std::endl; - } - else { - std::cout << "Database: couldn't open tmw.db" << std::endl; - } -#endif -#ifdef PGSQL_SUPPORT - std::cout << "Datbase: Postgresql currently not supported" << std::endl; - exit(1); -#endif - - //Create tables - create_tables_if_necessary(); } Storage::~Storage() { -#ifdef SQLITE_SUPPORT - // Close database - if (sqlite.Close()) { - std::cout << "Database: tmw.db closed" << std::endl; - } - else { - std::cout << "Database: couldn't close tmw.db" << std::endl; - } -#endif - - // Clean up managed accounts & characters - for (unsigned int i = 0; i < accounts.size(); i++) - delete accounts[i]; - for (unsigned int i = 0; i < characters.size(); i++) - delete characters[i]; - } -/** - * Create tables if necessary - */ -void Storage::create_tables_if_necessary() +Storage *Storage::getInstance() { -#ifdef SQLITE_SUPPORT - SQLiteWrapper::ResultTable r; - - if (!sqlite.SelectStmt("select count(*) from sqlite_master where tbl_name='topics' and type='table'", r)) { - std::cout << "Error with select count(*) [create_tables_if_necessary]" << sqlite.LastError().c_str() << std::endl; - } - - if (r.records_[0].fields_[0] != "0") - return; - - sqlite.Begin(); - // Accounts table - if (sqlite.DirectStatement(sqlAccountTable)) { - std::cout << "Database: table tmw_accounts created" << std::endl; - } - else { - std::cout << "Database: table tmw_accounts exists" << std::endl; - } - - // Characters table - if (sqlite.DirectStatement(sqlCharacterTable)) { - std::cout << "Database: table tmw_characters created" << std::endl; - } - else { - std::cout << "Database: table tmw_characters exists" << std::endl; + if (instance == NULL) + { + instance = new SQLiteStorage(); } - //populate table for the hell of it ;) - sqlite.DirectStatement("insert into tmw_accounts values ('nym', 'tHiSiSHaShEd', 'nym@test')"); - sqlite.DirectStatement("insert into tmw_accounts values ('Bjorn', 'tHiSiSHaShEd', 'bjorn@test')"); - sqlite.DirectStatement("insert into tmw_accounts values ('Usiu', 'tHiSiSHaShEd', 'usiu@test')"); - sqlite.DirectStatement("insert into tmw_accounts values ('ElvenProgrammer', 'tHiSiSHaShEd', 'elven@test')"); - sqlite.DirectStatement("insert into tmw_characters values ('Nym the Great', 'nym', 0, 99, 1000000, 0, 0, 'main.map', 1, 2, 3, 4, 5, 6)"); - - sqlite.Commit(); -#endif + return instance; } -void Storage::save() +void Storage::deleteInstance() { -#ifdef SQLITE_SUPPORT - sqlite.Commit(); -#endif -} - -unsigned int Storage::accountCount() -{ -#ifdef SQLITE_SUPPORT - SQLiteWrapper::ResultTable r; - std::stringstream s; - unsigned int v; - sqlite.SelectStmt("select count(*) from tmw_accounts", r); - s << r.records_[0].fields_[0]; - s >> v; - return v; -#else - return 0; -#endif -} - -Account* Storage::getAccount(const std::string &username) -{ -#ifdef SQLITE_SUPPORT - //give caller a initialized AccountData structure - SQLiteWrapper::ResultTable r; - - //make sure account isn't loaded already - for (unsigned int i = 0; i < accounts.size(); i++) - if (accounts[i]->getName() == username) - return accounts[i]; - - std::string selectStatement = "select * from tmw_accounts where user = \"" - + username + "\""; - if (!sqlite.SelectStmt(selectStatement, r)) + if (instance != NULL) { - return NULL; + delete instance; } - - Account *acc = new Account; - acc->setName(r.records_[0].fields_[0]); - acc->setPassword(r.records_[0].fields_[1]); - acc->setEmail(r.records_[0].fields_[2]); - accounts.push_back(acc); - - return acc; -#else - return NULL; -#endif } - -Being* Storage::getCharacter(const std::string &username) -{ -#ifdef SQLITE_SUPPORT - //give caller a initialized AccountData structure - SQLiteWrapper::ResultTable r; - - std::string selectStatement = "select * from tmw_characters where user = \"" - + username + "\""; - if (!sqlite.SelectStmt(selectStatement, r)) - return NULL; - if (r.records_.size() == 0) - return NULL; - - //make sure account isn't loaded already - for (unsigned int i = 0; i < characters.size(); i++) - if (characters[i]->getName() == r.records_[0].fields_[0]) - return characters[i]; - - Being *acc = new Being(r.records_[0].fields_[0], 1, 1, 1, 1, 1, 1, 1, 1); - characters.push_back(acc); - - return acc; -#else - return NULL; -#endif -} - |