wczystko pieknie dziala z jednym kluczem :)

master
jachoo 2012-02-15 12:14:39 +01:00
parent 892c504dc5
commit c769d802c5
5 changed files with 148 additions and 3 deletions

View File

@ -305,7 +305,8 @@ ServerEnvironment::ServerEnvironment(ServerMap *map, lua_State *L,
m_game_time_fraction_counter(0),
m_database( new Database(mapsavedir + DIR_DELIM "env.sqlite") ),
m_players_db( m_database->getTable<std::string,binary_t>("players") ),
m_meta_db( m_database->getTable<std::string,std::string>("meta") )
m_meta_db( m_database->getTable<std::string,std::string>("meta") ),
m_players_meta( m_database->getTable<std::string>("players_meta") )
{
}

View File

@ -37,6 +37,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <ostream>
#include "utility.h"
#include "activeobject.h"
#include "db.h"
class Server;
class ActiveBlockModifier;
@ -198,6 +199,16 @@ public:
return m_database;
}
template<class Data> Data getPlayerMeta(const Player& player, const std::string& name)
{
return m_players_meta.get<Data>(std::string(player.getName())+":"+name);
}
template<class Data> bool setPlayerMeta(const Player& player, const std::string& name, const Data& val)
{
return m_players_meta.put(std::string(player.getName())+":"+name,val);
}
/*
Save players
*/
@ -360,6 +371,7 @@ private:
Database* m_database;
Table<std::string,binary_t>& m_players_db; //players table
Table<std::string,binary_t>& m_meta_db; //env metadata table
Table<std::string>& m_players_meta; //players metadata table
};
#ifndef SERVER

View File

@ -36,7 +36,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
//#include "db.h"
class Database;
template<class Key, class Data> class Table;
class ITable;
template<class Key, class Data = void> class Table;
typedef std::string binary_t;
class MapSector;

View File

@ -126,7 +126,7 @@ public:
return NULL;
}
const char * getName()
const char * getName() const
{
return m_name;
}

View File

@ -3272,6 +3272,135 @@ static int l_get_player_privs(lua_State *L)
return 1;
}
// get_player_meta(player_name,meta_name,type)
// types: string, int, double, bool, v3s16, v3f, v3fpos
static int l_get_player_meta(lua_State *L)
{
const std::string player_name = luaL_checkstring(L, 1);
const std::string meta_name = luaL_checkstring(L, 2);
const std::string type = luaL_checkstring(L, 3);
try{
ServerEnvironment* env = get_env(L);
Player* player = env->getPlayer(player_name.c_str());
if(!player) throw BaseException("");
if(type=="string"){
const std::string val = env->getPlayerMeta<std::string>(*player,meta_name);
lua_pushstring(L,val.c_str());
return 1;
}
if(type=="int"){
int val = env->getPlayerMeta<int>(*player,meta_name);
lua_pushinteger(L,val);
return 1;
}
if(type=="double"){
double val = env->getPlayerMeta<double>(*player,meta_name);
lua_pushnumber(L,val);
return 1;
}
if(type=="bool"){
int val = env->getPlayerMeta<int>(*player,meta_name);
lua_pushboolean(L,val != 0);
return 1;
}
if(type=="v3s16"){
v3s16 val = env->getPlayerMeta<v3s16>(*player,meta_name);
push_v3s16(L,val);
return 1;
}
if(type=="v3f"){
v3f val = env->getPlayerMeta<v3f>(*player,meta_name);
push_v3f(L,val);
return 1;
}
if(type=="v3fpos"){
v3f val = env->getPlayerMeta<v3f>(*player,meta_name);
pushFloatPos(L,val);
return 1;
}
}catch(std::exception&){}
//we shall not be here if no error
lua_pushnil(L);
return 1;
}
// set_player_meta(player_name,meta_name,type,value)
// types: string, int, double, bool, v3s16, v3f, v3fpos
static int l_set_player_meta(lua_State *L)
{
const std::string player_name = luaL_checkstring(L, 1);
const std::string meta_name = luaL_checkstring(L, 2);
const std::string type = luaL_checkstring(L, 3);
try{
ServerEnvironment* env = get_env(L);
Player* player = env->getPlayer(player_name.c_str());
if(!player) throw BaseException("");
if(type=="string"){
const std::string val = luaL_checkstring(L, 4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="int"){
const int val = luaL_checkint(L, 4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="double"){
const double val = luaL_checknumber(L, 4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="bool"){
if(!lua_isboolean(L,4)) return luaL_error(L,"bool expected");
const int val = lua_toboolean(L, 4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="v3s16"){
v3s16 val = check_v3s16(L,4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="v3f"){
v3f val = check_v3f(L,4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="v3fpos"){
v3f val = checkFloatPos(L,4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
}catch(std::exception&){}
//we shall not be here if no error
return luaL_error(L,"set_player_meta - error occured");
}
// get_inventory(location)
static int l_get_inventory(lua_State *L)
{
@ -3327,6 +3456,8 @@ static const struct luaL_Reg minetest_f [] = {
{"chat_send_all", l_chat_send_all},
{"chat_send_player", l_chat_send_player},
{"get_player_privs", l_get_player_privs},
{"get_player_meta", l_get_player_meta},
{"set_player_meta", l_set_player_meta},
{"get_inventory", l_get_inventory},
{"get_modpath", l_get_modpath},
{NULL, NULL}