diff options
author | Andreas Habel <mail@exceptionfault.de> | 2008-09-15 10:43:11 +0000 |
---|---|---|
committer | Andreas Habel <mail@exceptionfault.de> | 2008-09-15 10:43:11 +0000 |
commit | a613c53609185c436dcdca7eae3221b792ad523d (patch) | |
tree | 8a367e8663fa96c955914f3179e80a80e4523be7 /src | |
parent | bfc0c10184442fba0b53e0aaafcf2532610e5cd7 (diff) | |
download | manaserv-a613c53609185c436dcdca7eae3221b792ad523d.tar.gz manaserv-a613c53609185c436dcdca7eae3221b792ad523d.tar.bz2 manaserv-a613c53609185c436dcdca7eae3221b792ad523d.tar.xz manaserv-a613c53609185c436dcdca7eae3221b792ad523d.zip |
* Added doxygen file, docu will be generated under docs/api/html
* Added table definition "tmw_world_states" to store map and world-specific variables
* Extended DALStorage to set and get those new variables
* Applied Kess's diff file from mantis #424 with modifications for PostgreSQL support
Diffstat (limited to 'src')
-rw-r--r-- | src/account-server/dalstorage.cpp | 80 | ||||
-rw-r--r-- | src/account-server/dalstorage.hpp | 32 | ||||
-rw-r--r-- | src/account-server/dalstoragesql.hpp | 269 | ||||
-rw-r--r-- | src/account-server/main-account.cpp | 13 |
4 files changed, 273 insertions, 121 deletions
diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index 26f99a12..601c0bb6 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -130,6 +130,8 @@ void DALStorage::open() createTable(GUILDS_TBL_NAME, SQL_GUILDS_TABLE); createTable(GUILD_MEMBERS_TBL_NAME, SQL_GUILD_MEMBERS_TABLE); createTable(QUESTS_TBL_NAME, SQL_QUESTS_TABLE); + createTable(WORLD_STATES_TBL_NAME, SQL_WORLD_STATES_TABLE); + // TODO: this is not the prefered way, but currently the complete // generation and maintenance of the database is a little dirty so @@ -1122,6 +1124,84 @@ std::string DALStorage::getQuestVar(int id, std::string const &name) return std::string(); } +std::string DALStorage::getWorldStateVar(std::string const &name) +{ + return getWorldStateVar(name, -1); +} + +std::string DALStorage::getWorldStateVar(std::string const &name, int map_id) +{ + try + { + std::ostringstream query; + query << "SELECT value " + << " FROM " << WORLD_STATES_TBL_NAME + << " WHERE state_name = '" << name << "'"; + + // add map filter if map_id is given + if (map_id >= 0) + { + query << " AND map_id = '" << map_id << "'"; + } + + query << ";"; + dal::RecordSet const &info = mDb->execSql(query.str()); + + if (!info.isEmpty()) return info(0, 0); + } + catch (dal::DbSqlQueryExecFailure const &e) + { + LOG_ERROR("(DALStorage::getWorldStateVar) SQL query failure: " << e.what()); + } + + return std::string(); +} + +void DALStorage::setWorldStateVar(std::string const &name, std::string const &value) +{ + return setWorldStateVar(name, -1, value); +} + +void DALStorage::setWorldStateVar(std::string const &name, + int map_id, + std::string const &value) +{ + try + { + std::ostringstream query1; + query1 << "DELETE FROM " << WORLD_STATES_TBL_NAME + << " WHERE state_name = '" << name << "'"; + if (map_id >= 0) + { + query1 << " AND map_id = '" << map_id << "'"; + } + query1 << ";"; + mDb->execSql(query1.str()); + + if (value.empty()) return; + + std::ostringstream query2; + query2 << "INSERT INTO " << WORLD_STATES_TBL_NAME + << " (state_name, map_id, value , moddate) VALUES (" + << "'" << name << "', "; + if (map_id >= 0) + { + query2 << "'" << map_id << "', "; + } + else + { + query2 << "NULL , "; + } + query2 << "'" << value << "', " + << "'" << time(NULL) << "');"; + mDb->execSql(query2.str()); + } + catch (dal::DbSqlQueryExecFailure const &e) + { + LOG_ERROR("(DALStorage::setWorldStateVar) SQL query failure: " << e.what()); + } +} + void DALStorage::setQuestVar(int id, std::string const &name, std::string const &value) { diff --git a/src/account-server/dalstorage.hpp b/src/account-server/dalstorage.hpp index 7a8208ff..099ec14a 100644 --- a/src/account-server/dalstorage.hpp +++ b/src/account-server/dalstorage.hpp @@ -223,6 +223,38 @@ class DALStorage */ void setQuestVar(int id, std::string const &, std::string const &); + /** + * Gets the string value of a world state variable. + * + * @param name Name of the requested world-state variable. + */ + std::string getWorldStateVar(std::string const &name); + + /** + * Gets the string value of a map specific world state variable. + * + * @param name Name of the requested world-state variable. + * @param map_id Id of the specific map. + */ + std::string getWorldStateVar(std::string const &name, int map_id); + + /** + * Sets the value of a world state variable. + * + * @param name Name of the world-state vairable. + * @param value New value of the world-state variable. + */ + void setWorldStateVar(std::string const &name, std::string const &value); + + /** + * Sets the value of a world state variable of a specific map. + * + * @param name Name of the world-state vairable. + * @param map_id ID of the specific map + * @param value New value of the world-state variable. + */ + void setWorldStateVar(std::string const &name, int map_id, + std::string const &value); private: /** diff --git a/src/account-server/dalstoragesql.hpp b/src/account-server/dalstoragesql.hpp index 311546ec..b773770f 100644 --- a/src/account-server/dalstoragesql.hpp +++ b/src/account-server/dalstoragesql.hpp @@ -70,33 +70,37 @@ static char const *SQL_ACCOUNTS_TABLE = "CREATE TABLE tmw_accounts \n " "( \n" #if defined (MYSQL_SUPPORT) - "id INTEGER PRIMARY KEY AUTO_INCREMENT," - "username VARCHAR(32) NOT NULL UNIQUE," - "password VARCHAR(32) NOT NULL," - "email VARCHAR(64) NOT NULL," - "level TINYINT UNSIGNED NOT NULL," - "banned TINYINT UNSIGNED NOT NULL," -// "activation VARCHAR(32)," + "id INTEGER PRIMARY KEY AUTO_INCREMENT," + "username VARCHAR(32) NOT NULL UNIQUE," + "password VARCHAR(32) NOT NULL," + "email VARCHAR(64) NOT NULL," + "level TINYINT UNSIGNED NOT NULL," + "banned TINYINT UNSIGNED NOT NULL," + "registration INTEGER NOT NULL," + "lastlogin INTEGER NOT NULL," +// "activation VARCHAR(32)," "INDEX (id)" #error "Incorrect definition. Please fix the types." #elif defined (SQLITE_SUPPORT) - "id INTEGER PRIMARY KEY, \n" - "username TEXT NOT NULL UNIQUE, \n" - "password TEXT NOT NULL, \n" - "email TEXT NOT NULL, \n" - "level INTEGER NOT NULL, \n" - "banned INTEGER NOT NULL, \n" - "registration INTEGER NOT NULL, \n" - "lastlogin INTEGER NOT NULL \n" -// "activation TEXT" + "id INTEGER PRIMARY KEY, \n" + "username TEXT NOT NULL UNIQUE, \n" + "password TEXT NOT NULL, \n" + "email TEXT NOT NULL, \n" + "level INTEGER NOT NULL, \n" + "banned INTEGER NOT NULL, \n" + "registration INTEGER NOT NULL, \n" + "lastlogin INTEGER NOT NULL \n" +// "activation TEXT" #elif defined (POSTGRESQL_SUPPORT) - "id SERIAL PRIMARY KEY," - "username TEXT NOT NULL UNIQUE," - "password TEXT NOT NULL," - "email TEXT NOT NULL," - "level INTEGER NOT NULL," - "banned INTEGER NOT NULL," -// "activation TEXT" + "id SERIAL PRIMARY KEY," + "username TEXT NOT NULL UNIQUE," + "password TEXT NOT NULL," + "email TEXT NOT NULL," + "level SMALLINT NOT NULL," + "banned SMALLINT NOT NULL," + "registration INTEGER NOT NULL," + "lastlogin INTEGER NOT NULL" +// "activation TEXT" #endif ");"; @@ -109,9 +113,9 @@ static char const *CHARACTERS_TBL_NAME = "tmw_characters"; static char const *SQL_CHARACTERS_TABLE = "CREATE TABLE tmw_characters (" #if defined (MYSQL_SUPPORT) - "id INTEGER PRIMARY KEY AUTO_INCREMENT," - "user_id INTEGER UNSIGNED NOT NULL," - "name VARCHAR(32) NOT NULL UNIQUE," + "id INTEGER PRIMARY KEY AUTO_INCREMENT," + "user_id INTEGER UNSIGNED NOT NULL," + "name VARCHAR(32) NOT NULL UNIQUE," // general information about the character "gender TINYINT UNSIGNED NOT NULL," "hair_style TINYINT UNSIGNED NOT NULL," @@ -119,39 +123,38 @@ static char const *SQL_CHARACTERS_TABLE = "level INTEGER UNSIGNED NOT NULL," "char_pts INTEGER UNSIGNED NOT NULL," "correct_pts INTEGER UNSIGNED NOT NULL," - "money INTEGER UNSIGNED NOT NULL," + "money INTEGER UNSIGNED NOT NULL," // location on the map - "x SMALLINT UNSIGNED NOT NULL," - "y SMALLINT UNSIGNED NOT NULL," - "map_id TINYINT NOT NULL," + "x SMALLINT UNSIGNED NOT NULL," + "y SMALLINT UNSIGNED NOT NULL," + "map_id TINYINT NOT NULL," // attributes - "str SMALLINT UNSIGNED NOT NULL," - "agi SMALLINT UNSIGNED NOT NULL," - "dex SMALLINT UNSIGNED NOT NULL," - "vit SMALLINT UNSIGNED NOT NULL," + "str SMALLINT UNSIGNED NOT NULL," + "agi SMALLINT UNSIGNED NOT NULL," + "dex SMALLINT UNSIGNED NOT NULL," + "vit SMALLINT UNSIGNED NOT NULL," // note: int must be backquoted as it's a MySQL keyword - "`int` SMALLINT UNSIGNED NOT NULL," - "will SMALLINT UNSIGNED NOT NULL," + "`int` SMALLINT UNSIGNED NOT NULL," + "will SMALLINT UNSIGNED NOT NULL," //skill experience - "unarmedExp INTEGER UNSIGNED NOT NULL," - "knife_exp INTEGER UNSIGNED NOT NULL," - "sword_exp INTEGER UNSIGNED NOT NULL," - "polearm_exp INTEGER UNSIGNED NOT NULL," - "staff_exp INTEGER UNSIGNED NOT NULL," - "whip_exp INTEGER UNSIGNED NOT NULL," - "bow_exp INTEGER UNSIGNED NOT NULL," - "shoot_exp INTEGER UNSIGNED NOT NULL," - "mace_exp INTEGER UNSIGNED NOT NULL," - "axe_exp INTEGER UNSIGNED NOT NULL," - "thrown_exp INTEGER UNSIGNED NOT NULL," - + "unarmedExp INTEGER UNSIGNED NOT NULL," + "knife_exp INTEGER UNSIGNED NOT NULL," + "sword_exp INTEGER UNSIGNED NOT NULL," + "polearm_exp INTEGER UNSIGNED NOT NULL," + "staff_exp INTEGER UNSIGNED NOT NULL," + "whip_exp INTEGER UNSIGNED NOT NULL," + "bow_exp INTEGER UNSIGNED NOT NULL," + "shoot_exp INTEGER UNSIGNED NOT NULL," + "mace_exp INTEGER UNSIGNED NOT NULL," + "axe_exp INTEGER UNSIGNED NOT NULL," + "thrown_exp INTEGER UNSIGNED NOT NULL," "FOREIGN KEY (user_id) REFERENCES tmw_accounts(id)," "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)," "INDEX (id)" #elif defined (SQLITE_SUPPORT) - "id INTEGER PRIMARY KEY," - "user_id INTEGER NOT NULL," - "name TEXT NOT NULL UNIQUE," + "id INTEGER PRIMARY KEY," + "user_id INTEGER NOT NULL," + "name TEXT NOT NULL UNIQUE," // general information about the character "gender INTEGER NOT NULL," "hair_style INTEGER NOT NULL," @@ -159,68 +162,67 @@ static char const *SQL_CHARACTERS_TABLE = "level INTEGER NOT NULL," "char_pts INTEGER NOT NULL," "correct_pts INTEGER NOT NULL," - "money INTEGER NOT NULL," + "money INTEGER NOT NULL," // location on the map - "x INTEGER NOT NULL," - "y INTEGER NOT NULL," - "map_id INTEGER NOT NULL," + "x INTEGER NOT NULL," + "y INTEGER NOT NULL," + "map_id INTEGER NOT NULL," // attributes - "str INTEGER NOT NULL," - "agi INTEGER NOT NULL," - "dex INTEGER NOT NULL," - "vit INTEGER NOT NULL," - "int INTEGER NOT NULL," - "will INTEGER NOT NULL," + "str INTEGER NOT NULL," + "agi INTEGER NOT NULL," + "dex INTEGER NOT NULL," + "vit INTEGER NOT NULL," + "int INTEGER NOT NULL," + "will INTEGER NOT NULL," //skill experience - "unarmed_exp INTEGER NOT NULL," - "knife_exp INTEGER NOT NULL," - "sword_exp INTEGER NOT NULL," - "polearm_exp INTEGER NOT NULL," - "staff_exp INTEGER NOT NULL," - "whip_exp INTEGER NOT NULL," - "bow_exp INTEGER NOT NULL," - "shoot_exp INTEGER NOT NULL," - "mace_exp INTEGER NOT NULL," - "axe_exp INTEGER NOT NULL," - "thrown_exp INTEGER NOT NULL," + "unarmed_exp INTEGER NOT NULL," + "knife_exp INTEGER NOT NULL," + "sword_exp INTEGER NOT NULL," + "polearm_exp INTEGER NOT NULL," + "staff_exp INTEGER NOT NULL," + "whip_exp INTEGER NOT NULL," + "bow_exp INTEGER NOT NULL," + "shoot_exp INTEGER NOT NULL," + "mace_exp INTEGER NOT NULL," + "axe_exp INTEGER NOT NULL," + "thrown_exp INTEGER NOT NULL," "FOREIGN KEY (user_id) REFERENCES tmw_accounts(id)," "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)" #elif defined (POSTGRESQL_SUPPORT) - "id SERIAL PRIMARY KEY," - "user_id INTEGER NOT NULL," - "name TEXT NOT NULL UNIQUE," + "id SERIAL PRIMARY KEY," + "user_id INTEGER NOT NULL," + "name TEXT NOT NULL UNIQUE," // general information about the character - "gender INTEGER NOT NULL," - "hair_style INTEGER NOT NULL," + "gender SMALLINT NOT NULL," + "hair_style SMALLINT NOT NULL," "hair_color INTEGER NOT NULL," - "level INTEGER NOT NULL," - - "char_pts INTEGER NOT NULL," - "correct_pts INTEGER NOT NULL," - "money INTEGER NOT NULL," + "level INTEGER NOT NULL," + "char_pts INTEGER NOT NULL," + "correct_pts INTEGER NOT NULL," + "money INTEGER NOT NULL," // location on the map - "x INTEGER NOT NULL," - "y INTEGER NOT NULL," - "map_id INTEGER NOT NULL," + "x SMALLINT NOT NULL," + "y SMALLINT NOT NULL," + "map_id SMALLINT NOT NULL," // attributes - "str INTEGER NOT NULL," - "agi INTEGER NOT NULL," - "dex INTEGER NOT NULL," - "vit INTEGER NOT NULL," - "int INTEGER NOT NULL," - "will INTEGER NOT NULL," + "str SMALLINT NOT NULL," + "agi SMALLINT NOT NULL," + "dex SMALLINT NOT NULL," + "vit SMALLINT NOT NULL," + "int SMALLINT NOT NULL," + "will SMALLINT NOT NULL," //skill experience - "unarmed_exp INTEGER NOT NULL," - "knife_exp INTEGER NOT NULL," - "sword_exp INTEGER NOT NULL," - "polearm_exp INTEGER NOT NULL," - "staff_exp INTEGER NOT NULL," - "whip_exp INTEGER NOT NULL," - "bow_exp INTEGER NOT NULL," - "shoot_exp INTEGER NOT NULL," - "mace_exp INTEGER NOT NULL," - "axe_exp INTEGER NOT NULL," - "thrown_exp INTEGER NOT NULL," + "unarmed_exp INTEGER NOT NULL," + "knife_exp INTEGER NOT NULL," + "sword_exp INTEGER NOT NULL," + "polearm_exp INTEGER NOT NULL," + "staff_exp INTEGER NOT NULL," + "whip_exp INTEGER NOT NULL," + "bow_exp INTEGER NOT NULL," + "shoot_exp INTEGER NOT NULL," + "mace_exp INTEGER NOT NULL," + "axe_exp INTEGER NOT NULL," + "thrown_exp INTEGER NOT NULL," "FOREIGN KEY (user_id) REFERENCES tmw_accounts(id)," "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)" #endif @@ -249,11 +251,11 @@ static char const *SQL_INVENTORIES_TABLE = "amount INTEGER NOT NULL," "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)" #elif defined (POSTGRESQL_SUPPORT) - "id SERIAL PRIMARY KEY," + "id SERIAL PRIMARY KEY," "owner_id INTEGER NOT NULL," - "slot INTEGER NOT NULL," + "slot SMALLINT NOT NULL," "class_id INTEGER NOT NULL," - "amount INTEGER NOT NULL," + "amount SMALLINT NOT NULL," "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)" #endif ");"; @@ -266,14 +268,14 @@ static char const *GUILDS_TBL_NAME = "tmw_guilds"; static char const *SQL_GUILDS_TABLE = "CREATE TABLE tmw_guilds (" #if defined (MYSQL_SUPPORT) - "id INTEGER PRIMARY KEY AUTO_INCREMENT," - "name VARCHAR(32) NOT NULL UNIQUE" + "id INTEGER PRIMARY KEY AUTO_INCREMENT," + "name VARCHAR(32) NOT NULL UNIQUE" #elif defined (SQLITE_SUPPORT) - "id INTEGER PRIMARY KEY," - "name TEXT NOT NULL UNIQUE" + "id INTEGER PRIMARY KEY," + "name TEXT NOT NULL UNIQUE" #elif defined (POSTGRESQL_SUPPORT) - "id SERIAL PRIMARY KEY," - "name TEXT NOT NULL UNIQUE" + "id SERIAL PRIMARY KEY," + "name TEXT NOT NULL UNIQUE" #endif ");"; @@ -285,21 +287,21 @@ static char const *GUILD_MEMBERS_TBL_NAME = "tmw_guild_members"; static char const *SQL_GUILD_MEMBERS_TABLE = "CREATE TABLE tmw_guild_members (" #if defined (MYSQL_SUPPORT) - "guild_id INTEGER NOT NULL," - "member_id INTEGER NOT NULL," - "rights INTEGER NOT NULL," + "guild_id INTEGER NOT NULL," + "member_id INTEGER NOT NULL," + "rights INTEGER NOT NULL," "FOREIGN KEY (guild_id) REFERENCES tmw_guilds(id)," "FOREIGN KEY (member_id) REFERENCES tmw_characters(id)" #elif defined (SQLITE_SUPPORT) - "guild_id INTEGER NOT NULL," - "member_id INTEGER NOT NULL," - "rights INTEGER NOT NULL," + "guild_id INTEGER NOT NULL," + "member_id INTEGER NOT NULL," + "rights INTEGER NOT NULL," "FOREIGN KEY (guild_id) REFERENCES tmw_guilds(id)," "FOREIGN KEY (member_id) REFERENCES tmw_characters(id)" #elif defined (POSTGRESQL_SUPPORT) - "guild_id INTEGER NOT NULL," - "member_id INTEGER NOT NULL," - "rights INTEGER NOT NULL," + "guild_id INTEGER NOT NULL," + "member_id INTEGER NOT NULL," + "rights INTEGER NOT NULL," "FOREIGN KEY (guild_id) REFERENCES tmw_guilds(id)," "FOREIGN KEY (member_id) REFERENCES tmw_characters(id)" #endif @@ -319,7 +321,32 @@ static char const *SQL_QUESTS_TABLE = "value TEXT NOT NULL," "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)" #elif defined (POSTGRESQL_SUPPORT) -#error "Missing definition. Please fill the blanks." + "owner_id INTEGER NOT NULL," + "name TEXT NOT NULL," + "value TEXT NOT NULL," + "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)" +#endif + ");"; + +/** + * TABLE: tmw_world_states + */ +static char const *WORLD_STATES_TBL_NAME = "tmw_world_states"; +static char const *SQL_WORLD_STATES_TABLE = + "CREATE TABLE tmw_world_states \n" + "( \n" +#if defined (MYSQL_SUPPORT) + "state_name VARCHAR(100) NOT NULL, \n" + "map_id SMALLINT UNSIGNED NULL, \n" + "value VARCHAR(255) NULL, \n" + "moddate INT UNSIGNED NOT NULL, \n" + "PRIMARY KEY(state_name) \n" +#elif defined (SQLITE_SUPPORT) + "state_name TEXT PRIMARY KEY, \n" + "map_id INTEGER NULL, \n" + "value TEXT NULL, \n" + "moddate INTEGER NOT NULL \n" +#elif defined (POSTGRESQL_SUPPORT) #endif ");"; diff --git a/src/account-server/main-account.cpp b/src/account-server/main-account.cpp index 5bd8a080..c4c451ee 100644 --- a/src/account-server/main-account.cpp +++ b/src/account-server/main-account.cpp @@ -143,6 +143,7 @@ static void initialize() storage = new DALStorage; storage->open(); + // --- Initialize the managers // Initialize the slang's and double quotes filter. stringFilter = new StringFilter; @@ -311,6 +312,18 @@ int main(int argc, char *argv[]) // Check for expired bans every 30 seconds utils::Timer banTimer(30000); + // ------------------------------------------------------------------------- + // FIXME: for testing purposes only... + // writing accountserver startup time and svn revision to database as global + // world state variable + const time_t startup = time(NULL); + std::stringstream timestamp; + timestamp << startup; + storage->setWorldStateVar("accountserver_startup", timestamp.str()); + const std::string revision = "$Revision$"; + storage->setWorldStateVar("accountserver_version", revision); + // ------------------------------------------------------------------------- + while (running) { AccountClientHandler::process(); GameServerHandler::process(); |