2023-11-11 16:43:58 +02:00
|
|
|
/*
|
|
|
|
* LobbyServer.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 "../lib/network/NetworkServer.h"
|
|
|
|
|
2023-11-12 16:35:53 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
class JsonNode;
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
2023-11-11 16:43:58 +02:00
|
|
|
class SQLiteInstance;
|
2023-11-12 15:32:54 +02:00
|
|
|
class SQLiteStatement;
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2023-11-12 15:32:54 +02:00
|
|
|
class LobbyDatabase
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
|
|
|
std::unique_ptr<SQLiteInstance> database;
|
2023-11-12 15:32:54 +02:00
|
|
|
std::unique_ptr<SQLiteStatement> insertChatMessageStatement;
|
2023-11-12 16:35:53 +02:00
|
|
|
std::unique_ptr<SQLiteStatement> getRecentMessageHistoryStatement;
|
2023-11-12 15:32:54 +02:00
|
|
|
|
|
|
|
void initializeDatabase();
|
|
|
|
void prepareStatements();
|
|
|
|
void createTableChatMessages();
|
|
|
|
public:
|
2023-11-12 16:35:53 +02:00
|
|
|
struct ChatMessage
|
|
|
|
{
|
|
|
|
std::string sender;
|
|
|
|
std::string messageText;
|
|
|
|
int messageAgeSeconds;
|
|
|
|
};
|
|
|
|
|
2023-11-12 15:32:54 +02:00
|
|
|
LobbyDatabase();
|
|
|
|
|
|
|
|
void insertChatMessage(const std::string & sender, const std::string & messageText);
|
2023-11-12 16:35:53 +02:00
|
|
|
std::vector<ChatMessage> getRecentMessageHistory();
|
2023-12-27 19:07:49 +02:00
|
|
|
bool isPlayerInGameRoom(const std::string & accountName);
|
2023-11-12 15:32:54 +02:00
|
|
|
};
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
class LobbyServer : public INetworkServerListener
|
2023-11-12 15:32:54 +02:00
|
|
|
{
|
2023-11-12 21:23:42 +02:00
|
|
|
struct AccountState
|
|
|
|
{
|
|
|
|
std::string accountName;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<std::shared_ptr<NetworkConnection>, AccountState> activeAccounts;
|
|
|
|
|
2023-11-12 15:32:54 +02:00
|
|
|
std::unique_ptr<LobbyDatabase> database;
|
2023-11-18 16:34:18 +02:00
|
|
|
std::unique_ptr<NetworkServer> networkServer;
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2023-11-12 13:27:22 +02:00
|
|
|
void onNewConnection(const std::shared_ptr<NetworkConnection> &) override;
|
2023-11-18 16:34:18 +02:00
|
|
|
void onDisconnected(const std::shared_ptr<NetworkConnection> &) override;
|
2023-11-12 13:27:22 +02:00
|
|
|
void onPacketReceived(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message) override;
|
2023-12-26 20:54:32 +02:00
|
|
|
void onTimer() override;
|
2023-11-12 16:35:53 +02:00
|
|
|
|
|
|
|
void sendMessage(const std::shared_ptr<NetworkConnection> & target, const JsonNode & json);
|
2023-12-27 14:24:49 +02:00
|
|
|
|
|
|
|
void receiveSendChatMessage(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
|
|
|
|
void receiveAuthentication(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
|
2023-12-27 19:07:49 +02:00
|
|
|
void receiveJoinGameRoom(const std::shared_ptr<NetworkConnection> & connection, const JsonNode & json);
|
2023-11-11 16:43:58 +02:00
|
|
|
public:
|
|
|
|
LobbyServer();
|
2023-11-18 16:34:18 +02:00
|
|
|
|
|
|
|
void start(uint16_t port);
|
|
|
|
void run();
|
2023-11-11 16:43:58 +02:00
|
|
|
};
|