Guest User

Untitled

a guest
Aug 14th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.24 KB | None | 0 0
  1. CharacterList = { }
  2. LESELECTED = 1;
  3.  
  4. -- private variables
  5. local charactersWindow
  6. local loadBox
  7. local characterList
  8. local errorBox
  9. local waitingWindow
  10. local updateWaitEvent
  11. local resendWaitEvent
  12.  
  13. -- private functions
  14. local function tryLogin(charInfo, tries)
  15.   tries = tries or 1
  16.  
  17.   if tries > 50 then
  18.     return
  19.   end
  20.  
  21.   if g_game.isOnline() then
  22.     if tries == 1 then
  23.       g_game.safeLogout()
  24.     end
  25.     scheduleEvent(function() tryLogin(charInfo, tries+1) end, 100)
  26.     return
  27.   end
  28.  
  29.   CharacterList.hide()
  30.  
  31.   g_game.loginWorld(G.account, G.password, charInfo.worldName, charInfo.worldHost, charInfo.worldPort, charInfo.characterName, G.authenticatorToken, G.sessionKey)
  32.  
  33.   loadBox = displayCancelBox(tr('Please wait'), tr('Connecting to game server...'))
  34.   connect(loadBox, { onCancel = function()
  35.                                   loadBox = nil
  36.                                   g_game.cancelLogin()
  37.                                   CharacterList.show()
  38.                                 end })
  39.  
  40.   -- save last used character
  41.   g_settings.set('last-used-character', charInfo.characterName)
  42.   g_settings.set('last-used-world', charInfo.worldName)
  43. end
  44.  
  45. local function updateWait(timeStart, timeEnd)
  46.   if waitingWindow then
  47.     local time = g_clock.seconds()
  48.     if time <= timeEnd then
  49.       local percent = ((time - timeStart) / (timeEnd - timeStart)) * 100
  50.       local timeStr = string.format("%.0f", timeEnd - time)
  51.  
  52.       local progressBar = waitingWindow:getChildById('progressBar')
  53.       progressBar:setPercent(percent)
  54.  
  55.       local label = waitingWindow:getChildById('timeLabel')
  56.       label:setText(tr('Trying to reconnect in %s seconds.', timeStr))
  57.  
  58.       updateWaitEvent = scheduleEvent(function() updateWait(timeStart, timeEnd) end, 1000 * progressBar:getPercentPixels() / 100 * (timeEnd - timeStart))
  59.       return true
  60.     end
  61.   end
  62.  
  63.   if updateWaitEvent then
  64.     updateWaitEvent:cancel()
  65.     updateWaitEvent = nil
  66.   end
  67. end
  68.  
  69. local function resendWait()
  70.   if waitingWindow then
  71.     waitingWindow:destroy()
  72.     waitingWindow = nil
  73.  
  74.     if updateWaitEvent then
  75.       updateWaitEvent:cancel()
  76.       updateWaitEvent = nil
  77.     end
  78.  
  79.     if charactersWindow then
  80.       local selected = characterList:getFocusedChild()
  81.       if selected then
  82.         local charInfo = { worldHost = selected.worldHost,
  83.                            worldPort = selected.worldPort,
  84.                            worldName = selected.worldName,
  85.                            outfit = selected.outfit,
  86.                            characterName = selected.characterName }
  87.         tryLogin(charInfo)
  88.       end
  89.     end
  90.   end
  91. end
  92.  
  93. local function onLoginWait(message, time)
  94.   CharacterList.destroyLoadBox()
  95.  
  96.   waitingWindow = g_ui.displayUI('waitinglist')
  97.  
  98.   local label = waitingWindow:getChildById('infoLabel')
  99.   label:setText(message)
  100.  
  101.   updateWaitEvent = scheduleEvent(function() updateWait(g_clock.seconds(), g_clock.seconds() + time) end, 0)
  102.   resendWaitEvent = scheduleEvent(resendWait, time * 1000)
  103. end
  104.  
  105. function onGameLoginError(message)
  106.   CharacterList.destroyLoadBox()
  107.   errorBox = displayErrorBox(tr("Login Error"), message)
  108.   errorBox.onOk = function()
  109.     errorBox = nil
  110.     CharacterList.showAgain()
  111.   end
  112. end
  113.  
  114. function onGameLoginToken(unknown)
  115.   CharacterList.destroyLoadBox()
  116.   -- TODO: make it possible to enter a new token here / prompt token
  117.   errorBox = displayErrorBox(tr("Two-Factor Authentification"), 'A new authentification token is required.\nPlease login again.')
  118.   errorBox.onOk = function()
  119.     errorBox = nil
  120.     EnterGame.show()
  121.   end
  122. end
  123.  
  124. function onGameConnectionError(message, code)
  125.   CharacterList.destroyLoadBox()
  126.   local text = translateNetworkError(code, g_game.getProtocolGame() and g_game.getProtocolGame():isConnecting(), message)
  127.   errorBox = displayErrorBox(tr("Connection Error"), text)
  128.   errorBox.onOk = function()
  129.     errorBox = nil
  130.     CharacterList.showAgain()
  131.   end
  132. end
  133.  
  134. function onGameUpdateNeeded(signature)
  135.   CharacterList.destroyLoadBox()
  136.   errorBox = displayErrorBox(tr("Update needed"), tr('Enter with your account again to update your client.'))
  137.   errorBox.onOk = function()
  138.     errorBox = nil
  139.     CharacterList.showAgain()
  140.   end
  141. end
  142.  
  143. -- public functions
  144. function CharacterList.init()
  145.   connect(g_game, { onLoginError = onGameLoginError })
  146.   connect(g_game, { onLoginToken = onGameLoginToken })
  147.   connect(g_game, { onUpdateNeeded = onGameUpdateNeeded })
  148.   connect(g_game, { onConnectionError = onGameConnectionError })
  149.   connect(g_game, { onGameStart = CharacterList.destroyLoadBox })
  150.   connect(g_game, { onLoginWait = onLoginWait })
  151.   connect(g_game, { onGameEnd = CharacterList.showAgain })
  152.  
  153.   if G.characters then
  154.     CharacterList.create(G.characters, G.characterAccount)
  155.   end
  156. end
  157.  
  158. function CharacterList.terminate()
  159.   disconnect(g_game, { onLoginError = onGameLoginError })
  160.   disconnect(g_game, { onLoginToken = onGameLoginToken })
  161.   disconnect(g_game, { onUpdateNeeded = onGameUpdateNeeded })
  162.   disconnect(g_game, { onConnectionError = onGameConnectionError })
  163.   disconnect(g_game, { onGameStart = CharacterList.destroyLoadBox })
  164.   disconnect(g_game, { onLoginWait = onLoginWait })
  165.   disconnect(g_game, { onGameEnd = CharacterList.showAgain })
  166.  
  167.   if charactersWindow then
  168.     characterList = nil
  169.     charactersWindow:destroy()
  170.     charactersWindow = nil
  171.   end
  172.  
  173.   if loadBox then
  174.     g_game.cancelLogin()
  175.     loadBox:destroy()
  176.     loadBox = nil
  177.   end
  178.  
  179.   if waitingWindow then
  180.     waitingWindow:destroy()
  181.     waitingWindow = nil
  182.   end
  183.  
  184.   if updateWaitEvent then
  185.     updateWaitEvent:cancel()
  186.     updateWaitEvent = nil
  187.   end
  188.  
  189.   if resendWaitEvent then
  190.     resendWaitEvent:cancel()
  191.     resendWaitEvent = nil
  192.   end
  193.  
  194.   CharacterList = nil
  195. end
  196.  
  197. function CharacterList.create(characters, account, otui)
  198.   if not otui then otui = 'characterlist' end
  199.  
  200.   if charactersWindow then
  201.     charactersWindow:destroy()
  202.   end
  203.  
  204.   charactersWindow = g_ui.displayUI(otui)
  205.   characterList = charactersWindow:getChildById('characters')
  206.  
  207.   -- characters
  208.   G.characters = characters
  209.   G.characterAccount = account
  210.  
  211.   characterList:destroyChildren()
  212.   local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel')
  213.  
  214.   local focusLabel
  215.   for i,characterInfo in ipairs(characters) do
  216.     local widget = g_ui.createWidget('CharacterWidget', characterList)
  217.           widget:recursiveGetChildById('health')
  218.           widget:recursiveGetChildById('mana')
  219.           widget:recursiveGetChildById('level')
  220.     for key,value in pairs(characterInfo) do
  221.     print(widget, key, value)
  222.       local subWidget = widget:getChildById(key)
  223.       if subWidget then
  224.         if key == 'outfit' then -- it's an exception
  225.           subWidget:setOutfit(value)
  226.         else
  227.           local text = (value:len() > 13 and (value:sub(1,10)..'...') or value);
  228.           if subWidget.baseText and subWidget.baseTranslate then
  229.             text = tr(subWidget.baseText, text)
  230.           elseif subWidget.baseText then
  231.             text = string.format(subWidget.baseText, text)
  232.           end
  233.           subWidget:setText(text)
  234.         end
  235.       end
  236.     end
  237.  
  238.     -- these are used by login
  239.     widget.characterName = characterInfo.name
  240.     widget.worldName = characterInfo.worldName
  241.     widget.worldHost = characterInfo.worldIp
  242.     widget.worldPort = characterInfo.worldPort
  243.    
  244.     widget.looktyp      =   characterInfo.looktyp
  245.     widget.lookhead     =   characterInfo.lookhead
  246.     widget.lookbody     =   characterInfo.lookbody
  247.     widget.looklegs     =   characterInfo.looklegs
  248.     widget.lookfeet     =   characterInfo.lookfeet
  249.     widget.lookaddons   =   characterInfo.lookaddons
  250.     widget.name         =   characterInfo.name
  251.     widget.level        =   characterInfo.level
  252.     widget.idmax            =   i
  253.     connect(widget, { onDoubleClick = function () CharacterList.doLogin() return true end } )
  254.  
  255.     if i == 1 or (g_settings.get('last-used-character') == widget.characterName and g_settings.get('last-used-world') == widget.worldName) then
  256.       focusLabel = widget
  257.       CharacterList.selecting(0,focusLabel,0)
  258.     end
  259.   end
  260.  
  261.   if focusLabel then
  262.     characterList:focusChild(focusLabel, KeyboardFocusReason)
  263.     addEvent(function() characterList:ensureChildVisible(focusLabel) end)
  264.   end
  265.  
  266.   -- account
  267.   if account.premDays > 0 and account.premDays < 65535 then
  268.     accountStatusLabel:setText(tr("Premium Account (%s) days left", account.premDays))
  269.   elseif account.premDays >= 65535 then
  270.     accountStatusLabel:setText(tr("Lifetime Premium Account"))
  271.   else
  272.     accountStatusLabel:setText(tr('Free Account'))
  273.   end
  274.  
  275.   if account.premDays > 0 and account.premDays <= 7 then
  276.     accountStatusLabel:setOn(true)
  277.   else
  278.     accountStatusLabel:setOn(false)
  279.   end
  280.   connect(characterList, { onChildFocusChange =  function (self, focusedChild,old) CharacterList.selecting(self,focusedChild,old) end } )
  281.  
  282.  
  283. end
  284.  
  285. function CharacterList.selecting(self, focusedChild, old)
  286.     local outfitCreatureBox = charactersWindow:getChildById('outfitCreatureBox')
  287.         charactersWindow:getChildById('namelabel'):setText(tr("Name")..": "..focusedChild.name)
  288.         charactersWindow:getChildById('levellabel'):setText(tr("Level")..": "..focusedChild.level)
  289.       if outfitCreatureBox then
  290.         outfitCreatureBox:setOutfit({type=focusedChild.looktyp,head=focusedChild.lookhead,body=focusedChild.lookbody,legs=focusedChild.looklegs,feet=focusedChild.lookfeet,addons=focusedChild.lookaddons})
  291.       end
  292. end
  293.  
  294. function CharacterList.UpdateSelecting()
  295.     CharacterList.selecting(self,characterList:getFocusedChild(),old)
  296. end
  297.  
  298. function CharacterList.destroy()
  299.   CharacterList.hide(true)
  300.  
  301.   if charactersWindow then
  302.     characterList = nil
  303.     charactersWindow:destroy()
  304.     charactersWindow = nil
  305.   end
  306. end
  307.  
  308. function CharacterList.show()
  309.   if loadBox or errorBox or not charactersWindow then return end
  310.   charactersWindow:show()
  311.   charactersWindow:raise()
  312.   charactersWindow:focus()
  313. end
  314.  
  315. function CharacterList.hide(showLogin)
  316.   showLogin = showLogin or false
  317.   charactersWindow:hide()
  318.  
  319.   if showLogin and EnterGame and not g_game.isOnline() then
  320.     EnterGame.show()
  321.   end
  322. end
  323.  
  324. function CharacterList.showAgain()
  325.   if characterList and characterList:hasChildren() then
  326.     CharacterList.show()
  327.   end
  328. end
  329.  
  330. function CharacterList.isVisible()
  331.   if charactersWindow and charactersWindow:isVisible() then
  332.     return true
  333.   end
  334.   return false
  335. end
  336.  
  337. function CharacterList.doLogin()
  338.   local selected = characterList:getFocusedChild()
  339.   if selected then
  340.     local charInfo = { worldHost = selected.worldHost,
  341.                        worldPort = selected.worldPort,
  342.                        worldName = selected.worldName,
  343.                        characterName = selected.characterName }
  344.     charactersWindow:hide()
  345.     tryLogin(charInfo)
  346.   else
  347.     displayErrorBox(tr('Error'), tr('You must select a character to login!'))
  348.   end
  349. end
  350.  
  351. function CharacterList.destroyLoadBox()
  352.   if loadBox then
  353.     loadBox:destroy()
  354.     loadBox = nil
  355.   end
  356. end
  357.  
  358. function CharacterList.cancelWait()
  359.   if waitingWindow then
  360.     waitingWindow:destroy()
  361.     waitingWindow = nil
  362.   end
  363.  
  364.   if updateWaitEvent then
  365.       updateWaitEvent:cancel()
  366.       updateWaitEvent = nil
  367.   end
  368.  
  369.   if resendWaitEvent then
  370.     resendWaitEvent:cancel()
  371.     resendWaitEvent = nil
  372.   end
  373.  
  374.   CharacterList.destroyLoadBox()
  375.   CharacterList.showAgain()
  376. end
Add Comment
Please, Sign In to add comment