From e48a38e86dc6d5f97ca6d52a291edd7d80cd8708 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 5 Sep 2025 14:48:33 +0200 Subject: [PATCH] =?UTF-8?q?F=C3=BCge=20Unterst=C3=BCtzung=20f=C3=BCr=20Far?= =?UTF-8?q?b=C3=A4nderungen=20in=20ChatUser=20und=20SSLServer=20hinzu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Implementiere die Methode `setColor` in `ChatUser`, um die Benutzerfarbe zu aktualisieren und in der Datenbank zu speichern, falls eine ID vorhanden ist. - Ergänze die Verarbeitung von Farbänderungen im `SSLServer`, um die Benutzerfarbe über WebSocket-Nachrichten zu ändern und entsprechende Benachrichtigungen an die Räume zu senden. - Füge Debug-Ausgaben hinzu, um den Ablauf der Farbänderungen zu protokollieren und die Nachverfolgbarkeit zu verbessern. --- src/core/chat_user.cpp | 17 +++ src/core/chat_user.h | 1 + src/core/ssl_server.cpp | 293 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 311 insertions(+) diff --git a/src/core/chat_user.cpp b/src/core/chat_user.cpp index 5b2da4f..93bacd7 100644 --- a/src/core/chat_user.cpp +++ b/src/core/chat_user.cpp @@ -541,6 +541,23 @@ namespace Yc return _color; } + void ChatUser::setColor(std::string color) + { + _color = color; + _user.set_color(color); + // Persistieren, falls DB-ID vorhanden + try { + if (_user.id() != 0) { + auto server = _parent->getServer(); + auto db = server->_database; + std::string query = "UPDATE chat.\"user\" SET color = '" + color + "', updated_at = NOW() WHERE id = " + std::to_string(_user.id()) + ";"; + (void)db->exec(query); + } + } catch (...) { + // Ignoriere DB-Fehler still + } + } + void ChatUser::setParent(std::shared_ptr parent) { _parent = std::move(parent); diff --git a/src/core/chat_user.h b/src/core/chat_user.h index 2a733ff..4a5a0e3 100644 --- a/src/core/chat_user.h +++ b/src/core/chat_user.h @@ -50,6 +50,7 @@ namespace Yc void start(); void stop(); std::string color() const; + void setColor(std::string color); void setParent(std::shared_ptr parent); public: diff --git a/src/core/ssl_server.cpp b/src/core/ssl_server.cpp index 392c084..2ef25d1 100644 --- a/src/core/ssl_server.cpp +++ b/src/core/ssl_server.cpp @@ -393,6 +393,299 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa std::cout << "[Debug] SSL Server: No token provided in message" << std::endl; #endif } + } else if (type == "color") { + // Handle color change + if (!token.empty()) { + auto user = getUserByToken(token); + if (user) { + std::string newColor = root.get("value", "").asString(); + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Processing color change from user: " << user->name() << ", new color: " << newColor << std::endl; + #endif + // Update user color + user->setColor(newColor); + // Process through room + for (auto &room: _rooms) { + if (room->userIsInRoom(user->name())) { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Broadcasting color change in room: " << room->name() << std::endl; + #endif + // Send color change message to room + Json::Value colorMsg = Json::objectValue; + colorMsg["tr"] = "user_color_changed"; + colorMsg["from"] = user->color(); + colorMsg["to"] = newColor; + room->addMessage(ChatUser::MsgType::system, colorMsg, user->name(), newColor); + break; + } + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: User not found for token: " << token << std::endl; + #endif + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: No token provided in color change" << std::endl; + #endif + } + } else if (type == "dice") { + // Handle dice roll + if (!token.empty()) { + auto user = getUserByToken(token); + if (user) { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Processing dice roll from user: " << user->name() << std::endl; + #endif + // Process through room + for (auto &room: _rooms) { + if (room->userIsInRoom(user->name())) { + if (root.isMember("value")) { + int diceValue = root.get("value", 0).asInt(); + if (!room->rollDice(user, diceValue)) { + // Send error message + Json::Value errorMsg = Json::objectValue; + errorMsg["type"] = ChatUser::error; + errorMsg["message"] = "dice_roll_failed"; + user->sendMsg(ChatUser::error, errorMsg, "", ""); + } + } else { + // Fallback: Simple dice roll + if (!room->addDice(user, (rand() % 6) + 1)) { + // Send error message + Json::Value errorMsg = Json::objectValue; + errorMsg["type"] = ChatUser::error; + errorMsg["message"] = "dice_roll_failed"; + user->sendMsg(ChatUser::error, errorMsg, "", ""); + } + } + break; + } + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: User not found for token: " << token << std::endl; + #endif + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: No token provided in dice roll" << std::endl; + #endif + } + } else if (type == "start_dice_game") { + // Handle dice game start + if (!token.empty()) { + auto user = getUserByToken(token); + if (user) { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Processing dice game start from user: " << user->name() << std::endl; + #endif + // Process through room + for (auto &room: _rooms) { + if (room->userIsInRoom(user->name())) { + if (root.isMember("rounds")) { + int rounds = root.get("rounds", 0).asInt(); + if (rounds < 1 || rounds > 10) { + Json::Value errorMsg = Json::objectValue; + errorMsg["type"] = ChatUser::error; + errorMsg["message"] = "invalid_rounds"; + user->sendMsg(ChatUser::error, errorMsg, "", ""); + } else { + if (!room->startDiceGame(rounds, user)) { + Json::Value errorMsg = Json::objectValue; + errorMsg["type"] = ChatUser::error; + errorMsg["message"] = "dice_game_start_failed"; + user->sendMsg(ChatUser::error, errorMsg, "", ""); + } + } + } else { + Json::Value errorMsg = Json::objectValue; + errorMsg["type"] = ChatUser::error; + errorMsg["message"] = "missing_rounds"; + user->sendMsg(ChatUser::error, errorMsg, "", ""); + } + break; + } + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: User not found for token: " << token << std::endl; + #endif + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: No token provided in dice game start" << std::endl; + #endif + } + } else if (type == "end_dice_game") { + // Handle dice game end + if (!token.empty()) { + auto user = getUserByToken(token); + if (user) { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Processing dice game end from user: " << user->name() << std::endl; + #endif + // Process through room + for (auto &room: _rooms) { + if (room->userIsInRoom(user->name())) { + room->endDiceGame(); + break; + } + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: User not found for token: " << token << std::endl; + #endif + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: No token provided in dice game end" << std::endl; + #endif + } + } else if (type == "scream") { + // Handle scream message + if (!token.empty()) { + auto user = getUserByToken(token); + if (user) { + std::string msg = root.get("message", "").asString(); + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Processing scream from user: " << user->name() << ", message: " << msg << std::endl; + #endif + // Process through room + for (auto &room: _rooms) { + if (room->userIsInRoom(user->name())) { + room->addMessage(ChatUser::MsgType::scream, msg, user->name(), user->color()); + break; + } + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: User not found for token: " << token << std::endl; + #endif + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: No token provided in scream" << std::endl; + #endif + } + } else if (type == "do") { + // Handle do action + if (!token.empty()) { + auto user = getUserByToken(token); + if (user) { + std::string action = root.get("value", "").asString(); + std::string targetUser = root.get("to", "").asString(); + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Processing do action from user: " << user->name() << ", action: " << action << ", target: " << targetUser << std::endl; + #endif + if (action.empty()) { + Json::Value errorMsg = Json::objectValue; + errorMsg["type"] = ChatUser::error; + errorMsg["message"] = "missing_action"; + user->sendMsg(ChatUser::error, errorMsg, "", ""); + } else { + // Process through room + for (auto &room: _rooms) { + if (room->userIsInRoom(user->name())) { + Json::Value doMsg = Json::objectValue; + doMsg["tr"] = "user_action"; + doMsg["action"] = action; + if (!targetUser.empty()) { + doMsg["to"] = targetUser; + // Find target user and add their info + auto targetUserObj = room->findUserByName(targetUser); + if (targetUserObj) { + doMsg["targetName"] = targetUserObj->name(); + doMsg["targetColor"] = targetUserObj->color(); + } + } + room->addMessage(ChatUser::MsgType::dosomething, doMsg, user->name(), user->color()); + break; + } + } + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: User not found for token: " << token << std::endl; + #endif + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: No token provided in do action" << std::endl; + #endif + } + } else if (type == "join") { + // Handle room join + if (!token.empty()) { + auto user = getUserByToken(token); + if (user) { + std::string roomName = root.get("room", "").asString(); + std::string password = root.get("password", "").asString(); + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Processing room join from user: " << user->name() << ", room: " << roomName << std::endl; + #endif + // Process through rooms + bool found = false; + for (auto &room: _rooms) { + if (room->name() == roomName) { + // Try to add user to new room + if (room->addUser(user, password)) { + // Remove user from old room + for (auto &oldRoom: _rooms) { + if (oldRoom->userIsInRoom(user->name())) { + oldRoom->removeUser(user, true); + break; + } + } + found = true; + } + break; + } + } + if (!found) { + Json::Value errorMsg = Json::objectValue; + errorMsg["type"] = ChatUser::error; + errorMsg["message"] = "room_join_failed"; + user->sendMsg(ChatUser::error, errorMsg, "", ""); + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: User not found for token: " << token << std::endl; + #endif + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: No token provided in room join" << std::endl; + #endif + } + } else if (type == "userlist") { + // Handle user list request + if (!token.empty()) { + auto user = getUserByToken(token); + if (user) { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: Processing user list request from user: " << user->name() << std::endl; + #endif + // Process through room + for (auto &room: _rooms) { + if (room->userIsInRoom(user->name())) { + Json::Value userList = room->userList(); + Json::Value msg = Json::objectValue; + msg["userlist"] = userList; + user->sendMsg(ChatUser::MsgType::userListe, msg, "", ""); + break; + } + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: User not found for token: " << token << std::endl; + #endif + } + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] SSL Server: No token provided in user list request" << std::endl; + #endif + } } // Add more message types as needed