Some fixes for the server list module

* Fixed an indexing issue in the settings
Note: Forgot to mention that it will store the last account/password used for that server
master
BeniS 11 years ago
parent 8e9f65779c
commit 01126eee62

@ -5,23 +5,6 @@ local serverListWindow = nil
local serverTextList = nil local serverTextList = nil
local servers = {} local servers = {}
-- private functions
function getServer(host)
for k,server in pairs(servers) do
if server.host == host then
return server
end
end
end
function getServerKey(host)
for k,server in pairs(servers) do
if server.host == host then
return k
end
end
end
-- public functions -- public functions
function ServerList.init() function ServerList.init()
serverListWindow = g_ui.displayUI('serverlist') serverListWindow = g_ui.displayUI('serverlist')
@ -41,16 +24,16 @@ end
function ServerList.load() function ServerList.load()
for k,server in pairs(servers) do for k,server in pairs(servers) do
ServerList.add(server.host, server.port, server.protocol, true) ServerList.add(k, server.port, server.protocol, true)
end end
end end
function ServerList.select() function ServerList.select()
local selected = serverTextList:getFocusedChild() local selected = serverTextList:getFocusedChild()
if selected then if selected then
local server = servers[getServerKey(selected:getId())] local server = servers[selected:getId()]
if server then if server then
EnterGame.setDefaultServer(server.host, server.port, server.protocol) EnterGame.setDefaultServer(selected:getId(), server.port, server.protocol)
EnterGame.setAccountName(server.account) EnterGame.setAccountName(server.account)
EnterGame.setPassword(server.password) EnterGame.setPassword(server.password)
ServerList.hide() ServerList.hide()
@ -59,7 +42,7 @@ function ServerList.select()
end end
function ServerList.add(host, port, protocol, load) function ServerList.add(host, port, protocol, load)
if not load and getServerKey(host) then if not load and servers[host] then
return false, 'Server already exists' return false, 'Server already exists'
elseif host == '' or port == '' then elseif host == '' or port == '' then
return false, 'Required fields are missing' return false, 'Required fields are missing'
@ -68,8 +51,7 @@ function ServerList.add(host, port, protocol, load)
widget:setId(host) widget:setId(host)
if not load then if not load then
servers[table.size(servers)+1] = { servers[host] = {
host = host,
port = port, port = port,
protocol = protocol, protocol = protocol,
account = '', account = '',
@ -88,7 +70,7 @@ function ServerList.add(host, port, protocol, load)
end end
function ServerList.remove(host) function ServerList.remove(host)
servers[getServerKey(host)] = nil servers[host] = nil
end end
function ServerList.destroy() function ServerList.destroy()
@ -113,15 +95,13 @@ function ServerList.hide()
end end
function ServerList.setServerAccount(host, account) function ServerList.setServerAccount(host, account)
local key = getServerKey(host) if servers[host] then
if servers[key] then servers[host].account = account
servers[key].account = account
end end
end end
function ServerList.setServerPassword(host, password) function ServerList.setServerPassword(host, password)
local key = getServerKey(host) if servers[host] then
if servers[key] then servers[host].password = password
servers[key].password = password
end end
end end
Loading…
Cancel
Save