qmlfrontend/net_session.cpp
changeset 15039 a4a058dcbbd6
parent 14915 a3ad06ac390e
child 15047 773beead236f
equal deleted inserted replaced
15038:01bb1d7ca14f 15039:a4a058dcbbd6
    82 
    82 
    83   qDebug() << "[SERVER]" << message;
    83   qDebug() << "[SERVER]" << message;
    84 
    84 
    85   using Handler = std::function<void(NetSession *, const QStringList &)>;
    85   using Handler = std::function<void(NetSession *, const QStringList &)>;
    86   static QMap<QString, Handler> commandsMap{
    86   static QMap<QString, Handler> commandsMap{
       
    87       {"ADD_TEAM", &NetSession::handleAddTeam},
       
    88       {"ASKPASSWORD", &NetSession::handleAskPassword},
       
    89       {"BANLIST", &NetSession::handleBanList},
       
    90       {"BYE", &NetSession::handleBye},
       
    91       {"CFG", &NetSession::handleCfg},
       
    92       {"CHAT", &NetSession::handleChat},
       
    93       {"CLIENT_FLAGS", &NetSession::handleClientFlags},
    87       {"CONNECTED", &NetSession::handleConnected},
    94       {"CONNECTED", &NetSession::handleConnected},
       
    95       {"EM", &NetSession::handleEm},
       
    96       {"ERROR", &NetSession::handleError},
       
    97       {"HH_NUM", &NetSession::handleHhNum},
       
    98       {"INFO", &NetSession::handleInfo},
       
    99       {"JOINED", &NetSession::handleJoined},
       
   100       {"JOINING", &NetSession::handleJoining},
       
   101       {"KICKED", &NetSession::handleKicked},
       
   102       {"LEFT", &NetSession::handleLeft},
       
   103       {"LOBBY:JOINED", &NetSession::handleLobbyJoined},
       
   104       {"LOBBY:LEFT", &NetSession::handleLobbyLeft},
       
   105       {"NICK", &NetSession::handleNick},
       
   106       {"NOTICE", &NetSession::handleNotice},
    88       {"PING", &NetSession::handlePing},
   107       {"PING", &NetSession::handlePing},
    89       {"BYE", &NetSession::handleBye}};
   108       {"PONG", &NetSession::handlePong},
       
   109       {"PROTO", &NetSession::handleProto},
       
   110       {"REDIRECT", &NetSession::handleRedirect},
       
   111       {"REMOVE_TEAM", &NetSession::handleRemoveTeam},
       
   112       {"REPLAY_START", &NetSession::handleReplayStart},
       
   113       {"ROOMABANDONED", &NetSession::handleRoomAbandoned},
       
   114       {"ROOM", &NetSession::handleRoom},
       
   115       {"ROOMS", &NetSession::handleRooms},
       
   116       {"ROUND_FINISHED", &NetSession::handleRoundFinished},
       
   117       {"RUN_GAME", &NetSession::handleRunGame},
       
   118       {"SERVER_AUTH", &NetSession::handleServerAuth},
       
   119       {"SERVER_MESSAGE", &NetSession::handleServerMessage},
       
   120       {"SERVER_VARS", &NetSession::handleServerVars},
       
   121       {"TEAM_ACCEPTED", &NetSession::handleTeamAccepted},
       
   122       {"TEAM_COLOR", &NetSession::handleTeamColor},
       
   123       {"WARNING", &NetSession::handleWarning},
       
   124   };
    90 
   125 
    91   auto handler =
   126   auto handler =
    92       commandsMap.value(message[0], &NetSession::handleUnknownCommand);
   127       commandsMap.value(message[0], &NetSession::handleUnknownCommand);
    93 
   128 
    94   handler(this, message.mid(1));
   129   handler(this, message.mid(1));
    95 }
   130 }
    96 
   131 
    97 void NetSession::handleConnected(const QStringList &parameters) {
   132 void NetSession::handleConnected(const QStringList &parameters) {
    98   setSessionState(Login);
   133   if (parameters.length() < 2 || parameters[1].toInt() < cMinServerVersion) {
       
   134     send("QUIT", "Server too old");
       
   135     emit error(tr("Server too old"));
       
   136     close();
       
   137   } else {
       
   138     setSessionState(Login);
       
   139 
       
   140     send("NICK", m_nickname);
       
   141     send("PROTO", QString::number(cProtocolVersion));
       
   142   }
    99 }
   143 }
   100 
   144 
   101 void NetSession::handlePing(const QStringList &parameters) {
   145 void NetSession::handlePing(const QStringList &parameters) {
   102   send("PONG", parameters);
   146   send("PONG", parameters);
   103 }
   147 }
   108   Q_UNUSED(parameters);
   152   Q_UNUSED(parameters);
   109 
   153 
   110   qWarning() << "Command is not recognized";
   154   qWarning() << "Command is not recognized";
   111 }
   155 }
   112 
   156 
       
   157 void NetSession::handleAddTeam(const QStringList &parameters) {}
       
   158 
       
   159 void NetSession::handleAskPassword(const QStringList &parameters) {}
       
   160 
       
   161 void NetSession::handleBanList(const QStringList &parameters) {}
       
   162 
       
   163 void NetSession::handleCfg(const QStringList &parameters) {}
       
   164 
       
   165 void NetSession::handleChat(const QStringList &parameters) {}
       
   166 
       
   167 void NetSession::handleClientFlags(const QStringList &parameters) {}
       
   168 
       
   169 void NetSession::handleEm(const QStringList &parameters) {}
       
   170 
       
   171 void NetSession::handleError(const QStringList &parameters) {}
       
   172 
       
   173 void NetSession::handleHhNum(const QStringList &parameters) {}
       
   174 
       
   175 void NetSession::handleInfo(const QStringList &parameters) {}
       
   176 
       
   177 void NetSession::handleJoined(const QStringList &parameters) {}
       
   178 
       
   179 void NetSession::handleJoining(const QStringList &parameters) {}
       
   180 
       
   181 void NetSession::handleKicked(const QStringList &parameters) {}
       
   182 
       
   183 void NetSession::handleLeft(const QStringList &parameters) {}
       
   184 
       
   185 void NetSession::handleLobbyJoined(const QStringList &parameters) {}
       
   186 
       
   187 void NetSession::handleLobbyLeft(const QStringList &parameters) {}
       
   188 
       
   189 void NetSession::handleNick(const QStringList &parameters) {
       
   190   if (parameters.length()) setNickname(parameters[0]);
       
   191 }
       
   192 
       
   193 void NetSession::handleNotice(const QStringList &parameters) {}
       
   194 
       
   195 void NetSession::handlePong(const QStringList &parameters) {
       
   196   Q_UNUSED(parameters)
       
   197 }
       
   198 
       
   199 void NetSession::handleProto(const QStringList &parameters) {}
       
   200 
       
   201 void NetSession::handleRedirect(const QStringList &parameters) {}
       
   202 
       
   203 void NetSession::handleRemoveTeam(const QStringList &parameters) {}
       
   204 
       
   205 void NetSession::handleReplayStart(const QStringList &parameters) {}
       
   206 
       
   207 void NetSession::handleRoomAbandoned(const QStringList &parameters) {}
       
   208 
       
   209 void NetSession::handleRoom(const QStringList &parameters) {}
       
   210 
       
   211 void NetSession::handleRooms(const QStringList &parameters) {}
       
   212 
       
   213 void NetSession::handleRoundFinished(const QStringList &parameters) {}
       
   214 
       
   215 void NetSession::handleRunGame(const QStringList &parameters) {}
       
   216 
       
   217 void NetSession::handleServerAuth(const QStringList &parameters) {}
       
   218 
       
   219 void NetSession::handleServerMessage(const QStringList &parameters) {}
       
   220 
       
   221 void NetSession::handleServerVars(const QStringList &parameters) {}
       
   222 
       
   223 void NetSession::handleTeamAccepted(const QStringList &parameters) {}
       
   224 
       
   225 void NetSession::handleTeamColor(const QStringList &parameters) {}
       
   226 
       
   227 void NetSession::handleWarning(const QStringList &parameters) {}
       
   228 
   113 void NetSession::send(const QString &message) { send(QStringList(message)); }
   229 void NetSession::send(const QString &message) { send(QStringList(message)); }
   114 
   230 
   115 void NetSession::send(const QString &message, const QString &param) {
   231 void NetSession::send(const QString &message, const QString &param) {
   116   send(QStringList{message, param});
   232   send(QStringList{message, param});
   117 }
   233 }