Converted the server's managers to singletons
This commit is contained in:
@@ -23,16 +23,14 @@
|
||||
#define ACCOUNTMANAGER_HPP_
|
||||
|
||||
#include "account_data.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "sqlite3/sqlite3.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
class AccountManager {
|
||||
class AccountManager : public Singleton<AccountManager> {
|
||||
public:
|
||||
AccountManager() = default;
|
||||
~AccountManager() { UnloadAll(); };
|
||||
|
||||
//public access methods
|
||||
int CreateAccount(std::string username, int clientIndex);
|
||||
int LoadAccount(std::string username, int clientIndex);
|
||||
@@ -50,6 +48,11 @@ public:
|
||||
sqlite3* GetDatabase();
|
||||
|
||||
private:
|
||||
friend Singleton<AccountManager>;
|
||||
|
||||
AccountManager() = default;
|
||||
~AccountManager() { UnloadAll(); };
|
||||
|
||||
std::map<int, AccountData> accountMap;
|
||||
sqlite3* database = nullptr;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#config
|
||||
INCLUDES+=.
|
||||
INCLUDES+=. ../../common/utilities
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
|
||||
@@ -23,17 +23,15 @@
|
||||
#define CHARACTERMANAGER_HPP_
|
||||
|
||||
#include "character_data.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "sqlite3/sqlite3.h"
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
|
||||
class CharacterManager {
|
||||
class CharacterManager : public Singleton<CharacterManager> {
|
||||
public:
|
||||
CharacterManager() = default;
|
||||
~CharacterManager() { UnloadAll(); };
|
||||
|
||||
//public access methods
|
||||
int CreateCharacter(int owner, std::string handle, std::string avatar);
|
||||
int LoadCharacter(int owner, std::string handle, std::string avatar);
|
||||
@@ -53,6 +51,11 @@ public:
|
||||
sqlite3* GetDatabase();
|
||||
|
||||
private:
|
||||
friend Singleton<CharacterManager>;
|
||||
|
||||
CharacterManager() = default;
|
||||
~CharacterManager() { UnloadAll(); };
|
||||
|
||||
std::map<int, CharacterData> characterMap;
|
||||
sqlite3* database = nullptr;
|
||||
};
|
||||
|
||||
@@ -33,7 +33,10 @@ using namespace std;
|
||||
int main(int argc, char** argv) {
|
||||
try {
|
||||
//create the singletons
|
||||
AccountManager::Create();
|
||||
CharacterManager::Create();
|
||||
ConfigUtility::Create();
|
||||
RoomManager::Create();
|
||||
UDPNetworkUtility::Create();
|
||||
|
||||
//call the server's routines
|
||||
@@ -47,7 +50,10 @@ int main(int argc, char** argv) {
|
||||
ServerApplication::Delete();
|
||||
|
||||
//delete the singletons
|
||||
AccountManager::Delete();
|
||||
CharacterManager::Delete();
|
||||
ConfigUtility::Delete();
|
||||
RoomManager::Delete();
|
||||
UDPNetworkUtility::Delete();
|
||||
}
|
||||
catch(exception& e) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#config
|
||||
INCLUDES+=. ../mapgen ../mapgen/generators ../../common/map
|
||||
INCLUDES+=. ../../common/map ../../common/utilities
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
|
||||
@@ -23,18 +23,14 @@
|
||||
#define ROOMMANAGER_HPP_
|
||||
|
||||
#include "room_data.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
#define ROOM_MANAGER_PSEUDOINDEX "RoomManager"
|
||||
|
||||
class RoomManager {
|
||||
class RoomManager : public Singleton<RoomManager> {
|
||||
public:
|
||||
RoomManager() = default;
|
||||
~RoomManager() = default;
|
||||
|
||||
//public access methods
|
||||
int CreateRoom();
|
||||
void UnloadRoom(int uid);
|
||||
@@ -52,6 +48,11 @@ public:
|
||||
lua_State* GetLuaState() { return luaState; }
|
||||
|
||||
private:
|
||||
friend Singleton<RoomManager>;
|
||||
|
||||
RoomManager() = default;
|
||||
~RoomManager() = default;
|
||||
|
||||
std::map<int, RoomData*> roomMap;
|
||||
lua_State* luaState = nullptr;
|
||||
int counter = 0;
|
||||
|
||||
@@ -26,45 +26,24 @@
|
||||
#include <string>
|
||||
|
||||
static int getRoom(lua_State* L) {
|
||||
//get the room manager
|
||||
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX);
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
|
||||
|
||||
//push the room and return it
|
||||
lua_pushlightuserdata(L, reinterpret_cast<void*>( roomMgr->GetRoom(lua_tointeger(L, -2)) ));
|
||||
//find, push and return the room
|
||||
RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2));
|
||||
lua_pushlightuserdata(L, reinterpret_cast<void*>(room));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int createRoom(lua_State* L) {
|
||||
//TODO: check parameter count for the glue functions
|
||||
|
||||
//get the room manager
|
||||
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX);
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
|
||||
|
||||
//create the room
|
||||
int uid = roomMgr->CreateRoom();
|
||||
|
||||
//TODO: any room parameters
|
||||
|
||||
//return the new room
|
||||
lua_pushlightuserdata(L, roomMgr->FindRoom(uid));
|
||||
//create, find and return the room
|
||||
int uid = RoomManager::GetSingleton().CreateRoom();
|
||||
lua_pushlightuserdata(L, RoomManager::GetSingleton().FindRoom(uid));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int unloadRoom(lua_State* L) {
|
||||
//get the room manager
|
||||
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX);
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
|
||||
|
||||
//TODO: any room parameters
|
||||
|
||||
//unload the specified room
|
||||
roomMgr->UnloadRoom(lua_tointeger(L, -2));
|
||||
|
||||
RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,14 +85,7 @@ void ServerApplication::Init(int argc, char** argv) {
|
||||
|
||||
roomMgr.SetLuaState(luaState);
|
||||
|
||||
std::cout << "Internal managers set" << std::endl;
|
||||
|
||||
//register the "globals"
|
||||
lua_pushstring(luaState, ROOM_MANAGER_PSEUDOINDEX);
|
||||
lua_pushlightuserdata(luaState, &roomMgr);
|
||||
lua_settable(luaState, LUA_REGISTRYINDEX);
|
||||
|
||||
std::cout << "Internal managers registered with lua" << std::endl;
|
||||
std::cout << "Internal managers initialized" << std::endl;
|
||||
|
||||
//-------------------------
|
||||
//Run the startup scripts
|
||||
|
||||
@@ -92,9 +92,9 @@ private:
|
||||
std::map<int, ClientData> clientMap;
|
||||
|
||||
//managers
|
||||
AccountManager accountMgr;
|
||||
CharacterManager characterMgr;
|
||||
RoomManager roomMgr;
|
||||
AccountManager& accountMgr = AccountManager::GetSingleton();
|
||||
CharacterManager& characterMgr = CharacterManager::GetSingleton();
|
||||
RoomManager& roomMgr = RoomManager::GetSingleton();
|
||||
|
||||
//misc
|
||||
bool running = true;
|
||||
|
||||
Reference in New Issue
Block a user