diff --git a/config/_file_loader.lua b/config/_file_loader.lua index 5fd71d8d..be200bf3 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -27,6 +27,7 @@ return { 'modules.commands.warnings', 'modules.commands.find', 'modules.commands.home', + 'modules.commands.connect', --- Addons 'modules.addons.chat-popups', diff --git a/config/expcore/command_general_parse.lua b/config/expcore/command_general_parse.lua index 09f42c37..065db0ba 100644 --- a/config/expcore/command_general_parse.lua +++ b/config/expcore/command_general_parse.lua @@ -38,13 +38,8 @@ end) Commands.add_parse('string-options',function(input, player, reject, options) if not input then return end -- nil check - input = input:lower() - for _, option in ipairs(options) do - if input == option:lower() then - return option - end - end - return reject{'expcore-commands.reject-string-options', table.concat(options, ', ')} + local option = _C.auto_complete(options, input) + return option or reject{'expcore-commands.reject-string-options', table.concat(options, ', ')} end) Commands.add_parse('string-max-length',function(input, player, reject, max_length) diff --git a/config/expcore/roles.lua b/config/expcore/roles.lua index c1904ada..df059559 100644 --- a/config/expcore/roles.lua +++ b/config/expcore/roles.lua @@ -49,6 +49,7 @@ Roles.new_role('Administrator','Admin') :allow{ 'gui/warp-list/bypass-cooldown', 'gui/warp-list/bypass-proximity', + 'command/connect-all', } Roles.new_role('Moderator','Mod') @@ -75,6 +76,7 @@ Roles.new_role('Moderator','Mod') 'command/home-set', 'command/home-get', 'command/return', + 'command/connect-player', 'gui/rocket-info/toggle-active', 'gui/rocket-info/remote_launch', 'fast-tree-decon', @@ -223,6 +225,7 @@ local default = Roles.new_role('Guest','') 'command/save-data', 'command/preference', 'command/set-preference', + 'command/connect', 'gui/player-list', 'gui/rocket-info', 'gui/science-info', diff --git a/expcore/external.lua b/expcore/external.lua index 26751fa0..745aafa3 100644 --- a/expcore/external.lua +++ b/expcore/external.lua @@ -73,7 +73,7 @@ function External.get_servers_filtered(search) search = search:lower() for server_id, server in pairs(servers) do local str = concat{server.name, server.short_name, server.id} - if str:lower():match(search, 1, true) then found_servers[server_id] = server end + if str:lower():find(search, 1, true) then found_servers[server_id] = server end end return found_servers end @@ -143,9 +143,8 @@ External.request_connection(player, 'eu-01', true) ]] function External.request_connection(player, server_id, self_requested) - assert(ext, 'No external data was found, use External.valid() to ensure external data exists.') - local servers = assert(ext.servers, 'No server list was found, please ensure that the external service is running') - local server = servers[server_id] or { address = server_id, name = 'Unknown Server', description = 'This server is not ran by us, please check the address of the server.' } + local server = { address = server_id, name = 'Unknown Server', description = 'This server is not ran by us, please check the address of the server.' } + if ext and ext.servers and ext.servers[server_id] then server = ext.servers[server_id] end local message = 'Please press the connect button below to join.' if not self_requested then message = 'You have been asked to switch to a different server.\n'..message end player.connect_to_server{ diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index 5bc738ba..6c9fb44f 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -76,4 +76,11 @@ return-set=Your return point has been set to x: __1__ y: __2__ home-get=Your home point is at x: __1__ y: __2__ [expcom-server-ups] -no-ext=No external source was found, cannot display server ups. \ No newline at end of file +no-ext=No external source was found, cannot display server ups. + +[expcom-connect] +too-many-matching=Multiple server were found with the given name: __1__ +wrong-version=Servers were found but are on a different version: __1__ +same-server=You are already connected to the server: __1__ +offline=You cannot connect as the server is currently offline: __1__ +none-matching=No servers were found with that name, if you used an address please append true to the end of your command. \ No newline at end of file diff --git a/modules/addons/chat-popups.lua b/modules/addons/chat-popups.lua index ab197e7d..22e9c772 100644 --- a/modules/addons/chat-popups.lua +++ b/modules/addons/chat-popups.lua @@ -29,7 +29,7 @@ Event.add(defines.events.on_console_chat, function(event) -- Loops over online players to see if they name is included for _, mentioned_player in pairs(game.connected_players) do if mentioned_player.index ~= player.index then - if search_string:match(mentioned_player.name:lower(), 1, true) then + if search_string:find(mentioned_player.name:lower(), 1, true) then send_text(mentioned_player.index, {'chat-popup.ping', player.name}, player.chat_color) end end diff --git a/modules/commands/connect.lua b/modules/commands/connect.lua new file mode 100644 index 00000000..7951e628 --- /dev/null +++ b/modules/commands/connect.lua @@ -0,0 +1,107 @@ +--[[-- Commands Module - Connect + - Adds a commands that allows you to request a player move to another server + @commands Connect +]] + +local Async = require 'expcore.async' --- @dep expcore.async +local External = require 'expcore.external' --- @dep expcore.external +local Commands = require 'expcore.commands' --- @dep expcore.commands +require 'config.expcore.command_role_parse' +local concat = table.concat + +local request_connection = Async.register(External.request_connection) + +local function get_server_id(server) + local current_server = External.get_current_server() + local current_version = current_server.version + local servers = External.get_servers_filtered(server) + + local server_names_before, server_names = {}, {} + local server_count_before, server_count = 0, 0 + for next_server_id, server_details in pairs(servers) do + server_count_before = server_count_before + 1 + server_names_before[server_count_before] = server_details.name + if server_details.version == current_version then + server_count = server_count + 1 + server_names[server_count] = server_details.name + else + servers[next_server_id] = nil + end + end + + if server_count > 1 then + return false, Commands.error{'expcom-connect.too-many-matching', concat(server_names, ', ')} + elseif server_count == 1 then + local server_id, server_details = next(servers) + local status = External.get_server_status(server_id) + if server_id == current_server.id then + return false, Commands.error{'expcom-connect.same-server', server_details.name} + elseif status == 'Offline' then + return false, Commands.error{'expcom-connect.offline', server_details.name} + end + return true, server_id + elseif server_count_before > 0 then + return false, Commands.error{'expcom-connect.wrong-version', concat(server_names_before, ', ')} + else + return false, Commands.error{'expcom-connect.none-matching'} + end +end + +--- Connect to a different server +-- @command connect +-- @tparam string server The address or name of the server to connect to +-- @tparam[opt=false] boolean is_address If an address was given for the server param +Commands.new_command('connect', 'Connect to another server') +:add_param('server') +:add_param('is_address', true, 'boolean') +:add_alias('join', 'server') +:register(function(player, server, is_address) + local server_id = server + if not is_address and External.valid() then + local success, new_server_id = get_server_id(server) + if not success then return new_server_id end + server_id = new_server_id + end + + Async(request_connection, player, server_id, true) +end) + +--- Connect a player to a different server +-- @command connect-player +-- @tparam string address The address or name of the server to connect to +-- @tparam LuaPlayer player The player to connect to a different server +-- @tparam[opt=false] boolean is_address If an address was given for the server param +Commands.new_command('connect-player', 'Send a player to a different server') +:add_param('player', 'player-role') +:add_param('server') +:add_param('is_address', true, 'boolean') +:register(function(_, player, server, is_address) + local server_id = server + if not is_address and External.valid() then + local success, new_server_id = get_server_id(server) + if not success then return new_server_id end + server_id = new_server_id + end + + External.request_connection(player, server_id) +end) + +--- Connect all players to a different server +-- @command connect-all +-- @tparam string address The address or name of the server to connect to +-- @tparam[opt=false] boolean is_address If an address was given for the server param +Commands.new_command('connect-all', 'Connect all players to another server') +:add_param('server') +:add_param('is_address', true, 'boolean') +:register(function(_, server, is_address) + local server_id = server + if not is_address and External.valid() then + local success, new_server_id = get_server_id(server) + if not success then return new_server_id end + server_id = new_server_id + end + + for _, player in pairs(game.connected_players) do + External.request_connection(player, server_id) + end +end) \ No newline at end of file