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:
Torsten Schulz (local)
2025-08-11 15:03:11 +02:00
parent b81f2de10f
commit 6ecdbda9de
9 changed files with 48 additions and 456 deletions

View File

@@ -5,6 +5,5 @@ project(YourChat VERSION 0.1)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR})
add_executable(yourchat main.cpp base.cpp config.cpp server.cpp room.cpp tools.cpp chat_user.cpp database.cpp) add_executable(yourchat main.cpp base.cpp config.cpp server.cpp chat_room.cpp tools.cpp chat_user.cpp database.cpp)
# Removed chatroom.cpp, chatuser.cpp, chatroom.h, chatuser.h from the build
target_link_libraries(yourchat jsoncpp pthread pqxx) target_link_libraries(yourchat jsoncpp pthread pqxx)

View File

@@ -1,4 +1,4 @@
#include "room.h" #include "chat_room.h"
#include <algorithm> #include <algorithm>
#include <server.h> #include <server.h>
@@ -12,7 +12,7 @@
namespace Yc { namespace Yc {
namespace Lib { namespace Lib {
Room::Room(std::shared_ptr<Server> parent, Json::Value roomParams) : ChatRoom::ChatRoom(std::shared_ptr<Server> parent, Json::Value roomParams) :
_parent(std::move(parent)), _parent(std::move(parent)),
_blocked(false), _blocked(false),
_stop(false), _stop(false),
@@ -26,12 +26,12 @@ namespace Yc {
_type = (RoomType)roomParams["type"].asInt(); _type = (RoomType)roomParams["type"].asInt();
_roundLength = roomParams["roundlength"].asInt(); _roundLength = roomParams["roundlength"].asInt();
_lastRoundEnd = std::time(NULL); _lastRoundEnd = std::time(NULL);
thread = std::make_unique<std::thread>(&Room::run, this); thread = std::make_unique<std::thread>(&ChatRoom::run, this);
} }
Room::~Room() = default; ChatRoom::~ChatRoom() = default;
void Room::run() { void ChatRoom::run() {
while (!_stop) { while (!_stop) {
if (_msgQueue.size() > 0 && !_blocked) { if (_msgQueue.size() > 0 && !_blocked) {
_blocked = true; _blocked = true;
@@ -51,7 +51,7 @@ namespace Yc {
} }
} }
bool Room::addUser(std::string _userName, std::string color, std::string _password, int socket) { bool ChatRoom::addUser(std::string _userName, std::string color, std::string _password, int socket) {
if (_password != "" && _password == _password && std::find(std::begin(_allowedUsers), std::end(_allowedUsers), _userName) == std::end(_allowedUsers)) { if (_password != "" && _password == _password && std::find(std::begin(_allowedUsers), std::end(_allowedUsers), _userName) == std::end(_allowedUsers)) {
return false; return false;
} }
@@ -63,7 +63,7 @@ namespace Yc {
return true; return true;
} }
bool Room::addUser(std::shared_ptr<ChatUser> user, std::string password) { bool ChatRoom::addUser(std::shared_ptr<ChatUser> user, std::string password) {
if (password == _password) { if (password == _password) {
_users.push_back(user); _users.push_back(user);
user->setParent(shared_from_this()); user->setParent(shared_from_this());
@@ -73,7 +73,7 @@ namespace Yc {
return false; return false;
} }
bool Room::userNameExists(std::string userName) { bool ChatRoom::userNameExists(std::string userName) {
for (const auto &user: _users) { for (const auto &user: _users) {
if (user->name() == userName) { if (user->name() == userName) {
return true; return true;
@@ -82,7 +82,7 @@ namespace Yc {
return false; return false;
} }
void Room::removeUser(std::string _token, bool silent) { void ChatRoom::removeUser(std::string _token, bool silent) {
for (auto it = _users.begin(); it != _users.end(); ++it) { for (auto it = _users.begin(); it != _users.end(); ++it) {
if ((*it)->validateToken(_token)) { if ((*it)->validateToken(_token)) {
if (!silent) { if (!silent) {
@@ -94,7 +94,7 @@ namespace Yc {
} }
} }
void Room::removeUser(std::shared_ptr<ChatUser> userToRemove, bool silent) { void ChatRoom::removeUser(std::shared_ptr<ChatUser> userToRemove, bool silent) {
for (auto it = _users.begin(); it != _users.end(); ++it) { for (auto it = _users.begin(); it != _users.end(); ++it) {
if (*it == userToRemove) { if (*it == userToRemove) {
if (!silent) { if (!silent) {
@@ -106,15 +106,15 @@ namespace Yc {
} }
} }
void Room::setStop() { void ChatRoom::setStop() {
_stop = true; _stop = true;
} }
void Room::addMessage(ChatUser::MsgType type, const char *messageText, std::string userName, std::string color) { void ChatRoom::addMessage(ChatUser::MsgType type, const char *messageText, std::string userName, std::string color) {
addMessage(type, (std::string)messageText, userName, color); addMessage(type, (std::string)messageText, userName, color);
} }
void Room::addMessage(ChatUser::MsgType type, std::string messageText, std::string userName, std::string color) { void ChatRoom::addMessage(ChatUser::MsgType type, std::string messageText, std::string userName, std::string color) {
Message message; Message message;
message.type = type; message.type = type;
message.messageTr = messageText; message.messageTr = messageText;
@@ -123,11 +123,11 @@ namespace Yc {
_msgQueue.push(message); _msgQueue.push(message);
} }
void Room::addMessage(ChatUser::MsgType type, Json::Value messageText, std::string userName, std::string color) { void ChatRoom::addMessage(ChatUser::MsgType type, Json::Value messageText, std::string userName, std::string color) {
addMessage(type, getJsonString(messageText), userName, color); addMessage(type, getJsonString(messageText), userName, color);
} }
void Room::addUserWhenQueueEmpty(std::shared_ptr<ChatUser> user) { void ChatRoom::addUserWhenQueueEmpty(std::shared_ptr<ChatUser> user) {
while (_msgQueue.size() > 0) { while (_msgQueue.size() > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
@@ -135,19 +135,19 @@ namespace Yc {
user->setParent(shared_from_this()); user->setParent(shared_from_this());
} }
bool Room::userToNewRoom(std::shared_ptr<ChatUser> user, std::string newRoom, std::string password) { bool ChatRoom::userToNewRoom(std::shared_ptr<ChatUser> user, std::string newRoom, std::string password) {
return _parent->changeRoom(user, newRoom, password); return _parent->changeRoom(user, newRoom, password);
} }
unsigned int Room::flags() { unsigned int ChatRoom::flags() {
unsigned int value = (unsigned int)_type; unsigned int value = (unsigned int)_type;
if (_password != "") { if (_password != "") {
value += (unsigned int)Room::password; value += (unsigned int)ChatRoom::password;
} }
return value; return value;
} }
Json::Value Room::userList() { Json::Value ChatRoom::userList() {
Json::Value users = Json::arrayValue; Json::Value users = Json::arrayValue;
for (auto &user: _users) { for (auto &user: _users) {
Json::Value jsonUser = Json::objectValue; Json::Value jsonUser = Json::objectValue;
@@ -157,19 +157,19 @@ namespace Yc {
return users; return users;
} }
Room::RoomType Room::type() { ChatRoom::RoomType ChatRoom::type() {
return _type; return _type;
} }
bool Room::isType(Room::RoomType type) { bool ChatRoom::isType(ChatRoom::RoomType type) {
return ((_type & type) == type); return ((_type & type) == type);
} }
bool Room::canDice() { bool ChatRoom::canDice() {
return (_roundRunning || (_type & rounds) == rounds) && (_type & dice) == dice; return (_roundRunning || (_type & rounds) == rounds) && (_type & dice) == dice;
} }
unsigned int Room::addDice(std::shared_ptr<ChatUser> user, int diceValue) { unsigned int ChatRoom::addDice(std::shared_ptr<ChatUser> user, int diceValue) {
if (!canDice()) { if (!canDice()) {
return 1; return 1;
} }
@@ -183,11 +183,11 @@ namespace Yc {
return 0; return 0;
} }
bool Room::accessAllowed(std::string userName, std::string password) { bool ChatRoom::accessAllowed(std::string userName, std::string password) {
return (_allowedUsers.size() == 0 || _password == "" || _password == password || std::find(_allowedUsers.begin(), _allowedUsers.end(), userName) != _allowedUsers.end()); return (_allowedUsers.size() == 0 || _password == "" || _password == password || std::find(_allowedUsers.begin(), _allowedUsers.end(), userName) != _allowedUsers.end());
} }
bool Room::userIsInRoom(std::string userName) { bool ChatRoom::userIsInRoom(std::string userName) {
for (auto &user: _users) { for (auto &user: _users) {
if (userName == user->name()) { if (userName == user->name()) {
return true; return true;
@@ -196,7 +196,7 @@ namespace Yc {
return false; return false;
} }
void Room::_handleDice() { void ChatRoom::_handleDice() {
if (((_type & rounds) == rounds)) { if (((_type & rounds) == rounds)) {
if (_roundRunning && (_users.size() < 2 || _roundStart + _roundLength >= time(NULL))) { if (_roundRunning && (_users.size() < 2 || _roundStart + _roundLength >= time(NULL))) {
_lastRoundEnd = time(NULL); _lastRoundEnd = time(NULL);
@@ -212,14 +212,14 @@ namespace Yc {
} }
} }
void Room::_initRound() { void ChatRoom::_initRound() {
if (_users.size() == 2) { if (_users.size() == 2) {
_lastRoundEnd = time(NULL); _lastRoundEnd = time(NULL);
addMessage(ChatUser::system, "next_round_starts_soon"); addMessage(ChatUser::system, "next_round_starts_soon");
} }
} }
void Room::_showDiceRoundResults() { void ChatRoom::_showDiceRoundResults() {
std::sort(_diceValues.begin(), _diceValues.end(), [=](const std::pair<std::shared_ptr<ChatUser>, int>& val1, const std::pair<std::shared_ptr<ChatUser>, int>& val2) { std::sort(_diceValues.begin(), _diceValues.end(), [=](const std::pair<std::shared_ptr<ChatUser>, int>& val1, const std::pair<std::shared_ptr<ChatUser>, int>& val2) {
return (val1.second > val2.second); return (val1.second > val2.second);
}); });
@@ -233,7 +233,7 @@ namespace Yc {
addMessage(ChatUser::result, userList); addMessage(ChatUser::result, userList);
} }
std::string Room::name() { std::string ChatRoom::name() {
return _name; return _name;
} }

View File

@@ -1,5 +1,6 @@
#ifndef YC_LIB_ROOM_H // renamed from room.h
#define YC_LIB_ROOM_H #ifndef YC_LIB_CHAT_ROOM_H
#define YC_LIB_CHAT_ROOM_H
#include <vector> #include <vector>
@@ -19,7 +20,7 @@ namespace Yc {
class Server; class Server;
class Room: public Base, public std::enable_shared_from_this<Room> { class ChatRoom: public Base, public std::enable_shared_from_this<ChatRoom> {
public: public:
enum RoomType { enum RoomType {
none = 0, none = 0,
@@ -29,8 +30,8 @@ namespace Yc {
password = 8 password = 8
}; };
Room(std::shared_ptr<Server> parent, Json::Value roomParams); ChatRoom(std::shared_ptr<Server> parent, Json::Value roomParams);
~Room(); ~ChatRoom();
void run(); void run();
std::string name(); std::string name();
bool addUser(std::string userName, std::string color, std::string password, int socket); bool addUser(std::string userName, std::string color, std::string password, int socket);
@@ -85,4 +86,4 @@ namespace Yc {
} // namespace Lib } // namespace Lib
} // namespace Yc } // namespace Yc
#endif // YC_LIB_ROOM_H #endif // YC_LIB_CHAT_ROOM_H

View File

@@ -6,7 +6,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include "room.h" #include "chat_room.h"
#include <iostream> #include <iostream>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
@@ -15,7 +15,7 @@
namespace Yc { namespace Yc {
namespace Lib { namespace Lib {
ChatUser::ChatUser(std::shared_ptr<Room> parent, std::string name, std::string color, int socket) : ChatUser::ChatUser(std::shared_ptr<ChatRoom> parent, std::string name, std::string color, int socket) :
_parent(std::move(parent)), _parent(std::move(parent)),
_name(std::move(name)), _name(std::move(name)),
_color(std::move(color)), _color(std::move(color)),
@@ -96,7 +96,7 @@ std::string ChatUser::color() const {
return _color; return _color;
} }
void ChatUser::setParent(std::shared_ptr<Room> parent) { void ChatUser::setParent(std::shared_ptr<ChatRoom> parent) {
_parent = std::move(parent); _parent = std::move(parent);
} }

View File

@@ -1,21 +1,18 @@
#ifndef YC_LIB_CHAT_USER_H #ifndef YC_LIB_CHAT_USER_H
#define YC_LIB_CHAT_USER_H #define YC_LIB_CHAT_USER_H
#include <memory> #include <memory>
#include <string> #include <string>
#include <set> #include <set>
#include <json/json.h> #include <json/json.h>
#include <thread> #include <thread>
#include "base.h" #include "base.h"
// Forward declaration to break cyclic dependency
namespace Yc { namespace Lib { class Room; } }
namespace Yc { namespace Yc {
namespace Lib { namespace Lib {
class ChatRoom;
class ChatUser: public Base, public std::enable_shared_from_this<ChatUser> { class ChatUser: public Base, public std::enable_shared_from_this<ChatUser> {
public: public:
enum MsgType { enum MsgType {
@@ -31,7 +28,7 @@ public:
result = 9 result = 9
}; };
ChatUser(std::shared_ptr<Room> parent, std::string name, std::string color, int socket); ChatUser(std::shared_ptr<ChatRoom> parent, std::string name, std::string color, int socket);
~ChatUser(); ~ChatUser();
std::string name() const; std::string name() const;
bool validateToken(std::string token); bool validateToken(std::string token);
@@ -42,10 +39,10 @@ public:
void checkerTask(); void checkerTask();
void stop(); void stop();
std::string color() const; std::string color() const;
void setParent(std::shared_ptr<Room> parent); void setParent(std::shared_ptr<ChatRoom> parent);
private: private:
std::shared_ptr<Room> _parent; std::shared_ptr<ChatRoom> _parent;
std::string _name; std::string _name;
std::string _color; std::string _color;
int _socket; int _socket;

View File

@@ -116,7 +116,7 @@ namespace Yc {
void Server::createRooms(Json::Value roomList) { void Server::createRooms(Json::Value roomList) {
auto self = shared_from_this(); auto self = shared_from_this();
for (auto &room: roomList) { for (auto &room: roomList) {
auto newRoom = std::make_shared<Room>(self, room); auto newRoom = std::make_shared<ChatRoom>(self, room);
_rooms.push_back(newRoom); _rooms.push_back(newRoom);
} }
} }

View File

@@ -4,7 +4,7 @@
#include "config.h" #include "config.h"
#include "database.h" #include "database.h"
#include <vector> #include <vector>
#include "room.h" #include "chat_room.h"
#include "base.h" #include "base.h"
#include <json/value.h> #include <json/value.h>
@@ -24,7 +24,7 @@ namespace Yc {
std::shared_ptr<Yc::Lib::Config> _config; std::shared_ptr<Yc::Lib::Config> _config;
std::shared_ptr<Yc::Lib::Database> _database; std::shared_ptr<Yc::Lib::Database> _database;
bool _stop; bool _stop;
std::vector<std::shared_ptr<Yc::Lib::Room>> _rooms; std::vector<std::shared_ptr<Yc::Lib::ChatRoom>> _rooms;
void createRooms(Json::Value roomList); void createRooms(Json::Value roomList);
private: private:
void handleRequest(); void handleRequest();

344
user.cpp
View File

@@ -1,344 +0,0 @@
#include "user.h"
#include <tools.h>
#include <json/json.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "room.h"
#include <iostream>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sstream>
namespace Yc {
namespace Lib {
User::User(std::shared_ptr<Room> parent, std::string name, std::string color, int socket) :
_parent(std::move(parent)),
_name(std::move(name)),
_color(std::move(color)),
_socket(socket),
_stop(false) {
_token = Yc::Lib::Tools::generateRandomString(32);
sendMsg(token, _token, "", "");
thread = std::make_unique<std::thread>(&User::checkerTask, this);
}
User::~User() {
_parent->addMessage(User::system, std::string("leaved_chat"), _name, _color);
}
std::string User::name() const {
return _name;
}
bool User::validateToken(std::string token) {
return (token == _token);
}
bool User::isUser(std::shared_ptr<User> toValidate) {
return (toValidate.get() == this);
}
void User::sendMsg(MsgType type, const char *message, std::string userName, std::string color) {
sendMsg(type, std::string(message), userName, color);
}
void User::sendMsg(MsgType type, std::string message , std::string userName, std::string color) {
Json::Value sendMessage;
sendMessage["type"] = type;
sendMessage["message"] = message;
sendMessage["userName"] = userName;
sendMessage["color"] = color;
send(sendMessage);
}
void User::sendMsg(User::MsgType type, Json::Value message, std::string userName, std::string color) {
Json::Value sendMessage;
sendMessage["type"] = type;
sendMessage["message"] = message;
sendMessage["userName"] = userName;
sendMessage["color"] = color;
send(sendMessage);
}
void User::checkerTask() {
while (!_stop) {
fd_set readSd;
FD_ZERO(&readSd);
FD_SET(_socket, &readSd);
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500;
int selectResult = select(_socket + 1, &readSd, NULL, NULL, &tv);
if (selectResult == 1 && FD_ISSET(_socket, &readSd) == 1) {
_parent->removeUser(_token);
_stop = true;
delete this;
return;
}
std::string msg = readSocket(_socket);
if (msg == "") {
continue;
}
handleMessage(msg);
}
}
void User::stop() {
_stop = true;
}
std::string User::color() const {
return _color;
}
void User::setParent(std::shared_ptr<Room> parent) {
_parent = std::move(parent);
}
void User::send(std::string out) {
Base::send(_socket, out);
}
void User::send(Json::Value out) {
Base::send(_socket, out);
}
void User::handleMessage(std::string message) {
Json::Value jsonTree = getJsonTree(message);
if (jsonTree["token"].asString() != _token) {
return;
}
if (jsonTree["type"].asString() == "message") {
checkString(jsonTree["message"].asString());
} else if (jsonTree["type"].asString() == "dice") {
doDice();
} else if (jsonTree["type"].asString() == "scream") {
_parent->addMessage(User::scream, jsonTree["message"].asString(), _name, _color);
} else if (jsonTree["type"].asString() == "do") {
_parent->addMessage(User::dosomething, jsonTree["message"].asString(), _name, _color);
} else if (jsonTree["type"].asString() == "join") {
changeRoom(jsonTree["newroom"].asString(), jsonTree["password"].asString());
} else if (jsonTree["type"].asString() == "userlist") {
sendUserList();
}
}
void User::checkString(std::string message) {
if (message.substr(0, 6) == "/join ") {
message = message.substr(6);
if (message.find(" ") == std::string::npos) {
changeRoom(message, "");
} else {
std::string room = message.substr(0, message.find(" "));
std::string password = message.substr(message.find(" ") + 1);
changeRoom(room, password);
}
} else if (message == "/dice") {
doDice();
} else {
_parent->addMessage(User::message, message, _name, _color);
}
}
void User::sendUserList() {
Json::Value userList = _parent->userList();
Json::Value msg = Json::objectValue;
msg["userlist"] = userList;
sendMsg(userListe, msg, "", "");
}
void User::doDice() {
switch (_parent->addDice(shared_from_this(), (rand() % 6) + 1)) {
case 1:
sendMsg(system, "dice_not_possible", "", "");
break;
case 2:
sendMsg(system, "dice_allready_done", "", "");
break;
default:
break;
}
}
void User::changeRoom(std::string newRoom, std::string password) {
if (!_parent->userToNewRoom(shared_from_this(), newRoom, password)) {
sendMsg(User::system, "room_not_possible", "", "");
}
}
} // namespace Lib
} // namespace Yc
#include "user.h"
#include <tools.h>
#include <json/json.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "room.h"
#include <iostream>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sstream>
namespace Yc {
namespace Lib {
User::User(std::shared_ptr<Room> parent, std::string name, std::string color, int socket) :
_parent(std::move(parent)),
_name(std::move(name)),
_color(std::move(color)),
_socket(socket),
_stop(false) {
_token = Yc::Lib::Tools::generateRandomString(32);
sendMsg(token, _token, "", "");
thread = std::make_unique<std::thread>(&User::checkerTask, this);
}
User::~User() {
_parent->addMessage(User::system, std::string("leaved_chat"), _name, _color);
}
std::string User::name() const {
return _name;
}
bool User::validateToken(std::string token) {
return (token == _token);
}
bool User::isUser(std::shared_ptr<User> toValidate) {
return (toValidate.get() == this);
}
void User::sendMsg(MsgType type, const char *message, std::string userName, std::string color) {
sendMsg(type, std::string(message), userName, color);
}
void User::sendMsg(MsgType type, std::string message , std::string userName, std::string color) {
Json::Value sendMessage;
sendMessage["type"] = type;
sendMessage["message"] = message;
sendMessage["userName"] = userName;
sendMessage["color"] = color;
send(sendMessage);
}
void User::sendMsg(User::MsgType type, Json::Value message, std::string userName, std::string color) {
Json::Value sendMessage;
sendMessage["type"] = type;
sendMessage["message"] = message;
sendMessage["userName"] = userName;
sendMessage["color"] = color;
send(sendMessage);
}
void User::checkerTask() {
while (!_stop) {
fd_set readSd;
FD_ZERO(&readSd);
FD_SET(_socket, &readSd);
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500;
int selectResult = select(_socket + 1, &readSd, NULL, NULL, &tv);
if (selectResult == 1 && FD_ISSET(_socket, &readSd) == 1) {
_parent->removeUser(_token);
_stop = true;
delete this;
return;
}
std::string msg = readSocket(_socket);
if (msg == "") {
continue;
}
handleMessage(msg);
}
}
void User::stop() {
_stop = true;
}
std::string User::color() const {
return _color;
}
void User::setParent(std::shared_ptr<Room> parent) {
_parent = std::move(parent);
}
void User::send(std::string out) {
Base::send(_socket, out);
}
void User::send(Json::Value out) {
Base::send(_socket, out);
}
void User::handleMessage(std::string message) {
Json::Value jsonTree = getJsonTree(message);
if (jsonTree["token"].asString() != _token) {
return;
}
if (jsonTree["type"].asString() == "message") {
checkString(jsonTree["message"].asString());
} else if (jsonTree["type"].asString() == "dice") {
doDice();
} else if (jsonTree["type"].asString() == "scream") {
_parent->addMessage(User::scream, jsonTree["message"].asString(), _name, _color);
} else if (jsonTree["type"].asString() == "do") {
_parent->addMessage(User::dosomething, jsonTree["message"].asString(), _name, _color);
} else if (jsonTree["type"].asString() == "join") {
changeRoom(jsonTree["newroom"].asString(), jsonTree["password"].asString());
} else if (jsonTree["type"].asString() == "userlist") {
sendUserList();
}
}
void User::checkString(std::string message) {
if (message.substr(0, 6) == "/join ") {
message = message.substr(6);
if (message.find(" ") == std::string::npos) {
changeRoom(message, "");
} else {
std::string room = message.substr(0, message.find(" "));
std::string password = message.substr(message.find(" ") + 1);
changeRoom(room, password);
}
} else if (message == "/dice") {
doDice();
} else {
_parent->addMessage(User::message, message, _name, _color);
}
}
void User::sendUserList() {
Json::Value userList = _parent->userList();
Json::Value msg = Json::objectValue;
msg["userlist"] = userList;
sendMsg(userListe, msg, "", "");
}
void User::doDice() {
switch (_parent->addDice(shared_from_this(), (rand() % 6) + 1)) {
case 1:
sendMsg(system, "dice_not_possible", "", "");
break;
case 2:
sendMsg(system, "dice_allready_done", "", "");
break;
default:
break;
}
}
void User::changeRoom(std::string newRoom, std::string password) {
if (!_parent->userToNewRoom(shared_from_this(), newRoom, password)) {
sendMsg(User::system, "room_not_possible", "", "");
}
}
} // namespace Lib
} // namespace Yc

61
user.h
View File

@@ -1,61 +0,0 @@
#ifndef YC_LIB_USER_H
#define YC_LIB_USER_H
#include <string>
#include <json/json.h>
#include <thread>
#include "base.h"
namespace Yc {
namespace Lib {
class Room;
class User: public Base, public std::enable_shared_from_this<User> {
public:
enum MsgType {
error = -1,
token = 1,
userListe = 2,
roomList = 3,
message = 4,
system = 5,
scream = 6,
dosomething = 7,
dice = 8,
result = 9
};
User(std::shared_ptr<Room> parent, std::string name, std::string color, int socket);
~User();
std::string name() const;
bool validateToken(std::string token);
bool isUser(std::shared_ptr<User> toValidate);
void sendMsg(MsgType type, std::string message, std::string userName, std::string color);
void sendMsg(MsgType type, const char *message, std::string userName, std::string color);
void sendMsg(MsgType type, Json::Value message, std::string userName, std::string color);
void checkerTask();
void stop();
std::string color() const;
void setParent(std::shared_ptr<Room> parent);
private:
std::shared_ptr<Room> _parent;
std::string _name;
std::string _color;
int _socket;
std::string _token;
bool _stop;
std::unique_ptr<std::thread> thread;
void send(std::string out);
void send(Json::Value out);
void handleMessage(std::string message);
void doDice();
void changeRoom(std::string newRoom, std::string password);
void checkString(std::string message);
void sendUserList();
};
} // namespace Lib
} // namespace Yc
#endif // YC_LIB_USER_H