1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Split LobbyWindow file into one file per class

This commit is contained in:
Ivan Savenko 2023-12-27 19:07:49 +02:00
parent 20a38d8514
commit 78833a1015
10 changed files with 216 additions and 109 deletions

View File

@ -95,7 +95,9 @@ set(client_SRCS
renderSDL/ScreenHandler.cpp
renderSDL/SDL_Extensions.cpp
serverLobby/LobbyWindow.cpp
globalLobby/GlobalLobbyClient.cpp
globalLobby/GlobalLobbyWidget.cpp
globalLobby/GlobalLobbyWindow.cpp
widgets/Buttons.cpp
widgets/CArtifactHolder.cpp
@ -272,7 +274,9 @@ set(client_HEADERS
renderSDL/SDL_Extensions.h
renderSDL/SDL_PixelAccess.h
serverLobby/LobbyWindow.h
globalLobby/GlobalLobbyClient.h
globalLobby/GlobalLobbyWidget.h
globalLobby/GlobalLobbyWindow.h
widgets/Buttons.h
widgets/CArtifactHolder.h

View File

@ -1,5 +1,5 @@
/*
* LobbyWindow.cpp, part of VCMI engine
* GlobalLobbyClient.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
@ -9,17 +9,20 @@
*/
#include "StdInc.h"
#include "LobbyWindow.h"
#include "GlobalLobbyClient.h"
#include "GlobalLobbyWindow.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../widgets/TextControls.h"
#include "../windows/InfoWindows.h"
#include "../../lib/MetaString.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/network/NetworkClient.h"
GlobalLobbyClient::~GlobalLobbyClient() = default;
GlobalLobbyClient::GlobalLobbyClient(GlobalLobbyWindow * window)
: networkClient(std::make_unique<NetworkClient>(*this))
, window(window)
@ -122,74 +125,3 @@ void GlobalLobbyClient::poll()
{
networkClient->poll();
}
GlobalLobbyWidget::GlobalLobbyWidget(GlobalLobbyWindow * window)
: window(window)
{
addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
build(config);
}
std::shared_ptr<CLabel> GlobalLobbyWidget::getAccountNameLabel()
{
return widget<CLabel>("accountNameLabel");
}
std::shared_ptr<CTextInput> GlobalLobbyWidget::getMessageInput()
{
return widget<CTextInput>("messageInput");
}
std::shared_ptr<CTextBox> GlobalLobbyWidget::getGameChat()
{
return widget<CTextBox>("gameChat");
}
GlobalLobbyWindow::GlobalLobbyWindow():
CWindowObject(BORDERED)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
widget = std::make_shared<GlobalLobbyWidget>(this);
pos = widget->pos;
center();
connection = std::make_shared<GlobalLobbyClient>(this);
connection->start("127.0.0.1", 30303);
widget->getAccountNameLabel()->setText(settings["general"]["playerName"].String());
addUsedEvents(TIME);
}
void GlobalLobbyWindow::tick(uint32_t msPassed)
{
connection->poll();
}
void GlobalLobbyWindow::doSendChatMessage()
{
std::string messageText = widget->getMessageInput()->getText();
JsonNode toSend;
toSend["type"].String() = "sendChatMessage";
toSend["messageText"].String() = messageText;
connection->sendMessage(toSend);
widget->getMessageInput()->setText("");
}
void GlobalLobbyWindow::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);
}

View File

@ -1,5 +1,5 @@
/*
* LobbyWindow.h, part of VCMI engine
* GlobalLobbyClient.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
@ -9,24 +9,14 @@
*/
#pragma once
#include "../gui/InterfaceObjectConfigurable.h"
#include "../windows/CWindowObject.h"
#include "../../lib/network/NetworkListener.h"
VCMI_LIB_NAMESPACE_BEGIN
class JsonNode;
VCMI_LIB_NAMESPACE_END
class GlobalLobbyWindow;
class GlobalLobbyWidget : public InterfaceObjectConfigurable
{
GlobalLobbyWindow * window;
public:
GlobalLobbyWidget(GlobalLobbyWindow * window);
std::shared_ptr<CLabel> getAccountNameLabel();
std::shared_ptr<CTextInput> getMessageInput();
std::shared_ptr<CTextBox> getGameChat();
};
class GlobalLobbyClient : public INetworkClientListener
{
std::unique_ptr<NetworkClient> networkClient;
@ -40,6 +30,7 @@ class GlobalLobbyClient : public INetworkClientListener
public:
explicit GlobalLobbyClient(GlobalLobbyWindow * window);
~GlobalLobbyClient();
void sendMessage(const JsonNode & data);
void start(const std::string & host, uint16_t port);
@ -47,20 +38,3 @@ public:
void poll();
};
class GlobalLobbyWindow : public CWindowObject
{
std::string chatHistory;
std::shared_ptr<GlobalLobbyWidget> widget;
std::shared_ptr<GlobalLobbyClient> connection;
void tick(uint32_t msPassed);
public:
GlobalLobbyWindow();
void doSendChatMessage();
void onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when);
};

View File

@ -0,0 +1,42 @@
/*
* GlobalLobbyWidget.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 "GlobalLobbyWidget.h"
#include "GlobalLobbyWindow.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../widgets/TextControls.h"
GlobalLobbyWidget::GlobalLobbyWidget(GlobalLobbyWindow * window)
: window(window)
{
addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
build(config);
}
std::shared_ptr<CLabel> GlobalLobbyWidget::getAccountNameLabel()
{
return widget<CLabel>("accountNameLabel");
}
std::shared_ptr<CTextInput> GlobalLobbyWidget::getMessageInput()
{
return widget<CTextInput>("messageInput");
}
std::shared_ptr<CTextBox> GlobalLobbyWidget::getGameChat()
{
return widget<CTextBox>("gameChat");
}

View File

@ -0,0 +1,25 @@
/*
* GlobalLobbyWidget.h, 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
*
*/
#pragma once
#include "../gui/InterfaceObjectConfigurable.h"
class GlobalLobbyWindow;
class GlobalLobbyWidget : public InterfaceObjectConfigurable
{
GlobalLobbyWindow * window;
public:
GlobalLobbyWidget(GlobalLobbyWindow * window);
std::shared_ptr<CLabel> getAccountNameLabel();
std::shared_ptr<CTextInput> getMessageInput();
std::shared_ptr<CTextBox> getGameChat();
};

View File

@ -0,0 +1,67 @@
/*
* 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 "GlobalLobbyWidget.h"
#include "GlobalLobbyClient.h"
#include "../gui/CGuiHandler.h"
#include "../widgets/TextControls.h"
#include "../../lib/MetaString.h"
#include "../../lib/CConfigHandler.h"
GlobalLobbyWindow::GlobalLobbyWindow():
CWindowObject(BORDERED)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
widget = std::make_shared<GlobalLobbyWidget>(this);
pos = widget->pos;
center();
connection = std::make_shared<GlobalLobbyClient>(this);
connection->start("127.0.0.1", 30303);
widget->getAccountNameLabel()->setText(settings["general"]["playerName"].String());
addUsedEvents(TIME);
}
void GlobalLobbyWindow::tick(uint32_t msPassed)
{
connection->poll();
}
void GlobalLobbyWindow::doSendChatMessage()
{
std::string messageText = widget->getMessageInput()->getText();
JsonNode toSend;
toSend["type"].String() = "sendChatMessage";
toSend["messageText"].String() = messageText;
connection->sendMessage(toSend);
widget->getMessageInput()->setText("");
}
void GlobalLobbyWindow::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);
}

View File

@ -0,0 +1,32 @@
/*
* GlobalLobbyWindow.h, 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
*
*/
#pragma once
#include "../windows/CWindowObject.h"
class GlobalLobbyWidget;
class GlobalLobbyClient;
class GlobalLobbyWindow : public CWindowObject
{
std::string chatHistory;
std::shared_ptr<GlobalLobbyWidget> widget;
std::shared_ptr<GlobalLobbyClient> connection;
void tick(uint32_t msPassed);
public:
GlobalLobbyWindow();
void doSendChatMessage();
void onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when);
};

View File

@ -24,7 +24,7 @@
#include "../gui/Shortcut.h"
#include "../gui/WindowHandler.h"
#include "../render/Canvas.h"
#include "../serverLobby/LobbyWindow.h"
#include "../globalLobby/GlobalLobbyWindow.h"
#include "../widgets/CComponent.h"
#include "../widgets/Buttons.h"
#include "../widgets/MiscWidgets.h"

View File

@ -78,6 +78,11 @@ void LobbyDatabase::insertChatMessage(const std::string & sender, const std::str
insertChatMessageStatement->reset();
}
bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountName)
{
return false; //TODO
}
std::vector<LobbyDatabase::ChatMessage> LobbyDatabase::getRecentMessageHistory()
{
std::vector<LobbyDatabase::ChatMessage> result;
@ -132,6 +137,9 @@ void LobbyServer::onPacketReceived(const std::shared_ptr<NetworkConnection> & co
if (json["type"].String() == "authentication")
return receiveAuthentication(connection, json);
if (json["type"].String() == "joinGameRoom")
return receiveJoinGameRoom(connection, json);
}
void LobbyServer::receiveSendChatMessage(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json)
@ -157,6 +165,12 @@ void LobbyServer::receiveAuthentication(const std::shared_ptr<NetworkConnection>
{
std::string accountName = json["accountName"].String();
// TODO: account cookie check
// TODO: account password check
// TODO: protocol version number
// TODO: client/server mode flag
// TODO: client language
activeAccounts[connection].accountName = accountName;
auto history = database->getRecentMessageHistory();
@ -178,6 +192,21 @@ void LobbyServer::receiveAuthentication(const std::shared_ptr<NetworkConnection>
sendMessage(connection, reply);
}
void LobbyServer::receiveJoinGameRoom(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json)
{
if (activeAccounts.count(connection) == 0)
return; // unauthenticated
std::string senderName = activeAccounts[connection].accountName;
if (database->isPlayerInGameRoom(senderName))
return; // only 1 room per player allowed
// TODO: roomType: private, public
// TODO: additional flags, e.g. allowCheats
// TODO: connection mode: direct or proxy
}
LobbyServer::LobbyServer()
: database(new LobbyDatabase())
, networkServer(new NetworkServer(*this))

View File

@ -39,6 +39,7 @@ public:
void insertChatMessage(const std::string & sender, const std::string & messageText);
std::vector<ChatMessage> getRecentMessageHistory();
bool isPlayerInGameRoom(const std::string & accountName);
};
class LobbyServer : public INetworkServerListener
@ -62,6 +63,7 @@ class LobbyServer : public INetworkServerListener
void receiveSendChatMessage(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
void receiveAuthentication(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
void receiveJoinGameRoom(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
public:
LobbyServer();