2023-12-27 19:07:49 +02:00
|
|
|
/*
|
|
|
|
* GlobalLobbyWindow.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "GlobalLobbyWindow.h"
|
|
|
|
|
|
|
|
#include "GlobalLobbyClient.h"
|
2024-01-10 15:31:11 +02:00
|
|
|
#include "GlobalLobbyServerSetup.h"
|
2024-01-11 22:51:36 +02:00
|
|
|
#include "GlobalLobbyWidget.h"
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
#include "../CServerHandler.h"
|
2023-12-27 19:07:49 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2024-01-10 15:31:11 +02:00
|
|
|
#include "../gui/WindowHandler.h"
|
2024-05-12 12:24:13 +02:00
|
|
|
#include "../widgets/CTextInput.h"
|
2024-05-11 22:42:14 +02:00
|
|
|
#include "../widgets/Slider.h"
|
2024-01-21 16:48:36 +02:00
|
|
|
#include "../widgets/ObjectLists.h"
|
2024-05-12 12:24:13 +02:00
|
|
|
#include "../widgets/TextControls.h"
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/texts/Languages.h"
|
|
|
|
#include "../../lib/texts/MetaString.h"
|
|
|
|
#include "../../lib/texts/TextOperations.h"
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
GlobalLobbyWindow::GlobalLobbyWindow()
|
|
|
|
: CWindowObject(BORDERED)
|
2023-12-27 19:07:49 +02:00
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2023-12-27 19:07:49 +02:00
|
|
|
widget = std::make_shared<GlobalLobbyWidget>(this);
|
|
|
|
pos = widget->pos;
|
|
|
|
center();
|
|
|
|
|
2024-03-29 17:04:52 +02:00
|
|
|
widget->getAccountNameLabel()->setText(CSH->getGlobalLobby().getAccountDisplayName());
|
2024-03-21 18:00:05 +02:00
|
|
|
doOpenChannel("global", "english", Languages::getLanguageOptions("english").nameNative);
|
|
|
|
|
|
|
|
widget->getChannelListHeader()->setText(MetaString::createFromTextID("vcmi.lobby.header.channels").toString());
|
|
|
|
}
|
|
|
|
|
2024-03-27 13:08:41 +02:00
|
|
|
bool GlobalLobbyWindow::isChannelOpen(const std::string & testChannelType, const std::string & testChannelName) const
|
2024-03-21 18:00:05 +02:00
|
|
|
{
|
|
|
|
return testChannelType == currentChannelType && testChannelName == currentChannelName;
|
2024-03-13 23:02:53 +02:00
|
|
|
}
|
|
|
|
|
2024-03-21 18:00:05 +02:00
|
|
|
void GlobalLobbyWindow::doOpenChannel(const std::string & channelType, const std::string & channelName, const std::string & roomDescription)
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
|
|
|
currentChannelType = channelType;
|
|
|
|
currentChannelName = channelName;
|
|
|
|
chatHistory.clear();
|
2024-03-25 18:04:44 +02:00
|
|
|
unreadChannels.erase(channelType + "_" + channelName);
|
2024-03-13 23:02:53 +02:00
|
|
|
|
|
|
|
auto history = CSH->getGlobalLobby().getChannelHistory(channelType, channelName);
|
|
|
|
|
2024-03-29 17:04:52 +02:00
|
|
|
for(const auto & entry : history)
|
2024-03-13 23:02:53 +02:00
|
|
|
onGameChatMessage(entry.displayName, entry.messageText, entry.timeFormatted, channelType, channelName);
|
2024-03-21 18:00:05 +02:00
|
|
|
|
2024-05-11 18:44:31 +02:00
|
|
|
refreshChatText();
|
|
|
|
|
2024-03-21 18:00:05 +02:00
|
|
|
MetaString text;
|
|
|
|
text.appendTextID("vcmi.lobby.header.chat." + channelType);
|
|
|
|
text.replaceRawString(roomDescription);
|
|
|
|
widget->getGameChatHeader()->setText(text.toString());
|
|
|
|
|
|
|
|
// Update currently selected item in UI
|
2024-03-30 13:51:55 +02:00
|
|
|
// WARNING: this invalidates function parameters since some of them are members of objects that will be destroyed by reset
|
2024-03-21 18:00:05 +02:00
|
|
|
widget->getAccountList()->reset();
|
|
|
|
widget->getChannelList()->reset();
|
|
|
|
widget->getMatchList()->reset();
|
|
|
|
|
|
|
|
redraw();
|
2023-12-27 19:07:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyWindow::doSendChatMessage()
|
|
|
|
{
|
|
|
|
std::string messageText = widget->getMessageInput()->getText();
|
|
|
|
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "sendChatMessage";
|
2024-03-13 23:02:53 +02:00
|
|
|
toSend["channelType"].String() = currentChannelType;
|
|
|
|
toSend["channelName"].String() = currentChannelName;
|
2023-12-27 19:07:49 +02:00
|
|
|
toSend["messageText"].String() = messageText;
|
|
|
|
|
2024-03-29 17:04:52 +02:00
|
|
|
assert(TextOperations::isValidUnicodeString(messageText));
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
CSH->getGlobalLobby().sendMessage(toSend);
|
2023-12-27 19:07:49 +02:00
|
|
|
|
|
|
|
widget->getMessageInput()->setText("");
|
|
|
|
}
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
void GlobalLobbyWindow::doCreateGameRoom()
|
|
|
|
{
|
2024-01-10 15:31:11 +02:00
|
|
|
GH.windows().createAndPushWindow<GlobalLobbyServerSetup>();
|
2023-12-28 21:27:21 +02:00
|
|
|
}
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
void GlobalLobbyWindow::doInviteAccount(const std::string & accountID)
|
|
|
|
{
|
2024-02-10 19:02:25 +02:00
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "sendInvite";
|
|
|
|
toSend["accountID"].String() = accountID;
|
|
|
|
|
|
|
|
CSH->getGlobalLobby().sendMessage(toSend);
|
2024-01-21 16:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyWindow::doJoinRoom(const std::string & roomID)
|
|
|
|
{
|
2024-01-28 00:04:54 +02:00
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "joinGameRoom";
|
|
|
|
toSend["gameRoomID"].String() = roomID;
|
2024-01-21 16:48:36 +02:00
|
|
|
|
2024-01-28 00:04:54 +02:00
|
|
|
CSH->getGlobalLobby().sendMessage(toSend);
|
2024-01-21 16:48:36 +02:00
|
|
|
}
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
void GlobalLobbyWindow::onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when, const std::string & channelType, const std::string & channelName)
|
2023-12-27 19:07:49 +02:00
|
|
|
{
|
2024-03-13 23:02:53 +02:00
|
|
|
if (channelType != currentChannelType || channelName != currentChannelName)
|
2024-03-25 18:04:44 +02:00
|
|
|
{
|
|
|
|
// mark channel as unread
|
|
|
|
unreadChannels.insert(channelType + "_" + channelName);
|
|
|
|
widget->getAccountList()->reset();
|
|
|
|
widget->getChannelList()->reset();
|
|
|
|
widget->getMatchList()->reset();
|
|
|
|
return;
|
|
|
|
}
|
2024-03-13 23:02:53 +02:00
|
|
|
|
2023-12-27 19:07:49 +02:00
|
|
|
MetaString chatMessageFormatted;
|
|
|
|
chatMessageFormatted.appendRawString("[%s] {%s}: %s\n");
|
|
|
|
chatMessageFormatted.replaceRawString(when);
|
|
|
|
chatMessageFormatted.replaceRawString(sender);
|
|
|
|
chatMessageFormatted.replaceRawString(message);
|
|
|
|
|
|
|
|
chatHistory += chatMessageFormatted.toString();
|
2024-05-11 18:44:31 +02:00
|
|
|
}
|
|
|
|
void GlobalLobbyWindow::refreshChatText()
|
|
|
|
{
|
2023-12-27 19:07:49 +02:00
|
|
|
widget->getGameChat()->setText(chatHistory);
|
2024-05-11 22:42:14 +02:00
|
|
|
if (widget->getGameChat()->slider)
|
|
|
|
widget->getGameChat()->slider->scrollToMax();
|
2023-12-27 19:07:49 +02:00
|
|
|
}
|
2024-01-21 16:48:36 +02:00
|
|
|
|
2024-03-27 13:08:41 +02:00
|
|
|
bool GlobalLobbyWindow::isChannelUnread(const std::string & channelType, const std::string & channelName) const
|
2024-03-25 18:04:44 +02:00
|
|
|
{
|
|
|
|
return unreadChannels.count(channelType + "_" + channelName) > 0;
|
|
|
|
}
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
void GlobalLobbyWindow::onActiveAccounts(const std::vector<GlobalLobbyAccount> & accounts)
|
|
|
|
{
|
2024-03-11 17:51:11 +02:00
|
|
|
if (accounts.size() == widget->getAccountList()->size())
|
|
|
|
widget->getAccountList()->reset();
|
|
|
|
else
|
|
|
|
widget->getAccountList()->resize(accounts.size());
|
2024-03-21 18:00:05 +02:00
|
|
|
|
|
|
|
MetaString text = MetaString::createFromTextID("vcmi.lobby.header.players");
|
|
|
|
text.replaceNumber(accounts.size());
|
|
|
|
widget->getAccountListHeader()->setText(text.toString());
|
2024-01-21 16:48:36 +02:00
|
|
|
}
|
|
|
|
|
2024-05-13 15:48:01 +02:00
|
|
|
void GlobalLobbyWindow::onActiveGameRooms(const std::vector<GlobalLobbyRoom> & rooms)
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
2024-03-20 18:40:37 +02:00
|
|
|
if (rooms.size() == widget->getRoomList()->size())
|
2024-03-11 17:51:11 +02:00
|
|
|
widget->getRoomList()->reset();
|
|
|
|
else
|
|
|
|
widget->getRoomList()->resize(rooms.size());
|
2024-03-21 18:00:05 +02:00
|
|
|
|
|
|
|
MetaString text = MetaString::createFromTextID("vcmi.lobby.header.rooms");
|
|
|
|
text.replaceNumber(rooms.size());
|
|
|
|
widget->getRoomListHeader()->setText(text.toString());
|
2024-01-21 16:48:36 +02:00
|
|
|
}
|
2024-02-10 19:02:25 +02:00
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
void GlobalLobbyWindow::onMatchesHistory(const std::vector<GlobalLobbyRoom> & history)
|
|
|
|
{
|
|
|
|
if (history.size() == widget->getMatchList()->size())
|
|
|
|
widget->getMatchList()->reset();
|
|
|
|
else
|
|
|
|
widget->getMatchList()->resize(history.size());
|
2024-03-21 18:00:05 +02:00
|
|
|
|
|
|
|
MetaString text = MetaString::createFromTextID("vcmi.lobby.header.history");
|
|
|
|
text.replaceNumber(history.size());
|
|
|
|
widget->getMatchListHeader()->setText(text.toString());
|
2024-03-20 18:40:37 +02:00
|
|
|
}
|
|
|
|
|
2024-03-25 20:23:57 +02:00
|
|
|
void GlobalLobbyWindow::onInviteReceived(const std::string & invitedRoomID)
|
2024-03-25 18:04:44 +02:00
|
|
|
{
|
|
|
|
widget->getRoomList()->reset();
|
|
|
|
}
|
|
|
|
|
2024-02-10 19:02:25 +02:00
|
|
|
void GlobalLobbyWindow::onJoinedRoom()
|
|
|
|
{
|
|
|
|
widget->getAccountList()->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyWindow::onLeftRoom()
|
|
|
|
{
|
|
|
|
widget->getAccountList()->reset();
|
|
|
|
}
|