1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/map_gen/maps/crash_site/commands.lua
Jayefuu 467092cd6e
Update commands.lua
- Changed behaviour for non-admins. Now checks for spawners, biters and turrets but doesn't require walls and power structures to be destroyed for restart
- Added checks to limit non-admins from changing the scenario by using an argument after /restart. Now defaults to current scenario for non-admins.
2020-10-01 21:08:09 +01:00

171 lines
4.5 KiB
Lua

local Command = require 'utils.command'
local Rank = require 'features.rank_system'
local Task = require 'utils.task'
local Token = require 'utils.token'
local Server = require 'features.server'
local Popup = require 'features.gui.popup'
local Global = require 'utils.global'
local Ranks = require 'resources.ranks'
local Utils = require 'utils.core'
local Public = {}
function Public.control(config)
local server_player = {name = '<server>', print = print}
local global_data = {restarting = nil}
Global.register(
global_data,
function(tbl)
global_data = tbl
end
)
local function double_print(str)
game.print(str)
print(str)
end
local callback
callback =
Token.register(
function(data)
if not global_data.restarting then
return
end
local state = data.state
if state == 0 then
Server.start_scenario(data.scenario_name)
double_print('restarting')
global_data.restarting = nil
return
elseif state == 1 then
Popup.all('\nServer restarting!\nInitiated by ' .. data.name .. '\n')
end
double_print(state)
data.state = state - 1
Task.set_timeout_in_ticks(60, callback, data)
end
)
local entities_to_check = {
'spitter-spawner','biter-spawner',
'small-worm-turret', 'medium-worm-turret','big-worm-turret', 'behemoth-worm-turret',
'small-spitter', 'medium-spitter', 'big-spitter', 'behemoth-spitter',
'small-biter', 'medium-biter', 'big-biter', 'behemoth-biter'
}
local function map_cleared()
local get_entity_count = game.forces["enemy"].get_entity_count
for i = 1, #entities_to_check do
local name = entities_to_check[i]
if get_entity_count(name) > 0 then
return false
end
end
return true
end
local function restart(args, player)
player = player or server_player
local sanitised_scenario = args.scenario_name
if global_data.restarting then
player.print('Restart already in progress')
return
end
if Rank.less_than(player.name, Ranks.admin) then
-- Check enemy count
if not map_cleared() then
game.player.print('All enemy spawners, worms, buildings, biters and spitters must be cleared for non-admin restart.')
return
end
-- Limit the ability of non-admins to call the restart function with arguments to change the scenario
-- If not an admin, restart the same scenario always
sanitised_scenario = config.scenario_name
end
global_data.restarting = true
double_print('#################-Attention-#################')
double_print('Server restart initiated by ' .. player.name)
double_print('###########################################')
for k, v in pairs(game.players) do
if v.admin then
game.print('Abort restart with /abort')
end
end
print('Abort restart with /abort')
Task.set_timeout_in_ticks(60, callback, {name = player.name, scenario_name = sanitised_scenario, state = 10})
end
local function abort(_, player)
player = player or server_player
if global_data.restarting then
global_data.restarting = nil
double_print('Restart aborted by ' .. player.name)
else
player.print('Cannot abort a restart that is not in progress.')
end
end
Command.add(
'crash-site-restart-abort',
{
description = {'command_description.crash_site_restart_abort'},
required_rank = Ranks.admin,
allowed_by_server = true
},
abort
)
Command.add(
'abort',
{
description = {'command_description.crash_site_restart_abort'},
required_rank = Ranks.admin,
allowed_by_server = true
},
abort
)
local default_name = config.scenario_name or 'crashsite'
Command.add(
'crash-site-restart',
{
description = {'command_description.crash_site_restart'},
arguments = {'scenario_name'},
default_values = {scenario_name = default_name},
required_rank = Ranks.admin,
allowed_by_server = true
},
restart
)
Command.add(
'restart',
{
description = {'command_description.crash_site_restart'},
arguments = {'scenario_name'},
default_values = {scenario_name = default_name},
required_rank = Ranks.auto_trusted,
allowed_by_server = true
},
restart
)
end
return Public