Added index to CharacterData, removed some expensive lookups

This commit is contained in:
2016-08-25 23:23:57 +10:00
parent 518ea93adc
commit 64833f86da
5 changed files with 27 additions and 28 deletions
+18 -26
View File
@@ -28,50 +28,42 @@
#include "server_utilities.hpp" #include "server_utilities.hpp"
#include <stdexcept> #include <stdexcept>
#include <iostream>
static int setRoom(lua_State* L) { static int setRoom(lua_State* L) {
//reverse engineer the character index //variables
int characterIndex = -1;
CharacterData* character = static_cast<CharacterData*>(lua_touserdata(L, 1)); CharacterData* character = static_cast<CharacterData*>(lua_touserdata(L, 1));
CharacterManager& characterMgr = CharacterManager::GetSingleton(); CharacterManager& characterMgr = CharacterManager::GetSingleton();
for (auto& it : *characterMgr.GetContainer()) {
if (character == &it.second) {
characterIndex = it.first;
break;
}
}
//error checking //error checking
if (characterIndex == -1) { if (characterMgr.Find(character->GetIndex()) != character) {
throw(std::runtime_error("Lua Error: Failed to find character index by reference")); throw(std::runtime_error("Lua Error: Failed to verify character index by reference"));
} }
//get the room index, depending on the parameter type //get the room index, depending on the parameter type
int roomIndex = -1; int roomIndex = -1;
RoomManager& roomMgr = RoomManager::GetSingleton();
switch(lua_type(L, 2)) { switch(lua_type(L, 2)) {
case LUA_TNUMBER: case LUA_TNUMBER:
//simple integer
roomIndex = lua_tointeger(L, 2); roomIndex = lua_tointeger(L, 2);
break; break;
case LUA_TLIGHTUSERDATA: case LUA_TLIGHTUSERDATA: {
//reverse engineer the room index //check that this is a room first
for (auto& it : *roomMgr.GetContainer()) { RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 2));
if (lua_touserdata(L, 2) == &it.second) { RoomManager& roomMgr = RoomManager::GetSingleton();
roomIndex = it.first;
break;
}
}
break;
}
//error checking if (roomMgr.Find(room->GetRoomIndex()) != room) {
if (roomIndex == -1) { std::cout << room->GetRoomIndex() << std::endl;
throw(std::runtime_error("Lua Error: Failed to verify room index by reference"));
}
roomIndex = room->GetRoomIndex();
}
break;
default:
throw(std::runtime_error("Lua Error: Failed to find room index by reference")); throw(std::runtime_error("Lua Error: Failed to find room index by reference"));
} }
//send the delete & create messages //send the delete & create messages
pumpAndChangeRooms(character, roomIndex, characterIndex); pumpAndChangeRooms(character, roomIndex, character->GetIndex());
return 0; return 0;
} }
+4
View File
@@ -30,6 +30,10 @@ CharacterData::CharacterData(): Entity("character") {
}); });
} }
int CharacterData::GetIndex() {
return index;
}
int CharacterData::GetOwner() { int CharacterData::GetOwner() {
return owner; return owner;
} }
+2
View File
@@ -36,6 +36,7 @@ public:
~CharacterData() = default; ~CharacterData() = default;
//database stuff //database stuff
int GetIndex();
int GetOwner(); int GetOwner();
std::string GetHandle(); std::string GetHandle();
std::string GetAvatar(); std::string GetAvatar();
@@ -44,6 +45,7 @@ public:
private: private:
friend class CharacterManager; friend class CharacterManager;
int index = -1;
int owner = -1; int owner = -1;
std::string handle; std::string handle;
std::string avatar; std::string avatar;
+1
View File
@@ -141,6 +141,7 @@ int CharacterManager::Load(int owner, std::string handle, std::string avatar) {
CharacterData& newChar = elementMap[uid]; CharacterData& newChar = elementMap[uid];
//metadata //metadata
newChar.index = uid;
newChar.owner = owner; newChar.owner = owner;
newChar.handle = reinterpret_cast<const char*>(sqlite3_column_text(statement, 2)); newChar.handle = reinterpret_cast<const char*>(sqlite3_column_text(statement, 2));
newChar.avatar = reinterpret_cast<const char*>(sqlite3_column_text(statement, 3)); newChar.avatar = reinterpret_cast<const char*>(sqlite3_column_text(statement, 3));
+1 -1
View File
@@ -89,7 +89,7 @@ private:
std::string tilesetName; std::string tilesetName;
//members //members
int roomIndex = 0; //NOTE: What's this doing here? int roomIndex = 0;
BarrierManager barrierMgr; BarrierManager barrierMgr;
std::list<CharacterData*> characterList; std::list<CharacterData*> characterList;
BattleManager battleMgr; BattleManager battleMgr;