From 7c7a1348faa3867f6f2e5fcaf2b671774b1b018a Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sun, 12 Jun 2005 22:44:11 +0000 Subject: Moved SQLiteStorage to subclass of Storage, made Storage a singleton and updated SQL tables a bit. --- src/Makefile.am | 4 +- src/account.cpp | 30 +++++-- src/account.h | 2 + src/main.cpp | 62 +++++-------- src/object.cpp | 12 +-- src/sqlitestorage.cpp | 245 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/sqlitestorage.h | 72 +++++++++++++++ src/storage.cpp | 199 +++------------------------------------- src/storage.h | 63 +++++-------- 9 files changed, 407 insertions(+), 282 deletions(-) create mode 100644 src/sqlitestorage.cpp create mode 100644 src/sqlitestorage.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index e01357c3..6ffe719c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,7 +36,9 @@ tmwserv_SOURCES = main.cpp \ if BUILD_SQLITE tmwserv_SOURCES += sqlite/SQLiteWrapper.cpp \ - sqlite/SQLiteWrapper.h + sqlite/SQLiteWrapper.h \ + sqlitestorage.cpp \ + sqlitestorage.h endif # set the include path found by configure diff --git a/src/account.cpp b/src/account.cpp index a72068b8..d9d8ba0f 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -55,18 +55,36 @@ const std::string& Account::getEmail() return email; } -void Account::setName(const std::string &n) +Being* Account::getCharacter(const std::string &name) { - name = n; + for (unsigned int i = 0; i < beings.size(); i++) + { + if (beings[i]->getName() == name) + { + return beings[i]; + } + } + + return NULL; +} + +void Account::setName(const std::string &name) +{ + this->name = name; } -void Account::setPassword(const std::string &p) +void Account::setPassword(const std::string &password) { // A hash of p needs to be made then hash stored in password - password = p; + this->password = password; +} + +void Account::setEmail(const std::string &email) +{ + this->email = email; } -void Account::setEmail(const std::string &e) +void Account::setCharacters(const std::vector &beings) { - email = e; + this->beings = beings; } diff --git a/src/account.h b/src/account.h index cedc1bcc..8bdeb7ff 100644 --- a/src/account.h +++ b/src/account.h @@ -44,10 +44,12 @@ class Account void setName(const std::string &name); void setPassword(const std::string &password); void setEmail(const std::string &email); + void setCharacters(const std::vector &beings); const std::string& getEmail(); const std::string& getPassword(); const std::string& getName(); + Being* getCharacter(const std::string &name); private: std::string name; /**< Username */ diff --git a/src/main.cpp b/src/main.cpp index b67fa4d5..60f67ff9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,15 +24,14 @@ #define TMWSERV_VERSION "0.0.1" #include -#include "storage.h" #include "netsession.h" #include "connectionhandler.h" #include "accounthandler.h" +#include "storage.h" #include #include #include "skill.h" - #include "log.h" #define LOG_FILE "tmwserv.log" @@ -65,7 +64,6 @@ bool running = true; /**< Determines if server keeps running */ Skill skillTree("base"); /**< Skill tree */ -Storage store; /** * SDL timer callback, sends a TMW_WORLD_TICK event. @@ -90,7 +88,8 @@ void initialize() logger = new Logger(LOG_FILE); // Initialize SDL - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) + { logger->log("SDL_Init: %s", SDL_GetError()); exit(1); } @@ -99,7 +98,8 @@ void initialize() atexit(SDL_Quit); // Initialize SDL_net - if (SDLNet_Init() == -1) { + if (SDLNet_Init() == -1) + { logger->log("SDLNet_Init: %s", SDLNet_GetError()); exit(2); } @@ -112,9 +112,10 @@ void initialize() logger->log("Script Language %s", scriptLanguage.c_str()); if (scriptLanguage == "squirrel") + { script = new ScriptSquirrel("main.nut"); + } #endif - } /** @@ -128,39 +129,14 @@ void deinitialize() // Quit SDL_net SDLNet_Quit(); - // Destroy scripting subsystem #ifdef SCRIPT_SUPPORT -#ifdef SCRIPT_SUPPORT - script->update(); + // Destroy scripting subsystem + delete script; #endif -} - -/** - * Main function, initializes and runs server. - */ -int main(int argc, char *argv[]) -{ - initialize(); - // Ready for server work... - ConnectionHandler *connectionHandler = new ConnectionHandler(); - NetSession *session = new NetSession(); + // Get rid of persistent data storage + Storage::deleteInstance(); - // Note: This is just an idea, we could also pass the connection handler - // to the constructor of the account handler, upon which is would register - // itself for the messages it handles. - // - //AccountHandler *accountHandler = new AccountHandler(); - //connectionHandler->registerHandler(C2S_LOGIN, accountHandler); - - logger->log("The Mana World Server v%s", TMWSERV_VERSION); - session->startListen(connectionHandler, SERVER_PORT); - logger->log("Listening on port %d...", SERVER_PORT); - - SDL_Event event; - - delete script; -#endif delete logger; } @@ -196,26 +172,30 @@ int main(int argc, char *argv[]) session->startListen(connectionHandler, SERVER_PORT); logger->log("Listening on port %d...", SERVER_PORT); - std::cout << "Number of accounts on server: " << store.accountCount() << std::endl; + Storage *store = Storage::getInstance(); + + std::cout << "Number of accounts on server: " << store->getAccountCount() << std::endl; // Test the database retrieval code std::cout << "Attempting to retrieve account with username 'nym'" << std::endl; - Account* acc = store.getAccount("nym"); + Account* acc = store->getAccount("nym"); if (acc) { std::cout << "Account name: " << acc->getName() << std::endl << "Account email: " << acc->getEmail() << std::endl; - std::cout << "Attempting to get character of 'nym'" << std::endl; - Being* character = store.getCharacter("nym"); + std::cout << "Attempting to get character 'Nym the Great'" << std::endl; + Being* character = acc->getCharacter("Nym the Great"); if (character) { std::cout << "Character name: " << character->getName() << std::endl; - } else + } + else { std::cout << "No characters found" << std::endl; } - } else + } + else { std::cout << "Account 'nym' not found" << std::endl; } diff --git a/src/object.cpp b/src/object.cpp index c2d26e3e..9ae2bd61 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -24,11 +24,11 @@ #include "object.h" Being::Being(const std::string &bName, unsigned int bGender, - unsigned int bLevel, unsigned int bMoney, - unsigned int bStrength, unsigned int bAgility, - unsigned int bVitality, unsigned int bDexterity, - unsigned int bLuck) - : name(bName), + unsigned int bLevel, unsigned int bMoney, + unsigned int bStrength, unsigned int bAgility, + unsigned int bVitality, unsigned int bDexterity, + unsigned int bLuck): + name(bName), gender(bGender), level(bLevel), money(bMoney), @@ -38,7 +38,7 @@ Being::Being(const std::string &bName, unsigned int bGender, dexterity(bDexterity), luck(bLuck) { - // + std::cout << "New being create with name \"" + name + "\"" << std::endl; } void Being::update() diff --git a/src/sqlitestorage.cpp b/src/sqlitestorage.cpp new file mode 100644 index 00000000..6fc98fcd --- /dev/null +++ b/src/sqlitestorage.cpp @@ -0,0 +1,245 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "sqlitestorage.h" +#include +#include + +/* Values for user level could be: + * 0: Normal user + * 1: Moderator (has medium level rights) + * 2: Administrator (can do basically anything) + */ +const char sqlAccountTable[] = + "create table tmw_accounts (" + "id int unique primary key not null," + "username varchar(32) not null," + "password varchar(32) not null," + "email varchar(128) not null," + "level int not null," // User level (normal, admin, etc.) + "banned int not null" // The UNIX time of unban (0 default) + ")"; + +/* Note: The stats will need to be thought over, as we'll be implementing a + * much more elaborate skill based system. We should probably have a separate + * table for storing the skill levels. + * + * Gender is 0 for male, 1 for female. + */ +const char sqlCharacterTable[] = + "create table tmw_characters (" + "id int unique primary key not null," + "user_id int not null," + "name varchar(32) not null," + "gender int not null," // Player information + "level int not null," + "money int not null," + "x int not null," // Location + "y int not null," + "map text not null," + "str int not null," // Stats + "agi int not null," + "vit int not null," + "int int not null," + "dex int not null," + "luck int not null," + "foreign key(user_id) references tmw_accounts(id)" + ")"; + +/* Note: Below the table for storing active items in the world. Maybe it would + * be better to make a difference between inventory items and items on the + * floor somewhere, cause either the location or the owner id isn't used. + */ +const char sqlItemTable[] = + "create table tmw_items (" + "id int unique primary key not null," + "char_id int," // Owner character (null for unowned) + "x int," // Location (null when in inventory) + "y int," + "map text," + "amount int not null," // Items of same kind can stack + "type int not null," // Type as defined in item database + "state text," // Optional item state saved by script + "foreign key(char_id) references tmw_characters(id)" + ")"; + +SQLiteStorage::SQLiteStorage() +{ + // 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; + } + + // Create tables + createTablesIfNecessary(); +} + +SQLiteStorage::~SQLiteStorage() +{ + // Make sure any changes have been written + flush(); + + // Close database + if (sqlite.Close()) + { + std::cout << "Database: tmw.db closed" << std::endl; + } + else + { + std::cout << "Database: couldn't close tmw.db" << std::endl; + } + + // 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]; + } +} + +void SQLiteStorage::createTablesIfNecessary() +{ + 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(*) [createTablesIfNecessary]" << 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; + } + + // Items table + if (sqlite.DirectStatement(sqlItemTable)) { + std::cout << "Database: table tmw_items created" << std::endl; + } + else { + std::cout << "Database: table tmw_items exists" << std::endl; + } + + // Populate table for the hell of it ;) + sqlite.DirectStatement("insert into tmw_accounts values (0, 'nym', 'tHiSiSHaShEd', 'nym@test', 1, 0)"); + sqlite.DirectStatement("insert into tmw_accounts values (1, 'Bjorn', 'tHiSiSHaShEd', 'bjorn@test', 1, 0)"); + sqlite.DirectStatement("insert into tmw_accounts values (2, 'Usiu', 'tHiSiSHaShEd', 'usiu@test', 1, 0)"); + sqlite.DirectStatement("insert into tmw_accounts values (3, 'ElvenProgrammer', 'tHiSiSHaShEd', 'elven@test', 1, 0)"); + sqlite.DirectStatement("insert into tmw_characters values (0, 0, 'Nym the Great', 0, 99, 1000000, 0, 0, 'main.map', 1, 2, 3, 4, 5, 6)"); + + flush(); +} + +void SQLiteStorage::flush() +{ + sqlite.Commit(); +} + +unsigned int SQLiteStorage::getAccountCount() +{ + 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; +} + +Account* SQLiteStorage::getAccount(const std::string &username) +{ + // Make sure account isn't loaded already + for (unsigned int i = 0; i < accounts.size(); i++) + { + if (accounts[i]->getName() == username) + { + return accounts[i]; + } + } + + // Give caller a initialized AccountData structure + SQLiteWrapper::ResultTable r; + + std::string selectStatement = + "select * from tmw_accounts where username = \"" + username + "\""; + + if (!sqlite.SelectStmt(selectStatement, r)) + { + // Throwing an exception here could be good + return NULL; + } + + Account *acc = new Account(); + acc->setName(r.records_[0].fields_[1]); + acc->setPassword(r.records_[0].fields_[2]); + acc->setEmail(r.records_[0].fields_[3]); + accounts.push_back(acc); + + std::string user_id = r.records_[0].fields_[0]; + + // Load the characters associated with the account + selectStatement = + "select * from tmw_characters where id = \"" + user_id + "\""; + + if (!sqlite.SelectStmt(selectStatement, r)) + { + // Throwing exception here could be good + return NULL; + } + + std::vector beings; + + for (int i = 0; i < r.records_.size(); i++) + { + Being *being = new Being( + r.records_[i].fields_[2], 1, 1, 1, 1, 1, 1, 1, 1); + characters.push_back(being); + beings.push_back(being); + } + + acc->setCharacters(beings); + + return acc; +} diff --git a/src/sqlitestorage.h b/src/sqlitestorage.h new file mode 100644 index 00000000..7f11e0f6 --- /dev/null +++ b/src/sqlitestorage.h @@ -0,0 +1,72 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef TMWSERV_SQLITESTORAGE_H +#define TMWSERV_SQLITESTORAGE_H + +#include "sqlite/SQLiteWrapper.h" +#include "storage.h" + +/** + * SQLite implemention of persistent dynamic data storage. + */ +class SQLiteStorage : public Storage +{ + public: + /** + * Constructor. + */ + SQLiteStorage(); + + /** + * Destructor. + */ + ~SQLiteStorage(); + + /** + * Save changes to database. + */ + void flush(); + + /** + * Account count (test function) + */ + unsigned int getAccountCount(); + + /** + * Get account & associated data + */ + Account* getAccount(const std::string &username); + + protected: + SQLiteWrapper sqlite; /**< Database */ + std::vector accounts; /**< Loaded accounts */ + std::vector characters; /**< Loaded characters */ + + /** + * Create tables if master is empty. + */ + void createTablesIfNecessary(); +}; + +#endif /* TMWSERV_SQLITESTORAGE_H */ 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 -#include -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 -} - diff --git a/src/storage.h b/src/storage.h index 5cf96eb4..e5be589d 100644 --- a/src/storage.h +++ b/src/storage.h @@ -20,66 +20,49 @@ * * $Id$ */ -#ifndef STORAGE_H -#define STORAGE_H - -#ifdef SQLITE_SUPPORT -#include "sqlite/SQLiteWrapper.h" -#endif +#ifndef TMWSERV_STORAGE_H +#define TMWSERV_STORAGE_H #include "object.h" #include "account.h" -/* - * Storage - * Storage is the resource manager - */ -class Storage { - //make storage singleton - Storage(const Storage &n) { } - -#ifdef SQLITE_SUPPORT - SQLiteWrapper sqlite; /**< Database */ -#endif - std::vector accounts; /**< Loaded account data */ - std::vector characters; /**< Loaded account data */ - +/** + * A storage of persistent dynamic data. + */ +class Storage +{ public: - /** - * Constructor. - */ - Storage(); + static Storage *getInstance(); + static void deleteInstance(); /** - * Destructor. + * Make sure any changes are saved. */ - ~Storage(); + virtual void flush() = 0; /** - * Create tables if master is empty + * Account count (test function). */ - void create_tables_if_necessary(); + virtual unsigned int getAccountCount() = 0; /** - * Save changes to database + * Get account by username. */ - void save(); + virtual Account *getAccount(const std::string &username) = 0; + protected: /** - * Account count (test function) + * Constructor. */ - unsigned int accountCount(); + Storage(); /** - * Get account & associated data + * Destructor. */ - Account* getAccount(const std::string &username); + virtual ~Storage(); - /** - * Get character of username - */ - Being* getCharacter(const std::string &username); + private: + static Storage *instance; }; -#endif /* STORAGE_H */ - +#endif /* TMWSERV_STORAGE_H */ -- cgit v1.2.3-60-g2f50