2023-11-11 16:43:58 +02:00
|
|
|
/*
|
|
|
|
* LobbyWindow.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 "LobbyWindow.h"
|
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/WindowHandler.h"
|
2023-11-12 15:32:54 +02:00
|
|
|
#include "../widgets/TextControls.h"
|
2023-11-12 13:27:22 +02:00
|
|
|
#include "../windows/InfoWindows.h"
|
|
|
|
|
2023-11-12 16:35:53 +02:00
|
|
|
#include "../../lib/MetaString.h"
|
|
|
|
|
2023-11-12 15:32:54 +02:00
|
|
|
LobbyClient::LobbyClient(LobbyWindow * window)
|
|
|
|
: window(window)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-11 16:43:58 +02:00
|
|
|
void LobbyClient::onPacketReceived(const std::vector<uint8_t> & message)
|
|
|
|
{
|
2023-11-12 16:35:53 +02:00
|
|
|
// FIXME: find better approach
|
|
|
|
const char * payloadBegin = reinterpret_cast<const char*>(message.data());
|
|
|
|
JsonNode json(payloadBegin, message.size());
|
|
|
|
|
|
|
|
if (json["type"].String() == "chatHistory")
|
|
|
|
{
|
|
|
|
for (auto const & entry : json["messages"].Vector())
|
|
|
|
{
|
|
|
|
std::string senderName = entry["senderName"].String();
|
|
|
|
std::string messageText = entry["messageText"].String();
|
|
|
|
int ageSeconds = entry["ageSeconds"].Integer();
|
|
|
|
|
|
|
|
// FIXME: better/unified way to format date
|
|
|
|
auto timeNowChrono = std::chrono::system_clock::now();
|
|
|
|
timeNowChrono -= std::chrono::seconds(ageSeconds);
|
|
|
|
|
|
|
|
std::time_t timeNowC = std::chrono::system_clock::to_time_t(timeNowChrono);
|
|
|
|
std::tm timeNowTm = *std::localtime(&timeNowC);
|
|
|
|
|
|
|
|
MetaString dateFormatted;
|
|
|
|
dateFormatted.appendRawString("%d:%d");
|
|
|
|
dateFormatted.replaceNumber(timeNowTm.tm_hour);
|
|
|
|
dateFormatted.replaceNumber(timeNowTm.tm_min);
|
|
|
|
|
|
|
|
window->onGameChatMessage(senderName, messageText, dateFormatted.toString());
|
|
|
|
}
|
|
|
|
}
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
2023-11-12 13:27:22 +02:00
|
|
|
void LobbyClient::onConnectionFailed(const std::string & errorMessage)
|
|
|
|
{
|
|
|
|
GH.windows().popWindows(1);
|
|
|
|
CInfoWindow::showInfoDialog("Failed to connect to game lobby!\n" + errorMessage, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyClient::onDisconnected()
|
|
|
|
{
|
|
|
|
GH.windows().popWindows(1);
|
|
|
|
CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
|
|
|
|
}
|
|
|
|
|
2023-11-12 15:32:54 +02:00
|
|
|
void LobbyClient::sendMessage(const JsonNode & data)
|
|
|
|
{
|
|
|
|
std::string payloadString = data.toJson(true);
|
|
|
|
|
|
|
|
// FIXME: find better approach
|
|
|
|
uint8_t * payloadBegin = reinterpret_cast<uint8_t*>(payloadString.data());
|
|
|
|
uint8_t * payloadEnd = payloadBegin + payloadString.size();
|
|
|
|
|
|
|
|
std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
|
|
|
|
|
|
|
|
sendPacket(payloadBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
LobbyWidget::LobbyWidget(LobbyWindow * window)
|
|
|
|
: window(window)
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
|
|
|
addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
|
2023-11-12 15:32:54 +02:00
|
|
|
addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
|
2023-11-11 16:43:58 +02:00
|
|
|
|
|
|
|
const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
|
|
|
|
build(config);
|
|
|
|
}
|
|
|
|
|
2023-11-12 15:32:54 +02:00
|
|
|
std::shared_ptr<CTextInput> LobbyWidget::getMessageInput()
|
|
|
|
{
|
|
|
|
return widget<CTextInput>("messageInput");
|
|
|
|
}
|
|
|
|
|
2023-11-12 16:35:53 +02:00
|
|
|
std::shared_ptr<CTextBox> LobbyWidget::getGameChat()
|
|
|
|
{
|
|
|
|
return widget<CTextBox>("gameChat");
|
|
|
|
}
|
|
|
|
|
2023-11-11 16:43:58 +02:00
|
|
|
LobbyWindow::LobbyWindow():
|
|
|
|
CWindowObject(BORDERED)
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
2023-11-12 15:32:54 +02:00
|
|
|
widget = std::make_shared<LobbyWidget>(this);
|
2023-11-11 16:43:58 +02:00
|
|
|
pos = widget->pos;
|
|
|
|
center();
|
2023-11-12 15:32:54 +02:00
|
|
|
connection = std::make_shared<LobbyClient>(this);
|
2023-11-11 16:43:58 +02:00
|
|
|
|
|
|
|
connection->start("127.0.0.1", 30303);
|
2023-11-12 13:27:22 +02:00
|
|
|
|
|
|
|
addUsedEvents(TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyWindow::tick(uint32_t msPassed)
|
|
|
|
{
|
|
|
|
connection->poll();
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
2023-11-12 15:32:54 +02:00
|
|
|
|
|
|
|
void LobbyWindow::doSendChatMessage()
|
|
|
|
{
|
|
|
|
std::string messageText = widget->getMessageInput()->getText();
|
|
|
|
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "sendChatMessage";
|
|
|
|
toSend["messageText"].String() = messageText;
|
|
|
|
|
|
|
|
connection->sendMessage(toSend);
|
|
|
|
|
|
|
|
widget->getMessageInput()->setText("");
|
|
|
|
}
|
2023-11-12 16:35:53 +02:00
|
|
|
|
|
|
|
void LobbyWindow::onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when)
|
|
|
|
{
|
|
|
|
MetaString chatMessageFormatted;
|
|
|
|
chatMessageFormatted.appendRawString("[%s] {%s}: %s\n");
|
|
|
|
chatMessageFormatted.replaceRawString(when);
|
|
|
|
chatMessageFormatted.replaceRawString(sender);
|
|
|
|
chatMessageFormatted.replaceRawString(message);
|
|
|
|
|
|
|
|
chatHistory += chatMessageFormatted.toString();
|
|
|
|
|
|
|
|
widget->getGameChat()->setText(chatHistory);
|
|
|
|
}
|