Implemented bare-bones character system API

I need to devise a way to update the clients about changes to their
characters directly from the lua scripts. This isn't too important per se,
but the pattern will be important for the monster and trigger systems.
This commit is contained in:
Kayne Ruse
2015-02-21 21:46:22 +11:00
parent 6a999a8a72
commit ddedc06e47
4 changed files with 85 additions and 11 deletions
+8 -10
View File
@@ -3,6 +3,7 @@ print("Lua script check")
mapMaker = require("map_maker") mapMaker = require("map_maker")
mapSaver = require("map_saver") mapSaver = require("map_saver")
roomSystem = require("room_system") roomSystem = require("room_system")
characterSystem = require("character_system")
local function dumpTable(t) local function dumpTable(t)
print(t) print(t)
@@ -11,26 +12,23 @@ local function dumpTable(t)
end end
end end
--debugging (only works correctly with one room)
globalTickTest = {
ticks = 0,
start = 0
}
--test the room hooks --test the room hooks
roomSystem.RoomManager.SetOnCreate(function(room, index) roomSystem.RoomManager.SetOnCreate(function(room, index)
print("", "Creating room: ", roomSystem.Room.GetName(room), index) print("", "Creating room: ", roomSystem.Room.GetName(room), index)
globalTickTest.start = os.clock()
--called ~60 times per second --called ~60 times per second
roomSystem.Room.SetOnTick(room, function(room) roomSystem.Room.SetOnTick(room, function(room)
globalTickTest.ticks = globalTickTest.ticks + 1 local character = characterSystem.CharacterManager.GetCharacter("handle")
if character ~= nil then
--debugging
local x, y = characterSystem.Character.GetOrigin(character)
print("character.Origin(x, y): ", x, y)
end
end) end)
end) end)
roomSystem.RoomManager.SetOnUnload(function(room, index) roomSystem.RoomManager.SetOnUnload(function(room, index)
print("", "Unloading room: ", roomSystem.Room.GetName(room), index) print("", "Unloading room: ", roomSystem.Room.GetName(room), index)
print("Time: ", (os.clock() - globalTickTest.start), "Ticks: ", globalTickTest.ticks)
end) end)
--NOTE: room 0 is the first that the client asks for, therefore it must exist --NOTE: room 0 is the first that the client asks for, therefore it must exist
+10
View File
@@ -193,6 +193,7 @@ void CharacterManager::Unload(int uid) {
} }
void CharacterManager::Delete(int uid) { void CharacterManager::Delete(int uid) {
//TODO: when deleting a character, move it to an archive table
//delete this character from the database, then remove it from memory //delete this character from the database, then remove it from memory
sqlite3_stmt* statement = nullptr; sqlite3_stmt* statement = nullptr;
@@ -252,6 +253,15 @@ CharacterData* CharacterManager::Get(int uid) {
return &it->second; return &it->second;
} }
CharacterData* CharacterManager::Get(std::string handle) {
for (std::map<int, CharacterData>::iterator it = elementMap.begin(); it != elementMap.end(); ++it) {
if (it->second.GetHandle() == handle) {
return &it->second;
}
}
return nullptr;
}
int CharacterManager::GetLoadedCount() { int CharacterManager::GetLoadedCount() {
return elementMap.size(); return elementMap.size();
} }
+5
View File
@@ -44,13 +44,18 @@ public:
//accessors and mutators //accessors and mutators
CharacterData* Get(int uid); CharacterData* Get(int uid);
CharacterData* Get(std::string handle);
int GetLoadedCount(); int GetLoadedCount();
int GetTotalCount(); int GetTotalCount();
std::map<int, CharacterData>* GetContainer(); std::map<int, CharacterData>* GetContainer();
//API interface
sqlite3* SetDatabase(sqlite3* db); sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase(); sqlite3* GetDatabase();
//hooks
//TODO: character hooks
private: private:
friend Singleton<CharacterManager>; friend Singleton<CharacterManager>;
+62 -1
View File
@@ -23,9 +23,70 @@
#include "character_manager.hpp" #include "character_manager.hpp"
//TODO: triggers, accessors static int setOnCreate(lua_State* L) {
//TODO: (9) empty
}
static int setOnLoad(lua_State* L) {
//TODO: (9) empty
}
static int setOnSave(lua_State* L) {
//TODO: (9) empty
}
static int setOnUnload(lua_State* L) {
//TODO: (9) empty
}
static int setOnDelete(lua_State* L) {
//TODO: (9) empty
}
static int getCharacter(lua_State* L) {
//integer vs name
CharacterManager& characterMgr = CharacterManager::GetSingleton();
CharacterData* characterData = nullptr;
switch(lua_type(L, 1)) {
case LUA_TNUMBER:
characterData = characterMgr.Get(lua_tointeger(L, 1));
break;
case LUA_TSTRING:
//access characters via their handles
characterData = characterMgr.Get(lua_tostring(L, 1));
break;
}
if (characterData) {
lua_pushlightuserdata(L, static_cast<void*>(characterData));
}
else {
lua_pushnil(L);
}
return 1;
}
static int getLoadedCount(lua_State* L) {
CharacterManager& characterMgr = CharacterManager::GetSingleton();
lua_pushinteger(L, characterMgr.GetLoadedCount());
return 1;
}
static int forEach(lua_State* L) {
//TODO: (1) find a way to update the clients when a script alters a character's data
}
static const luaL_Reg characterManagerLib[] = { static const luaL_Reg characterManagerLib[] = {
// {"SetOnCreate", setOnCreate},
// {"SetOnLoad", setOnLoad},
// {"SetOnSave", setOnSave},
// {"SetOnUnload", setOnUnload},
// {"SetOnDelete", setOnDelete},
{"GetCharacter", getCharacter},
{"GetLoadedCount", getLoadedCount},
// {"ForEach", forEach},
{nullptr, nullptr} {nullptr, nullptr}
}; };