qmlfrontend/net_session.cpp
changeset 15047 773beead236f
parent 15039 a4a058dcbbd6
child 15078 fb7a9b0119d3
equal deleted inserted replaced
15046:0730c68fdf97 15047:773beead236f
     1 #include "net_session.h"
     1 #include "net_session.h"
     2 
     2 
     3 NetSession::NetSession(QObject *parent) : QObject(parent) {}
     3 #include "players_model.h"
       
     4 #include "rooms_model.h"
       
     5 
       
     6 NetSession::NetSession(QObject *parent)
       
     7     : QObject(parent),
       
     8       m_playersModel(new PlayersListModel()),
       
     9       m_roomsModel(new RoomsListModel()) {}
       
    10 
       
    11 NetSession::~NetSession() { close(); }
     4 
    12 
     5 QUrl NetSession::url() const { return m_url; }
    13 QUrl NetSession::url() const { return m_url; }
     6 
    14 
     7 QAbstractSocket::SocketState NetSession::state() const {
    15 QAbstractSocket::SocketState NetSession::state() const {
     8   if (m_socket)
    16   if (m_socket)
    29 
    37 
    30 NetSession::SessionState NetSession::sessionState() const {
    38 NetSession::SessionState NetSession::sessionState() const {
    31   return m_sessionState;
    39   return m_sessionState;
    32 }
    40 }
    33 
    41 
       
    42 QString NetSession::room() const { return m_room; }
       
    43 
    34 void NetSession::setUrl(const QUrl &url) {
    44 void NetSession::setUrl(const QUrl &url) {
    35   if (m_url == url) return;
    45   if (m_url == url) return;
    36 
    46 
    37   m_url = url;
    47   m_url = url;
    38   emit urlChanged(m_url);
    48   emit urlChanged(m_url);
    48 void NetSession::setPassword(const QString &password) {
    58 void NetSession::setPassword(const QString &password) {
    49   if (m_password == password) return;
    59   if (m_password == password) return;
    50 
    60 
    51   m_password = password;
    61   m_password = password;
    52   emit passwordChanged(m_password);
    62   emit passwordChanged(m_password);
       
    63 }
       
    64 
       
    65 void NetSession::setRoom(const QString &room) {
       
    66   if (m_room == room) return;
       
    67 
       
    68   m_room = room;
       
    69   emit roomChanged(m_room);
    53 }
    70 }
    54 
    71 
    55 void NetSession::close() {
    72 void NetSession::close() {
    56   if (!m_socket.isNull()) {
    73   if (!m_socket.isNull()) {
    57     m_socket->disconnectFromHost();
    74     m_socket->disconnectFromHost();
    58     m_socket.clear();
    75     m_socket.clear();
    59 
    76 
    60     setSessionState(NotConnected);
    77     setSessionState(NotConnected);
       
    78     setRoom({});
    61   }
    79   }
    62 }
    80 }
    63 
    81 
    64 void NetSession::onReadyRead() {
    82 void NetSession::onReadyRead() {
    65   while (m_socket->canReadLine()) {
    83   while (m_socket->canReadLine()) {
   180 
   198 
   181 void NetSession::handleKicked(const QStringList &parameters) {}
   199 void NetSession::handleKicked(const QStringList &parameters) {}
   182 
   200 
   183 void NetSession::handleLeft(const QStringList &parameters) {}
   201 void NetSession::handleLeft(const QStringList &parameters) {}
   184 
   202 
   185 void NetSession::handleLobbyJoined(const QStringList &parameters) {}
   203 void NetSession::handleLobbyJoined(const QStringList &parameters) {
   186 
   204   for (auto player : parameters) {
   187 void NetSession::handleLobbyLeft(const QStringList &parameters) {}
   205     if (player == m_nickname) {
       
   206       // check if server is authenticated or no authentication was performed at
       
   207       // all
       
   208       if (!m_serverAuthHash.isEmpty()) {
       
   209         emit error(tr("Server authentication error"));
       
   210 
       
   211         close();
       
   212       }
       
   213 
       
   214       setSessionState(Lobby);
       
   215     }
       
   216 
       
   217     m_playersModel->addPlayer(player, false);
       
   218   }
       
   219 }
       
   220 
       
   221 void NetSession::handleLobbyLeft(const QStringList &parameters) {
       
   222   if (parameters.length() == 1) {
       
   223     m_playersModel->removePlayer(parameters[0]);
       
   224   } else if (parameters.length() == 2) {
       
   225     m_playersModel->removePlayer(parameters[0], parameters[1]);
       
   226   } else {
       
   227     qWarning("Malformed LOBBY:LEFT message");
       
   228   }
       
   229 }
   188 
   230 
   189 void NetSession::handleNick(const QStringList &parameters) {
   231 void NetSession::handleNick(const QStringList &parameters) {
   190   if (parameters.length()) setNickname(parameters[0]);
   232   if (parameters.length()) setNickname(parameters[0]);
   191 }
   233 }
   192 
   234 
   204 
   246 
   205 void NetSession::handleReplayStart(const QStringList &parameters) {}
   247 void NetSession::handleReplayStart(const QStringList &parameters) {}
   206 
   248 
   207 void NetSession::handleRoomAbandoned(const QStringList &parameters) {}
   249 void NetSession::handleRoomAbandoned(const QStringList &parameters) {}
   208 
   250 
   209 void NetSession::handleRoom(const QStringList &parameters) {}
   251 void NetSession::handleRoom(const QStringList &parameters) {
   210 
   252   if (parameters.size() == 10 && parameters[0] == "ADD") {
   211 void NetSession::handleRooms(const QStringList &parameters) {}
   253     m_roomsModel->addRoom(parameters.mid(1));
       
   254   } else if (parameters.length() == 11 && parameters[0] == "UPD") {
       
   255     m_roomsModel->updateRoom(parameters[1], parameters.mid(2));
       
   256 
       
   257     // keep track of room name so correct name is displayed
       
   258     if (m_room == parameters[1]) {
       
   259       setRoom(parameters[2]);
       
   260     }
       
   261   } else if (parameters.size() == 2 && parameters[0] == "DEL") {
       
   262     m_roomsModel->removeRoom(parameters[1]);
       
   263   }
       
   264 }
       
   265 
       
   266 void NetSession::handleRooms(const QStringList &parameters) {
       
   267   if (parameters.size() % 9) {
       
   268     qWarning("Net: Malformed ROOMS message");
       
   269     return;
       
   270   }
       
   271 
       
   272   m_roomsModel->setRoomsList(parameters);
       
   273 }
   212 
   274 
   213 void NetSession::handleRoundFinished(const QStringList &parameters) {}
   275 void NetSession::handleRoundFinished(const QStringList &parameters) {}
   214 
   276 
   215 void NetSession::handleRunGame(const QStringList &parameters) {}
   277 void NetSession::handleRunGame(const QStringList &parameters) {}
   216 
   278