1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
* 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"
#include "GlobalLobbyServerSetup.h"
#include "GlobalLobbyWidget.h"
#include "../CServerHandler.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../widgets/CTextInput.h"
#include "../widgets/Slider.h"
#include "../widgets/ObjectLists.h"
#include "../widgets/TextControls.h"
#include "../../lib/texts/Languages.h"
#include "../../lib/texts/MetaString.h"
#include "../../lib/texts/TextOperations.h"
GlobalLobbyWindow::GlobalLobbyWindow()
: CWindowObject(BORDERED)
{
OBJECT_CONSTRUCTION;
widget = std::make_shared<GlobalLobbyWidget>(this);
pos = widget->pos;
center();
widget->getAccountNameLabel()->setText(CSH->getGlobalLobby().getAccountDisplayName());
doOpenChannel("global", "english", Languages::getLanguageOptions("english").nameNative);
widget->getChannelListHeader()->setText(MetaString::createFromTextID("vcmi.lobby.header.channels").toString());
widget->getChannelList()->resize(CSH->getGlobalLobby().getActiveChannels().size()+1);
}
bool GlobalLobbyWindow::isChannelOpen(const std::string & testChannelType, const std::string & testChannelName) const
{
return testChannelType == currentChannelType && testChannelName == currentChannelName;
}
void GlobalLobbyWindow::doOpenChannel(const std::string & channelType, const std::string & channelName, const std::string & roomDescription)
{
currentChannelType = channelType;
currentChannelName = channelName;
chatHistory.clear();
unreadChannels.erase(channelType + "_" + channelName);
auto history = CSH->getGlobalLobby().getChannelHistory(channelType, channelName);
for(const auto & entry : history)
onGameChatMessage(entry.displayName, entry.messageText, entry.timeFormatted, channelType, channelName);
refreshChatText();
MetaString text;
text.appendTextID("vcmi.lobby.header.chat." + channelType);
text.replaceRawString(roomDescription);
widget->getGameChatHeader()->setText(text.toString());
// Update currently selected item in UI
// WARNING: this invalidates function parameters since some of them are members of objects that will be destroyed by reset
widget->getAccountList()->reset();
widget->getChannelList()->reset();
widget->getMatchList()->reset();
redraw();
}
void GlobalLobbyWindow::doSendChatMessage()
{
std::string messageText = widget->getMessageInput()->getText();
JsonNode toSend;
toSend["type"].String() = "sendChatMessage";
toSend["channelType"].String() = currentChannelType;
toSend["channelName"].String() = currentChannelName;
toSend["messageText"].String() = messageText;
assert(TextOperations::isValidUnicodeString(messageText));
CSH->getGlobalLobby().sendMessage(toSend);
widget->getMessageInput()->setText("");
}
void GlobalLobbyWindow::doCreateGameRoom()
{
GH.windows().createAndPushWindow<GlobalLobbyServerSetup>();
}
void GlobalLobbyWindow::doInviteAccount(const std::string & accountID)
{
JsonNode toSend;
toSend["type"].String() = "sendInvite";
toSend["accountID"].String() = accountID;
CSH->getGlobalLobby().sendMessage(toSend);
}
void GlobalLobbyWindow::doJoinRoom(const std::string & roomID)
{
JsonNode toSend;
toSend["type"].String() = "joinGameRoom";
toSend["gameRoomID"].String() = roomID;
CSH->getGlobalLobby().sendMessage(toSend);
}
void GlobalLobbyWindow::onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when, const std::string & channelType, const std::string & channelName)
{
if (channelType != currentChannelType || channelName != currentChannelName)
{
// mark channel as unread
unreadChannels.insert(channelType + "_" + channelName);
widget->getAccountList()->reset();
widget->getChannelList()->reset();
widget->getMatchList()->reset();
return;
}
MetaString chatMessageFormatted;
chatMessageFormatted.appendRawString("[%s] {%s}: %s\n");
chatMessageFormatted.replaceRawString(when);
chatMessageFormatted.replaceRawString(sender);
chatMessageFormatted.replaceRawString(message);
chatHistory += chatMessageFormatted.toString();
}
void GlobalLobbyWindow::refreshChatText()
{
widget->getGameChat()->setText(chatHistory);
if (widget->getGameChat()->slider)
widget->getGameChat()->slider->scrollToMax();
}
bool GlobalLobbyWindow::isChannelUnread(const std::string & channelType, const std::string & channelName) const
{
return unreadChannels.count(channelType + "_" + channelName) > 0;
}
void GlobalLobbyWindow::onActiveAccounts(const std::vector<GlobalLobbyAccount> & accounts)
{
if (accounts.size() == widget->getAccountList()->size())
widget->getAccountList()->reset();
else
widget->getAccountList()->resize(accounts.size());
MetaString text = MetaString::createFromTextID("vcmi.lobby.header.players");
text.replaceNumber(accounts.size());
widget->getAccountListHeader()->setText(text.toString());
}
void GlobalLobbyWindow::onActiveGameRooms(const std::vector<GlobalLobbyRoom> & rooms)
{
if (rooms.size() == widget->getRoomList()->size())
widget->getRoomList()->reset();
else
widget->getRoomList()->resize(rooms.size());
MetaString text = MetaString::createFromTextID("vcmi.lobby.header.rooms");
text.replaceNumber(rooms.size());
widget->getRoomListHeader()->setText(text.toString());
}
void GlobalLobbyWindow::onMatchesHistory(const std::vector<GlobalLobbyRoom> & history)
{
if (history.size() == widget->getMatchList()->size())
widget->getMatchList()->reset();
else
widget->getMatchList()->resize(history.size());
MetaString text = MetaString::createFromTextID("vcmi.lobby.header.history");
text.replaceNumber(history.size());
widget->getMatchListHeader()->setText(text.toString());
}
void GlobalLobbyWindow::refreshActiveChannels()
{
const auto & activeChannels = CSH->getGlobalLobby().getActiveChannels();
if (activeChannels.size()+1 == widget->getChannelList()->size())
widget->getChannelList()->reset();
else
widget->getChannelList()->resize(activeChannels.size()+1);
if (currentChannelType == "global" && !vstd::contains(activeChannels, currentChannelName) && !activeChannels.empty())
{
doOpenChannel("global", activeChannels.front(), Languages::getLanguageOptions(activeChannels.front()).nameNative);
}
}
void GlobalLobbyWindow::onInviteReceived(const std::string & invitedRoomID)
{
widget->getRoomList()->reset();
}
void GlobalLobbyWindow::onJoinedRoom()
{
widget->getAccountList()->reset();
}
void GlobalLobbyWindow::onLeftRoom()
{
widget->getAccountList()->reset();
}
|