Refactor chat room implementation
- Renamed Room class to ChatRoom and updated all references accordingly. - Removed the old room.cpp and room.h files. - Updated CMakeLists.txt to include chat_room.cpp instead of room.cpp. - Modified ChatUser class to use shared_ptr<ChatRoom> instead of shared_ptr<Room>. - Updated Server class to create instances of ChatRoom instead of Room. - Removed User class and its associated files, integrating its functionality into ChatUser. - Ensured all relevant includes and dependencies are updated to reflect the new class structure.
This commit is contained in:
89
chat_room.h
Executable file
89
chat_room.h
Executable file
@@ -0,0 +1,89 @@
|
||||
// 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 <base.h>
|
||||
#include "chat_user.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();
|
||||
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;
|
||||
void _handleDice();
|
||||
void _startDiceRound();
|
||||
void _endDiceRound();
|
||||
void _initRound();
|
||||
void _showDiceRoundResults();
|
||||
};
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yc
|
||||
|
||||
#endif // YC_LIB_CHAT_ROOM_H
|
||||
Reference in New Issue
Block a user