Rollback fixes and get_node_actions

This commit is contained in:
ShadowNinja 2013-11-12 16:13:00 -05:00
parent 1fd9a11e30
commit 7433d65d3e
8 changed files with 618 additions and 568 deletions

@ -515,34 +515,45 @@ minetest.register_on_punchnode(function(pos, node, puncher)
end) end)
minetest.register_chatcommand("rollback_check", { minetest.register_chatcommand("rollback_check", {
params = "[<range>] [<seconds>]", params = "[<range>] [<seconds>] [limit]",
description = "check who has last touched a node or near it, ".. description = "check who has last touched a node or near it, "..
"max. <seconds> ago (default range=0, seconds=86400=24h)", "max. <seconds> ago (default range=0, seconds=86400=24h, limit=5)",
privs = {rollback=true}, privs = {rollback=true},
func = function(name, param) func = function(name, param)
local range, seconds = string.match(param, "(%d+) *(%d*)") local range, seconds, limit =
param:match("(%d+) *(%d*) *(%d*)")
range = tonumber(range) or 0 range = tonumber(range) or 0
seconds = tonumber(seconds) or 86400 seconds = tonumber(seconds) or 86400
minetest.chat_send_player(name, "Punch a node (limits set: range=".. limit = tonumber(limit) or 5
dump(range).." seconds="..dump(seconds).."s)") if limit > 100 then
minetest.chat_send_player(name, "That limit is too high!")
return
end
minetest.chat_send_player(name, "Punch a node (range="..
range..", seconds="..seconds.."s, limit="..limit..")")
minetest.rollback_punch_callbacks[name] = function(pos, node, puncher) minetest.rollback_punch_callbacks[name] = function(pos, node, puncher)
local name = puncher:get_player_name() local name = puncher:get_player_name()
minetest.chat_send_player(name, "Checking...") minetest.chat_send_player(name, "Checking "..minetest.pos_to_string(pos).."...")
local actor, act_p, act_seconds = local actions = minetest.rollback_get_node_actions(pos, range, seconds, limit)
minetest.rollback_get_last_node_actor(pos, range, seconds) local num_actions = #actions
if actor == "" then if num_actions == 0 then
minetest.chat_send_player(name, "Nobody has touched the ".. minetest.chat_send_player(name, "Nobody has touched the "..
"specified location in "..dump(seconds).." seconds") "specified location in "..seconds.." seconds")
return return
end end
local nodedesc = "this node" local time = os.time()
if act_p.x ~= pos.x or act_p.y ~= pos.y or act_p.z ~= pos.z then for i = num_actions, 1, -1 do
nodedesc = minetest.pos_to_string(act_p) local action = actions[i]
minetest.chat_send_player(name,
("%s %s %s -> %s %d seconds ago.")
:format(
minetest.pos_to_string(action.pos),
action.actor,
action.oldnode.name,
action.newnode.name,
time - action.time))
end end
local nodename = minetest.get_node(act_p).name
minetest.chat_send_player(name, "Last actor on "..nodedesc..
" was "..actor..", "..dump(act_seconds)..
"s ago (node is now "..nodename..")")
end end
end, end,
}) })
@ -554,7 +565,7 @@ minetest.register_chatcommand("rollback", {
func = function(name, param) func = function(name, param)
local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)") local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
if not target_name then if not target_name then
local player_name = nil; local player_name = nil
player_name, seconds = string.match(param, "([^ ]+) *(%d*)") player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
if not player_name then if not player_name then
minetest.chat_send_player(name, "Invalid parameters. See /help rollback and /help rollback_check") minetest.chat_send_player(name, "Invalid parameters. See /help rollback and /help rollback_check")
@ -564,13 +575,13 @@ minetest.register_chatcommand("rollback", {
end end
seconds = tonumber(seconds) or 60 seconds = tonumber(seconds) or 60
minetest.chat_send_player(name, "Reverting actions of ".. minetest.chat_send_player(name, "Reverting actions of "..
dump(target_name).." since "..dump(seconds).." seconds.") target_name.." since "..seconds.." seconds.")
local success, log = minetest.rollback_revert_actions_by( local success, log = minetest.rollback_revert_actions_by(
target_name, seconds) target_name, seconds)
if #log > 10 then if #log > 100 then
minetest.chat_send_player(name, "(log is too long to show)") minetest.chat_send_player(name, "(log is too long to show)")
else else
for _,line in ipairs(log) do for _, line in pairs(log) do
minetest.chat_send_player(name, line) minetest.chat_send_player(name, line)
end end
end end

@ -46,3 +46,8 @@ setmetatable(minetest.env, {
return rawget(table, key) return rawget(table, key)
end end
}) })
function minetest.rollback_get_last_node_actor(pos, range, seconds)
return minetest.rollback_get_node_actions(pos, range, seconds, 1)[1]
end

@ -1402,8 +1402,8 @@ minetest.handle_node_drops(pos, drops, digger)
^ Can be overridden to get different functionality (eg. dropping items on ^ Can be overridden to get different functionality (eg. dropping items on
ground) ground)
Rollbacks: Rollback:
minetest.rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds minetest.rollback_get_node_actions(pos, range, seconds, limit) -> {{actor, pos, time, oldnode, newnode}, ...}
^ Find who has done something to a node, or near a node ^ Find who has done something to a node, or near a node
^ actor: "player:<name>", also "liquid". ^ actor: "player:<name>", also "liquid".
minetest.rollback_revert_actions_by(actor, seconds) -> bool, log messages minetest.rollback_revert_actions_by(actor, seconds) -> bool, log messages

File diff suppressed because it is too large Load Diff

@ -36,17 +36,17 @@ public:
virtual bool isActorGuess() = 0; virtual bool isActorGuess() = 0;
virtual void setActor(const std::string &actor, bool is_guess) = 0; virtual void setActor(const std::string &actor, bool is_guess) = 0;
virtual std::string getSuspect(v3s16 p, float nearness_shortcut, virtual std::string getSuspect(v3s16 p, float nearness_shortcut,
float min_nearness) = 0; float min_nearness) = 0;
virtual ~IRollbackManager(){} virtual ~IRollbackManager() {}
virtual void flush() = 0; virtual void flush() = 0;
// Get last actor that did something to position p, but not further than // Get all actors that did something to position p, but not further than
// <seconds> in history // <seconds> in history
virtual std::string getLastNodeActor(v3s16 p, int range, int seconds, virtual std::list<RollbackAction> getNodeActors(v3s16 pos, int range,
v3s16 *act_p, int *act_seconds) = 0; time_t seconds, int limit) = 0;
// Get actions to revert <seconds> of history made by <actor> // Get actions to revert <seconds> of history made by <actor>
virtual std::list<RollbackAction> getRevertActions(const std::string &actor, virtual std::list<RollbackAction> getRevertActions(const std::string &actor,
int seconds) = 0; time_t seconds) = 0;
}; };
IRollbackManager *createRollbackManager(const std::string &filepath, IGameDef *gamedef); IRollbackManager *createRollbackManager(const std::string &filepath, IGameDef *gamedef);

@ -63,7 +63,7 @@ struct RollbackAction
TYPE_MODIFY_INVENTORY_STACK, TYPE_MODIFY_INVENTORY_STACK,
} type; } type;
int unix_time; time_t unix_time;
std::string actor; std::string actor;
bool actor_is_guess; bool actor_is_guess;

@ -24,21 +24,54 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "rollback.h" #include "rollback.h"
// rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds void push_RollbackNode(lua_State *L, RollbackNode &node)
int ModApiRollback::l_rollback_get_last_node_actor(lua_State *L)
{ {
v3s16 p = read_v3s16(L, 1); lua_createtable(L, 0, 3);
lua_pushstring(L, node.name.c_str());
lua_setfield(L, -2, "name");
lua_pushnumber(L, node.param1);
lua_setfield(L, -2, "param1");
lua_pushnumber(L, node.param2);
lua_setfield(L, -2, "param2");
}
// rollback_get_node_actions(pos, range, seconds, limit) -> {{actor, pos, time, oldnode, newnode}, ...}
int ModApiRollback::l_rollback_get_node_actions(lua_State *L)
{
v3s16 pos = read_v3s16(L, 1);
int range = luaL_checknumber(L, 2); int range = luaL_checknumber(L, 2);
int seconds = luaL_checknumber(L, 3); time_t seconds = (time_t) luaL_checknumber(L, 3);
int limit = luaL_checknumber(L, 4);
Server *server = getServer(L); Server *server = getServer(L);
IRollbackManager *rollback = server->getRollbackManager(); IRollbackManager *rollback = server->getRollbackManager();
v3s16 act_p;
int act_seconds = 0; std::list<RollbackAction> actions = rollback->getNodeActors(pos, range, seconds, limit);
std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds); std::list<RollbackAction>::iterator iter = actions.begin();
lua_pushstring(L, actor.c_str());
push_v3s16(L, act_p); lua_createtable(L, actions.size(), 0);
lua_pushnumber(L, act_seconds); for (unsigned int i = 1; iter != actions.end(); ++iter, ++i) {
return 3; lua_pushnumber(L, i); // Push index
lua_createtable(L, 0, 5); // Make a table with enough space pre-allocated
lua_pushstring(L, iter->actor.c_str());
lua_setfield(L, -2, "actor");
push_v3s16(L, iter->p);
lua_setfield(L, -2, "pos");
lua_pushnumber(L, iter->unix_time);
lua_setfield(L, -2, "time");
push_RollbackNode(L, iter->n_old);
lua_setfield(L, -2, "oldnode");
push_RollbackNode(L, iter->n_new);
lua_setfield(L, -2, "newnode");
lua_settable(L, -3); // Add action table to main table
}
return 1;
} }
// rollback_revert_actions_by(actor, seconds) -> bool, log messages // rollback_revert_actions_by(actor, seconds) -> bool, log messages
@ -53,28 +86,19 @@ int ModApiRollback::l_rollback_revert_actions_by(lua_State *L)
bool success = server->rollbackRevertActions(actions, &log); bool success = server->rollbackRevertActions(actions, &log);
// Push boolean result // Push boolean result
lua_pushboolean(L, success); lua_pushboolean(L, success);
// Get the table insert function and push the log table lua_createtable(L, log.size(), 0);
lua_getglobal(L, "table"); unsigned long i = 0;
lua_getfield(L, -1, "insert"); for(std::list<std::string>::const_iterator iter = log.begin();
int table_insert = lua_gettop(L); iter != log.end(); ++i, ++iter) {
lua_newtable(L); lua_pushnumber(L, i);
int table = lua_gettop(L); lua_pushstring(L, iter->c_str());
for(std::list<std::string>::const_iterator i = log.begin(); lua_settable(L, -3);
i != log.end(); i++)
{
lua_pushvalue(L, table_insert);
lua_pushvalue(L, table);
lua_pushstring(L, i->c_str());
if(lua_pcall(L, 2, 0, 0))
script_error(L);
} }
lua_remove(L, -2); // Remove table
lua_remove(L, -2); // Remove insert
return 2; return 2;
} }
void ModApiRollback::Initialize(lua_State *L, int top) void ModApiRollback::Initialize(lua_State *L, int top)
{ {
API_FCT(rollback_get_last_node_actor); API_FCT(rollback_get_node_actions);
API_FCT(rollback_revert_actions_by); API_FCT(rollback_revert_actions_by);
} }

@ -22,10 +22,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_base.h" #include "lua_api/l_base.h"
class ModApiRollback : public ModApiBase { class ModApiRollback : public ModApiBase
{
private: private:
// rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds // rollback_get_node_actions(pos, range, seconds) -> {{actor, pos, time, oldnode, newnode}, ...}
static int l_rollback_get_last_node_actor(lua_State *L); static int l_rollback_get_node_actions(lua_State *L);
// rollback_revert_actions_by(actor, seconds) -> bool, log messages // rollback_revert_actions_by(actor, seconds) -> bool, log messages
static int l_rollback_revert_actions_by(lua_State *L); static int l_rollback_revert_actions_by(lua_State *L);