Wrote ManagerInterface, implemented it in RoomManager
ManagerInterface is a server-side pure abstract class; all of it's methods are defined as pure virtual methods, and as such should be defined in the derived classes. It works correctly along side the Singleton class, but does not implement it directly as originally planned. It should also support variadic parameters, which still need testing. I've implemented ManagerInterface in RoomManager, but I've also disabled a number of the RoomManager's features, including the lua interface. The project as a whole should build, but it won't run correctly. The variadic parameters will be tested using the other managers. It feels good just playing around instead of pushing forward all the time.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#config
|
||||
INCLUDES+=. ../../common/map ../../common/utilities
|
||||
INCLUDES+=. ../server_utilities ../../common/map ../../common/utilities
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
|
||||
@@ -29,9 +29,9 @@
|
||||
//public access methods
|
||||
//-------------------------
|
||||
|
||||
int RoomManager::CreateRoom() {
|
||||
//create the room
|
||||
RoomData* newRoom = new RoomData();
|
||||
int RoomManager::Create() {
|
||||
/* //create the room
|
||||
RoomData* newRoom =
|
||||
newRoom->pager.SetLuaState(luaState);
|
||||
|
||||
//register the room
|
||||
@@ -47,13 +47,21 @@ int RoomManager::CreateRoom() {
|
||||
lua_pop(luaState, 1);
|
||||
|
||||
//finish the routine
|
||||
return counter++;
|
||||
*/ return counter++;
|
||||
}
|
||||
|
||||
void RoomManager::UnloadRoom(int uid) {
|
||||
//find the room
|
||||
RoomData* room = FindRoom(uid);
|
||||
if (!room) {
|
||||
int RoomManager::Load() {
|
||||
//TODO: RoomManageR::Load()
|
||||
}
|
||||
|
||||
int RoomManager::Save(int uid) {
|
||||
//TODO: RoomManageR::Save(uid)
|
||||
}
|
||||
|
||||
void RoomManager::Unload(int uid) {
|
||||
/* //find the room
|
||||
RoomData* room = elementMap.find(uid);
|
||||
if (room == elementMap.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -69,28 +77,14 @@ void RoomManager::UnloadRoom(int uid) {
|
||||
//free the memory
|
||||
delete room;
|
||||
roomMap.erase(uid);
|
||||
}
|
||||
*/}
|
||||
|
||||
RoomData* RoomManager::GetRoom(int uid) {
|
||||
return FindRoom(uid);
|
||||
//TODO: expand this to auto-create the room
|
||||
}
|
||||
|
||||
RoomData* RoomManager::FindRoom(int uid) {
|
||||
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
|
||||
if (it == roomMap.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
int RoomManager::PushRoom(RoomData* room) {
|
||||
roomMap[counter++] = room;
|
||||
return counter;
|
||||
void RoomManager::Delete(int uid) {
|
||||
//
|
||||
}
|
||||
|
||||
void RoomManager::UnloadAll() {
|
||||
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
||||
/* lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
||||
|
||||
for (auto& it : roomMap) {
|
||||
//API hook
|
||||
@@ -103,4 +97,32 @@ void RoomManager::UnloadAll() {
|
||||
|
||||
lua_pop(luaState, 1);
|
||||
roomMap.clear();
|
||||
}
|
||||
*/}
|
||||
|
||||
void RoomManager::UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) {
|
||||
std::map<int, RoomData>::iterator it = elementMap.begin();
|
||||
while (it != elementMap.end()) {
|
||||
if (fn(*it)) {
|
||||
it = elementMap.erase(it);
|
||||
}
|
||||
else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RoomData* RoomManager::Get(int uid) {
|
||||
return &elementMap[uid];
|
||||
}
|
||||
|
||||
int RoomManager::GetLoadedCount() {
|
||||
return elementMap.size();
|
||||
}
|
||||
|
||||
int RoomManager::GetTotalCount() {
|
||||
return elementMap.size();
|
||||
}
|
||||
|
||||
std::map<int, RoomData>* RoomManager::GetContainer() {
|
||||
return &elementMap;
|
||||
}
|
||||
|
||||
@@ -23,38 +23,39 @@
|
||||
#define ROOMMANAGER_HPP_
|
||||
|
||||
#include "room_data.hpp"
|
||||
|
||||
#include "singleton.hpp"
|
||||
#include "manager_interface.hpp"
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
class RoomManager : public Singleton<RoomManager> {
|
||||
class RoomManager: public Singleton<RoomManager>, public ManagerInterface<RoomData> {
|
||||
public:
|
||||
//public access methods
|
||||
int CreateRoom();
|
||||
void UnloadRoom(int uid);
|
||||
|
||||
RoomData* GetRoom(int uid);
|
||||
RoomData* FindRoom(int uid);
|
||||
int PushRoom(RoomData*);
|
||||
|
||||
void UnloadAll();
|
||||
|
||||
//accessors and mutators
|
||||
std::map<int, RoomData*>* GetContainer() { return &roomMap; }
|
||||
|
||||
lua_State* SetLuaState(lua_State* L) { return luaState = L; }
|
||||
lua_State* GetLuaState() { return luaState; }
|
||||
|
||||
private:
|
||||
friend Singleton<RoomManager>;
|
||||
|
||||
RoomManager() = default;
|
||||
~RoomManager() = default;
|
||||
|
||||
std::map<int, RoomData*> roomMap;
|
||||
lua_State* luaState = nullptr;
|
||||
//common public methods
|
||||
int Create() override;
|
||||
int Load() override;
|
||||
int Save(int uid) override;
|
||||
void Unload(int uid) override;
|
||||
void Delete(int uid) override;
|
||||
|
||||
void UnloadAll() override;
|
||||
void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) override;
|
||||
|
||||
//accessors and mutators
|
||||
RoomData* Get(int uid) override;
|
||||
int GetLoadedCount() override;
|
||||
int GetTotalCount() override;
|
||||
std::map<int, RoomData>* GetContainer() override;
|
||||
|
||||
//hooks
|
||||
lua_State* SetLuaState(lua_State* L) { return lua = L; }
|
||||
lua_State* GetLuaState() { return lua; }
|
||||
|
||||
private:
|
||||
lua_State* lua = nullptr;
|
||||
int counter = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include "room_manager.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
/*
|
||||
static int getRoom(lua_State* L) {
|
||||
//find, push and return the room
|
||||
RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2));
|
||||
@@ -46,11 +46,11 @@ static int unloadRoom(lua_State* L) {
|
||||
RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
*/
|
||||
static const luaL_Reg roomManagerLib[] = {
|
||||
{"GetRoom",getRoom},
|
||||
{"CreateRoom",createRoom},
|
||||
{"UnloadRoom",unloadRoom},
|
||||
// {"GetRoom",getRoom},
|
||||
// {"CreateRoom",createRoom},
|
||||
// {"UnloadRoom",unloadRoom},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user