Refactor project structure: replace User class with ChatUser, integrate Database class, and update CMake configuration for new files

This commit is contained in:
Torsten Schulz (local)
2025-08-11 14:48:45 +02:00
parent 89956bd01a
commit b81f2de10f
14 changed files with 644 additions and 44 deletions

View File

@@ -55,15 +55,15 @@ namespace Yc {
if (_password != "" && _password == _password && std::find(std::begin(_allowedUsers), std::end(_allowedUsers), _userName) == std::end(_allowedUsers)) {
return false;
}
auto newUser = std::make_shared<User>(shared_from_this(), _userName, color, socket);
auto newUser = std::make_shared<ChatUser>(shared_from_this(), _userName, color, socket);
_users.push_back(newUser);
newUser->sendMsg(User::roomList, _parent->jsonRoomList(), "", "");
addMessage(User::system, "room_entered", newUser->name(), newUser->color());
newUser->sendMsg(ChatUser::roomList, _parent->jsonRoomList(), "", "");
addMessage(ChatUser::system, "room_entered", newUser->name(), newUser->color());
_initRound();
return true;
}
bool Room::addUser(std::shared_ptr<User> user, std::string password) {
bool Room::addUser(std::shared_ptr<ChatUser> user, std::string password) {
if (password == _password) {
_users.push_back(user);
user->setParent(shared_from_this());
@@ -86,7 +86,7 @@ namespace Yc {
for (auto it = _users.begin(); it != _users.end(); ++it) {
if ((*it)->validateToken(_token)) {
if (!silent) {
addMessage(User::system, "room_exit", (*it)->name(), (*it)->color());
addMessage(ChatUser::system, (*it)->name(), (*it)->color());
}
_users.erase(it);
break;
@@ -94,11 +94,11 @@ namespace Yc {
}
}
void Room::removeUser(std::shared_ptr<User> userToRemove, bool silent) {
void Room::removeUser(std::shared_ptr<ChatUser> userToRemove, bool silent) {
for (auto it = _users.begin(); it != _users.end(); ++it) {
if (*it == userToRemove) {
if (!silent) {
addMessage(User::system, "room_exit", (*it)->name(), (*it)->color());
addMessage(ChatUser::system, (*it)->name(), (*it)->color());
}
_users.erase(it);
break;
@@ -110,11 +110,11 @@ namespace Yc {
_stop = true;
}
void Room::addMessage(User::MsgType type, const char *messageText, std::string userName, std::string color) {
void Room::addMessage(ChatUser::MsgType type, const char *messageText, std::string userName, std::string color) {
addMessage(type, (std::string)messageText, userName, color);
}
void Room::addMessage(User::MsgType type, std::string messageText, std::string userName, std::string color) {
void Room::addMessage(ChatUser::MsgType type, std::string messageText, std::string userName, std::string color) {
Message message;
message.type = type;
message.messageTr = messageText;
@@ -123,11 +123,11 @@ namespace Yc {
_msgQueue.push(message);
}
void Room::addMessage(User::MsgType type, Json::Value messageText, std::string userName, std::string color) {
void Room::addMessage(ChatUser::MsgType type, Json::Value messageText, std::string userName, std::string color) {
addMessage(type, getJsonString(messageText), userName, color);
}
void Room::addUserWhenQueueEmpty(std::shared_ptr<User> user) {
void Room::addUserWhenQueueEmpty(std::shared_ptr<ChatUser> user) {
while (_msgQueue.size() > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
@@ -135,7 +135,7 @@ namespace Yc {
user->setParent(shared_from_this());
}
bool Room::userToNewRoom(std::shared_ptr<User> user, std::string newRoom, std::string password) {
bool Room::userToNewRoom(std::shared_ptr<ChatUser> user, std::string newRoom, std::string password) {
return _parent->changeRoom(user, newRoom, password);
}
@@ -169,7 +169,7 @@ namespace Yc {
return (_roundRunning || (_type & rounds) == rounds) && (_type & dice) == dice;
}
unsigned int Room::addDice(std::shared_ptr<User> user, int diceValue) {
unsigned int Room::addDice(std::shared_ptr<ChatUser> user, int diceValue) {
if (!canDice()) {
return 1;
}
@@ -179,7 +179,7 @@ namespace Yc {
}
}
_diceValues.push_back(std::make_pair(user, diceValue));
addMessage(User::dice, std::to_string(diceValue), user->name(), user->color());
addMessage(ChatUser::dice, std::to_string(diceValue), user->name(), user->color());
return 0;
}
@@ -201,13 +201,13 @@ namespace Yc {
if (_roundRunning && (_users.size() < 2 || _roundStart + _roundLength >= time(NULL))) {
_lastRoundEnd = time(NULL);
_roundRunning = false;
addMessage(User::system, "round_ends");
addMessage(ChatUser::system, "round_ends");
_showDiceRoundResults();
} else if (!_roundRunning && _lastRoundEnd <= time(NULL) - 15 && _users.size() >= 2) {
_roundStart = time(NULL);
_roundRunning = true;
_diceValues.clear();
addMessage(User::system, "next_round_starts_now");
addMessage(ChatUser::system, "next_round_starts_now");
}
}
}
@@ -215,12 +215,12 @@ namespace Yc {
void Room::_initRound() {
if (_users.size() == 2) {
_lastRoundEnd = time(NULL);
addMessage(User::system, "next_round_starts_soon");
addMessage(ChatUser::system, "next_round_starts_soon");
}
}
void Room::_showDiceRoundResults() {
std::sort(_diceValues.begin(), _diceValues.end(), [=](const std::pair<std::shared_ptr<User>, int>& val1, const std::pair<std::shared_ptr<User>, 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);
});
Json::Value userList = Json::arrayValue;
@@ -230,7 +230,7 @@ namespace Yc {
entry["value"] = user.second;
userList.append(entry);
}
addMessage(User::result, userList);
addMessage(ChatUser::result, userList);
}
std::string Room::name() {