1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

Added option to create new account even if player already have one

This commit is contained in:
Ivan Savenko 2024-02-09 23:02:41 +02:00
parent 9730b24214
commit 4a0dd2da2c
5 changed files with 67 additions and 25 deletions

View File

@ -76,6 +76,8 @@
"vcmi.lobby.login.username" : "Username:",
"vcmi.lobby.login.connecting" : "Connecting...",
"vcmi.lobby.login.error" : "Connection error: %s",
"vcmi.lobby.login.create" : "New Account",
"vcmi.lobby.login.login" : "Login",
"vcmi.lobby.room.create" : "Create Room",
"vcmi.lobby.room.players.limit" : "Players Limit",

View File

@ -207,24 +207,21 @@ void GlobalLobbyClient::receiveJoinRoomSuccess(const JsonNode & json)
void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
{
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
networkConnection = connection;
JsonNode toSend;
auto loginWindowPtr = loginWindow.lock();
std::string accountID = settings["lobby"]["accountID"].String();
if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
throw std::runtime_error("lobby connection established without active login window!");
if(accountID.empty())
sendClientRegister();
else
sendClientLogin();
loginWindowPtr->onConnectionSuccess();
}
void GlobalLobbyClient::sendClientRegister()
void GlobalLobbyClient::sendClientRegister(const std::string & accountName)
{
JsonNode toSend;
toSend["type"].String() = "clientRegister";
toSend["displayName"] = settings["lobby"]["displayName"];
toSend["displayName"].String() = accountName;
sendMessage(toSend);
}

View File

@ -35,9 +35,6 @@ class GlobalLobbyClient final : public INetworkClientListener, boost::noncopyabl
void onConnectionEstablished(const std::shared_ptr<INetworkConnection> &) override;
void onDisconnected(const std::shared_ptr<INetworkConnection> &, const std::string & errorMessage) override;
void sendClientRegister();
void sendClientLogin();
void receiveAccountCreated(const JsonNode & json);
void receiveOperationFailed(const JsonNode & json);
void receiveLoginSuccess(const JsonNode & json);
@ -60,6 +57,8 @@ public:
/// Activate interface and pushes lobby UI as top window
void activateInterface();
void sendMessage(const JsonNode & data);
void sendClientRegister(const std::string & accountName);
void sendClientLogin();
void sendOpenPublicRoom();
void sendOpenPrivateRoom();

View File

@ -32,28 +32,58 @@ GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
pos.w = 200;
pos.h = 200;
pos.w = 284;
pos.h = 220;
filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.login.title"));
labelUsername = std::make_shared<CLabel>( 10, 45, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.lobby.login.username"));
backgroundUsername = std::make_shared<TransparentFilledRectangle>(Rect(10, 70, 180, 20), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64));
inputUsername = std::make_shared<CTextInput>(Rect(15, 73, 176, 16), FONT_SMALL, nullptr, ETextAlignment::TOPLEFT, true);
buttonLogin = std::make_shared<CButton>(Point(10, 160), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onLogin(); });
buttonClose = std::make_shared<CButton>(Point(126, 160), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); });
labelStatus = std::make_shared<CTextBox>( "", Rect(15, 95, 175, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
labelUsername = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.lobby.login.username"));
backgroundUsername = std::make_shared<TransparentFilledRectangle>(Rect(10, 90, 264, 20), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64));
inputUsername = std::make_shared<CTextInput>(Rect(15, 93, 260, 16), FONT_SMALL, nullptr, ETextAlignment::TOPLEFT, true);
buttonLogin = std::make_shared<CButton>(Point(10, 180), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onLogin(); });
buttonClose = std::make_shared<CButton>(Point(210, 180), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); });
labelStatus = std::make_shared<CTextBox>( "", Rect(15, 115, 255, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
auto buttonRegister = std::make_shared<CToggleButton>(Point(10, 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), 0);
auto buttonLogin = std::make_shared<CToggleButton>(Point(146, 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), 0);
buttonRegister->addTextOverlay(CGI->generaltexth->translate("vcmi.lobby.login.create"), EFonts::FONT_SMALL, Colors::YELLOW);
buttonLogin->addTextOverlay(CGI->generaltexth->translate("vcmi.lobby.login.login"), EFonts::FONT_SMALL, Colors::YELLOW);
toggleMode = std::make_shared<CToggleGroup>(nullptr);
toggleMode->addToggle(0, buttonRegister);
toggleMode->addToggle(1, buttonLogin);
toggleMode->setSelected(settings["lobby"]["roomType"].Integer());
toggleMode->addCallback([this](int index){onLoginModeChanged(index);});
if (settings["lobby"]["accountID"].String().empty())
{
buttonLogin->block(true);
toggleMode->setSelected(0);
}
else
toggleMode->setSelected(1);
filledBackground->playerColored(PlayerColor(1));
inputUsername->setText(settings["lobby"]["displayName"].String());
inputUsername->cb += [this](const std::string & text)
{
buttonLogin->block(text.empty());
this->buttonLogin->block(text.empty());
};
center();
}
void GlobalLobbyLoginWindow::onLoginModeChanged(int value)
{
if (value == 0)
{
inputUsername->setText("");
}
else
{
inputUsername->setText(settings["lobby"]["displayName"].String());
}
}
void GlobalLobbyLoginWindow::onClose()
{
close();
@ -62,16 +92,26 @@ void GlobalLobbyLoginWindow::onClose()
void GlobalLobbyLoginWindow::onLogin()
{
Settings config = settings.write["lobby"]["displayName"];
config->String() = inputUsername->getText();
labelStatus->setText(CGI->generaltexth->translate("vcmi.lobby.login.connecting"));
if(!CSH->getGlobalLobby().isConnected())
CSH->getGlobalLobby().connect();
else
onConnectionSuccess();
buttonClose->block(true);
}
void GlobalLobbyLoginWindow::onConnectionSuccess()
{
std::string accountID = settings["lobby"]["accountID"].String();
if(toggleMode->getSelected() == 0)
CSH->getGlobalLobby().sendClientRegister(inputUsername->getText());
else
CSH->getGlobalLobby().sendClientLogin();
}
void GlobalLobbyLoginWindow::onLoginSuccess()
{
close();
CSH->getGlobalLobby().activateInterface();

View File

@ -14,6 +14,7 @@
class CLabel;
class CTextBox;
class CTextInput;
class CToggleGroup;
class FilledTexturePlayerColored;
class TransparentFilledRectangle;
class CButton;
@ -29,7 +30,9 @@ class GlobalLobbyLoginWindow : public CWindowObject
std::shared_ptr<CButton> buttonLogin;
std::shared_ptr<CButton> buttonClose;
std::shared_ptr<CToggleGroup> toggleMode; // create account or use existing
void onLoginModeChanged(int value);
void onClose();
void onLogin();
@ -37,5 +40,6 @@ public:
GlobalLobbyLoginWindow();
void onConnectionSuccess();
void onLoginSuccess();
void onConnectionFailed(const std::string & reason);
};