otclient-redemption/modules/client_serverlist/serverlist.lua

161 lines
3.9 KiB
Lua
Raw Permalink Normal View History

ServerList = {}
-- private variables
local serverListWindow = nil
local serverTextList = nil
local removeWindow = nil
local servers = {}
-- public functions
function ServerList.init()
2021-07-19 19:24:59 -03:00
serverListWindow = g_ui.displayUI('serverlist')
serverTextList = serverListWindow:getChildById('serverList')
local processedServers = {}
2021-07-19 19:24:59 -03:00
servers = g_settings.getNode('ServerList') or {}
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
if servers then
ServerList.load()
end
end
function ServerList.terminate()
2021-07-19 19:24:59 -03:00
ServerList.destroy()
2021-07-19 19:24:59 -03:00
g_settings.setNode('ServerList', servers)
2021-07-19 19:24:59 -03:00
ServerList = nil
serverListWindow = nil
serverTextList = nil
end
function ServerList.load()
for host, server in pairs(servers) do
2024-03-07 13:04:04 -03:00
ServerList.add(host, server.port, server.protocol, httpLogin, true)
end
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
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
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
end
function ServerList.remove(widget)
2021-07-19 19:24:59 -03:00
local host = widget:getId()
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)
end
function ServerList.destroy()
2021-07-19 19:24:59 -03:00
if serverListWindow then
serverTextList = nil
serverListWindow:destroy()
serverListWindow = nil
end
end
function ServerList.show()
if g_game.isOnline() then
return
end
2021-07-19 19:24:59 -03:00
serverListWindow:show()
serverListWindow:raise()
serverListWindow:focus()
end
function ServerList.hide()
serverListWindow:hide()
end
function ServerList.setServerAccount(host, account)
if servers[host] then
servers[host].account = account
end
end
function ServerList.setServerPassword(host, password)
if servers[host] then
servers[host].password = password
end
end