tibia-client/modules/client_entergame/entergame.lua

148 lines
4.4 KiB
Lua
Raw Normal View History

EnterGame = { }
2011-08-26 20:00:22 +02:00
-- private variables
local loadBox
2011-11-02 04:02:56 +01:00
local enterGame
2011-11-02 02:55:36 +01:00
local motdNumber
local motdMessage
2012-01-07 18:36:58 +01:00
local motdButton
local enterGameButton
-- private functions
2011-11-16 22:00:40 +01:00
local function clearAccountFields()
enterGame:getChildById('accountNameLineEdit'):clearText()
enterGame:getChildById('accountPasswordLineEdit'):clearText()
enterGame:getChildById('accountNameLineEdit'):focus()
2012-01-06 10:35:48 +01:00
Settings.remove('account')
Settings.remove('password')
2011-11-16 22:00:40 +01:00
end
local function onError(protocol, message, connectionError)
loadBox:destroy()
2012-02-06 20:19:47 +01:00
loadBox = nil
if not connectionError then
clearAccountFields()
end
local errorBox = displayErrorBox('Login Error', message)
2012-02-06 20:19:47 +01:00
connect(errorBox, { onOk = EnterGame.show })
2011-08-26 20:00:22 +02:00
end
local function onMotd(protocol, motd)
2011-12-07 01:31:55 +01:00
motdNumber = tonumber(motd:sub(0, motd:find("\n")))
motdMessage = motd:sub(motd:find("\n") + 1, #motd)
2012-01-07 18:36:58 +01:00
motdButton:show()
2011-08-26 20:44:18 +02:00
end
local function onCharacterList(protocol, characters, premDays)
if enterGame:getChildById('rememberPasswordBox'):isChecked() then
2012-01-06 10:35:48 +01:00
Settings.set('account', EnterGame.account)
Settings.set('password', EnterGame.password)
Settings.set('autologin', enterGame:getChildById('autoLoginBox'):isChecked())
else
2011-11-16 22:00:40 +01:00
clearAccountFields()
end
2011-11-02 02:55:36 +01:00
loadBox:destroy()
2012-02-06 20:19:47 +01:00
loadBox = nil
CharacterList.create(characters, premDays)
2011-11-02 02:55:36 +01:00
2012-01-06 10:35:48 +01:00
local lastMotdNumber = Settings.getNumber("motd")
2011-11-02 02:55:36 +01:00
if motdNumber and motdNumber ~= lastMotdNumber then
2012-01-06 10:35:48 +01:00
Settings.set("motd", motdNumber)
2011-11-02 02:55:36 +01:00
local motdBox = displayInfoBox("Message of the day", motdMessage)
2012-02-06 20:19:47 +01:00
connect(motdBox, { onOk = CharacterList.show })
2011-10-31 18:56:00 +01:00
CharacterList.hide()
end
end
-- public functions
2012-01-07 18:36:58 +01:00
function EnterGame.init()
2012-01-07 21:00:07 +01:00
enterGameButton = TopMenu.addButton('enterGameButton', 'Login (Ctrl + G)', '/core_styles/icons/login.png', EnterGame.openWindow)
Keyboard.bindKeyDown('Ctrl+G', EnterGame.openWindow)
2012-01-07 18:36:58 +01:00
motdButton = TopMenu.addButton('motdButton', 'Message of the day', '/core_styles/icons/motd.png', EnterGame.displayMotd)
motdButton:hide()
enterGame = displayUI('entergame.otui')
2012-01-06 10:35:48 +01:00
local account = Settings.get('account')
local password = Settings.get('password')
local host = Settings.get('host')
2012-01-07 01:34:40 +01:00
local port = Settings.get('port')
2012-01-07 01:18:08 +01:00
local autologin = Settings.getBoolean('autologin')
enterGame:getChildById('accountNameLineEdit'):setText(account)
enterGame:getChildById('accountPasswordLineEdit'):setText(password)
enterGame:getChildById('serverHostLineEdit'):setText(host)
enterGame:getChildById('serverPortLineEdit'):setText(port)
enterGame:getChildById('autoLoginBox'):setChecked(autologin)
enterGame:getChildById('rememberPasswordBox'):setChecked(#account > 0)
2011-11-16 22:00:40 +01:00
enterGame:getChildById('accountNameLineEdit'):focus()
2012-02-06 20:19:47 +01:00
-- only open entergame when app starts
if not g_app.isRunning() then
if #account > 0 and autologin then
addEvent(EnterGame.doLogin)
end
else
enterGame:hide()
end
2011-11-02 02:55:36 +01:00
end
2012-01-07 18:36:58 +01:00
function EnterGame.terminate()
Keyboard.unbindKeyDown('Ctrl+G')
2011-11-02 04:02:56 +01:00
enterGame:destroy()
enterGame = nil
2012-01-07 18:36:58 +01:00
enterGameButton:destroy()
enterGameButton = nil
motdButton:destroy()
motdButton = nil
2012-02-07 01:41:53 +01:00
EnterGame = nil
2011-08-26 20:44:18 +02:00
end
2011-08-26 20:00:22 +02:00
2011-11-02 04:02:56 +01:00
function EnterGame.show()
enterGame:show()
enterGame:focus()
2011-11-02 04:02:56 +01:00
end
function EnterGame.hide()
enterGame:hide()
end
2011-07-17 08:56:57 +02:00
2012-01-07 18:36:58 +01:00
function EnterGame.openWindow()
2012-02-08 22:23:15 +01:00
if g_game.isOnline() then
2012-01-07 18:36:58 +01:00
CharacterList.show()
elseif not CharacterList.isVisible() then
EnterGame.show()
end
end
function EnterGame.doLogin()
2011-11-02 04:02:56 +01:00
EnterGame.account = enterGame:getChildById('accountNameLineEdit'):getText()
EnterGame.password = enterGame:getChildById('accountPasswordLineEdit'):getText()
EnterGame.host = enterGame:getChildById('serverHostLineEdit'):getText()
2012-01-07 01:18:08 +01:00
EnterGame.port = tonumber(enterGame:getChildById('serverPortLineEdit'):getText())
2011-11-02 02:55:36 +01:00
EnterGame.hide()
2011-07-17 08:56:57 +02:00
2012-01-06 10:35:48 +01:00
Settings.set('host', EnterGame.host)
Settings.set('port', EnterGame.port)
local protocolLogin = ProtocolLogin.create()
protocolLogin.onError = onError
protocolLogin.onMotd = onMotd
protocolLogin.onCharacterList = onCharacterList
2011-07-17 08:56:57 +02:00
loadBox = displayCancelBox('Please wait', 'Connecting to login server...')
2012-02-06 20:19:47 +01:00
connect(loadBox, { onCancel = function(msgbox)
loadBox = nil
protocolLogin:cancelLogin()
EnterGame.show()
end })
2011-07-17 08:56:57 +02:00
protocolLogin:login(EnterGame.host, EnterGame.port, EnterGame.account, EnterGame.password)
2011-07-17 08:56:57 +02:00
end
2011-11-16 19:08:42 +01:00
function EnterGame.displayMotd()
2012-01-07 18:36:58 +01:00
displayInfoBox('Message of the day', motdMessage)
2011-11-16 19:08:42 +01:00
end