2013-02-23 12:41:21 +01:00
|
|
|
ServerList = {}
|
|
|
|
|
|
|
|
-- private variables
|
|
|
|
local serverListWindow = nil
|
|
|
|
local serverTextList = nil
|
2013-02-23 13:26:49 +01:00
|
|
|
local removeWindow = nil
|
2013-02-23 12:41:21 +01:00
|
|
|
local servers = {}
|
|
|
|
|
|
|
|
-- public functions
|
|
|
|
function ServerList.init()
|
|
|
|
serverListWindow = g_ui.displayUI('serverlist')
|
|
|
|
serverTextList = serverListWindow:getChildById('serverList')
|
|
|
|
|
2014-04-01 05:51:38 +02:00
|
|
|
servers = g_settings.getNode('ServerList') or {}
|
2013-02-23 12:41:21 +01:00
|
|
|
ServerList.load()
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.terminate()
|
|
|
|
ServerList.destroy()
|
|
|
|
|
2014-04-01 05:51:38 +02:00
|
|
|
g_settings.setNode('ServerList', servers)
|
2013-02-23 12:41:21 +01:00
|
|
|
|
|
|
|
ServerList = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.load()
|
|
|
|
for k,server in pairs(servers) do
|
2013-02-23 13:02:24 +01:00
|
|
|
ServerList.add(k, server.port, server.protocol, true)
|
2013-02-23 12:41:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.select()
|
|
|
|
local selected = serverTextList:getFocusedChild()
|
|
|
|
if selected then
|
2013-02-23 13:02:24 +01:00
|
|
|
local server = servers[selected:getId()]
|
2013-02-23 12:41:21 +01:00
|
|
|
if server then
|
2013-02-23 13:02:24 +01:00
|
|
|
EnterGame.setDefaultServer(selected:getId(), server.port, server.protocol)
|
2013-02-23 12:41:21 +01:00
|
|
|
EnterGame.setAccountName(server.account)
|
|
|
|
EnterGame.setPassword(server.password)
|
|
|
|
ServerList.hide()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.add(host, port, protocol, load)
|
2013-02-23 13:02:24 +01:00
|
|
|
if not load and servers[host] then
|
2013-02-23 12:41:21 +01:00
|
|
|
return false, 'Server already exists'
|
|
|
|
elseif host == '' or port == '' then
|
|
|
|
return false, 'Required fields are missing'
|
|
|
|
end
|
|
|
|
local widget = g_ui.createWidget('ServerWidget', serverTextList)
|
|
|
|
widget:setId(host)
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-02-23 12:41:21 +01:00
|
|
|
if not load then
|
2013-02-23 13:02:24 +01:00
|
|
|
servers[host] = {
|
2013-02-23 12:41:21 +01:00
|
|
|
port = port,
|
|
|
|
protocol = protocol,
|
|
|
|
account = '',
|
|
|
|
password = ''
|
|
|
|
}
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2013-02-23 13:26:49 +01:00
|
|
|
function ServerList.remove(widget)
|
|
|
|
local host = widget:getId()
|
|
|
|
|
|
|
|
if removeWindow then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local yesCallback = function()
|
|
|
|
widget:destroy()
|
|
|
|
servers[host] = nil
|
|
|
|
removeWindow:destroy()
|
|
|
|
removeWindow=nil
|
|
|
|
end
|
|
|
|
local noCallback = function()
|
|
|
|
removeWindow:destroy()
|
|
|
|
removeWindow=nil
|
|
|
|
end
|
|
|
|
|
|
|
|
removeWindow = displayGeneralBox(tr('Remove'), tr('Remove '..host..'?'), {
|
|
|
|
{ text=tr('Yes'), callback=yesCallback },
|
|
|
|
{ text=tr('No'), callback=noCallback },
|
|
|
|
anchor=AnchorHorizontalCenter}, yesCallback, noCallback)
|
2013-02-23 12:41:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.destroy()
|
|
|
|
if serverListWindow then
|
|
|
|
serverTextList = nil
|
|
|
|
serverListWindow:destroy()
|
|
|
|
serverListWindow = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.show()
|
|
|
|
if g_game.isOnline() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
serverListWindow:show()
|
|
|
|
serverListWindow:raise()
|
|
|
|
serverListWindow:focus()
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.hide()
|
|
|
|
serverListWindow:hide()
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.setServerAccount(host, account)
|
2013-02-23 13:02:24 +01:00
|
|
|
if servers[host] then
|
|
|
|
servers[host].account = account
|
2013-02-23 12:41:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerList.setServerPassword(host, password)
|
2013-02-23 13:02:24 +01:00
|
|
|
if servers[host] then
|
|
|
|
servers[host].password = password
|
2013-02-23 12:41:21 +01:00
|
|
|
end
|
|
|
|
end
|