2011-08-29 16:14:21 +02:00
|
|
|
CharacterList = { }
|
|
|
|
|
|
|
|
-- private variables
|
|
|
|
local charactersWindow
|
|
|
|
local loadBox
|
|
|
|
local characterList
|
2012-03-19 18:54:47 +01:00
|
|
|
local errorBox
|
2012-06-06 01:46:36 +02:00
|
|
|
local waitingWindow
|
2012-06-08 22:58:24 +02:00
|
|
|
local updateWaitEvent
|
|
|
|
local resendWaitEvent
|
2011-08-29 16:14:21 +02:00
|
|
|
|
|
|
|
-- private functions
|
2011-08-30 01:20:30 +02:00
|
|
|
local function tryLogin(charInfo, tries)
|
|
|
|
tries = tries or 1
|
|
|
|
|
2013-01-26 23:40:07 +01:00
|
|
|
if tries > 50 then
|
2011-08-30 01:20:30 +02:00
|
|
|
return
|
|
|
|
end
|
2011-08-29 20:38:01 +02:00
|
|
|
|
2012-02-08 22:23:15 +01:00
|
|
|
if g_game.isOnline() then
|
2011-08-30 01:20:30 +02:00
|
|
|
if tries == 1 then
|
2012-09-04 03:16:15 +02:00
|
|
|
g_game.safeLogout()
|
2011-08-30 01:20:30 +02:00
|
|
|
end
|
2013-01-26 23:40:07 +01:00
|
|
|
scheduleEvent(function() tryLogin(charInfo, tries+1) end, 100)
|
2011-08-29 20:38:01 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-01-26 23:40:07 +01:00
|
|
|
CharacterList.hide()
|
2011-08-30 01:20:30 +02:00
|
|
|
|
2013-01-28 23:52:03 +01:00
|
|
|
g_game.loginWorld(G.account, G.password, charInfo.worldName, charInfo.worldHost, charInfo.worldPort, charInfo.characterName)
|
2011-08-29 20:38:01 +02:00
|
|
|
|
2012-04-26 21:54:16 +02:00
|
|
|
loadBox = displayCancelBox(tr('Please wait'), tr('Connecting to game server...'))
|
2012-02-06 20:19:47 +01:00
|
|
|
connect(loadBox, { onCancel = function()
|
|
|
|
loadBox = nil
|
2012-02-08 22:23:15 +01:00
|
|
|
g_game.cancelLogin()
|
2012-02-06 20:19:47 +01:00
|
|
|
CharacterList.show()
|
|
|
|
end })
|
2011-08-29 20:38:01 +02:00
|
|
|
|
|
|
|
-- save last used character
|
2013-01-28 19:33:10 +01:00
|
|
|
g_settings.set('last-used-character', charInfo.characterName)
|
|
|
|
g_settings.set('last-used-world', charInfo.worldName)
|
2011-08-29 20:38:01 +02:00
|
|
|
end
|
|
|
|
|
2012-06-08 22:58:24 +02:00
|
|
|
local function updateWait(timeStart, timeEnd)
|
2012-06-06 01:46:36 +02:00
|
|
|
if waitingWindow then
|
2012-06-08 22:58:24 +02:00
|
|
|
local time = g_clock.seconds()
|
|
|
|
if time <= timeEnd then
|
|
|
|
local percent = ((time - timeStart) / (timeEnd - timeStart)) * 100
|
|
|
|
local timeStr = string.format("%.0f", timeEnd - time)
|
2012-06-06 01:46:36 +02:00
|
|
|
|
|
|
|
local progressBar = waitingWindow:getChildById('progressBar')
|
|
|
|
progressBar:setPercent(percent)
|
|
|
|
|
|
|
|
local label = waitingWindow:getChildById('timeLabel')
|
2012-07-18 01:49:21 +02:00
|
|
|
label:setText(tr('Trying to reconnect in %s seconds.', timeStr))
|
2012-06-06 01:46:36 +02:00
|
|
|
|
2012-06-08 22:58:24 +02:00
|
|
|
updateWaitEvent = scheduleEvent(function() updateWait(timeStart, timeEnd) end, 1000 * progressBar:getPercentPixels() / 100 * (timeEnd - timeStart))
|
|
|
|
return true
|
2012-06-06 01:46:36 +02:00
|
|
|
end
|
|
|
|
end
|
2012-06-08 22:58:24 +02:00
|
|
|
|
|
|
|
if updateWaitEvent then
|
|
|
|
updateWaitEvent:cancel()
|
|
|
|
updateWaitEvent = nil
|
|
|
|
end
|
2012-06-06 01:46:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function resendWait()
|
|
|
|
if waitingWindow then
|
|
|
|
waitingWindow:destroy()
|
|
|
|
waitingWindow = nil
|
|
|
|
|
2012-06-08 22:58:24 +02:00
|
|
|
if updateWaitEvent then
|
|
|
|
updateWaitEvent:cancel()
|
|
|
|
updateWaitEvent = nil
|
|
|
|
end
|
|
|
|
|
2012-06-06 01:46:36 +02:00
|
|
|
if charactersWindow then
|
2012-08-31 06:56:10 +02:00
|
|
|
local selected = characterList:getFocusedChild()
|
2012-06-06 01:46:36 +02:00
|
|
|
if selected then
|
|
|
|
local charInfo = { worldHost = selected.worldHost,
|
|
|
|
worldPort = selected.worldPort,
|
2013-01-21 22:36:53 +01:00
|
|
|
worldName = selected.worldName,
|
2012-06-06 01:46:36 +02:00
|
|
|
characterName = selected.characterName }
|
|
|
|
tryLogin(charInfo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function onLoginWait(message, time)
|
2012-06-08 22:58:24 +02:00
|
|
|
CharacterList.destroyLoadBox()
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
waitingWindow = g_ui.displayUI('waitinglist')
|
2012-06-06 01:46:36 +02:00
|
|
|
|
|
|
|
local label = waitingWindow:getChildById('infoLabel')
|
|
|
|
label:setText(message)
|
|
|
|
|
2012-06-08 22:58:24 +02:00
|
|
|
updateWaitEvent = scheduleEvent(function() updateWait(g_clock.seconds(), g_clock.seconds() + time) end, 0)
|
|
|
|
resendWaitEvent = scheduleEvent(resendWait, time * 1000)
|
2012-06-06 01:46:36 +02:00
|
|
|
end
|
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
function onGameLoginError(message)
|
|
|
|
CharacterList.destroyLoadBox()
|
2012-04-26 21:54:16 +02:00
|
|
|
errorBox = displayErrorBox(tr("Login Error"), message)
|
2012-03-19 18:54:47 +01:00
|
|
|
errorBox.onOk = function()
|
|
|
|
errorBox = nil
|
|
|
|
CharacterList.showAgain()
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
end
|
|
|
|
|
2012-07-10 14:32:13 +02:00
|
|
|
function onGameConnectionError(message, code)
|
2012-02-20 03:27:08 +01:00
|
|
|
CharacterList.destroyLoadBox()
|
2013-02-18 17:16:22 +01:00
|
|
|
errorBox = displayErrorBox(tr("Connection Error"), message)
|
2012-03-19 18:54:47 +01:00
|
|
|
errorBox.onOk = function()
|
|
|
|
errorBox = nil
|
|
|
|
CharacterList.showAgain()
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
end
|
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
function onGameUpdateNeeded(signature)
|
|
|
|
CharacterList.destroyLoadBox()
|
|
|
|
errorBox = displayErrorBox(tr("Update needed"), tr('Enter with your account again to update your client.'))
|
|
|
|
errorBox.onOk = function()
|
|
|
|
errorBox = nil
|
|
|
|
CharacterList.showAgain()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
-- public functions
|
2012-02-20 03:27:08 +01:00
|
|
|
function CharacterList.init()
|
|
|
|
connect(g_game, { onLoginError = onGameLoginError })
|
2013-01-21 22:36:53 +01:00
|
|
|
connect(g_game, { onUpdateNeeded = onGameUpdateNeeded })
|
2012-02-20 03:27:08 +01:00
|
|
|
connect(g_game, { onConnectionError = onGameConnectionError })
|
|
|
|
connect(g_game, { onGameStart = CharacterList.destroyLoadBox })
|
2012-06-06 01:46:36 +02:00
|
|
|
connect(g_game, { onLoginWait = onLoginWait })
|
2012-03-18 21:59:00 +01:00
|
|
|
connect(g_game, { onGameEnd = CharacterList.showAgain })
|
2012-06-04 14:38:15 +02:00
|
|
|
|
|
|
|
if G.characters then
|
2012-08-31 06:56:10 +02:00
|
|
|
CharacterList.create(G.characters, G.characterAccount)
|
2012-06-04 14:38:15 +02:00
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
end
|
|
|
|
|
2012-02-05 23:42:35 +01:00
|
|
|
function CharacterList.terminate()
|
2012-03-18 14:34:39 +01:00
|
|
|
disconnect(g_game, { onLoginError = onGameLoginError })
|
2013-01-21 22:36:53 +01:00
|
|
|
disconnect(g_game, { onUpdateNeeded = onGameUpdateNeeded })
|
2012-03-18 14:34:39 +01:00
|
|
|
disconnect(g_game, { onConnectionError = onGameConnectionError })
|
|
|
|
disconnect(g_game, { onGameStart = CharacterList.destroyLoadBox })
|
2012-06-06 01:46:36 +02:00
|
|
|
disconnect(g_game, { onLoginWait = onLoginWait })
|
2012-03-18 21:59:00 +01:00
|
|
|
disconnect(g_game, { onGameEnd = CharacterList.showAgain })
|
2012-09-04 05:40:47 +02:00
|
|
|
|
|
|
|
if charactersWindow then
|
|
|
|
characterList = nil
|
|
|
|
charactersWindow:destroy()
|
|
|
|
charactersWindow = nil
|
|
|
|
end
|
2012-08-31 06:56:10 +02:00
|
|
|
|
2012-02-05 23:42:35 +01:00
|
|
|
if loadBox then
|
2012-02-20 03:27:08 +01:00
|
|
|
g_game.cancelLogin()
|
2012-02-05 23:42:35 +01:00
|
|
|
loadBox:destroy()
|
|
|
|
loadBox = nil
|
|
|
|
end
|
2012-06-08 22:58:24 +02:00
|
|
|
|
|
|
|
if waitingWindow then
|
|
|
|
waitingWindow:destroy()
|
|
|
|
waitingWindow = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if updateWaitEvent then
|
|
|
|
updateWaitEvent:cancel()
|
|
|
|
updateWaitEvent = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if resendWaitEvent then
|
|
|
|
resendWaitEvent:cancel()
|
|
|
|
resendWaitEvent = nil
|
|
|
|
end
|
|
|
|
|
2012-02-07 01:41:53 +01:00
|
|
|
CharacterList = nil
|
2012-02-05 23:42:35 +01:00
|
|
|
end
|
|
|
|
|
2012-08-31 06:56:10 +02:00
|
|
|
function CharacterList.create(characters, account, otui)
|
2013-01-18 23:39:11 +01:00
|
|
|
if not otui then otui = 'characterlist' end
|
2012-09-15 05:49:33 +02:00
|
|
|
|
2012-09-04 05:40:47 +02:00
|
|
|
if charactersWindow then
|
|
|
|
charactersWindow:destroy()
|
|
|
|
end
|
2012-08-31 06:56:10 +02:00
|
|
|
|
|
|
|
charactersWindow = g_ui.displayUI(otui)
|
|
|
|
characterList = charactersWindow:getChildById('characters')
|
|
|
|
|
|
|
|
-- characters
|
2012-06-04 14:38:15 +02:00
|
|
|
G.characters = characters
|
2012-08-31 06:56:10 +02:00
|
|
|
G.characterAccount = account
|
2012-06-04 14:38:15 +02:00
|
|
|
|
2012-03-18 14:34:39 +01:00
|
|
|
characterList:destroyChildren()
|
2011-08-29 16:14:21 +02:00
|
|
|
local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel')
|
|
|
|
|
2011-12-09 16:01:04 +01:00
|
|
|
local focusLabel
|
2011-08-29 16:14:21 +02:00
|
|
|
for i,characterInfo in ipairs(characters) do
|
2012-08-31 06:56:10 +02:00
|
|
|
local widget = g_ui.createWidget('CharacterWidget', characterList)
|
|
|
|
for key,value in pairs(characterInfo) do
|
|
|
|
local subWidget = widget:getChildById(key)
|
|
|
|
if subWidget then
|
|
|
|
if key == 'outfit' then -- it's an exception
|
|
|
|
subWidget:setOutfit(value)
|
|
|
|
else
|
|
|
|
local text = value
|
|
|
|
if subWidget.baseText and subWidget.baseTranslate then
|
|
|
|
text = tr(subWidget.baseText, text)
|
|
|
|
elseif subWidget.baseText then
|
|
|
|
text = string.format(subWidget.baseText, text)
|
|
|
|
end
|
|
|
|
subWidget:setText(text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-29 16:14:21 +02:00
|
|
|
|
2012-08-31 06:56:10 +02:00
|
|
|
-- these are used by login
|
|
|
|
widget.characterName = characterInfo.name
|
2013-01-21 22:36:53 +01:00
|
|
|
widget.worldName = characterInfo.worldName
|
2012-08-31 06:56:10 +02:00
|
|
|
widget.worldHost = characterInfo.worldIp
|
|
|
|
widget.worldPort = characterInfo.worldPort
|
2012-02-05 23:42:35 +01:00
|
|
|
|
2012-08-31 06:56:10 +02:00
|
|
|
connect(widget, { onDoubleClick = function () CharacterList.doLogin() return true end } )
|
2011-08-29 16:14:21 +02:00
|
|
|
|
2013-01-28 19:33:10 +01:00
|
|
|
if i == 1 or (g_settings.get('last-used-character') == widget.characterName and g_settings.get('last-used-world') == widget.worldName) then
|
2012-08-31 06:56:10 +02:00
|
|
|
focusLabel = widget
|
2011-08-29 16:14:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-28 19:33:10 +01:00
|
|
|
if focusLabel then
|
|
|
|
characterList:focusChild(focusLabel, KeyboardFocusReason)
|
|
|
|
addEvent(function() characterList:ensureChildVisible(focusLabel) end)
|
|
|
|
end
|
2011-12-09 16:01:04 +01:00
|
|
|
|
2012-08-31 06:56:10 +02:00
|
|
|
-- account
|
2012-12-01 23:15:22 +01:00
|
|
|
if account.premDays > 0 and account.premDays < 65535 then
|
2013-01-18 16:48:48 +01:00
|
|
|
accountStatusLabel:setText(tr("Premium Account (%s) days left", account.premDays))
|
2012-12-01 23:15:22 +01:00
|
|
|
elseif account.premDays >= 65535 then
|
2013-01-18 16:48:48 +01:00
|
|
|
accountStatusLabel:setText(tr("Lifetime Premium Account"))
|
2012-08-07 22:55:05 +02:00
|
|
|
else
|
2013-01-18 16:48:48 +01:00
|
|
|
accountStatusLabel:setText(tr('Free Account'))
|
|
|
|
end
|
|
|
|
|
|
|
|
if account.premDays > 0 and account.premDays <= 7 then
|
|
|
|
accountStatusLabel:setOn(true)
|
|
|
|
else
|
|
|
|
accountStatusLabel:setOn(false)
|
2011-08-29 16:14:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-02 04:02:56 +01:00
|
|
|
function CharacterList.destroy()
|
2012-12-30 07:14:49 +01:00
|
|
|
CharacterList.hide(true)
|
2012-09-04 05:40:47 +02:00
|
|
|
|
2012-08-31 06:56:10 +02:00
|
|
|
if charactersWindow then
|
|
|
|
characterList = nil
|
|
|
|
charactersWindow:destroy()
|
|
|
|
charactersWindow = nil
|
|
|
|
end
|
2011-11-02 02:55:36 +01:00
|
|
|
end
|
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
function CharacterList.show()
|
2013-01-22 22:48:25 +01:00
|
|
|
if loadBox or errorBox or not charactersWindow then return end
|
|
|
|
charactersWindow:show()
|
|
|
|
charactersWindow:raise()
|
|
|
|
charactersWindow:focus()
|
2011-08-29 16:14:21 +02:00
|
|
|
end
|
|
|
|
|
2012-12-30 07:14:49 +01:00
|
|
|
function CharacterList.hide(showLogin)
|
|
|
|
showLogin = showLogin or false
|
2012-08-31 06:56:10 +02:00
|
|
|
charactersWindow:hide()
|
2012-12-30 07:14:49 +01:00
|
|
|
|
|
|
|
if showLogin and EnterGame and not g_game.isOnline() then
|
|
|
|
EnterGame.show()
|
|
|
|
end
|
2012-08-31 06:56:10 +02:00
|
|
|
end
|
|
|
|
|
2012-03-18 21:59:00 +01:00
|
|
|
function CharacterList.showAgain()
|
2012-09-02 01:16:17 +02:00
|
|
|
if characterList and characterList:hasChildren() then
|
2012-03-18 21:59:00 +01:00
|
|
|
CharacterList.show()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-07 01:08:08 +01:00
|
|
|
function CharacterList.isVisible()
|
|
|
|
if charactersWindow and charactersWindow:isVisible() then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
function CharacterList.doLogin()
|
2012-08-31 06:56:10 +02:00
|
|
|
local selected = characterList:getFocusedChild()
|
2011-08-29 16:14:21 +02:00
|
|
|
if selected then
|
2011-08-29 20:38:01 +02:00
|
|
|
local charInfo = { worldHost = selected.worldHost,
|
|
|
|
worldPort = selected.worldPort,
|
2013-01-21 22:36:53 +01:00
|
|
|
worldName = selected.worldName,
|
2011-08-29 20:38:01 +02:00
|
|
|
characterName = selected.characterName }
|
2012-09-04 05:40:47 +02:00
|
|
|
charactersWindow:hide()
|
2011-08-29 20:38:01 +02:00
|
|
|
tryLogin(charInfo)
|
2011-08-29 16:14:21 +02:00
|
|
|
else
|
2012-04-26 21:54:16 +02:00
|
|
|
displayErrorBox(tr('Error'), tr('You must select a character to login!'))
|
2011-08-29 16:14:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterList.destroyLoadBox()
|
|
|
|
if loadBox then
|
|
|
|
loadBox:destroy()
|
|
|
|
loadBox = nil
|
|
|
|
end
|
|
|
|
end
|
2012-06-06 01:46:36 +02:00
|
|
|
|
|
|
|
function CharacterList.cancelWait()
|
|
|
|
if waitingWindow then
|
|
|
|
waitingWindow:destroy()
|
|
|
|
waitingWindow = nil
|
|
|
|
end
|
2012-06-08 22:58:24 +02:00
|
|
|
|
|
|
|
if updateWaitEvent then
|
|
|
|
updateWaitEvent:cancel()
|
|
|
|
updateWaitEvent = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if resendWaitEvent then
|
|
|
|
resendWaitEvent:cancel()
|
|
|
|
resendWaitEvent = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
CharacterList.destroyLoadBox()
|
|
|
|
CharacterList.showAgain()
|
2012-06-06 01:46:36 +02:00
|
|
|
end
|