Added connect command

This commit is contained in:
Cooldude2606
2020-06-15 00:29:23 +01:00
parent 5150d28fd3
commit 43c1bc2788
7 changed files with 125 additions and 13 deletions

View File

@@ -27,6 +27,7 @@ return {
'modules.commands.warnings', 'modules.commands.warnings',
'modules.commands.find', 'modules.commands.find',
'modules.commands.home', 'modules.commands.home',
'modules.commands.connect',
--- Addons --- Addons
'modules.addons.chat-popups', 'modules.addons.chat-popups',

View File

@@ -38,13 +38,8 @@ end)
Commands.add_parse('string-options',function(input, player, reject, options) Commands.add_parse('string-options',function(input, player, reject, options)
if not input then return end -- nil check if not input then return end -- nil check
input = input:lower() local option = _C.auto_complete(options, input)
for _, option in ipairs(options) do return option or reject{'expcore-commands.reject-string-options', table.concat(options, ', ')}
if input == option:lower() then
return option
end
end
return reject{'expcore-commands.reject-string-options', table.concat(options, ', ')}
end) end)
Commands.add_parse('string-max-length',function(input, player, reject, max_length) Commands.add_parse('string-max-length',function(input, player, reject, max_length)

View File

@@ -49,6 +49,7 @@ Roles.new_role('Administrator','Admin')
:allow{ :allow{
'gui/warp-list/bypass-cooldown', 'gui/warp-list/bypass-cooldown',
'gui/warp-list/bypass-proximity', 'gui/warp-list/bypass-proximity',
'command/connect-all',
} }
Roles.new_role('Moderator','Mod') Roles.new_role('Moderator','Mod')
@@ -75,6 +76,7 @@ Roles.new_role('Moderator','Mod')
'command/home-set', 'command/home-set',
'command/home-get', 'command/home-get',
'command/return', 'command/return',
'command/connect-player',
'gui/rocket-info/toggle-active', 'gui/rocket-info/toggle-active',
'gui/rocket-info/remote_launch', 'gui/rocket-info/remote_launch',
'fast-tree-decon', 'fast-tree-decon',
@@ -223,6 +225,7 @@ local default = Roles.new_role('Guest','')
'command/save-data', 'command/save-data',
'command/preference', 'command/preference',
'command/set-preference', 'command/set-preference',
'command/connect',
'gui/player-list', 'gui/player-list',
'gui/rocket-info', 'gui/rocket-info',
'gui/science-info', 'gui/science-info',

View File

@@ -73,7 +73,7 @@ function External.get_servers_filtered(search)
search = search:lower() search = search:lower()
for server_id, server in pairs(servers) do for server_id, server in pairs(servers) do
local str = concat{server.name, server.short_name, server.id} 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 end
return found_servers return found_servers
end end
@@ -143,9 +143,8 @@ External.request_connection(player, 'eu-01', true)
]] ]]
function External.request_connection(player, server_id, self_requested) 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 server = { address = server_id, name = 'Unknown Server', description = 'This server is not ran by us, please check the address of the server.' }
local servers = assert(ext.servers, 'No server list was found, please ensure that the external service is running') if ext and ext.servers and ext.servers[server_id] then server = ext.servers[server_id] end
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 message = 'Please press the connect button below to join.' 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 if not self_requested then message = 'You have been asked to switch to a different server.\n'..message end
player.connect_to_server{ player.connect_to_server{

View File

@@ -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__ home-get=Your home point is at x: __1__ y: __2__
[expcom-server-ups] [expcom-server-ups]
no-ext=No external source was found, cannot display server ups. 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.

View File

@@ -29,7 +29,7 @@ Event.add(defines.events.on_console_chat, function(event)
-- Loops over online players to see if they name is included -- Loops over online players to see if they name is included
for _, mentioned_player in pairs(game.connected_players) do for _, mentioned_player in pairs(game.connected_players) do
if mentioned_player.index ~= player.index then 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) send_text(mentioned_player.index, {'chat-popup.ping', player.name}, player.chat_color)
end end
end end

View File

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