Began modulating ServerApplication, beginning with the accounts

This commit is contained in:
Kayne Ruse
2014-06-06 21:42:27 +10:00
parent 6664f8a8bc
commit 310f701b0d
5 changed files with 89 additions and 17 deletions
-1
View File
@@ -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"
@@ -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 <stdexcept>
@@ -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<int, AccountData>::iterator it = accountMap.find(uid);
if (it == accountMap.end()) {
return nullptr;
}
return &it->second;
}
std::map<int, AccountData>* AccountManager::GetContainer() {
return &accountMap;
}
sqlite3* AccountManager::SetDatabase(sqlite3* db) {
return database = db;
}
sqlite3* AccountManager::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 ACCOUNTMANAGER_HPP_
#define ACCOUNTMANAGER_HPP_
#include "account_data.hpp"
#include "sqlite3/sqlite3.h"
#include <map>
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<int, AccountData>* GetContainer();
sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase();
private:
std::map<int, AccountData> accountMap;
sqlite3* database = nullptr;
};
#endif
-6
View File
@@ -79,11 +79,6 @@ 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);
@@ -101,7 +96,6 @@ private:
//server tables
std::map<int, ClientData> clientMap;
std::map<int, AccountData> accountMap;
std::map<int, CharacterData> characterMap;
std::map<int, CombatData> combatMap;
std::map<int, EnemyData> enemyMap;