Converted the server's managers to singletons

This commit is contained in:
Kayne Ruse
2014-08-17 10:06:29 +10:00
parent ce97245131
commit e7ba097e6a
10 changed files with 40 additions and 87 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
#config
INCLUDES+=. ../mapgen ../mapgen/generators ../../common/map
INCLUDES+=. ../../common/map ../../common/utilities
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+7 -6
View File
@@ -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;
+7 -28
View File
@@ -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;
}