Added some basic code to RoomManager
This commit is contained in:
@@ -27,11 +27,11 @@
|
||||
|
||||
struct RoomData {
|
||||
enum class RoomType {
|
||||
OVERWORLD,
|
||||
RUINS,
|
||||
TOWERS,
|
||||
FORESTS,
|
||||
CAVES,
|
||||
OVERWORLD = 0,
|
||||
RUINS = 1,
|
||||
TOWERS = 2,
|
||||
FORESTS = 3,
|
||||
CAVE = 4,
|
||||
};
|
||||
|
||||
//members
|
||||
|
||||
+34
-16
@@ -21,34 +21,52 @@
|
||||
*/
|
||||
#include "room_manager.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//public access methods
|
||||
//-------------------------
|
||||
|
||||
//TODO
|
||||
RoomData* RoomManager::CreateRoom(int uid) {
|
||||
//don't overwrite existing rooms
|
||||
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
|
||||
if (it != roomMap.end()) {
|
||||
throw(std::runtime_error("Cannot overwrite an existing room"));
|
||||
}
|
||||
roomMap[uid] = new RoomData();
|
||||
//TODO: create room in the API
|
||||
if (luaState) {
|
||||
roomMap[uid]->pager.SetLuaState(luaState);
|
||||
}
|
||||
return roomMap[uid];
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//accessors and mutators
|
||||
//-------------------------
|
||||
RoomData* RoomManager::UnloadRoom(int uid) {
|
||||
//TODO: unload room in the API
|
||||
delete roomMap[uid];
|
||||
roomMap.erase(uid);
|
||||
}
|
||||
|
||||
RoomData* RoomManager::GetRoom(int uid) {
|
||||
std::map<int, RoomData>::iterator it = roomMap.find(uid);
|
||||
RoomData* ptr = FindRoom(uid);
|
||||
if (ptr) return ptr;
|
||||
ptr = CreateRoom(uid);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
RoomData* RoomManager::FindRoom(int uid) {
|
||||
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
|
||||
if (it == roomMap.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &it->second;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::map<int, RoomData>* RoomManager::GetContainer() {
|
||||
return &roomMap;
|
||||
RoomData* RoomManager::PushRoom(int uid, RoomData* room) {
|
||||
//unload existing rooms with this index
|
||||
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
|
||||
if (it != roomMap.end()) {
|
||||
UnloadRoom(uid);
|
||||
}
|
||||
|
||||
lua_State* RoomManager::SetLuaState(lua_State* L) {
|
||||
return luaState = L;
|
||||
}
|
||||
|
||||
lua_State* RoomManager::GetLuaState() {
|
||||
return luaState;
|
||||
roomMap[uid] = room;
|
||||
}
|
||||
|
||||
+10
-6
@@ -36,17 +36,21 @@ public:
|
||||
~RoomManager() = default;
|
||||
|
||||
//public access methods
|
||||
//TODO: Fill this out
|
||||
RoomData* CreateRoom(int uid);
|
||||
RoomData* UnloadRoom(int uid);
|
||||
|
||||
RoomData* GetRoom(int uid);
|
||||
RoomData* FindRoom(int uid);
|
||||
RoomData* PushRoom(int uid, RoomData*);
|
||||
|
||||
//accessors and mutators
|
||||
RoomData* GetRoom(int uid);
|
||||
std::map<int, RoomData>* GetContainer();
|
||||
std::map<int, RoomData*>* GetContainer() { return &roomMap; }
|
||||
|
||||
lua_State* SetLuaState(lua_State*);
|
||||
lua_State* GetLuaState();
|
||||
lua_State* SetLuaState(lua_State* L) { return luaState = L; }
|
||||
lua_State* GetLuaState() { return luaState; }
|
||||
|
||||
private:
|
||||
std::map<int, RoomData> roomMap;
|
||||
std::map<int, RoomData*> roomMap;
|
||||
lua_State* luaState = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -316,7 +316,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
|
||||
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->pager.GetRegion(argPacket->x, argPacket->y);
|
||||
|
||||
//send the content
|
||||
network.SendTo(&argPacket->srcAddress, static_cast<SerialPacket*>(argPacket));
|
||||
network.SendTo(&argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
|
||||
Reference in New Issue
Block a user