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:
Kayne Ruse
2014-10-07 00:55:55 +11:00
parent 76206a1146
commit 5a42a7e36c
6 changed files with 136 additions and 58 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../../common/map ../../common/utilities INCLUDES+=. ../server_utilities ../../common/map ../../common/utilities
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+49 -27
View File
@@ -29,9 +29,9 @@
//public access methods //public access methods
//------------------------- //-------------------------
int RoomManager::CreateRoom() { int RoomManager::Create() {
//create the room /* //create the room
RoomData* newRoom = new RoomData(); RoomData* newRoom =
newRoom->pager.SetLuaState(luaState); newRoom->pager.SetLuaState(luaState);
//register the room //register the room
@@ -47,13 +47,21 @@ int RoomManager::CreateRoom() {
lua_pop(luaState, 1); lua_pop(luaState, 1);
//finish the routine //finish the routine
return counter++; */ return counter++;
} }
void RoomManager::UnloadRoom(int uid) { int RoomManager::Load() {
//find the room //TODO: RoomManageR::Load()
RoomData* room = FindRoom(uid); }
if (!room) {
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; return;
} }
@@ -69,28 +77,14 @@ void RoomManager::UnloadRoom(int uid) {
//free the memory //free the memory
delete room; delete room;
roomMap.erase(uid); roomMap.erase(uid);
} */}
RoomData* RoomManager::GetRoom(int uid) { void RoomManager::Delete(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::UnloadAll() { void RoomManager::UnloadAll() {
lua_getglobal(luaState, TORTUGA_ROOM_NAME); /* lua_getglobal(luaState, TORTUGA_ROOM_NAME);
for (auto& it : roomMap) { for (auto& it : roomMap) {
//API hook //API hook
@@ -103,4 +97,32 @@ void RoomManager::UnloadAll() {
lua_pop(luaState, 1); lua_pop(luaState, 1);
roomMap.clear(); 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;
}
+25 -24
View File
@@ -23,38 +23,39 @@
#define ROOMMANAGER_HPP_ #define ROOMMANAGER_HPP_
#include "room_data.hpp" #include "room_data.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include "manager_interface.hpp"
#include "lua/lua.hpp" #include "lua/lua.hpp"
#include <map> class RoomManager: public Singleton<RoomManager>, public ManagerInterface<RoomData> {
class RoomManager : public Singleton<RoomManager> {
public: 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;
~RoomManager() = default; ~RoomManager() = default;
std::map<int, RoomData*> roomMap; //common public methods
lua_State* luaState = nullptr; 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; int counter = 0;
}; };
+5 -5
View File
@@ -24,7 +24,7 @@
#include "room_manager.hpp" #include "room_manager.hpp"
#include <string> #include <string>
/*
static int getRoom(lua_State* L) { static int getRoom(lua_State* L) {
//find, push and return the room //find, push and return the room
RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2)); 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)); RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2));
return 0; return 0;
} }
*/
static const luaL_Reg roomManagerLib[] = { static const luaL_Reg roomManagerLib[] = {
{"GetRoom",getRoom}, // {"GetRoom",getRoom},
{"CreateRoom",createRoom}, // {"CreateRoom",createRoom},
{"UnloadRoom",unloadRoom}, // {"UnloadRoom",unloadRoom},
{nullptr, nullptr} {nullptr, nullptr}
}; };
+1 -1
View File
@@ -161,7 +161,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
newPacket.y = argPacket->y; newPacket.y = argPacket->y;
//BUG: possibly related to #35 //BUG: possibly related to #35
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y); newPacket.region = roomMgr.Get(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
//send the content //send the content
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket)); network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
@@ -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 MANAGERINTERFACE_HPP_
#define MANAGERINTERFACE_HPP_
#include <functional>
#include <map>
template<typename T, typename... Arguments>
class ManagerInterface {
public:
//common public methods
virtual int Create(Arguments... parameters) = 0;
virtual int Load(Arguments... parameters) = 0;
virtual int Save(int uid) = 0;
virtual void Unload(int uid) = 0;
virtual void Delete(int uid) = 0;
virtual void UnloadAll() = 0;
virtual void UnloadIf(std::function<bool(std::pair<const int, T>)> fn) = 0;
//accessors & mutators
virtual T* Get(int uid) = 0;
virtual int GetLoadedCount() = 0;
virtual int GetTotalCount() = 0; //can be an alias of GetLoadedCount()
virtual std::map<int, T>* GetContainer() = 0;
protected:
ManagerInterface() = default;
~ManagerInterface() = default;
//members
std::map<int, T> elementMap;
};
#endif