Refactored the character management into a separate class

This commit is contained in:
Kayne Ruse
2014-06-06 21:52:22 +10:00
parent 310f701b0d
commit 7fb458ddc1
3 changed files with 87 additions and 11 deletions
@@ -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<int, CharacterData>::iterator it = characterMap.find(uid);
if (it == characterMap.end()) {
return nullptr;
}
return &it->second;
}
std::map<int, CharacterData>* CharacterManager::GetContainer() {
return &characterMap;
}
sqlite3* CharacterManager::SetDatabase(sqlite3* db) {
return database = db;
}
sqlite3* CharacterManager::GetDatabase() {
return database;
}
+55
View File
@@ -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 <map>
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<int, CharacterData>* GetContainer();
sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase();
private:
std::map<int, CharacterData> characterMap;
sqlite3* database = nullptr;
};
#endif
-5
View File
@@ -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