qmlfrontend/net_session.cpp
author unc0rr
Fri, 24 May 2019 23:39:51 +0200
changeset 15052 773beead236f
parent 15044 a4a058dcbbd6
child 15083 fb7a9b0119d3
permissions -rw-r--r--
Add handling of some messages, reuse models from the old frontend
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     1
#include "net_session.h"
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     2
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
     3
#include "players_model.h"
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
     4
#include "rooms_model.h"
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
     5
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
     6
NetSession::NetSession(QObject *parent)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
     7
    : QObject(parent),
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
     8
      m_playersModel(new PlayersListModel()),
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
     9
      m_roomsModel(new RoomsListModel()) {}
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    10
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    11
NetSession::~NetSession() { close(); }
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    12
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    13
QUrl NetSession::url() const { return m_url; }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    14
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    15
QAbstractSocket::SocketState NetSession::state() const {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    16
  if (m_socket)
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    17
    return m_socket->state();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    18
  else
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    19
    return QAbstractSocket::UnconnectedState;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    20
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    21
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    22
void NetSession::open() {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    23
  m_socket.reset(new QTcpSocket());
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    24
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    25
  connect(m_socket.data(), &QAbstractSocket::stateChanged, this,
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    26
          &NetSession::stateChanged);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    27
  connect(m_socket.data(), &QTcpSocket::readyRead, this,
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    28
          &NetSession::onReadyRead);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    29
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    30
  m_socket->connectToHost(m_url.host(),
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    31
                          static_cast<quint16>(m_url.port(46631)));
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    32
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    33
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    34
QString NetSession::nickname() const { return m_nickname; }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    35
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    36
QString NetSession::password() const { return m_password; }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    37
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    38
NetSession::SessionState NetSession::sessionState() const {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    39
  return m_sessionState;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    40
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    41
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    42
QString NetSession::room() const { return m_room; }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    43
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    44
void NetSession::setUrl(const QUrl &url) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    45
  if (m_url == url) return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    46
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    47
  m_url = url;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    48
  emit urlChanged(m_url);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    49
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    50
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    51
void NetSession::setNickname(const QString &nickname) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    52
  if (m_nickname == nickname) return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    53
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    54
  m_nickname = nickname;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    55
  emit nicknameChanged(m_nickname);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    56
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    57
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    58
void NetSession::setPassword(const QString &password) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    59
  if (m_password == password) return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    60
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    61
  m_password = password;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    62
  emit passwordChanged(m_password);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    63
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    64
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    65
void NetSession::setRoom(const QString &room) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    66
  if (m_room == room) return;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    67
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    68
  m_room = room;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    69
  emit roomChanged(m_room);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    70
}
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    71
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    72
void NetSession::close() {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    73
  if (!m_socket.isNull()) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    74
    m_socket->disconnectFromHost();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    75
    m_socket.clear();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    76
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    77
    setSessionState(NotConnected);
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
    78
    setRoom({});
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    79
  }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    80
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    81
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    82
void NetSession::onReadyRead() {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    83
  while (m_socket->canReadLine()) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    84
    auto line = QString::fromUtf8(m_socket->readLine().simplified());
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    85
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    86
    if (line.isEmpty()) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    87
      parseNetMessage(m_buffer);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    88
      m_buffer.clear();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    89
    } else {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    90
      m_buffer.append(line);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    91
    }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    92
  }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    93
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    94
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    95
void NetSession::parseNetMessage(const QStringList &message) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    96
  if (message.isEmpty()) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    97
    qWarning() << "Empty net message received";
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    98
    return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    99
  }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   100
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   101
  qDebug() << "[SERVER]" << message;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   102
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   103
  using Handler = std::function<void(NetSession *, const QStringList &)>;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   104
  static QMap<QString, Handler> commandsMap{
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   105
      {"ADD_TEAM", &NetSession::handleAddTeam},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   106
      {"ASKPASSWORD", &NetSession::handleAskPassword},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   107
      {"BANLIST", &NetSession::handleBanList},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   108
      {"BYE", &NetSession::handleBye},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   109
      {"CFG", &NetSession::handleCfg},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   110
      {"CHAT", &NetSession::handleChat},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   111
      {"CLIENT_FLAGS", &NetSession::handleClientFlags},
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   112
      {"CONNECTED", &NetSession::handleConnected},
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   113
      {"EM", &NetSession::handleEm},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   114
      {"ERROR", &NetSession::handleError},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   115
      {"HH_NUM", &NetSession::handleHhNum},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   116
      {"INFO", &NetSession::handleInfo},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   117
      {"JOINED", &NetSession::handleJoined},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   118
      {"JOINING", &NetSession::handleJoining},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   119
      {"KICKED", &NetSession::handleKicked},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   120
      {"LEFT", &NetSession::handleLeft},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   121
      {"LOBBY:JOINED", &NetSession::handleLobbyJoined},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   122
      {"LOBBY:LEFT", &NetSession::handleLobbyLeft},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   123
      {"NICK", &NetSession::handleNick},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   124
      {"NOTICE", &NetSession::handleNotice},
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   125
      {"PING", &NetSession::handlePing},
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   126
      {"PONG", &NetSession::handlePong},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   127
      {"PROTO", &NetSession::handleProto},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   128
      {"REDIRECT", &NetSession::handleRedirect},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   129
      {"REMOVE_TEAM", &NetSession::handleRemoveTeam},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   130
      {"REPLAY_START", &NetSession::handleReplayStart},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   131
      {"ROOMABANDONED", &NetSession::handleRoomAbandoned},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   132
      {"ROOM", &NetSession::handleRoom},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   133
      {"ROOMS", &NetSession::handleRooms},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   134
      {"ROUND_FINISHED", &NetSession::handleRoundFinished},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   135
      {"RUN_GAME", &NetSession::handleRunGame},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   136
      {"SERVER_AUTH", &NetSession::handleServerAuth},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   137
      {"SERVER_MESSAGE", &NetSession::handleServerMessage},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   138
      {"SERVER_VARS", &NetSession::handleServerVars},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   139
      {"TEAM_ACCEPTED", &NetSession::handleTeamAccepted},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   140
      {"TEAM_COLOR", &NetSession::handleTeamColor},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   141
      {"WARNING", &NetSession::handleWarning},
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   142
  };
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   143
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   144
  auto handler =
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   145
      commandsMap.value(message[0], &NetSession::handleUnknownCommand);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   146
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   147
  handler(this, message.mid(1));
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   148
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   149
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   150
void NetSession::handleConnected(const QStringList &parameters) {
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   151
  if (parameters.length() < 2 || parameters[1].toInt() < cMinServerVersion) {
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   152
    send("QUIT", "Server too old");
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   153
    emit error(tr("Server too old"));
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   154
    close();
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   155
  } else {
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   156
    setSessionState(Login);
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   157
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   158
    send("NICK", m_nickname);
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   159
    send("PROTO", QString::number(cProtocolVersion));
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   160
  }
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   161
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   162
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   163
void NetSession::handlePing(const QStringList &parameters) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   164
  send("PONG", parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   165
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   166
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   167
void NetSession::handleBye(const QStringList &parameters) { close(); }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   168
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   169
void NetSession::handleUnknownCommand(const QStringList &parameters) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   170
  Q_UNUSED(parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   171
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   172
  qWarning() << "Command is not recognized";
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   173
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   174
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   175
void NetSession::handleAddTeam(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   176
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   177
void NetSession::handleAskPassword(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   178
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   179
void NetSession::handleBanList(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   180
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   181
void NetSession::handleCfg(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   182
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   183
void NetSession::handleChat(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   184
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   185
void NetSession::handleClientFlags(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   186
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   187
void NetSession::handleEm(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   188
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   189
void NetSession::handleError(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   190
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   191
void NetSession::handleHhNum(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   192
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   193
void NetSession::handleInfo(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   194
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   195
void NetSession::handleJoined(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   196
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   197
void NetSession::handleJoining(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   198
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   199
void NetSession::handleKicked(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   200
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   201
void NetSession::handleLeft(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   202
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   203
void NetSession::handleLobbyJoined(const QStringList &parameters) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   204
  for (auto player : parameters) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   205
    if (player == m_nickname) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   206
      // check if server is authenticated or no authentication was performed at
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   207
      // all
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   208
      if (!m_serverAuthHash.isEmpty()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   209
        emit error(tr("Server authentication error"));
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   210
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   211
        close();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   212
      }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   213
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   214
      setSessionState(Lobby);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   215
    }
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   216
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   217
    m_playersModel->addPlayer(player, false);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   218
  }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   219
}
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   220
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   221
void NetSession::handleLobbyLeft(const QStringList &parameters) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   222
  if (parameters.length() == 1) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   223
    m_playersModel->removePlayer(parameters[0]);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   224
  } else if (parameters.length() == 2) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   225
    m_playersModel->removePlayer(parameters[0], parameters[1]);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   226
  } else {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   227
    qWarning("Malformed LOBBY:LEFT message");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   228
  }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   229
}
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   230
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   231
void NetSession::handleNick(const QStringList &parameters) {
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   232
  if (parameters.length()) setNickname(parameters[0]);
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   233
}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   234
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   235
void NetSession::handleNotice(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   236
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   237
void NetSession::handlePong(const QStringList &parameters) {
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   238
  Q_UNUSED(parameters)
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   239
}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   240
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   241
void NetSession::handleProto(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   242
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   243
void NetSession::handleRedirect(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   244
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   245
void NetSession::handleRemoveTeam(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   246
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   247
void NetSession::handleReplayStart(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   248
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   249
void NetSession::handleRoomAbandoned(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   250
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   251
void NetSession::handleRoom(const QStringList &parameters) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   252
  if (parameters.size() == 10 && parameters[0] == "ADD") {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   253
    m_roomsModel->addRoom(parameters.mid(1));
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   254
  } else if (parameters.length() == 11 && parameters[0] == "UPD") {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   255
    m_roomsModel->updateRoom(parameters[1], parameters.mid(2));
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   256
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   257
    // keep track of room name so correct name is displayed
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   258
    if (m_room == parameters[1]) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   259
      setRoom(parameters[2]);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   260
    }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   261
  } else if (parameters.size() == 2 && parameters[0] == "DEL") {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   262
    m_roomsModel->removeRoom(parameters[1]);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   263
  }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   264
}
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   265
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   266
void NetSession::handleRooms(const QStringList &parameters) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   267
  if (parameters.size() % 9) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   268
    qWarning("Net: Malformed ROOMS message");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   269
    return;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   270
  }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   271
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   272
  m_roomsModel->setRoomsList(parameters);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 15044
diff changeset
   273
}
15044
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   274
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   275
void NetSession::handleRoundFinished(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   276
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   277
void NetSession::handleRunGame(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   278
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   279
void NetSession::handleServerAuth(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   280
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   281
void NetSession::handleServerMessage(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   282
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   283
void NetSession::handleServerVars(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   284
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   285
void NetSession::handleTeamAccepted(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   286
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   287
void NetSession::handleTeamColor(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   288
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   289
void NetSession::handleWarning(const QStringList &parameters) {}
a4a058dcbbd6 Add slots for all protocol messages
unC0Rr
parents: 14920
diff changeset
   290
14920
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   291
void NetSession::send(const QString &message) { send(QStringList(message)); }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   292
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   293
void NetSession::send(const QString &message, const QString &param) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   294
  send(QStringList{message, param});
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   295
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   296
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   297
void NetSession::send(const QString &message, const QStringList &parameters) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   298
  send(QStringList(message) + parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   299
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   300
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   301
void NetSession::send(const QStringList &message) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   302
  Q_ASSERT(!m_socket.isNull());
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   303
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   304
  qDebug() << "[CLIENT]" << message;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   305
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   306
  m_socket->write(message.join('\n').toUtf8() + "\n\n");
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   307
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   308
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   309
void NetSession::setSessionState(NetSession::SessionState sessionState) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   310
  if (m_sessionState == sessionState) return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   311
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   312
  m_sessionState = sessionState;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   313
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   314
  emit sessionStateChanged(sessionState);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   315
}