tibia-client/modules/entergame/characterlist.lua

122 lines
3.2 KiB
Lua
Raw Normal View History

CharacterList = { }
-- private variables
local charactersWindow
local loadBox
local characterList
-- private functions
local function onCharactersWindowKeyPress(self, keyCode, keyChar, keyboardModifiers)
if keyboardModifiers == KeyboardNoModifier then
if keyCode == KeyUp or keyCode == KeyTab then
characterList:focusPreviousChild(ActiveFocusReason)
elseif keyCode == KeyDown then
characterList:focusNextChild(ActiveFocusReason)
end
end
return false
end
2011-08-30 01:20:30 +02:00
local function tryLogin(charInfo, tries)
tries = tries or 1
if tries > 4 then
CharacterList.destroyLoadBox()
displayErrorBox('Error', 'Could not logout.')
return
end
2011-08-29 20:38:01 +02:00
if Game.isOnline() then
2011-08-30 01:20:30 +02:00
Game.logout(false)
if tries == 1 then
loadBox = displayCancelBox('Please wait', 'Loggin out...')
end
scheduleEvent(function() tryLogin(charInfo, tries+1) end, 250)
2011-08-29 20:38:01 +02:00
return
end
2011-08-30 01:20:30 +02:00
CharacterList.destroyLoadBox()
2011-08-29 20:38:01 +02:00
Game.loginWorld(EnterGame.account, EnterGame.password, charInfo.worldHost, charInfo.worldPort, charInfo.characterName)
loadBox = displayCancelBox('Please wait', 'Connecting to game server...')
function loadBox.onCancel()
2011-08-30 01:40:56 +02:00
loadBox = nil
2011-08-29 20:38:01 +02:00
Game.cancelLogin()
CharacterList.show()
end
-- save last used character
Configs.set('lastUsedCharacter', charInfo.characterName)
end
-- public functions
function CharacterList.create(characters, premDays)
if charactersWindow then
charactersWindow:destroy()
end
2011-11-02 04:02:56 +01:00
charactersWindow = UI.loadAndDisplay('/entergame/characterlist.otui')
characterList = charactersWindow:getChildById('characterList')
local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel')
charactersWindow.onKeyPress = onCharactersWindowKeyPress
for i,characterInfo in ipairs(characters) do
local characterName = characterInfo[1]
local worldName = characterInfo[2]
local worldHost = characterInfo[3]
local worldIp = characterInfo[4]
local label = UILabel.create()
characterList:addChild(label)
label:setText(characterName .. ' (' .. worldName .. ')')
label:setStyle('CharacterListLabel')
label.characterName = characterName
label.worldHost = worldHost
label.worldPort = worldIp
2011-10-31 18:56:00 +01:00
if i == 1 or Configs.get('lastUsedCharacter') == characterName then
characterList:focusChild(label, ActiveFocusReason)
end
end
if premDays > 0 then
accountStatusLabel:setText("Account Status:\nPremium Account (" .. premDays .. ' days left)')
end
end
function CharacterList.hide()
charactersWindow:hide()
end
2011-11-02 04:02:56 +01:00
function CharacterList.destroy()
2011-11-02 02:55:36 +01:00
CharacterList.hide()
EnterGame.show()
end
function CharacterList.show()
2011-08-30 01:20:30 +02:00
if not loadBox then
charactersWindow:show()
end
end
function CharacterList.doLogin()
local selected = charactersWindow:getChildById('characterList'):getFocusedChild()
if selected then
2011-08-29 20:38:01 +02:00
local charInfo = { worldHost = selected.worldHost,
worldPort = selected.worldPort,
characterName = selected.characterName }
2011-08-30 01:20:30 +02:00
CharacterList.hide()
2011-08-29 20:38:01 +02:00
tryLogin(charInfo)
else
displayErrorBox('Error', 'You must select a character to login!')
end
end
function CharacterList.destroyLoadBox()
if loadBox then
loadBox:destroy()
loadBox = nil
end
end