diff options
-rw-r--r-- | accountserver.cbp | 2 | ||||
-rw-r--r-- | src/account-server/accounthandler.cpp | 24 | ||||
-rw-r--r-- | src/account-server/dalstorage.cpp | 79 | ||||
-rw-r--r-- | src/account-server/dalstorage.hpp | 16 | ||||
-rw-r--r-- | src/account-server/dalstoragesql.hpp | 6 | ||||
-rw-r--r-- | src/account-server/transaction.hpp | 38 | ||||
-rw-r--r-- | src/sql/sqlite/createTables.sql | 8 | ||||
-rw-r--r-- | src/sql/sqlite/updates/update_2_to_3.sql | 17 |
8 files changed, 189 insertions, 1 deletions
diff --git a/accountserver.cbp b/accountserver.cbp index 6b6f5487..1cfd0bb8 100644 --- a/accountserver.cbp +++ b/accountserver.cbp @@ -68,6 +68,8 @@ <Unit filename="src/account-server/main-account.cpp" /> <Unit filename="src/account-server/serverhandler.cpp" /> <Unit filename="src/account-server/serverhandler.hpp" /> + <Unit filename="src/account-server/transaction.cpp" /> + <Unit filename="src/account-server/transaction.hpp" /> <Unit filename="src/chat-server/chatchannel.cpp" /> <Unit filename="src/chat-server/chatchannel.hpp" /> <Unit filename="src/chat-server/chatchannelmanager.cpp" /> diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp index 27d96176..a1280e35 100644 --- a/src/account-server/accounthandler.cpp +++ b/src/account-server/accounthandler.cpp @@ -28,6 +28,7 @@ #include "account-server/character.hpp" #include "account-server/dalstorage.hpp" #include "account-server/serverhandler.hpp" +#include "account-server/transaction.hpp" #include "chat-server/chathandler.hpp" #include "common/configuration.hpp" #include "net/connectionhandler.hpp" @@ -604,6 +605,14 @@ static void handleCharacterCreateMessage(AccountClient &computer, MessageIn &msg LOG_INFO("Character " << name << " was created for " << acc->getName() << "'s account."); + // log transaction + Transaction trans; + trans.mCharacterId = newCharacter->getDatabaseID(); + trans.mAction = TRANS_CHAR_CREATE; + trans.mMessage = acc->getName() + " created character "; + trans.mMessage.append(" called " + name); + storage->addTransaction(trans); + storage->flush(acc); // flush changes reply.writeByte(ERRMSG_OK); computer.send(reply); @@ -674,6 +683,13 @@ static void handleCharacterSelectMessage(AccountClient &computer, MessageIn &msg registerChatClient(magic_token, selectedChar->getName(), acc->getLevel()); computer.send(reply); + + // log transaction + Transaction trans; + trans.mCharacterId = selectedChar->getDatabaseID(); + trans.mAction = TRANS_CHAR_SELECTED; + trans.mMessage = ""; + storage->addTransaction(trans); } static void handleCharacterDeleteMessage(AccountClient &computer, MessageIn &msg) @@ -707,6 +723,14 @@ static void handleCharacterDeleteMessage(AccountClient &computer, MessageIn &msg reply.writeByte(ERRMSG_OK); computer.send(reply); + + // log transaction + Transaction trans; + trans.mCharacterId = chars[charNum]->getDatabaseID(); + trans.mAction = TRANS_CHAR_DELETED; + trans.mMessage = chars[charNum]->getName() + " deleted by "; + trans.mMessage.append(acc->getName()); + storage->addTransaction(trans); } void diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index 55a31d30..a524fc35 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -41,7 +41,7 @@ // defines the supported db version #define DB_VERSION_PARAMETER "database_version" -#define SUPPORTED_DB_VERSION "2" +#define SUPPORTED_DB_VERSION "3" /** @@ -1006,6 +1006,7 @@ void DALStorage::setMemberRights(int guildId, int memberId, int rights) << " set rights = '" << rights << "'" << " where member_id = \"" << memberId << "\";"; + mDb->execSql(sql.str()); } catch (const dal::DbSqlQueryExecFailure& e) { @@ -1627,3 +1628,79 @@ void DALStorage::setOnlineStatus(int charId, bool online) LOG_ERROR("(DALStorage::setOnlineStatus) SQL query failure: " << e.what()); } } + +void DALStorage::addTransaction(const Transaction &trans) +{ + try + { + std::stringstream sql; + sql << "INSERT INTO " << TRANSACTION_TBL_NAME + << " VALUES (" << trans.mCharacterId << ", " << trans.mAction + << ", '" << trans.mMessage << "', " << time(NULL) << ")"; + mDb->execSql(sql.str()); + } + catch (dal::DbSqlQueryExecFailure const &e) + { + LOG_ERROR("(DALStorage::addTransaction) SQL query failure: " << e.what()); + } +} + +std::vector<Transaction> DALStorage::getTransactions(unsigned int num) +{ + std::vector<Transaction> transactions; + string_to<unsigned int> toUint; + + try + { + std::stringstream sql; + sql << "SELECT * FROM " << TRANSACTION_TBL_NAME; + dal::RecordSet const &rec = mDb->execSql(sql.str()); + + int size = rec.rows(); + int start = size - num; + // Get the last <num> records and store them in transactions + for (int i = start; i < size; ++i) + { + Transaction trans; + trans.mCharacterId = toUint(rec(i, 0)); + trans.mAction = toUint(rec(i, 1)); + trans.mMessage = rec(i, 2); + transactions.push_back(trans); + } + } + catch (dal::DbSqlQueryExecFailure const &e) + { + LOG_ERROR("(DALStorage::getTransactions) SQL query failure: " << e.what()); + } + + return transactions; +} + +std::vector<Transaction> DALStorage::getTransactions(time_t date) +{ + std::vector<Transaction> transactions; + string_to<unsigned int> toUint; + + try + { + std::stringstream sql; + sql << "SELECT * FROM " << TRANSACTION_TBL_NAME << " WHERE time > " + << date; + dal::RecordSet const &rec = mDb->execSql(sql.str()); + + for (int i = 0; i < rec.rows(); ++i) + { + Transaction trans; + trans.mCharacterId = toUint(rec(i, 0)); + trans.mAction = toUint(rec(i, 1)); + trans.mMessage = rec(i, 2); + transactions.push_back(trans); + } + } + catch (dal::DbSqlQueryExecFailure const &e) + { + LOG_ERROR("(DALStorage::getTransactions) SQL query failure: " << e.what()); + } + + return transactions; +} diff --git a/src/account-server/dalstorage.hpp b/src/account-server/dalstorage.hpp index fb8ae138..131cf819 100644 --- a/src/account-server/dalstorage.hpp +++ b/src/account-server/dalstorage.hpp @@ -23,9 +23,12 @@ #include <list> #include <map> +#include <vector> #include "dal/dataprovider.h" +#include "transaction.hpp" + class Account; class Character; class ChatChannel; @@ -365,6 +368,19 @@ class DALStorage */ void setOnlineStatus(int charId, bool online); + /** + * Store a transaction + */ + void addTransaction(const Transaction &trans); + + /** + * Retrieve a series of transactions + * Either based on number of transactions last saved + * or by all transactions since a date + */ + std::vector<Transaction> getTransactions(unsigned int num); + std::vector<Transaction> getTransactions(time_t date); + private: /** * Copy constructor. diff --git a/src/account-server/dalstoragesql.hpp b/src/account-server/dalstoragesql.hpp index 4b9d27db..34cdec5b 100644 --- a/src/account-server/dalstoragesql.hpp +++ b/src/account-server/dalstoragesql.hpp @@ -137,4 +137,10 @@ static char const *AUCTION_BIDS_TBL_NAME = "tmw_auction_bids"; */ static char const *ONLINE_USERS_TBL_NAME = "tmw_online_list"; +/** + * TABLE: tmw_transactions + * Stores all transactions + */ +static char const *TRANSACTION_TBL_NAME = "tmw_transactions"; + #endif // _TMWSERV_DALSTORAGE_SQL_H_ diff --git a/src/account-server/transaction.hpp b/src/account-server/transaction.hpp new file mode 100644 index 00000000..8aa1b340 --- /dev/null +++ b/src/account-server/transaction.hpp @@ -0,0 +1,38 @@ +/* + * The Mana World Server + * Copyright 2009 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 + */ + +#ifndef _TMWSERV_TRANSACTION_H_ +#define _TMWSERV_TRANSACTION_H_ + +struct Transaction +{ + unsigned int mAction; + unsigned int mCharacterId; + std::string mMessage; +}; + +enum +{ + TRANS_CHAR_CREATE = 1, + TRANS_CHAR_SELECTED = 2, + TRANS_CHAR_DELETED = 3, +}; + +#endif diff --git a/src/sql/sqlite/createTables.sql b/src/sql/sqlite/createTables.sql index 68d4ed58..11478d9b 100644 --- a/src/sql/sqlite/createTables.sql +++ b/src/sql/sqlite/createTables.sql @@ -212,6 +212,14 @@ CREATE TABLE tmw_post_attachments CREATE INDEX tmw_post_attachments_ltr ON tmw_post_attachments ( letter_id ); CREATE INDEX tmw_post_attachments_itm ON tmw_post_attachments ( item_id ); +CREATE TABLE tmw_transactions +( + id INTEGER PRIMARY KEY, + char_id INTEGER NOT NULL, + action INTEGER NOT NULL, + message TEXT, + time INTEGER NOT NULL, +); CREATE TABLE tmw_online_list ( diff --git a/src/sql/sqlite/updates/update_2_to_3.sql b/src/sql/sqlite/updates/update_2_to_3.sql new file mode 100644 index 00000000..b2e4834d --- /dev/null +++ b/src/sql/sqlite/updates/update_2_to_3.sql @@ -0,0 +1,17 @@ + +-- add table tmw_online_list to store online users +CREATE TABLE tmw_transactions +( + id INTEGER PRIMARY KEY, + char_id INTEGER NOT NULL, + action INTEGER NOT NULL, + message TEXT, + time INTEGER NOT NULL, +); + +-- update the database version, and set date of update +UPDATE tmw_world_states + SET value = '3', + moddate = strftime('%s','now') + WHERE state_name = 'database_version'; + |