From 5123d51145b1d998386d0d30ef4f1a3e1ef3db0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Neves?= Date: Fri, 18 Nov 2022 01:54:03 +0000 Subject: [PATCH 1/4] Admins can disable clear-vacant-players. Admins get a toggle in the Config to toggle the module clear-vacant-players off and on, if it is loaded into the map. --- maps/mountain_fortress_v3/main.lua | 5 ++-- modules/clear_vacant_players.lua | 46 +++++++++++++++++++++++------- utils/gui/config.lua | 29 +++++++++++++++++++ 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/maps/mountain_fortress_v3/main.lua b/maps/mountain_fortress_v3/main.lua index 5f244961..272bd9ad 100644 --- a/maps/mountain_fortress_v3/main.lua +++ b/maps/mountain_fortress_v3/main.lua @@ -142,9 +142,8 @@ function Public.reset_map() game.reset_time_played() Public.reset_main_table() - OfflinePlayers.set_active_surface_index(this.active_surface_index) - OfflinePlayers.set_offline_players_enabled(true) - OfflinePlayers.clear_offline_players() + OfflinePlayers.init(this.active_surface_index) + OfflinePlayers.set_enabled(true) -- OfflinePlayers.set_offline_players_surface_removal(true) RPG.rpg_reset_all_players() diff --git a/modules/clear_vacant_players.lua b/modules/clear_vacant_players.lua index 4c331c39..87782f6a 100644 --- a/modules/clear_vacant_players.lua +++ b/modules/clear_vacant_players.lua @@ -1,10 +1,12 @@ +local tick_frequency = 200 + local Global = require 'utils.global' local Alert = require 'utils.alert' local Event = require 'utils.event' local this = { settings = { - offline_players_enabled = false, + is_enabled = false, offline_players_surface_removal = false, active_surface_index = nil, -- needs to be set else this will fail required_online_time = 18000, -- nearest prime to 5 minutes in ticks @@ -23,11 +25,12 @@ Global.register( local Public = {events = {remove_surface = Event.generate_event_name('remove_surface')}} local remove = table.remove -function Public.remove_offline_players() - if not this.settings.offline_players_enabled then +function Public.dump_expired_players() + if not this.settings.is_enabled then return end local tick = game.tick + -- Skip initial tick - not everything may be ready. if tick < 50 then return end @@ -129,10 +132,30 @@ function Public.remove_offline_players() end end ---- Activates the offline player module +--- Initializes the module with blank state, receiving all required parameters. +---
The module starts **disabled** by default. +--- @param active_surface_index number The index of the active surface. +---@param is_enabled boolean|nil Optional: when passed, sets the module to be enabled or disabled. +function Public.init(active_surface_index, is_enabled) + if not active_surface_index then + return error('An active surface index must be set', 2) + end + this.settings.active_surface_index = active_surface_index + if is_enabled ~= nil then + this.settings.is_enabled = is_enabled + end + Public.reset() +end + +--- Returns whether the module is enabled or disabled. +function Public.is_enabled() + return this.settings.is_enabled +end + +--- Enables or disables the vacant-player module. ---@param value boolean -function Public.set_offline_players_enabled(value) - this.settings.offline_players_enabled = value or false +function Public.set_enabled(value) + this.settings.is_enabled = value or false end --- Activates the surface removal for the module IC @@ -142,24 +165,27 @@ function Public.set_offline_players_surface_removal(value) end --- Sets the active surface for this module, needs to be set else it will fail ----@param value string +---@param value number|string Active surface index. Name of surface will also work. function Public.set_active_surface_index(value) this.settings.active_surface_index = value or nil end +function Public.reset() + Public.clear_offline_players(); +end --- Clears the offline table function Public.clear_offline_players() this.offline_players = {} end -local remove_offline_players = Public.remove_offline_players +local function should_ignore_surface(surface) -Event.on_nth_tick(200, remove_offline_players) +Event.on_nth_tick(tick_frequency, Public.dump_expired_players) Event.add( defines.events.on_pre_player_left_game, function(event) - if not this.settings.offline_players_enabled then + if not this.settings.is_enabled then return end diff --git a/utils/gui/config.lua b/utils/gui/config.lua index 130e3a7f..8403a79c 100644 --- a/utils/gui/config.lua +++ b/utils/gui/config.lua @@ -197,6 +197,24 @@ local functions = { get_actor(event, '[Blueprints]', 'has disabled blueprints!') end end, + ['vacant_toggle'] = function(event) + local vacant = is_loaded('modules.clear_vacant_players') + if not vacant then + log("Error: toggle related to clear_vacant_players was activated, but the module does not seem to be loaded.") + return + end + + local is_toggled, message + if event.element.switch_state == 'left' then + is_toggled = true + message = 'has enabled Clear Vacant Players!' + else + is_toggled = false + message = 'has disabled Clear Vacant Players!' + end + vacant.set_enabled(is_toggled) + get_actor(event, '[Clear Vacant Players]', message, true) + end, ['spaghett_toggle'] = function(event) if event.element.switch_state == 'left' then this.gui_config.spaghett.enabled = true @@ -588,6 +606,17 @@ local function build_config_gui(data) scroll_pane.add({type = 'line'}) + local vacant = is_loaded('modules.clear_vacant_players') + if vacant then + switch_state = 'right' + if vacant.is_enabled() then + switch_state = 'left' + end + add_switch(scroll_pane, switch_state, 'vacant_toggle', 'Clear Vacant Players', 'Toggles offline players dropping their inventories on spawn.') + + scroll_pane.add({type = 'line'}) + end + switch_state = 'right' if Gui.get_disable_clear_invalid_data() then switch_state = 'left' From cc9ca16228faec71c0824d7f9e1b181a767bb894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Neves?= Date: Fri, 18 Nov 2022 01:54:55 +0000 Subject: [PATCH 2/4] Uniformize table.remove and table.insert in module Use table.remove() and table.insert() uniformly in module clear_vacant_players. --- modules/clear_vacant_players.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/clear_vacant_players.lua b/modules/clear_vacant_players.lua index 87782f6a..039a03a3 100644 --- a/modules/clear_vacant_players.lua +++ b/modules/clear_vacant_players.lua @@ -24,6 +24,7 @@ Global.register( local Public = {events = {remove_surface = Event.generate_event_name('remove_surface')}} local remove = table.remove +local insert = table.insert function Public.dump_expired_players() if not this.settings.is_enabled then @@ -92,7 +93,7 @@ function Public.dump_expired_players() if player_inv[ii].valid then for iii = 1, #player_inv[ii], 1 do if player_inv[ii][iii].valid then - items[#items + 1] = player_inv[ii][iii] + insert(items, player_inv[ii][iii]) end end end @@ -193,11 +194,11 @@ Event.add( local ticker = game.tick if player and player.online_time >= this.settings.required_online_time then if player.character then - this.offline_players[#this.offline_players + 1] = { + insert(this.offline_players, { index = event.player_index, name = player.name, tick = ticker + this.settings.clear_player_after_tick - } + }) end end end From 13aa1b3d2d12369b93eb33161e0c9de9722ee017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Neves?= Date: Fri, 18 Nov 2022 01:56:10 +0000 Subject: [PATCH 3/4] =?UTF-8?q?Lint=20changes.=20=F0=9F=A7=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fully linted the clear_vacant_players file. --- modules/clear_vacant_players.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/clear_vacant_players.lua b/modules/clear_vacant_players.lua index 039a03a3..cc98fea3 100644 --- a/modules/clear_vacant_players.lua +++ b/modules/clear_vacant_players.lua @@ -22,7 +22,7 @@ Global.register( end ) -local Public = {events = {remove_surface = Event.generate_event_name('remove_surface')}} +local Public = { events = { remove_surface = Event.generate_event_name('remove_surface') } } local remove = table.remove local insert = table.insert @@ -61,7 +61,7 @@ function Public.dump_expired_players() player_inv[4] = target.get_inventory(defines.inventory.character_ammo) player_inv[5] = target.get_inventory(defines.inventory.character_trash) if this.offline_players_surface_removal then - Event.raise(this.events.remove_surface, {target = target}) + Event.raise(this.events.remove_surface, { target = target }) end if target.get_item_count() == 0 then -- if the player has zero items, don't do anything @@ -71,7 +71,7 @@ function Public.dump_expired_players() local pos = game.forces.player.get_spawn_position(surface) local e = - surface.create_entity( + surface.create_entity( { name = 'character', position = pos, @@ -105,7 +105,7 @@ function Public.dump_expired_players() end end - local message = ({'main.cleaner', name}) + local message = ({ 'main.cleaner', name }) local data = { position = pos } @@ -174,6 +174,7 @@ end function Public.reset() Public.clear_offline_players(); end + --- Clears the offline table function Public.clear_offline_players() this.offline_players = {} From 695a513e4e6a66b735650168de27ad79ed8712c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Neves?= Date: Sun, 15 Jan 2023 00:29:42 +0000 Subject: [PATCH 4/4] Remove empty function. Should be in 8a8b52ab of branch vacant/add-to-journey, got wrongly included here when rebasing. --- modules/clear_vacant_players.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/clear_vacant_players.lua b/modules/clear_vacant_players.lua index cc98fea3..8a403318 100644 --- a/modules/clear_vacant_players.lua +++ b/modules/clear_vacant_players.lua @@ -180,8 +180,6 @@ function Public.clear_offline_players() this.offline_players = {} end -local function should_ignore_surface(surface) - Event.on_nth_tick(tick_frequency, Public.dump_expired_players) Event.add(