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;