From 7fb458ddc14a16fbe10a09e13a09572991f8104c Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 6 Jun 2014 21:52:22 +1000 Subject: [PATCH] Refactored the character management into a separate class --- ...r_management.cpp => character_manager.cpp} | 38 +++++++++++-- server/character_manager.hpp | 55 +++++++++++++++++++ server/server_application.hpp | 5 -- 3 files changed, 87 insertions(+), 11 deletions(-) rename server/{character_management.cpp => character_manager.cpp} (91%) create mode 100644 server/character_manager.hpp diff --git a/server/character_management.cpp b/server/character_manager.cpp similarity index 91% rename from server/character_management.cpp rename to server/character_manager.cpp index 3991305..4064f5d 100644 --- a/server/character_management.cpp +++ b/server/character_manager.cpp @@ -19,7 +19,7 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "server_application.hpp" +#include "character_manager.hpp" #include "sqlite3/sqlite3.h" @@ -60,7 +60,7 @@ static const char* DELETE_CHARACTER = "DELETE FROM Characters WHERE uid = ?;"; //TODO: should statistics be stored separately? //TODO: default stats as a parameter? This would be good for differing beggining states or multiple classes -int ServerApplication::CreateCharacter(int owner, std::string handle, std::string avatar) { +int CharacterManager::CreateCharacter(int owner, std::string handle, std::string avatar) { //Create the character, failing if it exists sqlite3_stmt* statement = nullptr; @@ -93,7 +93,7 @@ int ServerApplication::CreateCharacter(int owner, std::string handle, std::strin return LoadCharacter(owner, handle, avatar); } -int ServerApplication::LoadCharacter(int owner, std::string handle, std::string avatar) { +int CharacterManager::LoadCharacter(int owner, std::string handle, std::string avatar) { //load the specified character, creating it if it doesn't exist //fail if it is already loaded, or does not belong to this account sqlite3_stmt* statement = nullptr; @@ -178,7 +178,7 @@ int ServerApplication::LoadCharacter(int owner, std::string handle, std::string throw(std::runtime_error(std::string() + "Unknown SQL error in LoadCharacter: " + sqlite3_errmsg(database) )); } -int ServerApplication::SaveCharacter(int uid) { +int CharacterManager::SaveCharacter(int uid) { //save this character 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. @@ -241,13 +241,13 @@ int ServerApplication::SaveCharacter(int uid) { return 0; } -void ServerApplication::UnloadCharacter(int uid) { +void CharacterManager::UnloadCharacter(int uid) { //save this character, then unload it SaveCharacter(uid); characterMap.erase(uid); } -void ServerApplication::DeleteCharacter(int uid) { +void CharacterManager::DeleteCharacter(int uid) { //delete this character from the database, then remove it from memory sqlite3_stmt* statement = nullptr; @@ -272,3 +272,29 @@ void ServerApplication::DeleteCharacter(int uid) { sqlite3_finalize(statement); characterMap.erase(uid); } + +//------------------------- +//Define the accessors and mutators +//------------------------- + +CharacterData* CharacterManager::GetCharacter(int uid) { + std::map::iterator it = characterMap.find(uid); + + if (it == characterMap.end()) { + return nullptr; + } + + return &it->second; +} + +std::map* CharacterManager::GetContainer() { + return &characterMap; +} + +sqlite3* CharacterManager::SetDatabase(sqlite3* db) { + return database = db; +} + +sqlite3* CharacterManager::GetDatabase() { + return database; +} \ No newline at end of file diff --git a/server/character_manager.hpp b/server/character_manager.hpp new file mode 100644 index 0000000..c334a7a --- /dev/null +++ b/server/character_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 CHARACTERMANAGER_HPP_ +#define CHARACTERMANAGER_HPP_ + +#include "character_data.hpp" + +#include "sqlite3/sqlite3.h" + +#include + +class CharacterManager { +public: + CharacterManager() = default; + ~CharacterManager() = default; + + //public access methods + int CreateCharacter(int owner, std::string handle, std::string avatar); + int LoadCharacter(int owner, std::string handle, std::string avatar); + int SaveCharacter(int uid); + void UnloadCharacter(int uid); + void DeleteCharacter(int uid); + + //accessors and mutators + CharacterData* GetCharacter(int uid); + std::map* GetContainer(); + + sqlite3* SetDatabase(sqlite3* db); + sqlite3* GetDatabase(); + +private: + std::map characterMap; + sqlite3* database = nullptr; +}; + +#endif diff --git a/server/server_application.hpp b/server/server_application.hpp index 87f34de..6016d8f 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -81,11 +81,6 @@ private: //Account management //character management - int CreateCharacter(int owner, std::string handle, std::string avatar); - int LoadCharacter(int owner, std::string handle, std::string avatar); - int SaveCharacter(int uid); - void UnloadCharacter(int uid); - void DeleteCharacter(int uid); //TODO: combat management