diff --git a/common/gameplay/sanity_check.cpp b/common/gameplay/sanity_check.cpp index 719db9f..22f1737 100644 --- a/common/gameplay/sanity_check.cpp +++ b/common/gameplay/sanity_check.cpp @@ -19,7 +19,6 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "account_data.hpp" #include "character_data.hpp" #include "client_data.hpp" #include "combat_data.hpp" diff --git a/common/gameplay/account_data.hpp b/server/account_data.hpp similarity index 100% rename from common/gameplay/account_data.hpp rename to server/account_data.hpp diff --git a/server/account_management.cpp b/server/account_manager.cpp similarity index 88% rename from server/account_management.cpp rename to server/account_manager.cpp index eb298d8..424c125 100644 --- a/server/account_management.cpp +++ b/server/account_manager.cpp @@ -19,9 +19,7 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "server_application.hpp" - -#include "sqlite3/sqlite3.h" +#include "account_manager.hpp" #include @@ -35,10 +33,10 @@ static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL Accounts SET blacklisted static const char* DELETE_USER_ACCOUNT = "DELETE FROM Accounts WHERE uid = ?;"; //------------------------- -//Define the methods +//Define the public methods //------------------------- -int ServerApplication::CreateUserAccount(std::string username, int clientIndex) { +int AccountManager::CreateUserAccount(std::string username, int clientIndex) { //create this user account, failing if it exists, leave this account in memory sqlite3_stmt* statement = nullptr; @@ -65,7 +63,7 @@ int ServerApplication::CreateUserAccount(std::string username, int clientIndex) return LoadUserAccount(username, clientIndex); } -int ServerApplication::LoadUserAccount(std::string username, int clientIndex) { +int AccountManager::LoadUserAccount(std::string username, int clientIndex) { //load this user account, failing if it is in memory, creating it if it doesn't exist sqlite3_stmt* statement = nullptr; @@ -117,7 +115,7 @@ int ServerApplication::LoadUserAccount(std::string username, int clientIndex) { throw(std::runtime_error(std::string() + "Unknown SQL error in LoadUserAccount: " + sqlite3_errmsg(database) )); } -int ServerApplication::SaveUserAccount(int uid) { +int AccountManager::SaveUserAccount(int uid) { //save this user account from memory, replacing it if it exists in the database //DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID. @@ -160,14 +158,14 @@ int ServerApplication::SaveUserAccount(int uid) { return 0; } -void ServerApplication::UnloadUserAccount(int uid) { +void AccountManager::UnloadUserAccount(int uid) { //save this user account, and then unload it //NOTE: the associated characters are unloaded externally SaveUserAccount(uid); accountMap.erase(uid); } -void ServerApplication::DeleteUserAccount(int uid) { +void AccountManager::DeleteUserAccount(int uid) { //delete a user account from the database, and remove it from memory //NOTE: the associated characters should be deleted externally sqlite3_stmt* statement = nullptr; @@ -193,3 +191,29 @@ void ServerApplication::DeleteUserAccount(int uid) { sqlite3_finalize(statement); accountMap.erase(uid); } + +//------------------------- +//Define the accessors and mutators +//------------------------- + +AccountData* AccountManager::GetAccount(int uid) { + std::map::iterator it = accountMap.find(uid); + + if (it == accountMap.end()) { + return nullptr; + } + + return &it->second; +} + +std::map* AccountManager::GetContainer() { + return &accountMap; +} + +sqlite3* AccountManager::SetDatabase(sqlite3* db) { + return database = db; +} + +sqlite3* AccountManager::GetDatabase() { + return database; +} \ No newline at end of file diff --git a/server/account_manager.hpp b/server/account_manager.hpp new file mode 100644 index 0000000..6d109ac --- /dev/null +++ b/server/account_manager.hpp @@ -0,0 +1,55 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef ACCOUNTMANAGER_HPP_ +#define ACCOUNTMANAGER_HPP_ + +#include "account_data.hpp" + +#include "sqlite3/sqlite3.h" + +#include + +class AccountManager { +public: + AccountManager() = default; + ~AccountManager() = default; + + //public access methods + int CreateUserAccount(std::string username, int clientIndex); + int LoadUserAccount(std::string username, int clientIndex); + int SaveUserAccount(int uid); + void UnloadUserAccount(int uid); + void DeleteUserAccount(int uid); + + //accessors and mutators + AccountData* GetAccount(int uid); + std::map* GetContainer(); + + sqlite3* SetDatabase(sqlite3* db); + sqlite3* GetDatabase(); + +private: + std::map accountMap; + sqlite3* database = nullptr; +}; + +#endif diff --git a/server/server_application.hpp b/server/server_application.hpp index cbc7511..87f34de 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -79,12 +79,7 @@ private: void PumpCharacterUnload(int uid); //Account management - int CreateUserAccount(std::string username, int clientIndex); - int LoadUserAccount(std::string username, int clientIndex); - int SaveUserAccount(int uid); - void UnloadUserAccount(int uid); - void DeleteUserAccount(int uid); - + //character management int CreateCharacter(int owner, std::string handle, std::string avatar); int LoadCharacter(int owner, std::string handle, std::string avatar); @@ -101,7 +96,6 @@ private: //server tables std::map clientMap; - std::map accountMap; std::map characterMap; std::map combatMap; std::map enemyMap;