Encapsulated RoomData, did some other refactoring

This commit is contained in:
Kayne Ruse
2014-08-14 00:13:19 +10:00
parent 6d98bab000
commit 98ffcb8cd3
6 changed files with 44 additions and 33 deletions
+11 -9
View File
@@ -21,13 +21,15 @@
*/
#include "room_manager.hpp"
#include "room_api.hpp"
#include <stdexcept>
//-------------------------
//public access methods
//-------------------------
RoomData* RoomManager::CreateRoom() {
int RoomManager::CreateRoom() {
//create the room
RoomData* newRoom = new RoomData();
@@ -37,11 +39,11 @@ RoomData* RoomManager::CreateRoom() {
}
//register the room
roomMap[counter++] = newRoom;
roomMap[counter] = newRoom;
//API hook
lua_getglobal(luaState, "Room");
lua_getfield(luaState, -1, "OnCreate");
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
lua_getfield(luaState, -1, "Create");
lua_pushlightuserdata(luaState, newRoom);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
@@ -49,7 +51,7 @@ RoomData* RoomManager::CreateRoom() {
lua_pop(luaState, 1);
//finish the routine
return newRoom;
return counter++;
}
void RoomManager::UnloadRoom(int uid) {
@@ -60,8 +62,8 @@ void RoomManager::UnloadRoom(int uid) {
}
//API hook
lua_getglobal(luaState, "Room");
lua_getfield(luaState, -1, "OnUnload");
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
lua_getfield(luaState, -1, "Unload");
lua_pushlightuserdata(luaState, room);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
@@ -92,11 +94,11 @@ int RoomManager::PushRoom(RoomData* room) {
}
void RoomManager::UnloadAll() {
lua_getglobal(luaState, "Room");
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
for (auto& it : roomMap) {
//API hook
lua_getfield(luaState, -1, "OnUnload");
lua_getfield(luaState, -1, "Unload");
lua_pushlightuserdata(luaState, it.second);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));