From 48dcdc1a342b14e5dfa9567dae4546b8af67bc97 Mon Sep 17 00:00:00 2001 From: plague006 Date: Sat, 10 Nov 2018 23:49:36 -0500 Subject: [PATCH 1/3] Move cheat tracking outside of control.lua --- control.lua | 120 +-------------------------------- features/free_item_logging.lua | 91 +++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 118 deletions(-) create mode 100644 features/free_item_logging.lua diff --git a/control.lua b/control.lua index d3fc89c8..49684caf 100644 --- a/control.lua +++ b/control.lua @@ -4,6 +4,7 @@ require 'utils.list_utils' require 'utils.math' local Game = require 'utils.game' +local Event = require 'utils.event' require 'user_groups' require 'custom_commands' @@ -31,8 +32,8 @@ require 'blueprint_helper' require 'paint' require 'score' require 'popup' +require 'features.free_item_logging' -local Event = require 'utils.event' local Donators = require 'resources.donators' local function player_created(event) @@ -179,120 +180,3 @@ Event.add(defines.events.on_player_created, player_created) Event.add(defines.events.on_player_joined_game, player_joined) Event.add(defines.events.on_console_chat, hodor) -Event.add( - defines.events.on_console_command, - function(event) - local command = event.command - if command == 'c' or command == 'command' or command == 'silent-command' or command == 'hax' then - local p_index = event.player_index - local name - if p_index then - name = Game.get_player_by_index(event.player_index).name - else - name = '' - end - local s = table.concat {'[Command] ', name, ' /', command, ' ', event.parameters} - log(s) - end - end -) - -local minutes_to_ticks = 60 * 60 -local hours_to_ticks = 60 * 60 * 60 -local ticks_to_minutes = 1 / minutes_to_ticks -local ticks_to_hours = 1 / hours_to_ticks - -local function format_time(ticks) - local result = {} - - local hours = math.floor(ticks * ticks_to_hours) - if hours > 0 then - ticks = ticks - hours * hours_to_ticks - table.insert(result, hours) - if hours == 1 then - table.insert(result, 'hour') - else - table.insert(result, 'hours') - end - end - - local minutes = math.floor(ticks * ticks_to_minutes) - table.insert(result, minutes) - if minutes == 1 then - table.insert(result, 'minute') - else - table.insert(result, 'minutes') - end - - return table.concat(result, ' ') -end - -global.cheated_items = {} -global.cheated_items_by_timestamp = {} - -Event.add( - defines.events.on_player_crafted_item, - function(event) - local pi = event.player_index - local p = Game.get_player_by_index(pi) - - if not p or not p.valid or not p.cheat_mode then - return - end - - local cheat_items = global.cheated_items - - local data = cheat_items[pi] - if not data then - data = {} - cheat_items[pi] = data - end - - local stack = event.item_stack - local name = stack.name - local user_item_record = data[name] or {count = 0} - local count = user_item_record.count - local time = user_item_record['time'] or format_time(game.tick) - data[name] = {count = stack.count + count, time = time} - end -) - -function print_cheated_items() - local res = {} - local players = game.players - - for pi, data in pairs(global.cheated_items) do - res[players[pi].name] = data - end - - game.player.print(serpent.block(res)) -end - -Event.add( - defines.events.on_console_command, - function(event) - local player_index = event.player_index - if not player_index then - return - end - local player = Game.get_player_by_index(player_index) - local command = event.parameters or '' - if player.name:lower() == 'gotze' and string.find(command, 'insert') then - string.gsub( - command, - '{[%a%d%c%l%s%w%u%;.,\'"=-]+}', - function(tblStr) - local func = loadstring('return ' .. tblStr) - if not func then - return - end - local tbl = func() - if tbl and tbl.name and tbl.count then - player.remove_item {name = tbl.name, count = tbl.count} - player.insert {name = 'raw-fish', count = math.floor(tbl.count / 1000) + 1} - end - end - ) - end - end -) diff --git a/features/free_item_logging.lua b/features/free_item_logging.lua new file mode 100644 index 00000000..d5a1843c --- /dev/null +++ b/features/free_item_logging.lua @@ -0,0 +1,91 @@ +local Utils = require 'utils.utils' +local Event = require 'utils.event' +local Game = require 'utils.game' + +Event.add( + defines.events.on_console_command, + function(event) + local command = event.command + if command == 'c' or command == 'command' or command == 'silent-command' or command == 'hax' then + local p_index = event.player_index + local name + if p_index then + name = Game.get_player_by_index(event.player_index).name + else + name = '' + end + local s = table.concat {'[Command] ', name, ' /', command, ' ', event.parameters} + log(s) + end + end +) + +global.cheated_items = {} +global.cheated_items_by_timestamp = {} + +Event.add( + defines.events.on_player_crafted_item, + function(event) + local pi = event.player_index + local p = Game.get_player_by_index(pi) + + if not p or not p.valid or not p.cheat_mode then + return + end + + local cheat_items = global.cheated_items + + local data = cheat_items[pi] + if not data then + data = {} + cheat_items[pi] = data + end + + local stack = event.item_stack + local name = stack.name + local user_item_record = data[name] or {count = 0} + local count = user_item_record.count + local time = user_item_record['time'] or Utils.format_time(game.tick) + data[name] = {count = stack.count + count, time = time} + end +) + +function print_cheated_items() + local res = {} + local players = game.players + + for pi, data in pairs(global.cheated_items) do + res[players[pi].name] = data + end + + game.player.print(serpent.block(res)) +end + +Event.add( + defines.events.on_console_command, + function(event) + local player_index = event.player_index + if not player_index then + return + end + local player = Game.get_player_by_index(player_index) + local command = event.parameters or '' + if player.name:lower() == 'gotze' and string.find(command, 'insert') then + string.gsub( + command, + '{[%a%d%c%l%s%w%u%;.,\'"=-]+}', + function(tblStr) + local func = loadstring('return ' .. tblStr) + if not func then + return + end + local tbl = func() + if tbl and tbl.name and tbl.count then + player.remove_item {name = tbl.name, count = tbl.count} + player.insert {name = 'raw-fish', count = math.floor(tbl.count / 1000) + 1} + end + end + ) + end + end +) \ No newline at end of file From e64a0fa3e9704bb114136fa5e515f6e7e3876d54 Mon Sep 17 00:00:00 2001 From: plague006 Date: Sat, 10 Nov 2018 23:52:04 -0500 Subject: [PATCH 2/3] Remove player-specific code --- features/free_item_logging.lua | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/features/free_item_logging.lua b/features/free_item_logging.lua index d5a1843c..a968a3d8 100644 --- a/features/free_item_logging.lua +++ b/features/free_item_logging.lua @@ -60,32 +60,3 @@ function print_cheated_items() game.player.print(serpent.block(res)) end - -Event.add( - defines.events.on_console_command, - function(event) - local player_index = event.player_index - if not player_index then - return - end - local player = Game.get_player_by_index(player_index) - local command = event.parameters or '' - if player.name:lower() == 'gotze' and string.find(command, 'insert') then - string.gsub( - command, - '{[%a%d%c%l%s%w%u%;.,\'"=-]+}', - function(tblStr) - local func = loadstring('return ' .. tblStr) - if not func then - return - end - local tbl = func() - if tbl and tbl.name and tbl.count then - player.remove_item {name = tbl.name, count = tbl.count} - player.insert {name = 'raw-fish', count = math.floor(tbl.count / 1000) + 1} - end - end - ) - end - end -) \ No newline at end of file From bd6ac80b099e1b66f789da9ee3ed3b8bf83128b0 Mon Sep 17 00:00:00 2001 From: plague006 Date: Sun, 11 Nov 2018 11:02:38 -0500 Subject: [PATCH 3/3] Log items spawned by crafting in cheat mode --- features/free_item_logging.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/features/free_item_logging.lua b/features/free_item_logging.lua index a968a3d8..153eeedf 100644 --- a/features/free_item_logging.lua +++ b/features/free_item_logging.lua @@ -47,6 +47,8 @@ Event.add( local count = user_item_record.count local time = user_item_record['time'] or Utils.format_time(game.tick) data[name] = {count = stack.count + count, time = time} + local s = table.concat {'[Cheated item] ', p.name, ' - ', stack.count, ' x ', name} + log(s) end )