1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

Move account login details from settings to persistent storage, with

separate entries depending on server hostname for more convenient
testing
This commit is contained in:
Ivan Savenko
2024-03-29 17:04:52 +02:00
parent 2deca842de
commit 8f0236f046
6 changed files with 104 additions and 28 deletions

View File

@@ -85,14 +85,9 @@ void GlobalLobbyClient::receiveAccountCreated(const JsonNode & json)
throw std::runtime_error("lobby connection finished without active login window!"); throw std::runtime_error("lobby connection finished without active login window!");
{ {
Settings configID = settings.write["lobby"]["accountID"]; setAccountID(json["accountID"].String());
configID->String() = json["accountID"].String(); setAccountDisplayName(json["displayName"].String());
setAccountCookie(json["accountCookie"].String());
Settings configName = settings.write["lobby"]["displayName"];
configName->String() = json["displayName"].String();
Settings configCookie = settings.write["lobby"]["accountCookie"];
configCookie->String() = json["accountCookie"].String();
} }
sendClientLogin(); sendClientLogin();
@@ -105,17 +100,15 @@ void GlobalLobbyClient::receiveOperationFailed(const JsonNode & json)
if(loginWindowPtr) if(loginWindowPtr)
loginWindowPtr->onConnectionFailed(json["reason"].String()); loginWindowPtr->onConnectionFailed(json["reason"].String());
logGlobal->warn("Operation failed! Reason: %s", json["reason"].String());
// TODO: handle errors in lobby menu // TODO: handle errors in lobby menu
} }
void GlobalLobbyClient::receiveClientLoginSuccess(const JsonNode & json) void GlobalLobbyClient::receiveClientLoginSuccess(const JsonNode & json)
{ {
{ {
Settings configCookie = settings.write["lobby"]["accountCookie"]; setAccountDisplayName(json["displayName"].String());
configCookie->String() = json["accountCookie"].String(); setAccountCookie(json["accountCookie"].String());
Settings configName = settings.write["lobby"]["displayName"];
configName->String() = json["displayName"].String();
} }
auto loginWindowPtr = loginWindow.lock(); auto loginWindowPtr = loginWindow.lock();
@@ -288,8 +281,8 @@ void GlobalLobbyClient::receiveJoinRoomSuccess(const JsonNode & json)
CSH->resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOBBY_GUEST, {}); CSH->resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOBBY_GUEST, {});
CSH->loadMode = ELoadMode::MULTI; CSH->loadMode = ELoadMode::MULTI;
std::string hostname = settings["lobby"]["hostname"].String(); std::string hostname = getServerHost();
uint16_t port = settings["lobby"]["port"].Integer(); uint16_t port = getServerPort();
CSH->connectToServer(hostname, port); CSH->connectToServer(hostname, port);
} }
@@ -324,8 +317,8 @@ void GlobalLobbyClient::sendClientLogin()
{ {
JsonNode toSend; JsonNode toSend;
toSend["type"].String() = "clientLogin"; toSend["type"].String() = "clientLogin";
toSend["accountID"] = settings["lobby"]["accountID"]; toSend["accountID"].String() = getAccountID();
toSend["accountCookie"] = settings["lobby"]["accountCookie"]; toSend["accountCookie"].String() = getAccountCookie();
toSend["language"].String() = CGI->generaltexth->getPreferredLanguage(); toSend["language"].String() = CGI->generaltexth->getPreferredLanguage();
toSend["version"].String() = VCMI_VERSION_STRING; toSend["version"].String() = VCMI_VERSION_STRING;
sendMessage(toSend); sendMessage(toSend);
@@ -370,7 +363,7 @@ void GlobalLobbyClient::sendOpenRoom(const std::string & mode, int playerLimit)
{ {
JsonNode toSend; JsonNode toSend;
toSend["type"].String() = "activateGameRoom"; toSend["type"].String() = "activateGameRoom";
toSend["hostAccountID"] = settings["lobby"]["accountID"]; toSend["hostAccountID"].String() = getAccountID();
toSend["roomType"].String() = mode; toSend["roomType"].String() = mode;
toSend["playerLimit"].Integer() = playerLimit; toSend["playerLimit"].Integer() = playerLimit;
sendMessage(toSend); sendMessage(toSend);
@@ -378,8 +371,8 @@ void GlobalLobbyClient::sendOpenRoom(const std::string & mode, int playerLimit)
void GlobalLobbyClient::connect() void GlobalLobbyClient::connect()
{ {
std::string hostname = settings["lobby"]["hostname"].String(); std::string hostname = getServerHost();
uint16_t port = settings["lobby"]["port"].Integer(); uint16_t port = getServerPort();
CSH->getNetworkHandler().connectToRemote(*this, hostname, port); CSH->getNetworkHandler().connectToRemote(*this, hostname, port);
} }
@@ -479,12 +472,55 @@ void GlobalLobbyClient::activateRoomInviteInterface()
GH.windows().createAndPushWindow<GlobalLobbyInviteWindow>(); GH.windows().createAndPushWindow<GlobalLobbyInviteWindow>();
} }
void GlobalLobbyClient::setAccountID(const std::string & accountID)
{
Settings configID = persistentStorage.write["lobby"][getServerHost()]["accountID"];
configID->String() = accountID;
}
void GlobalLobbyClient::setAccountCookie(const std::string & accountCookie)
{
Settings configCookie = persistentStorage.write["lobby"][getServerHost()]["accountCookie"];
configCookie->String() = accountCookie;
}
void GlobalLobbyClient::setAccountDisplayName(const std::string & accountDisplayName)
{
Settings configName = persistentStorage.write["lobby"][getServerHost()]["displayName"];
configName->String() = accountDisplayName;
}
const std::string & GlobalLobbyClient::getAccountID() const
{
return persistentStorage["lobby"][getServerHost()]["accountID"].String();
}
const std::string & GlobalLobbyClient::getAccountCookie() const
{
return persistentStorage["lobby"][getServerHost()]["accountCookie"].String();
}
const std::string & GlobalLobbyClient::getAccountDisplayName() const
{
return persistentStorage["lobby"][getServerHost()]["displayName"].String();
}
const std::string & GlobalLobbyClient::getServerHost() const
{
return settings["lobby"]["hostname"].String();
}
uint16_t GlobalLobbyClient::getServerPort() const
{
return settings["lobby"]["port"].Integer();
}
void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection) void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection)
{ {
JsonNode toSend; JsonNode toSend;
toSend["type"].String() = "clientProxyLogin"; toSend["type"].String() = "clientProxyLogin";
toSend["accountID"] = settings["lobby"]["accountID"]; toSend["accountID"].String() = getAccountID();
toSend["accountCookie"] = settings["lobby"]["accountCookie"]; toSend["accountCookie"].String() = getAccountCookie();
toSend["gameRoomID"].String() = currentGameRoomUUID; toSend["gameRoomID"].String() = currentGameRoomUUID;
assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack")); assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
@@ -510,6 +546,8 @@ void GlobalLobbyClient::sendMatchChatMessage(const std::string & messageText)
toSend["channelName"].String() = currentGameRoomUUID; toSend["channelName"].String() = currentGameRoomUUID;
toSend["messageText"].String() = messageText; toSend["messageText"].String() = messageText;
assert(TextOperations::isValidUnicodeString(messageText));
CSH->getGlobalLobby().sendMessage(toSend); CSH->getGlobalLobby().sendMessage(toSend);
} }

View File

@@ -58,6 +58,10 @@ class GlobalLobbyClient final : public INetworkClientListener, boost::noncopyabl
std::shared_ptr<GlobalLobbyLoginWindow> createLoginWindow(); std::shared_ptr<GlobalLobbyLoginWindow> createLoginWindow();
std::shared_ptr<GlobalLobbyWindow> createLobbyWindow(); std::shared_ptr<GlobalLobbyWindow> createLobbyWindow();
void setAccountID(const std::string & accountID);
void setAccountCookie(const std::string & accountCookie);
void setAccountDisplayName(const std::string & accountDisplayName);
public: public:
explicit GlobalLobbyClient(); explicit GlobalLobbyClient();
~GlobalLobbyClient(); ~GlobalLobbyClient();
@@ -68,6 +72,12 @@ public:
const std::vector<GlobalLobbyRoom> & getMatchesHistory() const; const std::vector<GlobalLobbyRoom> & getMatchesHistory() const;
const std::vector<GlobalLobbyChannelMessage> & getChannelHistory(const std::string & channelType, const std::string & channelName) const; const std::vector<GlobalLobbyChannelMessage> & getChannelHistory(const std::string & channelType, const std::string & channelName) const;
const std::string & getAccountID() const;
const std::string & getAccountCookie() const;
const std::string & getAccountDisplayName() const;
const std::string & getServerHost() const;
uint16_t getServerPort() const;
/// Activate interface and pushes lobby UI as top window /// Activate interface and pushes lobby UI as top window
void activateInterface(); void activateInterface();
void activateRoomInviteInterface(); void activateRoomInviteInterface();

View File

@@ -272,7 +272,7 @@ GlobalLobbyMatchCard::GlobalLobbyMatchCard(GlobalLobbyWindow * window, const Glo
if (matchDescription.participants.size() == 2) if (matchDescription.participants.size() == 2)
{ {
std::string ourAccountID = settings["lobby"]["accountID"].String(); std::string ourAccountID = CSH->getGlobalLobby().getAccountID();
opponentDescription.appendTextID("vcmi.lobby.match.duel"); opponentDescription.appendTextID("vcmi.lobby.match.duel");
// Find display name of our one and only opponent in this game // Find display name of our one and only opponent in this game

View File

@@ -24,6 +24,7 @@
#include "../../lib/CConfigHandler.h" #include "../../lib/CConfigHandler.h"
#include "../../lib/Languages.h" #include "../../lib/Languages.h"
#include "../../lib/MetaString.h" #include "../../lib/MetaString.h"
#include "../../lib/TextOperations.h"
GlobalLobbyWindow::GlobalLobbyWindow() GlobalLobbyWindow::GlobalLobbyWindow()
: CWindowObject(BORDERED) : CWindowObject(BORDERED)
@@ -33,7 +34,7 @@ GlobalLobbyWindow::GlobalLobbyWindow()
pos = widget->pos; pos = widget->pos;
center(); center();
widget->getAccountNameLabel()->setText(settings["lobby"]["displayName"].String()); widget->getAccountNameLabel()->setText(CSH->getGlobalLobby().getAccountDisplayName());
doOpenChannel("global", "english", Languages::getLanguageOptions("english").nameNative); doOpenChannel("global", "english", Languages::getLanguageOptions("english").nameNative);
widget->getChannelListHeader()->setText(MetaString::createFromTextID("vcmi.lobby.header.channels").toString()); widget->getChannelListHeader()->setText(MetaString::createFromTextID("vcmi.lobby.header.channels").toString());
@@ -54,7 +55,7 @@ void GlobalLobbyWindow::doOpenChannel(const std::string & channelType, const std
auto history = CSH->getGlobalLobby().getChannelHistory(channelType, channelName); auto history = CSH->getGlobalLobby().getChannelHistory(channelType, channelName);
for (auto const & entry : history) for(const auto & entry : history)
onGameChatMessage(entry.displayName, entry.messageText, entry.timeFormatted, channelType, channelName); onGameChatMessage(entry.displayName, entry.messageText, entry.timeFormatted, channelType, channelName);
MetaString text; MetaString text;
@@ -80,6 +81,8 @@ void GlobalLobbyWindow::doSendChatMessage()
toSend["channelName"].String() = currentChannelName; toSend["channelName"].String() = currentChannelName;
toSend["messageText"].String() = messageText; toSend["messageText"].String() = messageText;
assert(TextOperations::isValidUnicodeString(messageText));
CSH->getGlobalLobby().sendMessage(toSend); CSH->getGlobalLobby().sendMessage(toSend);
widget->getMessageInput()->setText(""); widget->getMessageInput()->setText("");

View File

@@ -122,8 +122,8 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
JsonNode toSend; JsonNode toSend;
toSend["type"].String() = "serverLogin"; toSend["type"].String() = "serverLogin";
toSend["gameRoomID"].String() = owner.uuid; toSend["gameRoomID"].String() = owner.uuid;
toSend["accountID"] = settings["lobby"]["accountID"]; toSend["accountID"].String() = getHostAccountID();
toSend["accountCookie"] = settings["lobby"]["accountCookie"]; toSend["accountCookie"].String() = getHostAccountCookie();
toSend["version"].String() = VCMI_VERSION_STRING; toSend["version"].String() = VCMI_VERSION_STRING;
sendMessage(connection, toSend); sendMessage(connection, toSend);
@@ -140,7 +140,7 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
toSend["type"].String() = "serverProxyLogin"; toSend["type"].String() = "serverProxyLogin";
toSend["gameRoomID"].String() = owner.uuid; toSend["gameRoomID"].String() = owner.uuid;
toSend["guestAccountID"].String() = guestAccountID; toSend["guestAccountID"].String() = guestAccountID;
toSend["accountCookie"] = settings["lobby"]["accountCookie"]; toSend["accountCookie"].String() = getHostAccountCookie();
sendMessage(connection, toSend); sendMessage(connection, toSend);
@@ -170,3 +170,24 @@ void GlobalLobbyProcessor::sendMessage(const NetworkConnectionPtr & targetConnec
assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack")); assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
targetConnection->sendPacket(toSend.toBytes()); targetConnection->sendPacket(toSend.toBytes());
} }
//FIXME: consider whether these methods should be replaced with some other way to define our account ID / account cookie
static const std::string & getServerHost()
{
return settings["lobby"]["hostname"].String();
}
const std::string & GlobalLobbyProcessor::getHostAccountID() const
{
return persistentStorage["lobby"][getServerHost()]["accountID"].String();
}
const std::string & GlobalLobbyProcessor::getHostAccountCookie() const
{
return persistentStorage["lobby"][getServerHost()]["accountCookie"].String();
}
const std::string & GlobalLobbyProcessor::getHostAccountDisplayName() const
{
return persistentStorage["lobby"][getServerHost()]["displayName"].String();
}

View File

@@ -35,6 +35,10 @@ class GlobalLobbyProcessor : public INetworkClientListener
void establishNewConnection(); void establishNewConnection();
void sendMessage(const NetworkConnectionPtr & targetConnection, const JsonNode & payload); void sendMessage(const NetworkConnectionPtr & targetConnection, const JsonNode & payload);
const std::string & getHostAccountID() const;
const std::string & getHostAccountCookie() const;
const std::string & getHostAccountDisplayName() const;
public: public:
void sendChangeRoomDescription(const std::string & description); void sendChangeRoomDescription(const std::string & description);
void sendGameStarted(); void sendGameStarted();