1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-06 00:23:49 +02:00

Merge pull request #379 from aneves/vacant/improvements

Vacant/improvements
This commit is contained in:
Gerkiz 2023-01-17 10:43:23 +01:00 committed by GitHub
commit f75ca6dc7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 21 deletions

View File

@ -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()

View File

@ -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
@ -20,14 +22,16 @@ 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
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
@ -57,7 +61,7 @@ function Public.remove_offline_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
@ -67,7 +71,7 @@ function Public.remove_offline_players()
local pos = game.forces.player.get_spawn_position(surface)
local e =
surface.create_entity(
surface.create_entity(
{
name = 'character',
position = pos,
@ -89,7 +93,7 @@ function Public.remove_offline_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
@ -101,7 +105,7 @@ function Public.remove_offline_players()
end
end
local message = ({'main.cleaner', name})
local message = ({ 'main.cleaner', name })
local data = {
position = pos
}
@ -129,10 +133,30 @@ function Public.remove_offline_players()
end
end
--- Activates the offline player module
--- Initializes the module with blank state, receiving all required parameters.
--- <br />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 +166,26 @@ 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
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
@ -167,11 +193,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

View File

@ -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'