Merge pull request #167 from Cooldude2606/feature/external

Added external server list
This commit is contained in:
Cooldude2606
2020-08-14 00:29:29 +01:00
committed by GitHub
12 changed files with 359 additions and 41 deletions

View File

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

View File

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

View File

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

151
expcore/external.lua Normal file
View File

@@ -0,0 +1,151 @@
--[[-- Core Module - External
- A module used to make accessing externally set data easier.
@core External
@alias External
@usage-- Printing all server to chat
local External = require 'expcore.external' --- @dep expcore.external
local message = 'id: %s name: %s version: %s status: %s'
for server_id, server in pairs(External.get_servers()) do
local status = External.get_server_status(server_id)
game.print(message:format(server_id, server.name, server.version, status))
end
]]
local ext, var
local concat = table.concat
local External = {}
--[[-- Checks that local links are valid, will try to add the links if invalid
@treturn boolean If the external data is valid, if false you should not call any other methods from External
@usage-- Check that external data is valid
if not External.valid() then
-- error code here
end
]]
function External.valid()
if global.ext == nil then return false end
if ext == global.ext and var == ext.var then
return var ~= nil
else
ext = global.ext
var = ext.var
return var ~= nil
end
end
--[[-- Gets a table of all the servers, key is the server id, value is the server details
@treturn table A table containing all the servers, key is the server id, value is the server details
@usage-- Get all servers
local servers = External.get_servers()
]]
function External.get_servers()
assert(ext, 'No external data was found, use External.valid() to ensure external data exists.')
return assert(ext.servers, 'No server list was found, please ensure that the external service is running')
end
--[[-- Gets a table of all the servers filtered by name, key is the server id, value is the server details
@tparam string search The string to search for, names, short_names and ids are checked for this string.
@treturn table A table containing all the servers filtered by name, key is the server id, value is the server details
@usage-- Get all servers with public in the name
local servers = External.get_servers_filtered(public)
]]
function External.get_servers_filtered(search)
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 found_servers = {}
search = search:lower()
for server_id, server in pairs(servers) do
local str = concat{server.name, server.short_name, server.id}
if str:lower():find(search, 1, true) then found_servers[server_id] = server end
end
return found_servers
end
--[[-- Gets the details of the current server
@treturn table The details of the current server
@usage-- Get the details of the current server
local server = External.get_current_server()
]]
function External.get_current_server()
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_id = assert(ext.current, 'No current id was found, please ensure that the external service is running')
return servers[server_id]
end
--[[-- Gets the details of the given server
@tparam string server_id The internal server if for the server you want the details of
@treturn table The details of the given server
@usage-- Get the details of the given server
local server = External.get_server_details('eu-01')
]]
function External.get_server_details(server_id)
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')
return servers[server_id]
end
--[[-- Gets the status of the given server
@tparam string server_id The internal server if for the server you want the status of
@treturn string The status of the given server, one of: Online, Modded, Protected, Offline
@usage-- Get the status of the given server
local status = External.get_server_status('eu-01')
]]
function External.get_server_status(server_id)
assert(var, 'No external data was found, use External.valid() to ensure external data exists.')
local servers = assert(var.status, 'No server status was found, please ensure that the external service is running')
return servers[server_id]
end
--[[-- Gets the ups of the current server
@usage-- Get the ups of the current server
local server_ups = External.get_server_ups()
]]
function External.get_server_ups()
assert(var, 'No external data was found, use External.valid() to ensure external data exists.')
return assert(var.server_ups, 'No server ups was found, please ensure that the external service is running')
end
--[[-- Connect a player to the given server
@tparam LuaPlayer player The player that you want to request to join a different server
@tparam string server_id The internal id of the server to connect to, can also be any address but this will show Unknown Server
@tparam[opt=false] boolean self_requested If the player requested the join them selfs, this will hide the message about being asked to switch
@usage-- Request that a player joins a different server
External.request_connection(player, 'eu-01')
@usage-- Request that a player joins a different server, by own request
External.request_connection(player, 'eu-01', true)
]]
function External.request_connection(player, server_id, self_requested)
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{
address = server.address,
name = '\n[color=orange][font=heading-1]'..server.name..'[/font][/color]\n',
description = server.description..'\n'..message
}
end
--- Module return
return External

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__
[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

@@ -146,6 +146,11 @@ servers-7=S7 Event
servers-d7=This is our event server, we try to run events at least once per week.
servers-8=S8 T̷-̶s̶-̴:̷
servers-d8=N̵o̴ ̶o̷-̶e̵ ̴k̸n̷-̶w̵s̸ ̴w̷h̷a̶-̶ ̷h̴a̴p̷p̴e̷n̷s̸ ̷o̶n̴ ̷t̶h̴-̶s̶ ̷s̷e̶r̸v̸e̴r̷,̶ ̸i̸t̴ ̷m̶-̸g̴h̶t̷ ̸n̸-̶t̵ ̷e̴v̸e̸n̶t̷ ̵-̷x̴i̵s̶t̸.̸
servers-connect-Offline=Server is currently offline
servers-connect-Version=Server is on a different version: __1__
servers-connect-Password=Server requires a password
servers-connect-Modded=Server requires mods to be downloaded
servers-connect-Online=Server is online
servers-external=External Links
servers-open-in-browser=Open in your browser
backers-tab=Backers

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

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)

View File

@@ -5,25 +5,23 @@
local Commands = require 'expcore.commands' --- @dep expcore.commands
local Global = require 'utils.global' --- @dep utils.global
local Common = require 'expcore.common' --- @dep expcore.common
-- modules that are loaded into the interface env to be accessed
local interface_modules = {
['Game']='utils.game',
['_C']=Common,
['Commands']=Commands,
['output']=Common.player_return,
['Group']='expcore.permission_groups',
['Roles']='expcore.roles',
['Gui']='expcore.gui',
['Async']='expcore.async',
['Datastore']='expcore.datastore'
['Commands'] = Commands,
['output'] = _C.player_return,
['Group'] = 'expcore.permission_groups',
['Roles'] = 'expcore.roles',
['Gui'] = 'expcore.gui',
['Async'] = 'expcore.async',
['Datastore'] = 'expcore.datastore',
['External'] = 'expcore.external'
}
-- loads all the modules given in the above table
for key, value in pairs(interface_modules) do
if type(value) == 'string' then
interface_modules[key] = Common.opt_require(value)
interface_modules[key] = _C.opt_require(value)
end
end

View File

@@ -4,12 +4,12 @@
@alias readme
]]
local Event = require 'utils.event' --- @dep utils.event
local Gui = require 'expcore.gui' --- @dep expcore.gui
local Roles = require 'expcore.roles' --- @dep expcore.roles
local Commands = require 'expcore.commands' --- @dep expcore.commands
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local Event = require 'utils.event' --- @dep utils.event
local Game = require 'utils.game' --- @dep utils.game
local External = require 'expcore.external' --- @dep expcore.external
local format_time = _C.format_time --- @dep expcore.common
local format_number = require('util').format_number --- @dep util
@@ -18,9 +18,9 @@ local function Tab(caption, tooltip, element_define)
tabs[#tabs+1] = {caption, tooltip, element_define}
end
local frame_width = 595 -- controls width of top descritions
local frame_width = 595 -- controls width of top descriptions
local title_width = 270 -- controls the centering of the titles
local scroll_hieght = 275 -- controls the height of the scrolls
local scroll_height = 275 -- controls the height of the scrolls
--- Sub content area used within the content areas
-- @element sub_content
@@ -70,15 +70,52 @@ Gui.element{
}
:style{
padding = {1, 3},
maximal_height = scroll_hieght,
maximal_height = scroll_height,
horizontally_stretchable = true,
}
--- Used to connect to servers in server list
-- @element join_server
local join_server =
Gui.element(function(event_trigger, parent, server_id, wrong_version)
local status = wrong_version and 'Version' or External.get_server_status(server_id) or 'Offline'
local flow = parent.add{ name = server_id, type = 'flow' }
local button = flow.add{
name = event_trigger,
type = 'sprite-button',
sprite = 'utility/circuit_network_panel_white', --- network panel white, warning white, download white
hovered_sprite = 'utility/circuit_network_panel_black', --- network panel black, warning black, download black
tooltip = {'readme.servers-connect-'..status, wrong_version}
}
if status == 'Offline' then
button.enabled = false
button.sprite = 'utility/circuit_network_panel_black'
elseif status == 'Version' then
button.enabled = false
button.sprite = 'utility/shuffle'
elseif status == 'Password' then
button.sprite = 'utility/warning_white'
button.hovered_sprite = 'utility/warning'
elseif status == 'Modded' then
button.sprite = 'utility/downloading_white'
button.hovered_sprite = 'utility/downloading'
end
return button
end)
:style(Gui.sprite_style(20, -1))
:on_click(function(player, element, _)
local server_id = element.parent.name
External.request_connection(player, server_id, true)
end)
--- Content area for the welcome tab
-- @element welcome_content
Tab({'readme.welcome-tab'}, {'readme.welcome-tooltip'},
Gui.element(function(_, parent)
local server_details = global.server_details or { name='ExpGaming S0 - Local', description='Failed to load description: disconnected from sync api.', reset_time='Non Set', branch='Unknown'}
local server_details = { name='ExpGaming S0 - Local', welcome='Failed to load description: disconnected from external api.', reset_time='Non Set', branch='Unknown'}
if External.valid() then server_details = External.get_current_server() end
local container = parent.add{ type='flow', direction='vertical' }
local player = Gui.get_player_from_element(parent)
@@ -91,7 +128,7 @@ Gui.element(function(_, parent)
-- Add the title and description to the top flow
Gui.title_label(top_vertical_flow, 62, 'Welcome to '..server_details.name)
Gui.centered_label(top_vertical_flow, 380, server_details.description)
Gui.centered_label(top_vertical_flow, 380, server_details.welcome)
Gui.bar(container)
-- Get the names of the roles the player has
@@ -124,7 +161,7 @@ Gui.element(function(_, parent)
container.add{ type='flow' }
-- Add a table for the rules
local rules = Gui.scroll_table(container, scroll_hieght, 1)
local rules = Gui.scroll_table(container, scroll_height, 1)
rules.style = 'bordered_table'
rules.style.cell_padding = 4
@@ -150,7 +187,7 @@ Gui.element(function(_, parent)
container.add{ type='flow' }
-- Add a table for the commands
local commands = Gui.scroll_table(container, scroll_hieght, 2)
local commands = Gui.scroll_table(container, scroll_height, 2)
commands.style = 'bordered_table'
commands.style.cell_padding = 0
@@ -177,13 +214,23 @@ Gui.element(function(_, parent)
-- Draw the scroll
local scroll_pane = title_table_scroll(container)
scroll_pane.style.maximal_height = scroll_hieght + 20 -- the text is a bit shorter
scroll_pane.style.maximal_height = scroll_height + 20 -- the text is a bit shorter
-- Add the factorio servers
local factorio_servers = title_table(scroll_pane, 225, {'readme.servers-factorio'}, 2)
for i = 1, 8 do
Gui.centered_label(factorio_servers, 110, {'readme.servers-'..i})
Gui.centered_label(factorio_servers, 460, {'readme.servers-d'..i})
if External.valid() then
local factorio_servers = title_table(scroll_pane, 225, {'readme.servers-factorio'}, 3)
local current_version = External.get_current_server().version
for server_id, server in pairs(External.get_servers()) do
Gui.centered_label(factorio_servers, 110, server.short_name)
Gui.centered_label(factorio_servers, 436, server.description)
join_server(factorio_servers, server_id, current_version ~= server.version and server.version)
end
else
local factorio_servers = title_table(scroll_pane, 225, {'readme.servers-factorio'}, 2)
for i = 1, 8 do
Gui.centered_label(factorio_servers, 110, {'readme.servers-'..i})
Gui.centered_label(factorio_servers, 460, {'readme.servers-d'..i})
end
end
-- Add the external links
@@ -418,7 +465,7 @@ end)
--- When a player joins the game for the first time show this gui
Event.add(defines.events.on_player_created, function(event)
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
local element = readme(player.gui.center)
element.pane.selected_tab_index = 1
player.opened = element
@@ -426,7 +473,7 @@ end)
--- When a player joins clear center unless the player has something open
Event.add(defines.events.on_player_joined_game, function(event)
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
if not player.opened then
player.gui.center.clear()
end
@@ -434,7 +481,7 @@ end)
--- When a player respawns clear center unless the player has something open
Event.add(defines.events.on_player_respawned, function(event)
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
if not player.opened then
player.gui.center.clear()
end

View File

@@ -7,6 +7,7 @@
local Gui = require 'expcore.gui' --- @dep expcore.gui
local Event = require 'utils.event' --- @dep utils.event
local Commands = require 'expcore.commands' --- @dep expcore.commands
local External = require 'expcore.external' --- @dep expcore.external
--- Stores the visible state of server ups
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
@@ -42,7 +43,8 @@ Commands.new_command('server-ups', 'Toggle the server UPS display')
:add_alias('sups', 'ups')
:register(function(player)
local label = player.gui.screen[server_ups.name]
if not global.ext or not global.ext.server_ups then
if not External.valid() then
label.visible = false
return Commands.error{'expcom-server-ups.no-ext'}
end
label.visible = not label.visible
@@ -69,8 +71,8 @@ end)
-- Update the caption for all online players
Event.on_nth_tick(60, function()
if global.ext and global.ext.server_ups then
local caption = 'SUPS = '..global.ext.server_ups
if External.valid() then
local caption = 'SUPS = '..External.get_server_ups()
for _, player in pairs(game.connected_players) do
player.gui.screen[server_ups.name].caption = caption
end

View File

@@ -1,3 +1,5 @@
return {
expgaming = '6.0.0'
expgaming_lua = '6.0.0',
expgaming_api = '2.0.0',
redmew_lua = '2019-02-24-76871ee'
}