Moved reports to control

This commit is contained in:
Cooldude2606
2019-06-21 22:00:15 +01:00
parent 4626f0d58f
commit fec4f0c184
83 changed files with 33670 additions and 8768 deletions

View File

@@ -76,7 +76,7 @@ end
--- Reports added and removed
if config.player_reports then
local Reports = require 'modules.addons.reports-control'
local Reports = require 'modules.control.reports'
Event.add(Reports.events.on_player_reported,function(event)
local player_name,by_player_name = get_player_name(event)
emit_event{
@@ -88,14 +88,14 @@ if config.player_reports then
['Reason:']=event.reason
}
end)
Event.add(Reports.events.on_player_report_removed,function(event)
local player_name,by_player_name = get_player_name(event)
Event.add(Reports.events.on_report_removed,function(event)
local player_name = get_player_name(event)
emit_event{
title='Report Removed',
description='A player has a report removed',
color=Colors.green,
['Player:']='<inline>'..player_name,
['By:']='<inline>'..by_player_name
['By:']='<inline>'..event.removed_by_name
}
end)
end

View File

@@ -1,141 +0,0 @@
local Game = require 'utils.game'
local Global = require 'utils.global'
local Reports = {
user_reports={},
events = {
on_player_reported = script.generate_event_name(),
on_player_report_removed = script.generate_event_name()
}
}
Global.register(Reports.user_reports,function(tbl)
Reports.user_reports = tbl
end)
local function event_emit(event,player,by_player_name)
local reports = Reports.user_reports[player.name]
local reason = reports and reports[by_player_name]
script.raise_event(event,{
name=event,
tick=game.tick,
player_index=player.index,
by_player_name=by_player_name,
reason=reason
})
end
--- Adds a report to a player, reports are stored in global table and can be accessed later
-- @tparam LuaPlayer player the player that will be reported
-- @tparam[opt='Non string Given.'] reason the reason that the player is being reported
-- @tparam[opt='<server>'] string by_player_name the name of the player doing the action
-- @treturn boolean true if the report was added, nil if there is an error
function Reports.report_player(player,reason,by_player_name)
player = Game.get_player_from_any(player)
if not player then return end
reason = reason or 'Non Given.'
by_player_name = by_player_name or '<server>'
local reports = Reports.user_reports[player.name]
if not reports then
Reports.user_reports[player.name] = {
[by_player_name] = reason
}
elseif not reports[by_player_name] then
reports[by_player_name] = reason
else return false end
event_emit(Reports.events.on_player_reported,player,by_player_name)
return true
end
--- Removes a report from a player by the given player, see clear_player_reports to remove all
-- @tparam LuaPlayer player the player that will have the report removed
-- @tparam[opt='<server>'] string by_player_name the name of the player doing the action
-- @treturn boolean true if the report was removed, nil if there was an error
function Reports.remove_player_report(player,by_player_name)
player = Game.get_player_from_any(player)
if not player then return end
by_player_name = by_player_name or '<server>'
local reports = Reports.user_reports[player.name]
if reports and reports[by_player_name] then
event_emit(Reports.events.on_player_report_removed,player,by_player_name)
reports[by_player_name] = nil
if Reports.count_player_reports(player) == 0 then
Reports.user_reports[player.name] = nil
end
return true
end
return false
end
--- Clears all reports from a player, will emit an event for each individual report as if remove_player_report was used
-- @tparam LuaPlayer player the player to clear the reports of
-- @treturn boolean true if the reports were cleared, nil if error
function Reports.clear_player_reports(player)
player = Game.get_player_from_any(player)
if not player then return end
local reports = Reports.user_reports[player.name]
if reports then
for by_player_name,reason in pairs(reports) do
event_emit(Reports.events.on_player_report_removed,player,by_player_name)
end
Reports.user_reports[player.name] = nil
return true
end
return false
end
--- Test for if a player has been reported by another player, can also return the reason from that player
-- @tparam LuaPlayer player the player to check the reports of
-- @tparam string by_player_name the player that made if the report if present (note server is not default here)
-- @tparam[opt=false] boolean rtn_reason true will return the reason for the report rather than a boolean
-- @treturn boolean true if a report from the player is present unless rtn_reason is true when a string is returned (or false)
function Reports.player_is_reported_by(player,by_player_name,rtn_reason)
player = Game.get_player_from_any(player)
if not player then return end
local reports = Reports.user_reports[player.name]
if reports and reports[by_player_name] then
return rtn_reason and reports[by_player_name] or true
end
return false
end
--- Gets all the reports that are on a player
-- @tparam LuaPlayer player the player to get the reports of
-- @treturn table a table of all the reports for this player, empty table if no reports
function Reports.get_player_reports(player)
player = Game.get_player_from_any(player)
if not player then return end
return Reports.user_reports[player.name] or {}
end
--- Counts all reports on a player returning a number, a custom count function can be given which should return a number
-- @tparam LuaPlayer player the player to count the reports of
-- @tparam[opt] number function count_callback should return a or true (for 1) this will be passed every report on the player
-- count_callback param - player_name string - the name of the player who made the report
-- count_callback param - reason string - the reason the reason was made
-- count_callback return - number or boolean - if number then this will be added to the count, if boolean then false = 0 and true = 1
-- @treturn number the number of reports on the player
function Reports.count_player_reports(player,count_callback)
player = Game.get_player_from_any(player)
if not player then return end
local reports = Reports.user_reports[player.name] or {}
if not count_callback then
local ctn = 0
for _ in pairs(reports) do
ctn=ctn+1
end
return ctn
else
local ctn = 0
for player_name,reason in pairs(reports) do
local success,err = pcall(count_callback,player_name,reason)
if success and err then
if err == true then err = 1 end
ctn = ctn+err
end
end
return ctn
end
end
return Reports

View File

@@ -1,6 +1,6 @@
local Roles = require 'expcore.roles'
local Commands = require 'expcore.commands'
local ReportsControl = require 'modules.addons.reports-control'
local Reports = require 'modules.control.reports'
local format_chat_player_name = ext_require('expcore.common','format_chat_player_name')
require 'config.expcore-commands.parse_general'
@@ -20,7 +20,7 @@ end)
:register(function(player,action_player,reason,raw)
local action_player_name_color = format_chat_player_name(action_player)
local by_player_name_color = format_chat_player_name(player)
if ReportsControl.report_player(action_player,reason,player.name) then
if Reports.report_player(action_player,player.name,reason) then
game.print{'expcom-report.non-admin',action_player_name_color,reason}
Roles.print_to_roles_higher('Trainee',{'expcom-report.admin',action_player_name_color,by_player_name_color,reason})
else
@@ -33,7 +33,7 @@ Commands.new_command('get-reports','Gets a list of all reports that a player has
:add_alias('reports','list-reports')
:register(function(player,action_player,raw)
if action_player then
local reports = ReportsControl.get_player_reports(action_player)
local reports = Reports.get_reports(action_player)
local action_player_name_color = format_chat_player_name(action_player)
Commands.print{'expcom-report.player-report-title',action_player_name_color}
for player_name,reason in pairs(reports) do
@@ -41,11 +41,11 @@ Commands.new_command('get-reports','Gets a list of all reports that a player has
Commands.print{'expcom-report.list',by_player_name_color,reason}
end
else
local user_reports = ReportsControl.user_reports
local user_reports = Reports.user_reports
Commands.print{'expcom-report.player-count-title'}
for player_name,reports in pairs(user_reports) do
local player_name_color = format_chat_player_name(player_name)
local report_count = ReportsControl.count_player_reports(player_name)
local report_count = Reports.count_reports(player_name)
Commands.print{'expcom-report.list',player_name_color,report_count}
end
end
@@ -56,11 +56,11 @@ Commands.new_command('clear-reports','Clears all reports from a player or just t
:add_param('from-player',true,'player')
:register(function(player,action_player,from_player,raw)
if from_player then
if not ReportsControl.remove_player_report(action_player,from_player.name) then
if not Reports.remove_report(action_player,from_player.name,player.name) then
return Commands.error{'expcom-report.not-reported'}
end
else
if not ReportsControl.clear_player_reports(action_player) then
if not Reports.remove_all(action_player,player.name) then
return Commands.error{'expcom-report.not-reported'}
end
end

212
modules/control/reports.lua Normal file
View File

@@ -0,0 +1,212 @@
--[[-- Control Module - Reports
Adds a way to report players and store report messages.
@module Reports
@alias Reports
@usage
-- This will place a report on "MrBiter" (must be a valid player) the report will have been made
-- by "Cooldude2606" (must be the player name) with the reason 'Liking biters too much' this can be
-- seen by using Reports.get_report.
Reports.report_player('MrBiter','Cooldude2606','Liking biters too much') -- true
-- The other get methods can be used to get all the reports on a player or to test if a player is reported.
Reports.get_report('MrBiter','Cooldude2606') -- 'Liking biters too much'
-- This will remove the warning on 'MrBiter' (must be a valid player) which was made by 'Cooldude2606'.
Reports.remove_report('MrBiter','Cooldude2606') -- true
-- This will remove all the report that have been made against 'MrBiter'. Note that the remove event will
-- be triggered once per report issused.
Reports.remove_all('MrBiter') -- true
]]
local Game = require 'utils.game'
local Global = require 'utils.global'
local valid_player = Game.get_player_from_any
local Reports = {
user_reports={}, -- stores all user reports, global table
events = {
--- When a player is reported
-- @event on_player_reported
-- @tparam number player_index the player index of the player who got reported
-- @tparam string by_player_name the name of the player who made the report
-- @tparam string reason the reason given for the report
on_player_reported = script.generate_event_name(),
--- When a report is removed from a player
-- @event on_report_removed
-- @tparam number player_index the player index of the player who has the report removed
-- @tparam string reported_by_name the name of the player who made the removed report
-- @tparam string removed_by_name the name of the player who removed the report
on_report_removed = script.generate_event_name()
}
}
local user_reports = Reports.user_reports
Global.register(user_reports,function(tbl)
user_reports = tbl
end)
--- Get functions.
-- Functions used to get information from reports
-- @section get-functions
--- Gets a list of all reports that a player has against them
-- @tparam LuaPlayer player the player to get the report for
-- @treturn table a list of all reports, key is by player name, value is reason
function Reports.get_reports(player)
player = valid_player(player)
if not player then return end
return user_reports[player.name] or {}
end
--- Gets a single report against a player given the name of the player who made the report
-- @tparam LuaPlayer player the player to get the report for
-- @tparam string by_player_name the name of the player who made the report
-- @treturn ?string|nil string is the reason that the player was reported, if the player is not reported
function Reports.get_report(player,by_player_name)
player = valid_player(player)
if not player then return end
if not by_player_name then return end
local reports = user_reports[player.name]
return reports and reports[by_player_name]
end
--- Checks if a player is reported, option to get if reported by a certain player
-- @tparam LuaPlayer player the player to check if reported
-- @tparam[opt] string by_player_name when given will check if reported by this player
-- @treturn boolean if the player has been reported
function Reports.is_reported(player,by_player_name)
player = valid_player(player)
if not player then return end
local reports = user_reports[player.name] or {}
if by_player_name then
return reports[by_player_name] ~= nil
else
return table_size(reports) > 0
end
end
--- Counts the number of reports that a player has aganist them
-- @tparam LuaPlayer player the player to count the reports for
-- @tparam[opt] function custom_count when given this function will be used to count the reports
-- @treturn number the number of reports that the user has
function Reports.count_reports(player,custom_count)
player = valid_player(player)
if not player then return end
local reports = user_reports[player.name] or {}
if custom_count then
local ctn = 0
for by_player_name,reason in pairs(reports) do
ctn = ctn + custom_count(player,by_player_name,reason)
end
return ctn
else
return table_size(reports)
end
end
--- Set functions.
-- Functions used to get information from reports
-- @section set-functions
--- Adds a report to a player, each player can only report another player once
-- @tparam LuaPlayer player the player to add the report to
-- @tparam string by_player_name the name of the player that is making the report
-- @tparam[opt='Non Given.'] string reason the reason that the player is being reported
-- @treturn boolean whether the report was added successfully
function Reports.report_player(player,by_player_name,reason)
player = valid_player(player)
if not player then return end
local player_name = player.name
reason = reason or 'Non Given.'
local reports = user_reports[player_name]
if not reports then
reports = {}
user_reports[player_name] = reports
end
if reports[by_player_name] then
return false
else
reports[by_player_name] = reason
end
script.raise_event(Reports.events.on_player_reported,{
name = Reports.events.on_player_reported,
tick = game.tick,
player_index = player.index,
by_player_name = by_player_name,
reason = reason
})
return true
end
--- Used to emit the report removed event, own function due to repeated use in Report.remove_all
-- @tparam LuaPlayer player the player who is having the report removed from them
-- @tparam string reported_by_name the player who had the report
-- @tparam string removed_by_name the player who is clearing the report
local function report_removed_event(player,reported_by_name,removed_by_name)
script.raise_event(Reports.events.on_report_removed,{
name = Reports.events.on_report_removed,
tick = game.tick,
player_index = player.index,
reported_by_name = reported_by_name,
removed_by_name = removed_by_name
})
end
--- Removes a report from a player
-- @tparam LuaPlayer player the player to remove the report from
-- @tparam string reported_by_name the name of the player that made the report
-- @treturn boolean whether the report was removed successfully
function Reports.remove_report(player,reported_by_name,removed_by_name)
player = valid_player(player)
if not player then return end
local reports = user_reports[player.name]
if not reports then
return false
end
local reason = reports[reported_by_name]
if not reason then
return false
end
report_removed_event(player,reported_by_name,removed_by_name)
reports[reported_by_name] = nil
return true
end
--- Removes all reports from a player
-- @tparam LuaPlayer player the player to remove the reports from
-- @treturn boolean whether the reports were removed successfully
function Reports.remove_all(player,removed_by_name)
player = valid_player(player)
if not player then return end
local reports = user_reports[player.name]
if not reports then
return false
end
for reported_by_name,_ in pairs(reports) do
report_removed_event(player,reported_by_name,removed_by_name)
end
user_reports[player.name] = nil
return true
end
return Reports