2013-02-24 00:41:21 +13:00
|
|
|
ServerList = {}
|
|
|
|
|
|
|
|
|
|
-- private variables
|
|
|
|
|
local serverListWindow = nil
|
|
|
|
|
local serverTextList = nil
|
2013-02-24 01:26:49 +13:00
|
|
|
local removeWindow = nil
|
2013-02-24 00:41:21 +13:00
|
|
|
local servers = {}
|
|
|
|
|
|
|
|
|
|
-- public functions
|
|
|
|
|
function ServerList.init()
|
2021-07-19 19:24:59 -03:00
|
|
|
serverListWindow = g_ui.displayUI('serverlist')
|
|
|
|
|
serverTextList = serverListWindow:getChildById('serverList')
|
2024-06-21 18:10:07 -03:00
|
|
|
local processedServers = {}
|
2021-07-19 19:24:59 -03:00
|
|
|
servers = g_settings.getNode('ServerList') or {}
|
2024-06-21 18:10:07 -03:00
|
|
|
if Servers_init then
|
|
|
|
|
for key, value in pairs(Servers_init) do
|
|
|
|
|
if not servers[key] then
|
|
|
|
|
servers[key] = value
|
|
|
|
|
if not processedServers[key] then
|
|
|
|
|
processedServers[key] = true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-08-17 12:51:56 -03:00
|
|
|
if servers then
|
|
|
|
|
ServerList.load()
|
|
|
|
|
end
|
2013-02-24 00:41:21 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ServerList.terminate()
|
2021-07-19 19:24:59 -03:00
|
|
|
ServerList.destroy()
|
2013-02-24 00:41:21 +13:00
|
|
|
|
2021-07-19 19:24:59 -03:00
|
|
|
g_settings.setNode('ServerList', servers)
|
2013-02-24 00:41:21 +13:00
|
|
|
|
2021-07-19 19:24:59 -03:00
|
|
|
ServerList = nil
|
|
|
|
|
serverListWindow = nil
|
|
|
|
|
serverTextList = nil
|
2013-02-24 00:41:21 +13:00
|
|
|
end
|
|
|
|
|
|
2015-07-19 06:34:21 +12:00
|
|
|
function ServerList.load()
|
2022-08-17 12:51:56 -03:00
|
|
|
for host, server in pairs(servers) do
|
2024-03-07 13:04:04 -03:00
|
|
|
ServerList.add(host, server.port, server.protocol, httpLogin, true)
|
2022-08-17 12:51:56 -03:00
|
|
|
end
|
2013-02-24 00:41:21 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ServerList.select()
|
2021-07-19 19:24:59 -03:00
|
|
|
local selected = serverTextList:getFocusedChild()
|
|
|
|
|
if selected then
|
|
|
|
|
local server = servers[selected:getId()]
|
|
|
|
|
if server then
|
2022-04-29 23:14:24 -03:00
|
|
|
EnterGame.setDefaultServer(selected:getId(), server.port, server.protocol)
|
2021-07-19 19:24:59 -03:00
|
|
|
EnterGame.setAccountName(server.account)
|
|
|
|
|
EnterGame.setPassword(server.password)
|
2024-03-07 13:04:04 -03:00
|
|
|
EnterGame.setHttpLogin(server.httpLogin)
|
2021-07-19 19:24:59 -03:00
|
|
|
ServerList.hide()
|
|
|
|
|
end
|
2013-02-24 00:41:21 +13:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-07 13:04:04 -03:00
|
|
|
function ServerList.add(host, port, protocol, httpLogin, load)
|
2021-07-19 19:24:59 -03:00
|
|
|
if not host or not port or not protocol then
|
|
|
|
|
return false, 'Failed to load settings'
|
|
|
|
|
elseif not load and servers[host] then
|
|
|
|
|
return false, 'Server already exists'
|
|
|
|
|
elseif host == '' or port == '' then
|
|
|
|
|
return false, 'Required fields are missing'
|
2024-03-07 13:04:04 -03:00
|
|
|
elseif httpLogin == nil then
|
2024-06-21 18:10:07 -03:00
|
|
|
httpLogin = false
|
2021-07-19 19:24:59 -03:00
|
|
|
end
|
|
|
|
|
local widget = g_ui.createWidget('ServerWidget', serverTextList)
|
|
|
|
|
widget:setId(host)
|
|
|
|
|
|
|
|
|
|
if not load then
|
|
|
|
|
servers[host] = {
|
|
|
|
|
port = port,
|
|
|
|
|
protocol = protocol,
|
|
|
|
|
account = '',
|
2024-03-07 13:04:04 -03:00
|
|
|
password = '',
|
|
|
|
|
httpLogin = httpLogin
|
2021-07-19 19:24:59 -03:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local details = widget:getChildById('details')
|
|
|
|
|
details:setText(host .. ':' .. port)
|
|
|
|
|
|
|
|
|
|
local proto = widget:getChildById('protocol')
|
|
|
|
|
proto:setText(protocol)
|
|
|
|
|
|
|
|
|
|
connect(widget, {
|
|
|
|
|
onDoubleClick = function()
|
|
|
|
|
ServerList.select()
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
return true
|
2013-02-24 00:41:21 +13:00
|
|
|
end
|
|
|
|
|
|
2013-02-24 01:26:49 +13:00
|
|
|
function ServerList.remove(widget)
|
2021-07-19 19:24:59 -03:00
|
|
|
local host = widget:getId()
|
|
|
|
|
|
2022-08-17 12:51:56 -03:00
|
|
|
if removeWindow then
|
|
|
|
|
return
|
|
|
|
|
end
|
2021-07-19 19:24:59 -03:00
|
|
|
|
|
|
|
|
local yesCallback = function()
|
|
|
|
|
widget:destroy()
|
|
|
|
|
servers[host] = nil
|
|
|
|
|
removeWindow:destroy()
|
|
|
|
|
removeWindow = nil
|
|
|
|
|
end
|
|
|
|
|
local noCallback = function()
|
|
|
|
|
removeWindow:destroy()
|
|
|
|
|
removeWindow = nil
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-29 23:14:24 -03:00
|
|
|
removeWindow = displayGeneralBox(tr('Remove'), tr('Remove ' .. host .. '?'), {
|
|
|
|
|
{
|
|
|
|
|
text = tr('Yes'),
|
|
|
|
|
callback = yesCallback
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
text = tr('No'),
|
|
|
|
|
callback = noCallback
|
|
|
|
|
},
|
2021-07-19 19:24:59 -03:00
|
|
|
anchor = AnchorHorizontalCenter
|
|
|
|
|
}, yesCallback, noCallback)
|
2013-02-24 00:41:21 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ServerList.destroy()
|
2021-07-19 19:24:59 -03:00
|
|
|
if serverListWindow then
|
|
|
|
|
serverTextList = nil
|
|
|
|
|
serverListWindow:destroy()
|
|
|
|
|
serverListWindow = nil
|
|
|
|
|
end
|
2013-02-24 00:41:21 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ServerList.show()
|
2022-08-17 12:51:56 -03:00
|
|
|
if g_game.isOnline() then
|
|
|
|
|
return
|
|
|
|
|
end
|
2021-07-19 19:24:59 -03:00
|
|
|
serverListWindow:show()
|
|
|
|
|
serverListWindow:raise()
|
|
|
|
|
serverListWindow:focus()
|
2013-02-24 00:41:21 +13:00
|
|
|
end
|
|
|
|
|
|
2022-08-17 12:51:56 -03:00
|
|
|
function ServerList.hide()
|
|
|
|
|
serverListWindow:hide()
|
|
|
|
|
end
|
2013-02-24 00:41:21 +13:00
|
|
|
|
2022-08-17 12:51:56 -03:00
|
|
|
function ServerList.setServerAccount(host, account)
|
|
|
|
|
if servers[host] then
|
|
|
|
|
servers[host].account = account
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-02-24 00:41:21 +13:00
|
|
|
|
2022-08-17 12:51:56 -03:00
|
|
|
function ServerList.setServerPassword(host, password)
|
|
|
|
|
if servers[host] then
|
|
|
|
|
servers[host].password = password
|
|
|
|
|
end
|
|
|
|
|
end
|