Verbessere die Verarbeitung von Farbänderungen in ChatUser und SSLServer

- 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.
This commit is contained in:
Torsten Schulz (local)
2025-09-05 15:10:58 +02:00
parent e48a38e86d
commit 21c224835f
2 changed files with 15 additions and 11 deletions

View File

@@ -547,11 +547,13 @@ namespace Yc
_user.set_color(color); _user.set_color(color);
// Persistieren, falls DB-ID vorhanden // Persistieren, falls DB-ID vorhanden
try { try {
if (_user.id() != 0) { if (_user.id() != 0 && _parent) {
auto server = _parent->getServer(); auto server = _parent->getServer();
auto db = server->_database; if (server && server->_database) {
std::string query = "UPDATE chat.\"user\" SET color = '" + color + "', updated_at = NOW() WHERE id = " + std::to_string(_user.id()) + ";"; auto db = server->_database;
(void)db->exec(query); std::string query = "UPDATE chat.\"user\" SET color = '" + color + "', updated_at = NOW() WHERE id = " + std::to_string(_user.id()) + ";";
(void)db->exec(query);
}
} }
} catch (...) { } catch (...) {
// Ignoriere DB-Fehler still // Ignoriere DB-Fehler still

View File

@@ -402,18 +402,20 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
#ifdef YC_DEBUG #ifdef YC_DEBUG
std::cout << "[Debug] SSL Server: Processing color change from user: " << user->name() << ", new color: " << newColor << std::endl; std::cout << "[Debug] SSL Server: Processing color change from user: " << user->name() << ", new color: " << newColor << std::endl;
#endif #endif
// Store old color before updating
std::string oldColor = user->color();
// Update user color // Update user color
user->setColor(newColor); user->setColor(newColor);
// Process through room // Process through room
for (auto &room: _rooms) { for (auto &room: _rooms) {
if (room->userIsInRoom(user->name())) { if (room && room->userIsInRoom(user->name())) {
#ifdef YC_DEBUG #ifdef YC_DEBUG
std::cout << "[Debug] SSL Server: Broadcasting color change in room: " << room->name() << std::endl; std::cout << "[Debug] SSL Server: Broadcasting color change in room: " << room->name() << std::endl;
#endif #endif
// Send color change message to room // Send color change message to room
Json::Value colorMsg = Json::objectValue; Json::Value colorMsg = Json::objectValue;
colorMsg["tr"] = "user_color_changed"; colorMsg["tr"] = "user_color_changed";
colorMsg["from"] = user->color(); colorMsg["from"] = oldColor;
colorMsg["to"] = newColor; colorMsg["to"] = newColor;
room->addMessage(ChatUser::MsgType::system, colorMsg, user->name(), newColor); room->addMessage(ChatUser::MsgType::system, colorMsg, user->name(), newColor);
break; break;
@@ -439,7 +441,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
#endif #endif
// Process through room // Process through room
for (auto &room: _rooms) { for (auto &room: _rooms) {
if (room->userIsInRoom(user->name())) { if (room && room->userIsInRoom(user->name())) {
if (root.isMember("value")) { if (root.isMember("value")) {
int diceValue = root.get("value", 0).asInt(); int diceValue = root.get("value", 0).asInt();
if (!room->rollDice(user, diceValue)) { if (!room->rollDice(user, diceValue)) {
@@ -482,7 +484,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
#endif #endif
// Process through room // Process through room
for (auto &room: _rooms) { for (auto &room: _rooms) {
if (room->userIsInRoom(user->name())) { if (room && room->userIsInRoom(user->name())) {
if (root.isMember("rounds")) { if (root.isMember("rounds")) {
int rounds = root.get("rounds", 0).asInt(); int rounds = root.get("rounds", 0).asInt();
if (rounds < 1 || rounds > 10) { if (rounds < 1 || rounds > 10) {
@@ -527,7 +529,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
#endif #endif
// Process through room // Process through room
for (auto &room: _rooms) { for (auto &room: _rooms) {
if (room->userIsInRoom(user->name())) { if (room && room->userIsInRoom(user->name())) {
room->endDiceGame(); room->endDiceGame();
break; break;
} }
@@ -553,7 +555,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
#endif #endif
// Process through room // Process through room
for (auto &room: _rooms) { 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()); room->addMessage(ChatUser::MsgType::scream, msg, user->name(), user->color());
break; break;
} }
@@ -668,7 +670,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
#endif #endif
// Process through room // Process through room
for (auto &room: _rooms) { for (auto &room: _rooms) {
if (room->userIsInRoom(user->name())) { if (room && room->userIsInRoom(user->name())) {
Json::Value userList = room->userList(); Json::Value userList = room->userList();
Json::Value msg = Json::objectValue; Json::Value msg = Json::objectValue;
msg["userlist"] = userList; msg["userlist"] = userList;