From db5a67b333cc0e938479def9cbb8a938dc852eab Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 11 Sep 2022 00:07:18 +0200 Subject: [PATCH] added showing of npc logfile via trade list --- fs_show_log.lua | 107 ++++++++++++++++++++++++++++++++++++++++++++++ fs_trade_list.lua | 16 ++++++- show_fs.lua | 10 +++++ 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/fs_show_log.lua b/fs_show_log.lua index 7266118..f75e03a 100644 --- a/fs_show_log.lua +++ b/fs_show_log.lua @@ -68,3 +68,110 @@ yl_speak_up.log_with_position = function(pname, n_id, text, log_level) yl_speak_up.log_change(pname, n_id, "NPC at position "..pos_str.." "..tostring(text), log_level) end + + +yl_speak_up.input_show_log = function(player, formname, fields) + local pname = player:get_player_name() + local n_id = yl_speak_up.speak_to[pname].n_id + if(not(yl_speak_up.may_edit_npc(player, n_id))) then + return + end + if(fields.back) then + -- back to the normal trade list + yl_speak_up.show_fs(player, "trade_list") + return + end +end + + +-- TODO: make use of log_type and toggle between trade entries and full log? +yl_speak_up.get_fs_show_log = function(player, log_type) + local pname = player:get_player_name() + local n_id = yl_speak_up.speak_to[pname].n_id + local dialog = yl_speak_up.speak_to[pname].dialog + if(not(yl_speak_up.may_edit_npc(player, n_id))) then + return "size[5,1]label[0,0;Error: You do not own this NPC.]" + end + + local file, err = io.open(yl_speak_up.worldpath..yl_speak_up.log_path..DIR_DELIM.. + "log_"..tostring(n_id)..".txt", "r") + local formspec = {'size[18,12]'.. + 'button[0.5,11.1;17,0.8;back;Back]'.. + 'label[4.5,0.5;Log of ', + minetest.formspec_escape(tostring(dialog.n_npc).. + " [ID: "..tostring(n_id).."]"), + ']', + 'tablecolumns[' .. + 'color;text,align=left;'.. -- the date + 'color;text,align=center;'.. -- name of the player + 'color;text,align=right;'.. -- how many times the entry was repeated + 'color;text,align=left]'.. -- actual log file entry + 'table[0.1,1.0;17.8,9.8;show_log_of_npc;'.. + '#FFFFFF,Date,#FFFFFF,Player,#FFFFFF,,#FFFFFF,Log entry,', + } + -- without the time information, some entries (in particular when someone buys from a shpo) + -- may be the same. Those entries are combined into one when viewing the log. + local text = {} + local last_line = "" + local last_day = "" + local last_who = "" + local same_lines = 0 + local count = 0 + if(err) then + -- we don't want to get too much into detail here for players + return "size[5,1]label[0,0;Error reading NPC logfile.]" + else + for line in file:lines() do + local parts = string.split(line, " ") + -- suppress the time information as that would be too detailled; + -- it is still logged so that admins can check + local this_line = table.concat(parts, " ", 4) + if(this_line == last_line and parts[1] == last_day + and #parts > 3 and parts[3] == last_who) then + -- just count the line + same_lines = same_lines + 1 + else + table.insert(text, '#AAAAAA') + table.insert(text, minetest.formspec_escape(last_day)) + table.insert(text, '#FFFF00') + table.insert(text, minetest.formspec_escape(last_who)) + table.insert(text, '#AAAAAA') + if(same_lines > 1) then + table.insert(text, tostring(same_lines).."x") + else + table.insert(text, "") + end + table.insert(text, '#FFFFFF') + table.insert(text, minetest.formspec_escape(last_line)) + -- store information about the next line + same_lines = 0 + last_line = this_line + last_day = parts[1] + last_who = parts[3] + end + count = count + 1 + end + -- cover the last line + table.insert(text, '#AAAAAA') + table.insert(text, minetest.formspec_escape(last_day)) + table.insert(text, '#FFFF00') + table.insert(text, minetest.formspec_escape(last_who)) + table.insert(text, '#AAAAAA') + if(same_lines > 1) then + table.insert(text, tostring(same_lines).."x") + else + table.insert(text, "") + end + table.insert(text, '#FFFFFF') + table.insert(text, minetest.formspec_escape(last_line)) + file:close() + + table.insert(formspec, table.concat(text, ',')) + end + + -- selected row + table.insert(formspec, ";1]") + -- TODO: show lines read? (count) + return table.concat(formspec, '') +end + diff --git a/fs_trade_list.lua b/fs_trade_list.lua index 58ae5ae..b04f586 100644 --- a/fs_trade_list.lua +++ b/fs_trade_list.lua @@ -25,6 +25,12 @@ yl_speak_up.input_trade_list = function(player, formname, fields) return end + if(fields.show_log) then + -- show a log + yl_speak_up.show_fs(player, "show_log") + return + end + -- toggle between view of dialog option trades and trade list trades if(fields.show_dialog_option_trades or fields.show_trade_list) then @@ -211,11 +217,19 @@ yl_speak_up.get_fs_trade_list = function(player, show_dialog_option_trades) text, text, true, nil, nil, pname_for_old_fs) -- show a list of how much the NPC will buy and sell - text = "Limits: Do not buy or sell more than what I will tell you." + text = minetest.formspec_escape( + "[Limits] Do not buy or sell more than what I will tell you.") h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, "trade_limit", text, text, true, nil, nil, pname_for_old_fs) + -- button "show log" for those who can edit the NPC (entering edit mode is not required) + text = minetest.formspec_escape( + "[Log] Show me who gave you which orders and who bought what.") + h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, + "show_log", + text, text, + true, nil, nil, pname_for_old_fs) end local text = "That was all. Let's continue talking." diff --git a/show_fs.lua b/show_fs.lua index ba0f86b..ef2139e 100644 --- a/show_fs.lua +++ b/show_fs.lua @@ -56,6 +56,9 @@ minetest.register_on_player_receive_fields( function(player, formname, fields) elseif formname == "yl_speak_up:trade_limit" then yl_speak_up.input_trade_limit(player, formname, fields) return true + elseif formname == "yl_speak_up:show_log" then + yl_speak_up.input_show_log(player, formname, fields) + return true elseif formname == "yl_speak_up:edit_trade_limit" then yl_speak_up.input_edit_trade_limit(player, formname, fields) return true @@ -323,6 +326,13 @@ yl_speak_up.show_fs = function(player, fs_name, param) yl_speak_up.show_fs_ver(pname, "yl_speak_up:trade_limit", yl_speak_up.get_fs_trade_limit(player, param.selected)) + elseif(fs_name == "show_log") then + if(not(param)) then + param = {} + end + yl_speak_up.show_fs_ver(pname, "yl_speak_up:show_log", + yl_speak_up.get_fs_show_log(player, param.log_type)) + elseif(fs_name == "edit_trade_limit") then if(not(param)) then param = {}