diff --git a/modules/game/game.lua b/modules/game/game.lua index 71759ee5..d1e8cc19 100644 --- a/modules/game/game.lua +++ b/modules/game/game.lua @@ -1,6 +1,3 @@ --- private variables -local showCharacterListOnLogout = true - -- private functions local function onGameKeyPress(self, keyCode, keyChar, keyboardModifiers) if keyboardModifiers == KeyboardCtrlModifier then @@ -8,7 +5,7 @@ local function onGameKeyPress(self, keyCode, keyChar, keyboardModifiers) CharacterList.show() return true elseif keyCode == KeyQ then - Game.logout() + Game.logout(false) return true end end @@ -35,21 +32,20 @@ function Game.onLogin() createMainInterface() end +function Game.onLoginError(message) + CharacterList.destroyLoadBox() + local errorBox = displayErrorBox("Login Error", "Login error: " .. message) + errorBox.onOk = CharacterList.show +end + function Game.onConnectionError(message) CharacterList.destroyLoadBox() local errorBox = displayErrorBox("Login Error", "Connection error: " .. message) errorBox.onOk = CharacterList.show - showCharacterListOnLogout = false end function Game.onLogout() MainMenu.show() - - if showCharacterListOnLogout then - CharacterList.show() - else - showCharacterListOnLogout = true - end - + CharacterList.show() destroyMainInterface() end diff --git a/modules/game/ui/gameinterface.otui b/modules/game/ui/gameinterface.otui index 476c95ad..df13a520 100644 --- a/modules/game/ui/gameinterface.otui +++ b/modules/game/ui/gameinterface.otui @@ -10,4 +10,4 @@ UIWidget anchors.left: gameMap.right anchors.top: parent.top text: Logout - onClick: Game.logout() \ No newline at end of file + onClick: Game.logout(false) \ No newline at end of file diff --git a/modules/mainmenu/characterlist.lua b/modules/mainmenu/characterlist.lua index 87975053..27c0ea05 100644 --- a/modules/mainmenu/characterlist.lua +++ b/modules/mainmenu/characterlist.lua @@ -17,15 +17,26 @@ local function onCharactersWindowKeyPress(self, keyCode, keyChar, keyboardModifi return false end -local function tryLogin(charInfo) - CharacterList.hide() +local function tryLogin(charInfo, tries) + tries = tries or 1 + + if tries > 4 then + CharacterList.destroyLoadBox() + displayErrorBox('Error', 'Could not logout.') + return + end if Game.isOnline() then - Game.logout() - scheduleEvent(function() tryLogin(charInfo) end, 250) + Game.logout(false) + if tries == 1 then + loadBox = displayCancelBox('Please wait', 'Loggin out...') + end + scheduleEvent(function() tryLogin(charInfo, tries+1) end, 250) return end + CharacterList.destroyLoadBox() + Game.loginWorld(EnterGame.account, EnterGame.password, charInfo.worldHost, charInfo.worldPort, charInfo.characterName) loadBox = displayCancelBox('Please wait', 'Connecting to game server...') @@ -79,8 +90,10 @@ function CharacterList.hide() end function CharacterList.show() - charactersWindow:show() - charactersWindow:lock() + if not loadBox then + charactersWindow:show() + charactersWindow:lock() + end end function CharacterList.doLogin() @@ -89,6 +102,7 @@ function CharacterList.doLogin() local charInfo = { worldHost = selected.worldHost, worldPort = selected.worldPort, characterName = selected.characterName } + CharacterList.hide() tryLogin(charInfo) else displayErrorBox('Error', 'You must select a character to login!') diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 0f7e401c..c3584524 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -34,7 +34,7 @@ void Game::init() void Game::terminate() { if(m_online) - logout(); + logout(true); } void Game::loginWorld(const std::string& account, const std::string& password, const std::string& worldHost, int worldPort, const std::string& characterName) @@ -44,20 +44,23 @@ void Game::loginWorld(const std::string& account, const std::string& password, c } void Game::cancelLogin() +{ + processLogout(); +} + +void Game::logout(bool force) { if(m_protocolGame) { - if(m_protocolGame->isConnected()) { - logout(); - } else if(m_protocolGame->isConnecting()) { - m_protocolGame->disconnect(); - m_protocolGame.reset(); - } + m_protocolGame->sendLogout(); + + if(force) + processLogout(); } } -void Game::logout() +void Game::processLoginError(const std::string& error) { - m_protocolGame->sendLogout(); + g_lua.callGlobalField("Game", "onLoginError", error); } void Game::processConnectionError(const boost::system::error_code& error) @@ -67,12 +70,7 @@ void Game::processConnectionError(const boost::system::error_code& error) if(error != asio::error::eof) g_lua.callGlobalField("Game", "onConnectionError", error.message()); - if(m_online) { - processLogout(); - } else { - m_protocolGame->disconnect(); - m_protocolGame.reset(); - } + processLogout(); } } @@ -86,21 +84,21 @@ void Game::processLogin(const LocalPlayerPtr& localPlayer) void Game::processLogout() { - g_lua.callGlobalField("Game", "onLogout", m_localPlayer); + if(m_online) { + g_lua.callGlobalField("Game", "onLogout", m_localPlayer); + + m_localPlayer.reset(); + m_online = false; + } if(m_protocolGame) { m_protocolGame->disconnect(); m_protocolGame.reset(); } - - m_localPlayer.reset(); - m_online = false; } void Game::walk(Otc::Direction direction) { - if(!m_online) - return; // TODO: check if we can walk. diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 42d7f6fa..5ea22fcb 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -37,8 +37,9 @@ public: const std::string& worldHost, int worldPort, const std::string& characterName); void cancelLogin(); - void logout(); + void logout(bool force); + void processLoginError(const std::string& error); void processConnectionError(const boost::system::error_code& error); void processLogin(const LocalPlayerPtr& localPlayer); void processLogout(); diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index c43a97e1..bb3b6673 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -275,7 +275,7 @@ void ProtocolGame::parseGMActions(InputMessage& msg) void ProtocolGame::parseErrorMessage(InputMessage& msg) { std::string error = msg.getString(); - callLuaField("onError", error); + g_game.processLoginError(error); } void ProtocolGame::parseFYIMessage(InputMessage& msg) diff --git a/src/otclient/otclient.cpp b/src/otclient/otclient.cpp index f21e14e5..7767686c 100644 --- a/src/otclient/otclient.cpp +++ b/src/otclient/otclient.cpp @@ -170,11 +170,11 @@ void OTClient::terminate() // hide the window because there is no render anymore g_platform.hideWindow(); + g_game.terminate(); + // run modules unload event g_modules.unloadModules(); - g_game.terminate(); - // terminate ui g_ui.terminate(); diff --git a/src/otclient/otclientluafunctions.cpp b/src/otclient/otclientluafunctions.cpp index 211479c6..db1ad403 100644 --- a/src/otclient/otclientluafunctions.cpp +++ b/src/otclient/otclientluafunctions.cpp @@ -60,7 +60,7 @@ void OTClient::registerLuaFunctions() g_lua.registerClass(); g_lua.bindClassStaticFunction("loginWorld", std::bind(&Game::loginWorld, &g_game, _1, _2, _3, _4, _5)); - g_lua.bindClassStaticFunction("logout", std::bind(&Game::logout, &g_game)); + g_lua.bindClassStaticFunction("logout", std::bind(&Game::logout, &g_game, _1)); g_lua.bindClassStaticFunction("cancelLogin", std::bind(&Game::cancelLogin, &g_game)); g_lua.bindClassStaticFunction("isOnline", std::bind(&Game::isOnline, &g_game));