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:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user