Make direct messages (/msg) look nicer for the next version clients draft
authorS.D.
Sun, 16 Oct 2022 20:15:57 +0300
changeset 15909 7409084d891f
parent 15908 014f4edd0421
child 15914 08b88572b1c2
Make direct messages (/msg) look nicer for the next version clients
QTfrontend/hwform.cpp
QTfrontend/net/newnetclient.cpp
QTfrontend/net/newnetclient.h
QTfrontend/net/proto.cpp
QTfrontend/net/proto.h
QTfrontend/res/css/chat.css
QTfrontend/ui/widget/chatwidget.cpp
QTfrontend/ui/widget/chatwidget.h
gameServer/HWProtoCore.hs
gameServer/HandlerUtils.hs
--- a/QTfrontend/hwform.cpp	Sun Oct 16 13:14:16 2022 +0300
+++ b/QTfrontend/hwform.cpp	Sun Oct 16 20:15:57 2022 +0300
@@ -1434,6 +1434,8 @@
             ui.pageNetGame->chatWidget, SLOT(onChatAction(const QString&, const QString&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(roomChatMessage(const QString&, const QString&)),
             ui.pageNetGame->chatWidget, SLOT(onChatMessage(const QString&, const QString&)), Qt::QueuedConnection);
+    connect(hwnet, SIGNAL(roomDirectMessage(const QString&, const QString&, bool)),
+            ui.pageNetGame->chatWidget, SLOT(onDirectMessage(const QString&, const QString&, bool)), Qt::QueuedConnection);
 
     connect(hwnet, SIGNAL(roomMaster(bool)),
             ui.pageNetGame->chatWidget, SLOT(adminAccess(bool)), Qt::QueuedConnection);
@@ -1480,6 +1482,8 @@
             ui.pageRoomsList->chatWidget, SLOT(onChatAction(const QString&,const QString&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(lobbyChatMessage(const QString&, const QString&)),
             ui.pageRoomsList->chatWidget, SLOT(onChatMessage(const QString&, const QString&)), Qt::QueuedConnection);
+    connect(hwnet, SIGNAL(lobbyDirectMessage(const QString&, const QString&, bool)),
+            ui.pageRoomsList->chatWidget, SLOT(onDirectMessage(const QString&, const QString&, bool)), Qt::QueuedConnection);
 
 // nick list stuff
     {
--- a/QTfrontend/net/newnetclient.cpp	Sun Oct 16 13:14:16 2022 +0300
+++ b/QTfrontend/net/newnetclient.cpp	Sun Oct 16 20:15:57 2022 +0300
@@ -474,13 +474,37 @@
         return;
     }
 
+    if (lst[0] == "MSG" || lst[0] == "MSG_ECHO")
+    {
+        if(lst.size() < 3)
+        {
+            qWarning("Net: Empty MSG message");
+            return;
+        }
+
+        bool isEcho = lst[0] == "MSG_ECHO";
+        QString nick = lst[1];
+        QString message = lst[2];
+
+        if (netClientState == InLobby)
+        {
+            emit lobbyDirectMessage(nick, message, isEcho);
+        }
+        else
+        {
+            emit chatStringFromNet(HWProto::formatDirectMsg(mynick, nick, message, isEcho));
+            emit roomDirectMessage(nick, message, isEcho);
+        }
+        return;
+    }
+
     if (netClientState == InRoom || netClientState == InGame || netClientState == InDemo)
     {
         if (lst[0] == "TEAMDRAW")
         {
             if(lst.size() < 3)
             {
-                qWarning("Net: Empty CHAT message");
+                qWarning("Net: Empty TEAMDRAW message");
                 return;
             }
 
--- a/QTfrontend/net/newnetclient.h	Sun Oct 16 13:14:16 2022 +0300
+++ b/QTfrontend/net/newnetclient.h	Sun Oct 16 20:15:57 2022 +0300
@@ -134,6 +134,8 @@
         void lobbyChatAction(const QString & nick, const QString & action);
         void roomChatMessage(const QString & nick, const QString & message);
         void roomChatAction(const QString & nick, const QString & action);
+        void lobbyDirectMessage(const QString & nick, const QString & message, bool isEcho);
+        void roomDirectMessage(const QString & nick, const QString & message, bool isEcho);
         void chatStringFromNet(const QString&);
 
         void roomsList(const QStringList&);
--- a/QTfrontend/net/proto.cpp	Sun Oct 16 13:14:16 2022 +0300
+++ b/QTfrontend/net/proto.cpp	Sun Oct 16 20:15:57 2022 +0300
@@ -56,6 +56,15 @@
         return QString("\x01%1: %2").arg(nick).arg(msg);
 }
 
+QString HWProto::formatDirectMsg(const QString & myNick, const QString & nick,
+                                 const QString & msg, bool isEcho)
+{
+    if (isEcho)
+        return QString("\x04[msg] %1: /msg [%2] %3").arg(myNick).arg(nick).arg(msg);
+    else
+        return QString("\x04[msg] %1: %2").arg(nick).arg(msg);
+}
+
 QString HWProto::chatStringToAction(const QString & string)
 {
     if(string.left(4) == "/me ")
--- a/QTfrontend/net/proto.h	Sun Oct 16 13:14:16 2022 +0300
+++ b/QTfrontend/net/proto.h	Sun Oct 16 20:15:57 2022 +0300
@@ -35,6 +35,8 @@
         static QByteArray & addStringListToBuffer(QByteArray & buf, const QStringList & strList);
         static QString formatChatMsg(const QString & nick, const QString & msg);
         static QString formatChatMsgForFrontend(const QString & msg);
+        static QString formatDirectMsg(const QString & myNick, const QString & nick,
+                                       const QString & msg, bool isEcho);
         /**
          * @brief Determines if a chat string represents a chat action and returns the action.
          * @param string chat string
--- a/QTfrontend/res/css/chat.css	Sun Oct 16 13:14:16 2022 +0300
+++ b/QTfrontend/res/css/chat.css	Sun Oct 16 20:15:57 2022 +0300
@@ -57,8 +57,12 @@
 /* various chat messages */
 .msg_UserChat { color:#ffcc00; } /* chat of people who aren't your friends */
 .msg_UserChat .nick { color:#ffec20; }
+.msg_UserDirectMsg { color:#ffcc00; } /* direct message from people who aren't your friends */
+.msg_UserDirectMsg .nick { color:#ffec20; }
 .msg_FriendChat { color: #b0ff08; }
 .msg_FriendChat .nick { color: #30ff30; }
+.msg_FriendDirectMsg { color:#ffcc00; }
+.msg_FriendDirectMsg .nick { color:#ffec20; }
 .msg_UserJoin { color: #c0c0c0; }
 .msg_UserJoin .nick { color: #d0d0d0; }
 .msg_UserLeave { color: #b8b8b8; }
@@ -91,6 +95,7 @@
 
 /* you can also set timestamp style for different msg types */
 .msg_FriendChat .timestamp { color: #ffffff; }
+.msg_FriendDirectMsg .timestamp { color: #ffffff; }
 
 /* messages that contain your nickname */
 .highlight {  }
--- a/QTfrontend/ui/widget/chatwidget.cpp	Sun Oct 16 13:14:16 2022 +0300
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Sun Oct 16 20:15:57 2022 +0300
@@ -420,6 +420,17 @@
     printChatString(nick, linkedNick(nick) + ": " + messageToHTML(message), "Chat", containsHighlight(nick, message));
 }
 
+void HWChatWidget::onDirectMessage(const QString & nick, const QString & message, bool isEcho)
+{
+    if (isEcho)
+        printChatString(nick, "[msg] " + linkedNick(m_userNick) + ": " +
+                        messageToHTML(QString("/msg [%1] %2").arg(nick).arg(message)),
+                        "DirectMsg", false);
+    else
+        printChatString(nick, "[msg] " + linkedNick(nick) + ": " +
+                        messageToHTML(message), "DirectMsg", true);
+}
+
 void HWChatWidget::printChatString(
     const QString & nick, const QString & str, const QString & cssClassPart, bool highlight)
 {
--- a/QTfrontend/ui/widget/chatwidget.h	Sun Oct 16 13:14:16 2022 +0300
+++ b/QTfrontend/ui/widget/chatwidget.h	Sun Oct 16 20:15:57 2022 +0300
@@ -112,6 +112,7 @@
     public slots:
         void onChatAction(const QString & nick, const QString & str);
         void onChatMessage(const QString & nick, const QString & str);
+        void onDirectMessage(const QString & nick, const QString & message, bool isEcho);
         void onServerMessage(const QString& str);
         void nickAdded(const QString& nick, bool notifyNick);
         void nickRemoved(const QString& nick);
--- a/gameServer/HWProtoCore.hs	Sun Oct 16 13:14:16 2022 +0300
+++ b/gameServer/HWProtoCore.hs	Sun Oct 16 20:15:57 2022 +0300
@@ -143,20 +143,21 @@
 handleCmd_loggedin ["MSG", nickMsg] = do
     thisCl <- thisClient
     thisNick <- clientNick
-    clChans <- thisClientChans
-    let addEcho nick msg a = AnswerClients clChans ["CHAT", thisNick, B.concat ["/msg [", nick, "] ", msg]] : a
+    clChansProto <- thisClientChansProto
+    let echoByProto nick msg p = if p < 60 then ["CHAT", thisNick, B.concat ["/msg [", nick, "] ", msg]] else ["MSG_ECHO", nick, msg]
+    let addEcho nick msg a = AnswerClientsByProto clChansProto (echoByProto nick msg) : a
     let sendingMsgAllowed clientInfo = case allowMsgState clientInfo of
           AllowAll -> True
           AllowRegistered -> isRegistered thisCl
           AllowNone -> False
+    let answerByProto msg p = if p < 60 then ["CHAT", thisNick, B.concat ["[direct] ", msg]] else ["MSG", thisNick, msg]
     let sendNickMsg nick msg = do
           (_, rnc) <- ask
           maybeClientId <- clientByNick nick
           case maybeClientId of
               Just cl -> let ci = client rnc cl in
                   if sendingMsgAllowed ci  then
-                      return [AnswerClients [sendChan ci]
-                              ["CHAT", thisNick, B.concat ["[direct] ", msg]]]
+                      return [AnswerClientsByProto [(sendChan ci, clientProto ci)] (answerByProto msg)]
                   else
                       return [Warning $ loc "Player is not allowing direct messages."]
               Nothing -> return [Warning $ loc "Player is not online."]
--- a/gameServer/HandlerUtils.hs	Sun Oct 16 13:14:16 2022 +0300
+++ b/gameServer/HandlerUtils.hs	Sun Oct 16 20:15:57 2022 +0300
@@ -69,6 +69,11 @@
     (ci, rnc) <- ask
     return [sendChan (rnc `client` ci)]
 
+thisClientChansProto :: Reader (ClientIndex, IRnC) [(ClientChan, Word16)]
+thisClientChansProto = do
+    (ci, rnc) <- ask
+    return [(sendChan (rnc `client` ci), clientProto (rnc `client` ci))]
+
 sameProtoChans :: Reader (ClientIndex, IRnC) [ClientChan]
 sameProtoChans = do
     (ci, rnc) <- ask