Handle ROOM* commands in rooms list model
authorunc0rr
Thu, 23 Feb 2012 18:41:52 +0400
changeset 6733 5abbc345a82f
parent 6732 c906dc78091f
child 6734 9709d15dabfa
Handle ROOM* commands in rooms list model
QTfrontend/hwform.cpp
QTfrontend/model/roomslistmodel.cpp
QTfrontend/model/roomslistmodel.h
QTfrontend/net/newnetclient.cpp
gameServer/Actions.hs
--- a/QTfrontend/hwform.cpp	Thu Feb 23 00:46:03 2012 +0400
+++ b/QTfrontend/hwform.cpp	Thu Feb 23 18:41:52 2012 +0400
@@ -1042,8 +1042,6 @@
 
 // rooms list page stuff
     ui.pageRoomsList->roomsList->setModel(hwnet->roomsListModel());
-    connect(hwnet, SIGNAL(roomsList(const QStringList&)),
-            ui.pageRoomsList, SLOT(setRoomsList(const QStringList&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(adminAccess(bool)),
             ui.pageRoomsList, SLOT(setAdmin(bool)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(adminAccess(bool)),
--- a/QTfrontend/model/roomslistmodel.cpp	Thu Feb 23 00:46:03 2012 +0400
+++ b/QTfrontend/model/roomslistmodel.cpp	Thu Feb 23 18:41:52 2012 +0400
@@ -49,3 +49,74 @@
 
     return m_data.at(index.row()).at(index.column());
 }
+
+void RoomsListModel::setRoomsList(const QStringList & rooms)
+{
+    if(m_data.size())
+    {
+        beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
+        m_data.clear();
+        endRemoveRows();
+    }
+
+    for(int i = 0; i < rooms.size(); i += 8)
+    {
+        QStringList l;
+        l.reserve(8);
+        for(int t = 0; t < 8; ++t)
+            l.append(rooms[i + t]);
+
+        m_data.append(roomInfo2RoomRecord(l));
+    }
+
+    beginInsertRows(QModelIndex(), 0, m_data.size() - 1);
+    endInsertRows();
+}
+
+void RoomsListModel::addRoom(const QStringList & info)
+{
+    beginInsertRows(QModelIndex(), 0, 0);
+
+    m_data.prepend(roomInfo2RoomRecord(info));
+
+    endInsertRows();
+}
+
+void RoomsListModel::removeRoom(const QString & name)
+{
+    int i = 0;
+    while(i < m_data.size() && m_data[i].at(0) != name)
+        ++i;
+    if(i >= m_data.size())
+        return;
+
+    beginRemoveRows(QModelIndex(), i, i);
+
+    m_data.removeAt(i);
+
+    endRemoveRows();
+}
+
+void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
+{
+    int i = 0;
+    while(i < m_data.size() && m_data[i].at(0) != name)
+        ++i;
+    if(i >= m_data.size())
+        return;
+
+
+    m_data[i] = roomInfo2RoomRecord(info);
+
+    emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
+}
+
+QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
+{
+    QStringList result;
+
+    result = info;
+    result.removeFirst();
+
+    return result;
+}
--- a/QTfrontend/model/roomslistmodel.h	Thu Feb 23 00:46:03 2012 +0400
+++ b/QTfrontend/model/roomslistmodel.h	Thu Feb 23 18:41:52 2012 +0400
@@ -15,9 +15,17 @@
     int columnCount(const QModelIndex & parent) const;
     QVariant data(const QModelIndex &index, int role) const;
 
+public slots:
+    void setRoomsList(const QStringList & rooms);
+    void addRoom(const QStringList & info);
+    void removeRoom(const QString & name);
+    void updateRoom(const QString & name, const QStringList & info);
+
 private:
     QList<QStringList> m_data;
     QStringList m_headerData;
+
+    QStringList roomInfo2RoomRecord(const QStringList & info);
 };
 
 #endif // ROOMSLISTMODEL_H
--- a/QTfrontend/net/newnetclient.cpp	Thu Feb 23 00:46:03 2012 +0400
+++ b/QTfrontend/net/newnetclient.cpp	Thu Feb 23 18:41:52 2012 +0400
@@ -257,7 +257,7 @@
         }
 
         RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
-        RawSendNet(QString("PROTO%1%2").arg(delimeter).arg(*cProtoVer));
+        RawSendNet(QString("PROTO%1%2").arg(delimeter).arg("41"));
         netClientState = Connected;
         m_game_connected = true;
         emit adminAccess(false);
@@ -275,9 +275,14 @@
 
     if (lst[0] == "ROOMS")
     {
+        if(lst.size() % 8 != 1)
+        {
+            qWarning("Net: Malformed ROOMS message");
+            return;
+        }
         QStringList tmp = lst;
         tmp.removeFirst();
-        emit roomsList(tmp);
+        m_roomsListModel->setRoomsList(tmp);
         return;
     }
 
@@ -478,14 +483,30 @@
         return;
     }
 
-    if(lst[0] == "ROOM")
+    if(lst[0] == "ROOM" && lst.size() == 10 && lst[1] == "ADD")
+    {
+        QStringList tmp = lst;
+        tmp.removeFirst();
+        tmp.removeFirst();
+
+        m_roomsListModel->addRoom(tmp);
+        return;
+    }
+
+    if(lst[0] == "ROOM" && lst.size() == 11 && lst[1] == "UPD")
     {
-        if(lst.size() < 2)
-        {
-            qWarning("Net: Bad ROOM message");
-            return;
-        }
-        RawSendNet(QString("LIST"));
+        QStringList tmp = lst;
+        tmp.removeFirst();
+        tmp.removeFirst();
+
+        QString roomName = tmp.takeFirst();
+        m_roomsListModel->updateRoom(roomName, tmp);
+        return;
+    }
+
+    if(lst[0] == "ROOM" && lst.size() == 3 && lst[1] == "DEL")
+    {
+        m_roomsListModel->removeRoom(lst[2]);
         return;
     }
 
--- a/gameServer/Actions.hs	Thu Feb 23 00:46:03 2012 +0400
+++ b/gameServer/Actions.hs	Thu Feb 23 18:41:52 2012 +0400
@@ -236,6 +236,7 @@
     rnc <- gets roomsClients
     newMasterId <- liftM (head . filter (/= ci)) . io $ roomClientsIndicesM rnc ri
     newMaster <- io $ client'sM rnc id newMasterId
+    oldRoomName <- io $ room'sM rnc name ri
     let newRoomName = nick newMaster
     mapM_ processAction [
         ModifyRoom (\r -> r{masterID = newMasterId, name = newRoomName}),
@@ -246,7 +247,7 @@
     proto <- client's clientProto
     newRoom <- io $ room'sM rnc id ri
     chans <- liftM (map sendChan) $! sameProtoClientsS proto
-    processAction $ AnswerClients chans ("ROOM" : "ADD" : roomInfo (nick newMaster) newRoom)
+    processAction $ AnswerClients chans ("ROOM" : "UPD" : oldRoomName : roomInfo (nick newMaster) newRoom)
 
 processAction (AddRoom roomName roomPassword) = do
     Just clId <- gets clientIndex