Refactor chat system: Introduce ChatRoom and ChatUser classes

- Created ChatRoom class to manage chat room functionalities, including user management, message handling, and game mechanics.
- Developed ChatUser class to represent individual users, handling user-specific actions and interactions within chat rooms.
- Implemented a Config class for loading configuration settings from a JSON file.
- Established a Server class to manage connections, handle requests, and facilitate communication between users and chat rooms.
- Introduced a Database class for database interactions, utilizing PostgreSQL for user and room data management.
- Added utility functions in the Base class for JSON handling and socket communication.
- Created Object classes for Room and User to encapsulate their properties and behaviors.
- Updated main function to initialize server and load chat rooms from configuration.
This commit is contained in:
Torsten Schulz (local)
2025-08-11 16:07:15 +02:00
parent 6ecdbda9de
commit 864d86aa09
28 changed files with 693 additions and 343 deletions

96
src/core/chat_room.h Executable file
View File

@@ -0,0 +1,96 @@
// renamed from room.h
#ifndef YC_LIB_CHAT_ROOM_H
#define YC_LIB_CHAT_ROOM_H
#include <vector>
#include <string>
#include <queue>
#include <thread>
#include <future>
#include <utility>
#include <vector>
#include "lib/base.h"
#include "chat_user.h"
#include "object/room.h"
namespace Yc
{
namespace Lib
{
class Server;
class ChatRoom : public Base, public std::enable_shared_from_this<ChatRoom>
{
public:
enum RoomType
{
none = 0,
dice = 1,
poker = 2,
rounds = 4,
password = 8
};
ChatRoom(std::shared_ptr<Server> parent, Json::Value roomParams);
~ChatRoom();
std::shared_ptr<Server> getServer() const { return _parent; }
void run();
std::string name();
bool addUser(std::string userName, std::string color, std::string password, int socket);
bool addUser(std::shared_ptr<ChatUser> user, std::string password);
bool userNameExists(std::string userName);
void removeUser(std::string _token, bool silent = false);
void removeUser(std::shared_ptr<ChatUser> user, bool silent = false);
void setStop();
void addMessage(ChatUser::MsgType type, const char *messageText, std::string userName = "", std::string color = "");
void addMessage(ChatUser::MsgType type, std::string messageText, std::string userName = "", std::string color = "");
void addMessage(ChatUser::MsgType type, Json::Value messageText, std::string userName = "", std::string color = "");
RoomType type();
bool isType(RoomType type);
bool canDice();
unsigned int addDice(std::shared_ptr<ChatUser> user, int diceValue);
bool accessAllowed(std::string userName, std::string password);
bool userIsInRoom(std::string userName);
void addUserWhenQueueEmpty(std::shared_ptr<ChatUser> user);
bool userToNewRoom(std::shared_ptr<ChatUser> user, std::string newRoom, std::string password);
unsigned int flags();
Json::Value userList();
private:
struct Message
{
ChatUser::MsgType type;
std::string messageTr;
std::string userName;
std::string color;
};
std::shared_ptr<Server> _parent;
std::string _name;
std::string _password;
std::vector<std::string> _allowedUsers;
RoomType _type;
std::vector<std::shared_ptr<ChatUser>> _users;
bool _blocked;
bool _stop;
std::queue<Message> _msgQueue;
std::unique_ptr<std::thread> thread;
bool _roundRunning;
time_t _roundStart;
time_t _lastRoundEnd;
int _roundLength;
std::vector<std::pair<std::shared_ptr<ChatUser>, int>> _diceValues;
Yc::Object::Room _room;
void _handleDice();
void _startDiceRound();
void _endDiceRound();
void _initRound();
void _showDiceRoundResults();
};
} // namespace Lib
} // namespace Yc
#endif // YC_LIB_CHAT_ROOM_H