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 ¶meters) { |
132 void NetSession::handleConnected(const QStringList ¶meters) { |
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 ¶meters) { |
145 void NetSession::handlePing(const QStringList ¶meters) { |
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 ¶meters) {} |
|
158 |
|
159 void NetSession::handleAskPassword(const QStringList ¶meters) {} |
|
160 |
|
161 void NetSession::handleBanList(const QStringList ¶meters) {} |
|
162 |
|
163 void NetSession::handleCfg(const QStringList ¶meters) {} |
|
164 |
|
165 void NetSession::handleChat(const QStringList ¶meters) {} |
|
166 |
|
167 void NetSession::handleClientFlags(const QStringList ¶meters) {} |
|
168 |
|
169 void NetSession::handleEm(const QStringList ¶meters) {} |
|
170 |
|
171 void NetSession::handleError(const QStringList ¶meters) {} |
|
172 |
|
173 void NetSession::handleHhNum(const QStringList ¶meters) {} |
|
174 |
|
175 void NetSession::handleInfo(const QStringList ¶meters) {} |
|
176 |
|
177 void NetSession::handleJoined(const QStringList ¶meters) {} |
|
178 |
|
179 void NetSession::handleJoining(const QStringList ¶meters) {} |
|
180 |
|
181 void NetSession::handleKicked(const QStringList ¶meters) {} |
|
182 |
|
183 void NetSession::handleLeft(const QStringList ¶meters) {} |
|
184 |
|
185 void NetSession::handleLobbyJoined(const QStringList ¶meters) {} |
|
186 |
|
187 void NetSession::handleLobbyLeft(const QStringList ¶meters) {} |
|
188 |
|
189 void NetSession::handleNick(const QStringList ¶meters) { |
|
190 if (parameters.length()) setNickname(parameters[0]); |
|
191 } |
|
192 |
|
193 void NetSession::handleNotice(const QStringList ¶meters) {} |
|
194 |
|
195 void NetSession::handlePong(const QStringList ¶meters) { |
|
196 Q_UNUSED(parameters) |
|
197 } |
|
198 |
|
199 void NetSession::handleProto(const QStringList ¶meters) {} |
|
200 |
|
201 void NetSession::handleRedirect(const QStringList ¶meters) {} |
|
202 |
|
203 void NetSession::handleRemoveTeam(const QStringList ¶meters) {} |
|
204 |
|
205 void NetSession::handleReplayStart(const QStringList ¶meters) {} |
|
206 |
|
207 void NetSession::handleRoomAbandoned(const QStringList ¶meters) {} |
|
208 |
|
209 void NetSession::handleRoom(const QStringList ¶meters) {} |
|
210 |
|
211 void NetSession::handleRooms(const QStringList ¶meters) {} |
|
212 |
|
213 void NetSession::handleRoundFinished(const QStringList ¶meters) {} |
|
214 |
|
215 void NetSession::handleRunGame(const QStringList ¶meters) {} |
|
216 |
|
217 void NetSession::handleServerAuth(const QStringList ¶meters) {} |
|
218 |
|
219 void NetSession::handleServerMessage(const QStringList ¶meters) {} |
|
220 |
|
221 void NetSession::handleServerVars(const QStringList ¶meters) {} |
|
222 |
|
223 void NetSession::handleTeamAccepted(const QStringList ¶meters) {} |
|
224 |
|
225 void NetSession::handleTeamColor(const QStringList ¶meters) {} |
|
226 |
|
227 void NetSession::handleWarning(const QStringList ¶meters) {} |
|
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 ¶m) { |
231 void NetSession::send(const QString &message, const QString ¶m) { |
116 send(QStringList{message, param}); |
232 send(QStringList{message, param}); |
117 } |
233 } |