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)
minetest.register_chatcommand("rollback_check", {
params = "[<range>] [<seconds>]",
params = "[<range>] [<seconds>] [limit]",
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},
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
seconds = tonumber(seconds) or 86400
minetest.chat_send_player(name, "Punch a node (limits set: range="..
dump(range).." seconds="..dump(seconds).."s)")
limit = tonumber(limit) or 5
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)
local name = puncher:get_player_name()
minetest.chat_send_player(name, "Checking...")
local actor, act_p, act_seconds =
minetest.rollback_get_last_node_actor(pos, range, seconds)
if actor == "" then
minetest.chat_send_player(name, "Checking "..minetest.pos_to_string(pos).."...")
local actions = minetest.rollback_get_node_actions(pos, range, seconds, limit)
local num_actions = #actions
if num_actions == 0 then
minetest.chat_send_player(name, "Nobody has touched the "..
"specified location in "..dump(seconds).." seconds")
"specified location in "..seconds.." seconds")
return
end
local nodedesc = "this node"
if act_p.x ~= pos.x or act_p.y ~= pos.y or act_p.z ~= pos.z then
nodedesc = minetest.pos_to_string(act_p)
local time = os.time()
for i = num_actions, 1, -1 do
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
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,
})
@ -554,7 +565,7 @@ minetest.register_chatcommand("rollback", {
func = function(name, param)
local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
if not target_name then
local player_name = nil;
local player_name = nil
player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
if not player_name then
minetest.chat_send_player(name, "Invalid parameters. See /help rollback and /help rollback_check")
@ -564,13 +575,13 @@ minetest.register_chatcommand("rollback", {
end
seconds = tonumber(seconds) or 60
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(
target_name, seconds)
if #log > 10 then
if #log > 100 then
minetest.chat_send_player(name, "(log is too long to show)")
else
for _,line in ipairs(log) do
for _, line in pairs(log) do
minetest.chat_send_player(name, line)
end
end

@ -46,3 +46,8 @@ setmetatable(minetest.env, {
return rawget(table, key)
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
ground)
Rollbacks:
minetest.rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
Rollback:
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
^ actor: "player:<name>", also "liquid".
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 void setActor(const std::string &actor, bool is_guess) = 0;
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;
// 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
virtual std::string getLastNodeActor(v3s16 p, int range, int seconds,
v3s16 *act_p, int *act_seconds) = 0;
virtual std::list<RollbackAction> getNodeActors(v3s16 pos, int range,
time_t seconds, int limit) = 0;
// Get actions to revert <seconds> of history made by <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);

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

@ -24,21 +24,54 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "rollback.h"
// rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
int ModApiRollback::l_rollback_get_last_node_actor(lua_State *L)
void push_RollbackNode(lua_State *L, RollbackNode &node)
{
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 seconds = luaL_checknumber(L, 3);
time_t seconds = (time_t) luaL_checknumber(L, 3);
int limit = luaL_checknumber(L, 4);
Server *server = getServer(L);
IRollbackManager *rollback = server->getRollbackManager();
v3s16 act_p;
int act_seconds = 0;
std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
lua_pushstring(L, actor.c_str());
push_v3s16(L, act_p);
lua_pushnumber(L, act_seconds);
return 3;
std::list<RollbackAction> actions = rollback->getNodeActors(pos, range, seconds, limit);
std::list<RollbackAction>::iterator iter = actions.begin();
lua_createtable(L, actions.size(), 0);
for (unsigned int i = 1; iter != actions.end(); ++iter, ++i) {
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
@ -53,28 +86,19 @@ int ModApiRollback::l_rollback_revert_actions_by(lua_State *L)
bool success = server->rollbackRevertActions(actions, &log);
// Push boolean result
lua_pushboolean(L, success);
// Get the table insert function and push the log table
lua_getglobal(L, "table");
lua_getfield(L, -1, "insert");
int table_insert = lua_gettop(L);
lua_newtable(L);
int table = lua_gettop(L);
for(std::list<std::string>::const_iterator i = log.begin();
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_createtable(L, log.size(), 0);
unsigned long i = 0;
for(std::list<std::string>::const_iterator iter = log.begin();
iter != log.end(); ++i, ++iter) {
lua_pushnumber(L, i);
lua_pushstring(L, iter->c_str());
lua_settable(L, -3);
}
lua_remove(L, -2); // Remove table
lua_remove(L, -2); // Remove insert
return 2;
}
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);
}

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