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:
@@ -193,6 +193,7 @@ void CharacterManager::Unload(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
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
@@ -252,6 +253,15 @@ CharacterData* CharacterManager::Get(int uid) {
|
||||
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() {
|
||||
return elementMap.size();
|
||||
}
|
||||
|
||||
@@ -44,13 +44,18 @@ public:
|
||||
|
||||
//accessors and mutators
|
||||
CharacterData* Get(int uid);
|
||||
CharacterData* Get(std::string handle);
|
||||
int GetLoadedCount();
|
||||
int GetTotalCount();
|
||||
std::map<int, CharacterData>* GetContainer();
|
||||
|
||||
//API interface
|
||||
sqlite3* SetDatabase(sqlite3* db);
|
||||
sqlite3* GetDatabase();
|
||||
|
||||
//hooks
|
||||
//TODO: character hooks
|
||||
|
||||
private:
|
||||
friend Singleton<CharacterManager>;
|
||||
|
||||
|
||||
@@ -23,9 +23,70 @@
|
||||
|
||||
#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[] = {
|
||||
// {"SetOnCreate", setOnCreate},
|
||||
// {"SetOnLoad", setOnLoad},
|
||||
// {"SetOnSave", setOnSave},
|
||||
// {"SetOnUnload", setOnUnload},
|
||||
// {"SetOnDelete", setOnDelete},
|
||||
{"GetCharacter", getCharacter},
|
||||
{"GetLoadedCount", getLoadedCount},
|
||||
// {"ForEach", forEach},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user