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:
Kayne Ruse
2014-10-07 01:23:55 +11:00
parent 5a42a7e36c
commit 254b97aa80
8 changed files with 90 additions and 71 deletions
+19 -17
View File
@@ -87,13 +87,13 @@ int AccountManager::Load(std::string username, int clientIndex) {
int uid = sqlite3_column_int(statement, 0);
//check to see if this account is already loaded
if (accountMap.find(uid) != accountMap.end()) {
if (elementMap.find(uid) != elementMap.end()) {
sqlite3_finalize(statement);
return -1;
}
//extract the data into memory
AccountData& newAccount = accountMap[uid];
AccountData& newAccount = elementMap[uid];
newAccount.username = reinterpret_cast<const char*>(sqlite3_column_text(statement, 1));
newAccount.blackListed = sqlite3_column_int(statement, 2);
newAccount.whiteListed = sqlite3_column_int(statement, 3);
@@ -121,11 +121,11 @@ int AccountManager::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 account is not loaded
if (accountMap.find(uid) == accountMap.end()) {
if (elementMap.find(uid) == elementMap.end()) {
return -1;
}
AccountData& account = accountMap[uid];
AccountData& account = elementMap[uid];
sqlite3_stmt* statement = nullptr;
//prep
@@ -163,7 +163,7 @@ void AccountManager::Unload(int uid) {
//save this user account, and then unload it
//NOTE: the associated characters are unloaded externally
Save(uid);
accountMap.erase(uid);
elementMap.erase(uid);
}
void AccountManager::Delete(int uid) {
@@ -190,25 +190,27 @@ void AccountManager::Delete(int uid) {
//finish the routine
sqlite3_finalize(statement);
accountMap.erase(uid);
elementMap.erase(uid);
}
void AccountManager::UnloadAll() {
for (auto& it : accountMap) {
for (auto& it : elementMap) {
Save(it.first);
}
accountMap.clear();
elementMap.clear();
}
void AccountManager::UnloadIf(std::function<bool(std::pair<int, AccountData>)> fn) {
void AccountManager::UnloadIf(std::function<bool(std::pair<const int, AccountData>)> fn) {
//replicate std::remove_if, using custom code
for (std::map<int, AccountData>::iterator it = accountMap.begin(); it != accountMap.end(); /* empty */) {
std::map<int, AccountData>::iterator it = elementMap.begin();
while (it != elementMap.end()) {
if (fn(*it)) {
Save(it->first);
it = accountMap.erase(it);
continue;
it = elementMap.erase(it);
}
else {
++it;
}
++it;
}
}
@@ -218,9 +220,9 @@ void AccountManager::UnloadIf(std::function<bool(std::pair<int, AccountData>)> f
AccountData* AccountManager::Get(int uid) {
//TODO: could this load an account first?
std::map<int, AccountData>::iterator it = accountMap.find(uid);
std::map<int, AccountData>::iterator it = elementMap.find(uid);
if (it == accountMap.end()) {
if (it == elementMap.end()) {
return nullptr;
}
@@ -228,7 +230,7 @@ AccountData* AccountManager::Get(int uid) {
}
int AccountManager::GetLoadedCount() {
return accountMap.size();
return elementMap.size();
}
int AccountManager::GetTotalCount() {
@@ -250,7 +252,7 @@ int AccountManager::GetTotalCount() {
}
std::map<int, AccountData>* AccountManager::GetContainer() {
return &accountMap;
return &elementMap;
}
sqlite3* AccountManager::SetDatabase(sqlite3* db) {
+17 -14
View File
@@ -24,29 +24,33 @@
#include "account_data.hpp"
#include "singleton.hpp"
#include "manager_interface.hpp"
#include "sqlite3/sqlite3.h"
#include <functional>
#include <map>
class AccountManager : public Singleton<AccountManager> {
class AccountManager:
public Singleton<AccountManager>,
public ManagerInterface<AccountData, std::string, int>
{
public:
//public access methods
int Create(std::string username, int clientIndex);
int Load(std::string username, int clientIndex);
int Save(int uid);
void Unload(int uid);
void Delete(int uid);
//common public methods
int Create(std::string username, int clientIndex) override;
int Load(std::string username, int clientIndex) 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, AccountData>)> fn);
void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, AccountData>)> fn) override;
//accessors and mutators
AccountData* Get(int uid);
int GetLoadedCount();
int GetTotalCount();
std::map<int, AccountData>* GetContainer();
AccountData* Get(int uid) override;
int GetLoadedCount() override;
int GetTotalCount() override;
std::map<int, AccountData>* GetContainer() override;
sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase();
@@ -57,7 +61,6 @@ private:
AccountManager() = default;
~AccountManager() = default;
std::map<int, AccountData> accountMap;
sqlite3* database = nullptr;
};
+1 -1
View File
@@ -1,5 +1,5 @@
#config
INCLUDES+=. ../../common/utilities
INCLUDES+=. ../server_utilities ../../common/utilities
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))