1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00
RedMew/report.lua

151 lines
5.0 KiB
Lua
Raw Normal View History

2018-08-13 17:36:21 +02:00
local Module = {}
2018-08-13 17:04:02 +02:00
local Gui = require("utils.gui")
local Utils = require("utils.utils");
local report_frame_name = Gui.uid_name()
local report_close_button_name = Gui.uid_name()
local report_tab_button_name = Gui.uid_name()
local report_body_name = Gui.uid_name()
global.reports = {}
global.player_report_data = {}
local function draw_report(parent, report_id)
local report = global.reports[report_id]
local reported_player_name = game.players[report.reported_player_index].name
local reporting_player_name = game.players[report.reporting_player_index].name
local time = Utils.format_time(report.tick)
local time_ago = Utils.format_time(game.tick - report.tick)
local message = report.message
for _,child in pairs(parent.children) do
Gui.remove_data_recursivly(child)
child.destroy()
end
parent.add {type="label", caption="Offender: " .. reported_player_name}
local msg_label = parent.add {type="label", caption="Message: " .. message}
msg_label.style.single_line = false
msg_label.style.maximal_width = 680
parent.add {type="label", caption=string.format("Time: %s (%s ago)", time, time_ago)}
parent.add {type="label", caption="Reported by: " .. reporting_player_name}
end
local function show_reports(player)
2018-08-13 17:08:24 +02:00
local reports = global.reports or {}
2018-08-13 17:04:02 +02:00
2018-08-13 17:08:24 +02:00
local center = player.gui.center
if player.opened then --Destroy whatever is open
Gui.remove_data_recursivly(player.opened)
player.opened.destroy()
end
2018-08-13 17:04:02 +02:00
2018-08-13 17:08:24 +02:00
report_frame =
center.add {
type = 'frame',
name = report_frame_name,
direction = 'vertical',
caption = 'User reports'
}
report_frame.style.maximal_width = 700
player.opened = report_frame
2018-08-13 17:04:02 +02:00
2018-08-13 17:08:24 +02:00
local scroll_pane = report_frame.add{type = "scroll-pane", horizontal_scroll_policy = "auto-and-reserve-space", vertical_scroll_policy="never"}
local tab_flow = scroll_pane.add{type="flow"}
for k,report in pairs(reports) do
local button_cell = tab_flow.add{type="flow", caption="reportuid" .. k}
button_cell.add {
type="button",
name=report_tab_button_name,
caption = game.players[report.reporting_player_index].name
}
end
2018-08-13 17:04:02 +02:00
2018-08-13 17:08:24 +02:00
local report_body = report_frame.add {type = "scroll-pane", name = report_body_name, horizontal_scroll_policy = "never", vertical_scroll_policy="never"}
report_frame.add {type = 'button', name = report_close_button_name, caption = 'Close'}
2018-08-13 17:04:02 +02:00
2018-08-13 17:08:24 +02:00
draw_report(report_body, #reports)
2018-08-13 17:04:02 +02:00
end
local function report(reporting_player, reported_player, message)
2018-08-13 17:08:24 +02:00
table.insert(global.reports, {reporting_player_index = reporting_player.index, reported_player_index = reported_player.index, message = message, tick = game.tick})
2018-08-13 17:04:02 +02:00
2018-08-13 17:08:24 +02:00
local notified = false
2018-08-13 17:04:02 +02:00
for _,p in pairs(game.players) do
2018-08-13 17:08:24 +02:00
if p.admin and p.connected then
show_reports(p)
if p.afk_time < 3600 then notified = true end
2018-08-13 17:04:02 +02:00
end
2018-08-13 17:08:24 +02:00
end
if not notified then
for _,p in pairs(game.players) do
if p.admin then
show_reports(p)
end
end
end
2018-08-13 17:04:02 +02:00
end
Gui.on_custom_close(
report_frame_name,
function(event)
Gui.remove_data_recursivly(event.element)
event.element.destroy()
end
)
Gui.on_click(
report_close_button_name,
function(event)
Gui.remove_data_recursivly(event.element)
event.element.parent.destroy()
end
)
Gui.on_click(
report_tab_button_name,
function(event)
2018-08-13 17:08:24 +02:00
local center = event.player.gui.center
local report_frame = center[report_frame_name]
local report_uid_str = string.sub(event.element.parent.caption, 10)
local report_uid = tonumber(report_uid_str)
draw_report(report_frame[report_body_name], report_uid)
2018-08-13 17:04:02 +02:00
end
)
2018-08-13 17:36:21 +02:00
Module.spawn_report_popup = function(reporting_player, reported_player)
end
2018-08-13 17:04:02 +02:00
local function report_cmd(cmd)
if game.player then
local params = {}
for param in string.gmatch(cmd.parameter, '%S+') do
table.insert(params, param)
end
if #params < 2 then
game.player.print("Please enter then name of the offender and the reason for the report.")
return nil
end
if not game.players[params[1]] then
game.player.print(params[1] .. " does not exist.")
return nil
end
report(game.player, game.players[params[1]], string.sub(cmd.parameter, string.len(params[1]) + 2))
end
end
commands.add_command('report', '<griefer-name> <message> Reports a user to admins', report_cmd)
commands.add_command('showreports', 'Shows user reports (Admins only)',
function(event)
if game.player and game.player.admin then
show_reports(game.players[event.player_index])
end
end
)
2018-08-13 17:36:21 +02:00
return Module