Implemented ManagerInterface in AccountManager and CharacterManager
ManagerInterface was already designed for those two anyway. The only thing left to do is to rewrite the room API, and the whole thing should work fine again. I still think map data should be saved and loaded by lua code, so the rooms will still implement lua hooks. There may be other things that can be loaded from SQL, but I don't know what.
This commit is contained in:
@@ -98,7 +98,7 @@ int CharacterManager::Load(int owner, std::string handle, std::string avatar) {
|
||||
int uid = sqlite3_column_int(statement, 0);
|
||||
|
||||
//check to see if this character is already loaded
|
||||
if (characterMap.find(uid) != characterMap.end()) {
|
||||
if (elementMap.find(uid) != elementMap.end()) {
|
||||
sqlite3_finalize(statement);
|
||||
return -1;
|
||||
}
|
||||
@@ -110,7 +110,7 @@ int CharacterManager::Load(int owner, std::string handle, std::string avatar) {
|
||||
}
|
||||
|
||||
//extract the data into memory
|
||||
CharacterData& newChar = characterMap[uid];
|
||||
CharacterData& newChar = elementMap[uid];
|
||||
|
||||
//metadata
|
||||
newChar.owner = owner;
|
||||
@@ -145,11 +145,11 @@ int CharacterManager::Save(int uid) {
|
||||
//DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID.
|
||||
|
||||
//this method fails if this character is not loaded
|
||||
if (characterMap.find(uid) == characterMap.end()) {
|
||||
if (elementMap.find(uid) == elementMap.end()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
CharacterData& character = characterMap[uid];
|
||||
CharacterData& character = elementMap[uid];
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
@@ -187,7 +187,7 @@ int CharacterManager::Save(int uid) {
|
||||
void CharacterManager::Unload(int uid) {
|
||||
//save this character, then unload it
|
||||
Save(uid);
|
||||
characterMap.erase(uid);
|
||||
elementMap.erase(uid);
|
||||
}
|
||||
|
||||
void CharacterManager::Delete(int uid) {
|
||||
@@ -213,25 +213,26 @@ void CharacterManager::Delete(int uid) {
|
||||
|
||||
//finish the routine
|
||||
sqlite3_finalize(statement);
|
||||
characterMap.erase(uid);
|
||||
elementMap.erase(uid);
|
||||
}
|
||||
|
||||
void CharacterManager::UnloadAll() {
|
||||
for (auto& it : characterMap) {
|
||||
for (auto& it : elementMap) {
|
||||
Save(it.first);
|
||||
}
|
||||
characterMap.clear();
|
||||
elementMap.clear();
|
||||
}
|
||||
|
||||
void CharacterManager::UnloadIf(std::function<bool(std::pair<int, CharacterData>)> fn) {
|
||||
//replicate std::remove_if, using custom code
|
||||
for (std::map<int, CharacterData>::iterator it = characterMap.begin(); it != characterMap.end(); /* empty */) {
|
||||
void CharacterManager::UnloadIf(std::function<bool(std::pair<const int, CharacterData>)> fn) {
|
||||
std::map<int, CharacterData>::iterator it = elementMap.begin();
|
||||
while (it != elementMap.end()) {
|
||||
if (fn(*it)) {
|
||||
Save(it->first);
|
||||
it = characterMap.erase(it);
|
||||
continue;
|
||||
it = elementMap.erase(it);
|
||||
}
|
||||
else {
|
||||
++it;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,9 +241,9 @@ void CharacterManager::UnloadIf(std::function<bool(std::pair<int, CharacterData>
|
||||
//-------------------------
|
||||
|
||||
CharacterData* CharacterManager::Get(int uid) {
|
||||
std::map<int, CharacterData>::iterator it = characterMap.find(uid);
|
||||
std::map<int, CharacterData>::iterator it = elementMap.find(uid);
|
||||
|
||||
if (it == characterMap.end()) {
|
||||
if (it == elementMap.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -250,7 +251,7 @@ CharacterData* CharacterManager::Get(int uid) {
|
||||
}
|
||||
|
||||
int CharacterManager::GetLoadedCount() {
|
||||
return characterMap.size();
|
||||
return elementMap.size();
|
||||
}
|
||||
|
||||
int CharacterManager::GetTotalCount() {
|
||||
@@ -272,7 +273,7 @@ int CharacterManager::GetTotalCount() {
|
||||
}
|
||||
|
||||
std::map<int, CharacterData>* CharacterManager::GetContainer() {
|
||||
return &characterMap;
|
||||
return &elementMap;
|
||||
}
|
||||
|
||||
sqlite3* CharacterManager::SetDatabase(sqlite3* db) {
|
||||
|
||||
@@ -24,29 +24,33 @@
|
||||
|
||||
#include "character_data.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "manager_interface.hpp"
|
||||
|
||||
#include "sqlite3/sqlite3.h"
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
class CharacterManager : public Singleton<CharacterManager> {
|
||||
class CharacterManager:
|
||||
public Singleton<CharacterManager>,
|
||||
public ManagerInterface<CharacterData, int, std::string, std::string>
|
||||
{
|
||||
public:
|
||||
//public access methods
|
||||
int Create(int owner, std::string handle, std::string avatar);
|
||||
int Load(int owner, std::string handle, std::string avatar);
|
||||
int Save(int uid);
|
||||
void Unload(int uid);
|
||||
void Delete(int uid);
|
||||
//common public methods
|
||||
int Create(int owner, std::string handle, std::string avatar) override;
|
||||
int Load(int owner, std::string handle, std::string avatar) override;
|
||||
int Save(int uid) override;
|
||||
void Unload(int uid) override;
|
||||
void Delete(int uid) override;
|
||||
|
||||
void UnloadAll();
|
||||
void UnloadIf(std::function<bool(std::pair<int, CharacterData>)> fn);
|
||||
void UnloadAll() override;
|
||||
void UnloadIf(std::function<bool(std::pair<const int, CharacterData>)> fn) override;
|
||||
|
||||
//accessors and mutators
|
||||
CharacterData* Get(int uid);
|
||||
int GetLoadedCount();
|
||||
int GetTotalCount();
|
||||
std::map<int, CharacterData>* GetContainer();
|
||||
CharacterData* Get(int uid) override;
|
||||
int GetLoadedCount() override;
|
||||
int GetTotalCount() override;
|
||||
std::map<int, CharacterData>* GetContainer() override;
|
||||
|
||||
sqlite3* SetDatabase(sqlite3* db);
|
||||
sqlite3* GetDatabase();
|
||||
@@ -57,7 +61,6 @@ private:
|
||||
CharacterManager() = default;
|
||||
~CharacterManager() = default;
|
||||
|
||||
std::map<int, CharacterData> characterMap;
|
||||
sqlite3* database = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#config
|
||||
INCLUDES+=. ../../common/gameplay ../../common/utilities
|
||||
INCLUDES+=. ../server_utilities ../../common/gameplay ../../common/utilities
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user