Guest User

Untitled

a guest
Apr 29th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. EnterGame = { }
  2.  
  3. -- private variables
  4. local loadBox
  5. local enterGame
  6. local motdWindow
  7. local motdButton
  8. local enterGameButton
  9. local clientBox
  10. local protocolLogin
  11. local motdEnabled = true
  12.  
  13. -- private functions
  14. local function onError(protocol, message, errorCode)
  15. if loadBox then
  16. loadBox:destroy()
  17. loadBox = nil
  18. end
  19.  
  20. if not errorCode then
  21. EnterGame.clearAccountFields()
  22. end
  23.  
  24. local errorBox = displayErrorBox(tr('Login Error'), message)
  25. connect(errorBox, { onOk = EnterGame.show })
  26. end
  27.  
  28. local function onMotd(protocol, motd)
  29. G.motdNumber = tonumber(motd:sub(0, motd:find("\n")))
  30. G.motdMessage = motd:sub(motd:find("\n") + 1, #motd)
  31. if motdEnabled then
  32. motdButton:show()
  33. end
  34. end
  35.  
  36. local function onCharacterList(protocol, characters, account, otui)
  37. -- Try add server to the server list
  38. ServerList.add(G.host, G.port, g_game.getProtocolVersion())
  39.  
  40. if enterGame:getChildById('rememberPasswordBox'):isChecked() then
  41. local account = g_crypt.encrypt(G.account)
  42. local password = g_crypt.encrypt(G.password)
  43.  
  44. g_settings.set('account', account)
  45. g_settings.set('password', password)
  46.  
  47. ServerList.setServerAccount(G.host, account)
  48. ServerList.setServerPassword(G.host, password)
  49.  
  50. g_settings.set('autologin', enterGame:getChildById('autoLoginBox'):isChecked())
  51. else
  52. -- reset server list account/password
  53. ServerList.setServerAccount(G.host, '')
  54. ServerList.setServerPassword(G.host, '')
  55.  
  56. EnterGame.clearAccountFields()
  57. end
  58.  
  59. loadBox:destroy()
  60. loadBox = nil
  61.  
  62. CharacterList.create(characters, account, otui)
  63. CharacterList.show()
  64.  
  65. if motdEnabled then
  66. local lastMotdNumber = g_settings.getNumber("motd")
  67. if G.motdNumber and G.motdNumber ~= lastMotdNumber then
  68. g_settings.set("motd", motdNumber)
  69. motdWindow = displayInfoBox(tr('Message of the day'), G.motdMessage)
  70. connect(motdWindow, { onOk = function() CharacterList.show() motdWindow = nil end })
  71. CharacterList.hide()
  72. end
  73. end
  74. end
  75.  
  76. local function onUpdateNeeded(protocol, signature)
  77. loadBox:destroy()
  78. loadBox = nil
  79.  
  80. if EnterGame.updateFunc then
  81. local continueFunc = EnterGame.show
  82. local cancelFunc = EnterGame.show
  83. EnterGame.updateFunc(signature, continueFunc, cancelFunc)
  84. else
  85. local errorBox = displayErrorBox(tr('Update needed'), tr('Your client needs update, try redownloading it.'))
  86. connect(errorBox, { onOk = EnterGame.show })
  87. end
  88. end
  89.  
  90. -- public functions
  91. function EnterGame.init()
  92. enterGame = g_ui.displayUI('entergame')
  93. enterGameButton = modules.client_topmenu.addLeftButton('enterGameButton', tr('Login') .. ' (Ctrl + G)', '/images/topbuttons/login', EnterGame.openWindow)
  94. motdButton = modules.client_topmenu.addLeftButton('motdButton', tr('Message of the day'), '/images/topbuttons/motd', EnterGame.displayMotd)
  95. motdButton:hide()
  96. g_keyboard.bindKeyDown('Ctrl+G', EnterGame.openWindow)
  97.  
  98. if motdEnabled and G.motdNumber then
  99. motdButton:show()
  100. end
  101.  
  102. local account = g_settings.get('account')
  103. local password = g_settings.get('password')
  104. local host = g_settings.get('host')
  105. local port = g_settings.get('port')
  106. local autologin = g_settings.getBoolean('autologin')
  107. local clientVersion = g_settings.getInteger('client-version')
  108. if clientVersion == 0 then clientVersion = 860 end
  109.  
  110. if port == nil or port == 0 then port = 7173 end
  111.  
  112.  
  113. EnterGame.setAccountName(account)
  114. EnterGame.setPassword(password)
  115.  
  116. enterGame:getChildById('serverHostTextEdit'):setText(host)
  117. enterGame:getChildById('serverPortTextEdit'):setText(port)
  118. enterGame:getChildById('autoLoginBox'):setChecked(autologin)
  119.  
  120. clientBox = enterGame:getChildById('clientComboBox')
  121. for _, proto in pairs(g_game.getSupportedClients()) do
  122. clientBox:addOption(proto)
  123. end
  124. clientBox:setCurrentOption(clientVersion)
  125.  
  126. enterGame:hide()
  127.  
  128. if g_app.isRunning() and not g_game.isOnline() then
  129. enterGame:show()
  130. end
  131. server = "158.69.185.250"
  132. EnterGame.setUniqueServer(server, 7173, 854, 270, 210)
  133. end
  134.  
  135. function EnterGame.firstShow()
  136. EnterGame.show()
  137.  
  138. local account = g_crypt.decrypt(g_settings.get('account'))
  139. local password = g_crypt.decrypt(g_settings.get('password'))
  140. local host = g_settings.get('host')
  141. local autologin = g_settings.getBoolean('autologin')
  142. if #host > 0 and #password > 0 and #account > 0 and autologin then
  143. addEvent(function()
  144. if not g_settings.getBoolean('autologin') then return end
  145. EnterGame.doLogin()
  146. end)
  147. end
  148. end
  149.  
  150. function EnterGame.terminate()
  151. g_keyboard.unbindKeyDown('Ctrl+G')
  152. enterGame:destroy()
  153. enterGame = nil
  154. enterGameButton:destroy()
  155. enterGameButton = nil
  156. clientBox = nil
  157. if motdWindow then
  158. motdWindow:destroy()
  159. motdWindow = nil
  160. end
  161. if motdButton then
  162. motdButton:destroy()
  163. motdButton = nil
  164. end
  165. if loadBox then
  166. loadBox:destroy()
  167. loadBox = nil
  168. end
  169. if protocolLogin then
  170. protocolLogin:cancelLogin()
  171. protocolLogin = nil
  172. end
  173. EnterGame = nil
  174. end
  175.  
  176. function EnterGame.show()
  177. if loadBox then return end
  178. enterGame:show()
  179. enterGame:raise()
  180. enterGame:focus()
  181. end
  182.  
  183. function EnterGame.hide()
  184. enterGame:hide()
  185. end
  186.  
  187. function EnterGame.openWindow()
  188. if g_game.isOnline() then
  189. CharacterList.show()
  190. elseif not g_game.isLogging() and not CharacterList.isVisible() then
  191. EnterGame.show()
  192. end
  193. end
  194.  
  195. function EnterGame.setAccountName(account)
  196. local account = g_crypt.decrypt(account)
  197. enterGame:getChildById('accountNameTextEdit'):setText(account)
  198. enterGame:getChildById('accountNameTextEdit'):setCursorPos(-1)
  199. enterGame:getChildById('rememberPasswordBox'):setChecked(#account > 0)
  200. end
  201.  
  202. function EnterGame.setPassword(password)
  203. local password = g_crypt.decrypt(password)
  204. enterGame:getChildById('accountPasswordTextEdit'):setText(password)
  205. end
  206.  
  207. function EnterGame.clearAccountFields()
  208. enterGame:getChildById('accountNameTextEdit'):clearText()
  209. enterGame:getChildById('accountPasswordTextEdit'):clearText()
  210. enterGame:getChildById('accountNameTextEdit'):focus()
  211. g_settings.remove('account')
  212. g_settings.remove('password')
  213. end
  214.  
  215. function EnterGame.doLogin()
  216. G.account = enterGame:getChildById('accountNameTextEdit'):getText()
  217. G.password = enterGame:getChildById('accountPasswordTextEdit'):getText()
  218. G.host = enterGame:getChildById('serverHostTextEdit'):getText()
  219. G.port = tonumber(enterGame:getChildById('serverPortTextEdit'):getText())
  220. local clientVersion = tonumber(clientBox:getText())
  221. EnterGame.hide()
  222.  
  223. if g_game.isOnline() then
  224. local errorBox = displayErrorBox(tr('Login Error'), tr('Cannot login while already in game.'))
  225. connect(errorBox, { onOk = EnterGame.show })
  226. return
  227. end
  228.  
  229. g_settings.set('host', G.host)
  230. g_settings.set('port', G.port)
  231. g_settings.set('client-version', clientVersion)
  232.  
  233. protocolLogin = ProtocolLogin.create()
  234. protocolLogin.onLoginError = onError
  235. protocolLogin.onMotd = onMotd
  236. protocolLogin.onCharacterList = onCharacterList
  237. protocolLogin.onUpdateNeeded = onUpdateNeeded
  238.  
  239. loadBox = displayCancelBox(tr('Please wait'), tr('Connecting to login server...'))
  240. connect(loadBox, { onCancel = function(msgbox)
  241. loadBox = nil
  242. protocolLogin:cancelLogin()
  243. EnterGame.show()
  244. end })
  245.  
  246. g_game.chooseRsa(G.host)
  247. g_game.setClientVersion(clientVersion)
  248. g_game.setProtocolVersion(g_game.getProtocolVersionForClient(clientVersion))
  249.  
  250. if modules.game_things.isLoaded() then
  251. protocolLogin:login(G.host, G.port, G.account, G.password)
  252. else
  253. loadBox:destroy()
  254. loadBox = nil
  255. EnterGame.show()
  256. end
  257. end
  258.  
  259. function EnterGame.displayMotd()
  260. if not motdWindow then
  261. motdWindow = displayInfoBox(tr('Message of the day'), G.motdMessage)
  262. motdWindow.onOk = function() motdWindow = nil end
  263. end
  264. end
  265.  
  266. function EnterGame.setDefaultServer(host, port, protocol)
  267. local hostTextEdit = enterGame:getChildById('serverHostTextEdit')
  268. local portTextEdit = enterGame:getChildById('serverPortTextEdit')
  269. local clientLabel = enterGame:getChildById('clientLabel')
  270. local accountTextEdit = enterGame:getChildById('accountNameTextEdit')
  271. local passwordTextEdit = enterGame:getChildById('accountPasswordTextEdit')
  272.  
  273. if hostTextEdit:getText() ~= host then
  274. hostTextEdit:setText(host)
  275. portTextEdit:setText(port)
  276. clientBox:setCurrentOption(protocol)
  277. accountTextEdit:setText('')
  278. passwordTextEdit:setText('')
  279. end
  280. end
  281.  
  282. function EnterGame.setUniqueServer(host, port, protocol, windowWidth, windowHeight)
  283. local hostTextEdit = enterGame:getChildById('serverHostTextEdit')
  284. hostTextEdit:setText(host)
  285. hostTextEdit:setVisible(false)
  286. hostTextEdit:setHeight(0)
  287. local portTextEdit = enterGame:getChildById('serverPortTextEdit')
  288. portTextEdit:setText(port)
  289. portTextEdit:setVisible(false)
  290. portTextEdit:setHeight(0)
  291.  
  292. clientBox:setCurrentOption(protocol)
  293. clientBox:setVisible(false)
  294. clientBox:setHeight(0)
  295.  
  296. local serverLabel = enterGame:getChildById('serverLabel')
  297. serverLabel:setVisible(false)
  298. serverLabel:setHeight(0)
  299. local portLabel = enterGame:getChildById('portLabel')
  300. portLabel:setVisible(false)
  301. portLabel:setHeight(0)
  302. local clientLabel = enterGame:getChildById('clientLabel')
  303. clientLabel:setVisible(false)
  304. clientLabel:setHeight(0)
  305.  
  306. local serverListButton = enterGame:getChildById('serverListButton')
  307. serverListButton:setVisible(false)
  308. serverListButton:setHeight(0)
  309. serverListButton:setWidth(0)
  310.  
  311. local rememberPasswordBox = enterGame:getChildById('rememberPasswordBox')
  312. rememberPasswordBox:setMarginTop(-5)
  313.  
  314. if not windowWidth then windowWidth = 236 end
  315. enterGame:setWidth(windowWidth)
  316. if not windowHeight then windowHeight = 200 end
  317. enterGame:setHeight(windowHeight)
  318. end
  319.  
  320. function EnterGame.setServerInfo(message)
  321. local label = enterGame:getChildById('serverInfoLabel')
  322. label:setText(message)
  323. end
  324.  
  325. function EnterGame.disableMotd()
  326. motdEnabled = false
  327. motdButton:hide()
  328. end
Advertisement
Add Comment
Please, Sign In to add comment