From 21c224835f0c6914dc2444646d8d200d9cefe574 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 5 Sep 2025 15:10:58 +0200 Subject: [PATCH] =?UTF-8?q?Verbessere=20die=20Verarbeitung=20von=20Farb?= =?UTF-8?q?=C3=A4nderungen=20in=20ChatUser=20und=20SSLServer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ergänze eine Überprüfung, ob der Server und die Datenbank vorhanden sind, bevor die Benutzerfarbe in der Datenbank aktualisiert wird. - Speichere die alte Farbe des Benutzers, bevor die neue Farbe gesetzt wird, um korrekte Benachrichtigungen an die Räume zu senden. - Optimiere die Logik zur Überprüfung, ob der Benutzer in einem Raum ist, um potenzielle Nullzeiger-Dereferenzierungen zu vermeiden. --- src/core/chat_user.cpp | 10 ++++++---- src/core/ssl_server.cpp | 16 +++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/core/chat_user.cpp b/src/core/chat_user.cpp index 93bacd7..d9b90a5 100644 --- a/src/core/chat_user.cpp +++ b/src/core/chat_user.cpp @@ -547,11 +547,13 @@ namespace Yc _user.set_color(color); // Persistieren, falls DB-ID vorhanden try { - if (_user.id() != 0) { + if (_user.id() != 0 && _parent) { 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); + if (server && server->_database) { + 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 diff --git a/src/core/ssl_server.cpp b/src/core/ssl_server.cpp index 2ef25d1..6eb6654 100644 --- a/src/core/ssl_server.cpp +++ b/src/core/ssl_server.cpp @@ -402,18 +402,20 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa #ifdef YC_DEBUG std::cout << "[Debug] SSL Server: Processing color change from user: " << user->name() << ", new color: " << newColor << std::endl; #endif + // Store old color before updating + std::string oldColor = user->color(); // Update user color user->setColor(newColor); // Process through room for (auto &room: _rooms) { - if (room->userIsInRoom(user->name())) { + if (room && 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["from"] = oldColor; colorMsg["to"] = newColor; room->addMessage(ChatUser::MsgType::system, colorMsg, user->name(), newColor); break; @@ -439,7 +441,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa #endif // Process through room for (auto &room: _rooms) { - if (room->userIsInRoom(user->name())) { + if (room && room->userIsInRoom(user->name())) { if (root.isMember("value")) { int diceValue = root.get("value", 0).asInt(); if (!room->rollDice(user, diceValue)) { @@ -482,7 +484,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa #endif // Process through room for (auto &room: _rooms) { - if (room->userIsInRoom(user->name())) { + if (room && room->userIsInRoom(user->name())) { if (root.isMember("rounds")) { int rounds = root.get("rounds", 0).asInt(); if (rounds < 1 || rounds > 10) { @@ -527,7 +529,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa #endif // Process through room for (auto &room: _rooms) { - if (room->userIsInRoom(user->name())) { + if (room && room->userIsInRoom(user->name())) { room->endDiceGame(); break; } @@ -553,7 +555,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa #endif // Process through room for (auto &room: _rooms) { - if (room->userIsInRoom(user->name())) { + if (room && room->userIsInRoom(user->name())) { room->addMessage(ChatUser::MsgType::scream, msg, user->name(), user->color()); break; } @@ -668,7 +670,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa #endif // Process through room for (auto &room: _rooms) { - if (room->userIsInRoom(user->name())) { + if (room && room->userIsInRoom(user->name())) { Json::Value userList = room->userList(); Json::Value msg = Json::objectValue; msg["userlist"] = userList;