Helo guys
Just implemented a litle storage system where key is a string. This can be usefull in cases you want to have a visual view of data in you database and if you wanna get less confused while programming your lua scripts
The system is very litle tested if I find bugs I will update the post.
Any comments are wellcome
so to install:
create the new player_special_storage table:
add this code to your iologindata.cpp at IOLoginData::loadPlayer(Player* player, DBResult_ptr result):
add to you iologindata.cpp at IOLoginData::savePlayer(Player* player):
add this 2 new methods to play.cpp:
at player.h where you have your player storage(after std::map<uint32_t, std::string> storageStringMap; for example) system you should add the new map:
also declair new methods at player.h like this:
at luascript.h:
at luascript.cpp:
and also add at luascript.cpp:
and thats it, hope it is usefull for someone
Just implemented a litle storage system where key is a string. This can be usefull in cases you want to have a visual view of data in you database and if you wanna get less confused while programming your lua scripts
The system is very litle tested if I find bugs I will update the post.
Any comments are wellcome
so to install:
create the new player_special_storage table:
SQL:
CREATE TABLE `player_special_storage` (
`player_id` int(11) NOT NULL DEFAULT 0,
`key` varchar(255) NOT NULL DEFAULT '0',
`value` varchar(255) NOT NULL DEFAULT '0') -- value is atm as varchar but is used for ints only
ALTER TABLE `player_special_storage`
ADD PRIMARY KEY (`player_id`,`key`);
ALTER TABLE `player_special_storage`
ADD CONSTRAINT `player_special_storage_ibfk_1` FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE;
add this code to your iologindata.cpp at IOLoginData::loadPlayer(Player* player, DBResult_ptr result):
Lua:
//load special player storage
query.str(std::string());
query << "SELECT `key`, `value` FROM `player_special_storage` WHERE `player_id` = " << player->getGUID();
if ((result = db.storeQuery(query.str()))) {
do {
player->addPlayerSpecialStorageValue(result->getString("key"), result->getNumber<int32_t>("value"));
} while (result->next());
}
add to you iologindata.cpp at IOLoginData::savePlayer(Player* player):
Lua:
query.str(std::string());
query << "DELETE FROM `player_special_storage` WHERE `player_id` = " << player->getGUID();
if (!db.executeQuery(query.str())) {
return false;
}
query.str(std::string());
DBInsert playerSpecialStorageQuery("INSERT INTO `player_special_storage` (`player_id`, `key`, `value`) VALUES ");
for (const auto& it : player->playerSpecialStorageMap) {
query << player->getGUID() << ',' << db.escapeString(it.first) << ',' << it.second;
if (!playerSpecialStorageQuery.addRow(query)) {
return false;
}
}
if (!playerSpecialStorageQuery.execute()) {
return false;
}
add this 2 new methods to play.cpp:
Lua:
void Player::addPlayerSpecialStorageValue(const std::string& key, const int32_t value)
{
if (value == -1) {
playerSpecialStorageMap.erase(key);
}
else {
playerSpecialStorageMap[key] = value;
}
}
bool Player::getPlayerSpecialStorageValue(const std::string key, int32_t& value) const
{
auto it = playerSpecialStorageMap.find(key);
if (it == playerSpecialStorageMap.end()) {
value = -1;
return false;
}
value = it->second;
return true;
}
at player.h where you have your player storage(after std::map<uint32_t, std::string> storageStringMap; for example) system you should add the new map:
Lua:
std::map<std::string, int32_t> playerSpecialStorageMap;
also declair new methods at player.h like this:
Lua:
bool getPlayerSpecialStorageValue(const std::string key, int32_t& value) const;
void addPlayerSpecialStorageValue(const std::string& key, const int32_t value);
at luascript.h:
Lua:
static int luaPlayerGetPlayerSpecialStorageValue(lua_State* L);
static int luaPlayerSetPlayerSpecialStorageValue(lua_State* L);
at luascript.cpp:
Lua:
registerMethod("Player", "getPlayerSpecialStorageValue", LuaScriptInterface::luaPlayerGetPlayerSpecialStorageValue);
registerMethod("Player", "setPlayerSpecialStorageValue", LuaScriptInterface::luaPlayerSetPlayerSpecialStorageValue);
and also add at luascript.cpp:
Lua:
int LuaScriptInterface::luaPlayerGetPlayerSpecialStorageValue(lua_State* L)
{
//player:getPlayerSpecialStorageValue(key)
Player* player = getUserdata<Player>(L, 1);
if (!player) {
lua_pushnil(L);
return 1;
}
std::string key = getString(L, 2);
int32_t value;
std::string strValue;
if (player->getPlayerSpecialStorageValue(key, value)) {
lua_pushnumber(L, value);
}
else {
lua_pushnumber(L, -1);
}
return 1;
}
int LuaScriptInterface::luaPlayerSetPlayerSpecialStorageValue(lua_State* L)
{
//player:setPlayerSpecialStorageValue(key, value)
std::string key = getString(L, 2);
Player* player = getUserdata<Player>(L, 1);
if (player) {
player->addPlayerSpecialStorageValue(key, getNumber<int32_t>(L, 3));
pushBoolean(L, true);
}
else {
lua_pushnil(L);
}
return 1;
}
and thats it, hope it is usefull for someone