From 307ba3684c377106b858eaee081a0f8c32005bb2 Mon Sep 17 00:00:00 2001 From: SimonFlapse Date: Sun, 17 Feb 2019 02:28:14 -0500 Subject: [PATCH 01/10] Prevent train grief --- features/nuke_control.lua | 80 ++++++++++++++++++++++++++++++++++++++- locale/en/redmew.cfg | 6 +++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 2cf34572..4a3888c4 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -2,7 +2,11 @@ local Event = require 'utils.event' local Rank = require 'features.rank_system' local Utils = require 'utils.core' local Game = require 'utils.game' +local Task = require 'utils.task' +local Token = require 'utils.token' local Server = require 'features.server' +local Report = require 'features.report' +local Popup = require 'features.gui.popup' local Ranks = require 'resources.ranks' local format = string.format @@ -168,8 +172,82 @@ local function on_player_joined(event) end end +local train_to_manual = + Token.register( + function(train) + if train and train.valid then + train.manual_mode = true + end + end +) + +local function on_entity_died(event) + -- Check that they destroyed train of force player + -- (we don't care about anything else that happened to be on the tracks) + local entity = event.entity + if not entity or not entity.valid or not entity.train or entity.force ~= game.forces.player then + return + end + -- Check that the player force did the killing + local force = event.force + if not force or not force.valid or force ~= game.forces.player then + return + end + -- Check that an entity did the killing + local cause = event.cause + if not cause or not cause.valid then + return + end + -- Check that the entity was a train and in manual + local train = cause.train + if not train or not train.manual_mode then + return + end + -- Check if the train has passengers + local passengers = train.passengers + local num_passengers = #passengers + if num_passengers == 0 then + train.manual_mode = false -- if the train is in manual and has no passengers, stop it + Task.set_timeout_in_ticks(30, train_to_manual, train) + return + end + + -- Go through the passengers and punish any guests involved + local player_punished + local player_unpunished + local name_list = {} + for i = 1, #passengers do + local player = passengers[i] + if not player.valid or allowed_to_nuke(player) then + player_unpunished = true + name_list[#name_list + 1] = player.name + else + -- If they aren't allowed to nuke, stop the train and act accordingly. + player_punished = true + name_list[#name_list + 1] = player.name + player.driving = false + train.manual_mode = false + Task.set_timeout_in_ticks(30, train_to_manual, train) + if players_warned[player.index] then -- jail for later offenses + Report.jail(player) + Utils.print_admins({'nuke_control.train_jailing', player.name}) + else -- warn for first offense + players_warned[player.index] = true + Utils.print_admins({'nuke_control.train_warning', player.name}) + Popup.player(player, {'nuke_control.train_player_warning'}) + end + end + end + + -- If there was a passenger who was unpunished along with a punished passenger, let the admins know + if player_punished and player_unpunished then + local name_string = table.concat(name_list, ' - ') + Utils.print_admins({'nuke_control.multiple_passengers', num_passengers, name_string}) + end +end + Event.add(defines.events.on_player_ammo_inventory_changed, ammo_changed) Event.add(defines.events.on_player_joined_game, on_player_joined) Event.add(defines.events.on_player_deconstructed_area, on_player_deconstructed_area) ---Event.add(defines.events.on_player_mined_entity, on_player_mined_item) Event.add(defines.events.on_player_used_capsule, on_capsule_used) +Event.add(defines.events.on_entity_died, on_entity_died) diff --git a/locale/en/redmew.cfg b/locale/en/redmew.cfg index c08194fb..bce7133b 100644 --- a/locale/en/redmew.cfg +++ b/locale/en/redmew.cfg @@ -38,3 +38,9 @@ toast_message=The end times are here. The four biters of the apocalypse have bee [toast] toast_all=__1__ sent a toast to all players. toast_player=__1__ sent a toast to __2__. + +[nuke_control] +train_warning=__1__ used a train to destroy an entity and has been warned. +train_player_warning=You have destroyed another train with yours.\nRepeated infractions will be punished. +train_jailing=__1__ used a train to destroy an entity and has been jailed. +multiple_passengers=Note: There were __1__ players in the train and any could have been controlling it: __2__ From ce4174b0bdec6a8b31d0cc64ea8733cdff6865d6 Mon Sep 17 00:00:00 2001 From: plague006 Date: Sun, 17 Feb 2019 12:09:24 -0500 Subject: [PATCH 02/10] Use commas as sep --- features/nuke_control.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 4a3888c4..3ca0fb7f 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -241,7 +241,7 @@ local function on_entity_died(event) -- If there was a passenger who was unpunished along with a punished passenger, let the admins know if player_punished and player_unpunished then - local name_string = table.concat(name_list, ' - ') + local name_string = table.concat(name_list, ', ') Utils.print_admins({'nuke_control.multiple_passengers', num_passengers, name_string}) end end From ead93440bab22b7e7439408148759d17f29cda58 Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Tue, 19 Feb 2019 18:18:19 -0500 Subject: [PATCH 03/10] Don't restrict train antigrief to force 'player', fix ref to player.name when player.valid is not true --- features/nuke_control.lua | 47 ++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 3ca0fb7f..02e562e6 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -175,22 +175,17 @@ end local train_to_manual = Token.register( function(train) - if train and train.valid then + if train.valid then train.manual_mode = true end end ) local function on_entity_died(event) - -- Check that they destroyed train of force player - -- (we don't care about anything else that happened to be on the tracks) + -- We only care is a train is killed by a member of its own force local entity = event.entity - if not entity or not entity.valid or not entity.train or entity.force ~= game.forces.player then - return - end - -- Check that the player force did the killing local force = event.force - if not force or not force.valid or force ~= game.forces.player then + if (not entity or not entity.valid) or (not force or not force.valid) or (force ~= entity.force) then return end -- Check that an entity did the killing @@ -218,23 +213,25 @@ local function on_entity_died(event) local name_list = {} for i = 1, #passengers do local player = passengers[i] - if not player.valid or allowed_to_nuke(player) then - player_unpunished = true - name_list[#name_list + 1] = player.name - else - -- If they aren't allowed to nuke, stop the train and act accordingly. - player_punished = true - name_list[#name_list + 1] = player.name - player.driving = false - train.manual_mode = false - Task.set_timeout_in_ticks(30, train_to_manual, train) - if players_warned[player.index] then -- jail for later offenses - Report.jail(player) - Utils.print_admins({'nuke_control.train_jailing', player.name}) - else -- warn for first offense - players_warned[player.index] = true - Utils.print_admins({'nuke_control.train_warning', player.name}) - Popup.player(player, {'nuke_control.train_player_warning'}) + if player.valid then + if allowed_to_nuke(player) then + player_unpunished = true + name_list[#name_list + 1] = player.name + else + -- If they aren't allowed to nuke, stop the train and act accordingly. + player_punished = true + name_list[#name_list + 1] = player.name + player.driving = false + train.manual_mode = false + Task.set_timeout_in_ticks(30, train_to_manual, train) + if players_warned[player.index] then -- jail for later offenses + Report.jail(player) + Utils.print_admins({'nuke_control.train_jailing', player.name}) + else -- warn for first offense + players_warned[player.index] = true + Utils.print_admins({'nuke_control.train_warning', player.name}) + Popup.player(player, {'nuke_control.train_player_warning'}) + end end end end From f49dccd1f492d1901e667520daf39873ac50a0f0 Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Tue, 19 Feb 2019 18:42:46 -0500 Subject: [PATCH 04/10] Remove unneccessary nil and validity check --- features/nuke_control.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 02e562e6..809bbc1a 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -184,8 +184,7 @@ local train_to_manual = local function on_entity_died(event) -- We only care is a train is killed by a member of its own force local entity = event.entity - local force = event.force - if (not entity or not entity.valid) or (not force or not force.valid) or (force ~= entity.force) then + if (not entity or not entity.valid) or (event.force ~= entity.force) then return end -- Check that an entity did the killing From d7b3bac49497b1340fa4a2dda75c8d19740f5ab0 Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Tue, 19 Feb 2019 18:47:22 -0500 Subject: [PATCH 05/10] Check if train --- features/nuke_control.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 809bbc1a..62679d21 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -182,9 +182,9 @@ local train_to_manual = ) local function on_entity_died(event) - -- We only care is a train is killed by a member of its own force + -- We only care if a train is killed by a member of its own force local entity = event.entity - if (not entity or not entity.valid) or (event.force ~= entity.force) then + if (not entity or not entity.valid) or not entity.train or (event.force ~= entity.force) then return end -- Check that an entity did the killing From 3d226588768b1ff5384d000c94dd91599648d16a Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Tue, 19 Feb 2019 19:26:04 -0500 Subject: [PATCH 06/10] Rename allowed_to_nuke to is_trusted --- features/nuke_control.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 62679d21..2963d3f3 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -12,13 +12,13 @@ local Ranks = require 'resources.ranks' local format = string.format local match = string.match -local function allowed_to_nuke(player) +local function is_trusted(player) return Rank.equal_or_greater_than(player.name, Ranks.auto_trusted) end local function ammo_changed(event) local player = Game.get_player_by_index(event.player_index) - if allowed_to_nuke(player) then + if is_trusted(player) then return end local nukes = player.remove_item({name = 'atomic-bomb', count = 1000}) @@ -39,7 +39,7 @@ end local function on_player_deconstructed_area(event) local player = Game.get_player_by_index(event.player_index) - if allowed_to_nuke(player) then + if is_trusted(player) then return end player.remove_item({name = 'deconstruction-planner', count = 1000}) @@ -141,7 +141,7 @@ local function on_capsule_used(event) return end - if not allowed_to_nuke(player) then + if not is_trusted(player) then local area = {{event.position.x - 5, event.position.y - 5}, {event.position.x + 5, event.position.y + 5}} local count = 0 local entities = player.surface.find_entities_filtered {force = player.force, area = area} @@ -213,7 +213,7 @@ local function on_entity_died(event) for i = 1, #passengers do local player = passengers[i] if player.valid then - if allowed_to_nuke(player) then + if is_trusted(player) then player_unpunished = true name_list[#name_list + 1] = player.name else From 3fe50a437fed3c83f41fb8a82a279c97146d5d5f Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Tue, 19 Feb 2019 19:38:11 -0500 Subject: [PATCH 07/10] Switch to Global --- features/nuke_control.lua | 70 ++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 2963d3f3..3e1b07ba 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -4,6 +4,7 @@ local Utils = require 'utils.core' local Game = require 'utils.game' local Task = require 'utils.task' local Token = require 'utils.token' +local Global = require 'utils.global' local Server = require 'features.server' local Report = require 'features.report' local Popup = require 'features.gui.popup' @@ -12,6 +13,44 @@ local Ranks = require 'resources.ranks' local format = string.format local match = string.match +local players_warned = {} +local entities_allowed_to_bomb = { + ['stone-wall'] = true, + ['transport-belt'] = true, + ['fast-transport-belt'] = true, + ['express-transport-belt'] = true, + ['construction-robot'] = true, + ['player'] = true, + ['gun-turret'] = true, + ['laser-turret'] = true, + ['flamethrower-turret'] = true, + ['rail'] = true, + ['rail-chain-signal'] = true, + ['rail-signal'] = true, + ['tile-ghost'] = true, + ['entity-ghost'] = true, + ['gate'] = true, + ['electric-pole'] = true, + ['small-electric-pole'] = true, + ['medium-electric-pole'] = true, + ['big-electric-pole'] = true, + ['logistic-robot'] = true, + ['defender'] = true, + ['destroyer'] = true, + ['distractor'] = true +} + +Global.register( + { + players_warned = players_warned, + entities_allowed_to_bomb = entities_allowed_to_bomb + }, + function(tbl) + players_warned = tbl.players_warned + entities_allowed_to_bomb = tbl.entities_allowed_to_bomb + end +) + local function is_trusted(player) return Rank.equal_or_greater_than(player.name, Ranks.auto_trusted) end @@ -80,37 +119,6 @@ local function item_not_sanctioned(item) return (name:find('capsule') or name == 'cliff-explosives' or name == 'raw-fish' or name == 'discharge-defense-remote') end -global.nuke_control = { - entities_allowed_to_bomb = { - ['stone-wall'] = true, - ['transport-belt'] = true, - ['fast-transport-belt'] = true, - ['express-transport-belt'] = true, - ['construction-robot'] = true, - ['player'] = true, - ['gun-turret'] = true, - ['laser-turret'] = true, - ['flamethrower-turret'] = true, - ['rail'] = true, - ['rail-chain-signal'] = true, - ['rail-signal'] = true, - ['tile-ghost'] = true, - ['entity-ghost'] = true, - ['gate'] = true, - ['electric-pole'] = true, - ['small-electric-pole'] = true, - ['medium-electric-pole'] = true, - ['big-electric-pole'] = true, - ['logistic-robot'] = true, - ['defender'] = true, - ['destroyer'] = true, - ['distractor'] = true - }, - players_warned = {} -} -local entities_allowed_to_bomb = global.nuke_control.entities_allowed_to_bomb -local players_warned = global.nuke_control.players_warned - local function entity_allowed_to_bomb(entity) return entities_allowed_to_bomb[entity.name] end From 5c362be41005210fed30ebb3401780a594cbaac4 Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Wed, 20 Feb 2019 12:27:03 -0500 Subject: [PATCH 08/10] print_admins to use locale --- locale/en/redmew.cfg | 3 +++ utils/core.lua | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/locale/en/redmew.cfg b/locale/en/redmew.cfg index bce7133b..038c2f96 100644 --- a/locale/en/redmew.cfg +++ b/locale/en/redmew.cfg @@ -44,3 +44,6 @@ train_warning=__1__ used a train to destroy an entity and has been warned. train_player_warning=You have destroyed another train with yours.\nRepeated infractions will be punished. train_jailing=__1__ used a train to destroy an entity and has been jailed. multiple_passengers=Note: There were __1__ players in the train and any could have been controlling it: __2__ + +[utils_core] +print_admins='__1__(ADMIN) __2__: __3__ diff --git a/utils/core.lua b/utils/core.lua index c43937fb..d8bdc6b3 100644 --- a/utils/core.lua +++ b/utils/core.lua @@ -65,8 +65,8 @@ function Module.print_admins(msg, source) source_name = 'Server' chat_color = Color.yellow end - local formatted_msg = format('%s(ADMIN) %s: %s', prefix, source_name, msg) -- to the server - print(formatted_msg) + local formatted_msg = {'utils_core.print_admins',prefix, source_name, msg} + log(formatted_msg) for _, p in pairs(game.connected_players) do if p.admin then p.print(formatted_msg, chat_color) From 8907e828f5994aa4cb19cf57b94fff712085a183 Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Wed, 20 Feb 2019 12:40:25 -0500 Subject: [PATCH 09/10] Remove quote in locale --- locale/en/redmew.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locale/en/redmew.cfg b/locale/en/redmew.cfg index 038c2f96..4559b089 100644 --- a/locale/en/redmew.cfg +++ b/locale/en/redmew.cfg @@ -46,4 +46,4 @@ train_jailing=__1__ used a train to destroy an entity and has been jailed. multiple_passengers=Note: There were __1__ players in the train and any could have been controlling it: __2__ [utils_core] -print_admins='__1__(ADMIN) __2__: __3__ +print_admins=__1__(ADMIN) __2__: __3__ From ffed15c30eb4c9dbfc758aa969212c8921fbe557 Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Wed, 20 Feb 2019 13:07:47 -0500 Subject: [PATCH 10/10] Do not punish if there are multiple passengers --- features/nuke_control.lua | 6 +++--- locale/en/redmew.cfg | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/features/nuke_control.lua b/features/nuke_control.lua index 3e1b07ba..059dcc27 100644 --- a/features/nuke_control.lua +++ b/features/nuke_control.lua @@ -218,7 +218,7 @@ local function on_entity_died(event) local player_punished local player_unpunished local name_list = {} - for i = 1, #passengers do + for i = 1, num_passengers do local player = passengers[i] if player.valid then if is_trusted(player) then @@ -231,10 +231,10 @@ local function on_entity_died(event) player.driving = false train.manual_mode = false Task.set_timeout_in_ticks(30, train_to_manual, train) - if players_warned[player.index] then -- jail for later offenses + if players_warned[player.index] and num_passengers == 1 then -- jail for later offenses if they're solely guilty Report.jail(player) Utils.print_admins({'nuke_control.train_jailing', player.name}) - else -- warn for first offense + else -- warn for first offense or if there's someone else in the train players_warned[player.index] = true Utils.print_admins({'nuke_control.train_warning', player.name}) Popup.player(player, {'nuke_control.train_player_warning'}) diff --git a/locale/en/redmew.cfg b/locale/en/redmew.cfg index 4559b089..95dd9091 100644 --- a/locale/en/redmew.cfg +++ b/locale/en/redmew.cfg @@ -40,9 +40,9 @@ toast_all=__1__ sent a toast to all players. toast_player=__1__ sent a toast to __2__. [nuke_control] -train_warning=__1__ used a train to destroy an entity and has been warned. +train_warning=__1__ used a train to destroy another train and has been warned. train_player_warning=You have destroyed another train with yours.\nRepeated infractions will be punished. -train_jailing=__1__ used a train to destroy an entity and has been jailed. +train_jailing=__1__ used a train to destroy another train and has been jailed. multiple_passengers=Note: There were __1__ players in the train and any could have been controlling it: __2__ [utils_core]