184
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include <QMessageBox>
|
|
20 |
#include <QUuid>
|
|
21 |
#include "netclient.h"
|
|
22 |
#include "game.h"
|
|
23 |
#include "gameuiconfig.h"
|
|
24 |
|
|
25 |
HWNet::HWNet(GameUIConfig * config)
|
|
26 |
: QObject()
|
|
27 |
{
|
|
28 |
this->config = config;
|
|
29 |
state = nsDisconnected;
|
|
30 |
IRCmsg_cmd_text = new QRegExp("^[A-Z]+ :.+$");
|
|
31 |
IRCmsg_number_param = new QRegExp("^:\\S+ [0-9]{3} .+$");
|
|
32 |
IRCmsg_who_cmd_target = new QRegExp("^:\\S+ [A-Z]+ \\S+$"); // last \\S should mean 'the 1st char is not ":"'
|
|
33 |
IRCmsg_who_cmd_text = new QRegExp("^:\\S+ [A-Z]+ :.+$");
|
|
34 |
IRCmsg_who_cmd_target_text = new QRegExp("^:\\S+ [A-Z]+ \\S+ :.+$");
|
|
35 |
isOp = false;
|
|
36 |
teamsCount = 0;
|
|
37 |
|
|
38 |
connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
39 |
connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect()));
|
|
40 |
connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
|
|
41 |
connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
|
|
42 |
SLOT(displayError(QAbstractSocket::SocketError)));
|
|
43 |
}
|
|
44 |
|
|
45 |
void HWNet::ClientRead()
|
|
46 |
{
|
|
47 |
while (NetSocket.canReadLine())
|
|
48 |
{
|
|
49 |
ParseLine(NetSocket.readLine().trimmed());
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 |
void HWNet::displayError(QAbstractSocket::SocketError socketError)
|
|
54 |
{
|
|
55 |
switch (socketError)
|
|
56 |
{
|
|
57 |
case QAbstractSocket::RemoteHostClosedError:
|
|
58 |
break;
|
|
59 |
case QAbstractSocket::HostNotFoundError:
|
|
60 |
QMessageBox::information(0, tr("Error"),
|
|
61 |
tr("The host was not found. Please check the host name and port settings."));
|
|
62 |
break;
|
|
63 |
case QAbstractSocket::ConnectionRefusedError:
|
|
64 |
QMessageBox::information(0, tr("Error"),
|
|
65 |
tr("Connection refused"));
|
|
66 |
break;
|
|
67 |
default:
|
|
68 |
QMessageBox::information(0, tr("Error"),
|
|
69 |
NetSocket.errorString());
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
void HWNet::Connect(const QString & hostName, quint16 port, const QString & nick)
|
|
74 |
{
|
|
75 |
state = nsConnecting;
|
|
76 |
NetSocket.connectToHost(hostName, port);
|
|
77 |
mynick = nick;
|
|
78 |
opnick = "";
|
|
79 |
opCount = 0;
|
|
80 |
}
|
|
81 |
|
|
82 |
|
|
83 |
void HWNet::OnConnect()
|
|
84 |
{
|
|
85 |
state = nsConnected;
|
|
86 |
RawSendNet(QString("USER hwgame 1 2 Hedgewars game"));
|
|
87 |
RawSendNet(QString("NICK %1").arg(mynick));
|
|
88 |
}
|
|
89 |
|
|
90 |
void HWNet::OnDisconnect()
|
|
91 |
{
|
|
92 |
state = nsDisconnected;
|
|
93 |
}
|
|
94 |
|
|
95 |
void HWNet::Perform()
|
|
96 |
{
|
|
97 |
|
|
98 |
}
|
|
99 |
|
|
100 |
void HWNet::JoinGame(const QString & game)
|
|
101 |
{
|
|
102 |
state = nsJoining;
|
|
103 |
RawSendNet(QString("JOIN %1").arg(game));
|
|
104 |
}
|
|
105 |
|
|
106 |
void HWNet::Disconnect()
|
|
107 |
{
|
|
108 |
switch (state)
|
|
109 |
{
|
|
110 |
case nsDisconnected:
|
|
111 |
{
|
|
112 |
break;
|
|
113 |
}
|
|
114 |
case nsConnecting:
|
|
115 |
case nsQuitting:
|
|
116 |
{
|
|
117 |
NetSocket.disconnect();
|
|
118 |
break;
|
|
119 |
}
|
|
120 |
default:
|
|
121 |
{
|
|
122 |
state = nsQuitting;
|
|
123 |
RawSendNet(QString("QUIT :oops"));
|
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
void HWNet::RawSendNet(const QString & str)
|
|
129 |
{
|
|
130 |
RawSendNet(str.toUtf8());
|
|
131 |
}
|
|
132 |
|
|
133 |
void HWNet::RawSendNet(const QByteArray & buf)
|
|
134 |
{
|
|
135 |
if (buf.size() > 510) return;
|
|
136 |
NetSocket.write(buf);
|
|
137 |
NetSocket.write("\x0d\x0a", 2);
|
|
138 |
}
|
|
139 |
|
|
140 |
void HWNet::SendNet(const QByteArray & buf)
|
|
141 |
{
|
|
142 |
if ((state == nsGaming) || (state == nsStarting))
|
|
143 |
{
|
|
144 |
QString msg = QString(buf.toBase64());
|
|
145 |
if ((msg == "AUM=") && (mynick == opnick))
|
|
146 |
{
|
|
147 |
ConfigAsked();
|
|
148 |
} else
|
|
149 |
if (msg == "AT8=")
|
|
150 |
{
|
|
151 |
// its ping ("?")
|
|
152 |
} else
|
|
153 |
{
|
|
154 |
if (state == nsGaming)
|
|
155 |
{
|
|
156 |
NetBuffer += buf;
|
|
157 |
} else
|
|
158 |
{
|
|
159 |
RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, msg));
|
|
160 |
}
|
|
161 |
}
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
void HWNet::FlushNetBuf()
|
|
166 |
{
|
|
167 |
if (NetBuffer.size() > 0)
|
|
168 |
{
|
|
169 |
RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, QString(NetBuffer.toBase64())));
|
|
170 |
NetBuffer.clear();
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
void HWNet::ParseLine(const QByteArray & line)
|
|
175 |
{
|
|
176 |
QString msg = QString::fromUtf8 (line.data(), line.size());
|
|
177 |
//QMessageBox::information(0, "", msg);
|
|
178 |
if (IRCmsg_cmd_text->exactMatch(msg))
|
|
179 |
{
|
|
180 |
msgcmd_textHandler(msg);
|
|
181 |
} else
|
|
182 |
if (IRCmsg_number_param->exactMatch(msg))
|
|
183 |
{
|
|
184 |
msgnumber_paramHandler(msg);
|
|
185 |
} else
|
|
186 |
if (IRCmsg_who_cmd_text->exactMatch(msg))
|
|
187 |
{
|
|
188 |
msgwho_cmd_textHandler(msg);
|
|
189 |
} else
|
|
190 |
if (IRCmsg_who_cmd_target->exactMatch(msg))
|
|
191 |
{
|
|
192 |
msgwho_cmd_targetHandler(msg);
|
|
193 |
} else
|
|
194 |
if (IRCmsg_who_cmd_target_text->exactMatch(msg))
|
|
195 |
{
|
|
196 |
msgwho_cmd_target_textHandler(msg);
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
void HWNet::msgcmd_textHandler(const QString & msg)
|
|
201 |
{
|
|
202 |
QStringList list = msg.split(" :");
|
|
203 |
if (list[0] == "PING")
|
|
204 |
{
|
|
205 |
RawSendNet(QString("PONG %1").arg(list[1]));
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
void HWNet::msgnumber_paramHandler(const QString & msg)
|
|
210 |
{
|
|
211 |
int pos = msg.indexOf(" :");
|
|
212 |
QString text = msg.mid(pos + 2);
|
|
213 |
QStringList list = msg.mid(0, pos).split(" ");
|
|
214 |
bool ok;
|
|
215 |
quint16 number = list[1].toInt(&ok);
|
|
216 |
if (!ok)
|
|
217 |
return ;
|
|
218 |
switch (number)
|
|
219 |
{
|
|
220 |
case 001 :
|
|
221 |
{
|
|
222 |
Perform();
|
|
223 |
emit Connected();
|
|
224 |
break;
|
|
225 |
}
|
|
226 |
case 322 : // RPL_LIST
|
|
227 |
{
|
|
228 |
emit AddGame(list[3]);
|
|
229 |
break;
|
|
230 |
}
|
|
231 |
case 353 : // RPL_NAMREPLY
|
|
232 |
{
|
|
233 |
QStringList ops = text.split(" ").filter(QRegExp("^@\\S+$"));
|
|
234 |
opCount += ops.size();
|
|
235 |
if (ops.size() == 1)
|
|
236 |
{
|
|
237 |
opnick = ops[0].mid(1);
|
|
238 |
}
|
|
239 |
break;
|
|
240 |
}
|
|
241 |
case 366 : // RPL_ENDOFNAMES
|
|
242 |
{
|
|
243 |
if (opCount != 1)
|
|
244 |
{
|
|
245 |
opnick = "";
|
|
246 |
}
|
|
247 |
opCount = 0;
|
|
248 |
break;
|
|
249 |
}
|
|
250 |
case 432 : // ERR_ERRONEUSNICKNAME
|
|
251 |
case 433 : // ERR_NICKNAMEINUSE
|
|
252 |
{
|
|
253 |
QMessageBox::information(0, "Your net nickname is in use or cannot be used", msg);
|
|
254 |
// ask for another nick
|
|
255 |
}
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
void HWNet::msgwho_cmd_targetHandler(const QString & msg)
|
|
260 |
{
|
|
261 |
QStringList list = msg.split(" ");
|
|
262 |
QString who = list[0].mid(1).split("!")[0];
|
|
263 |
if (list[1] == "NICK")
|
|
264 |
{
|
|
265 |
if (mynick == who)
|
|
266 |
mynick = list[2];
|
|
267 |
if (opnick == who)
|
|
268 |
opnick = list[2];
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
void HWNet::msgwho_cmd_textHandler(const QString & msg)
|
|
273 |
{
|
|
274 |
int pos = msg.indexOf(" :");
|
|
275 |
QString text = msg.mid(pos + 2);
|
|
276 |
QStringList list = msg.mid(0, pos).split(" ");
|
|
277 |
QString who = list[0].mid(1).split("!")[0];
|
|
278 |
if (list[1] == "JOIN")
|
|
279 |
{
|
|
280 |
if (who == mynick)
|
|
281 |
{
|
|
282 |
channel = text;
|
|
283 |
state = nsJoined;
|
|
284 |
emit EnteredGame();
|
|
285 |
RawSendNet(QString("PRIVMSG %1 :Hello!").arg(channel));
|
|
286 |
}
|
|
287 |
}
|
|
288 |
}
|
|
289 |
void HWNet::msgwho_cmd_target_textHandler(const QString & msg)
|
|
290 |
{
|
|
291 |
int pos = msg.indexOf(" :");
|
|
292 |
QString text = msg.mid(pos + 2);
|
|
293 |
QStringList list = msg.mid(0, pos).split(" ");
|
|
294 |
QString who = list[0].mid(1).split("!")[0];
|
|
295 |
if (list[1] == "PRIVMSG")
|
|
296 |
{
|
|
297 |
if (list[2] == opnick)
|
|
298 |
{
|
|
299 |
hwp_opmsg(who, text);
|
|
300 |
} else
|
|
301 |
if (list[2] == channel)
|
|
302 |
{
|
|
303 |
hwp_chanmsg(who, text);
|
|
304 |
}
|
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
void HWNet::hwp_opmsg(const QString & who, const QString & msg)
|
|
309 |
{
|
|
310 |
if (state != nsJoined)
|
|
311 |
return ;
|
|
312 |
if (!msg.startsWith(MAGIC_CHAR))
|
|
313 |
return ;
|
|
314 |
QStringList list = msg.split(MAGIC_CHAR, QString::SkipEmptyParts);
|
|
315 |
if (list[0] == "A")
|
|
316 |
{
|
|
317 |
list.removeFirst();
|
|
318 |
if (list.size() != 9)
|
|
319 |
return ;
|
|
320 |
if (teamsCount < 5)
|
|
321 |
{
|
|
322 |
teams[teamsCount].nick = who;
|
|
323 |
teams[teamsCount].hhs = list;
|
|
324 |
teamsCount++;
|
|
325 |
QString teamnames;
|
|
326 |
for(int i = 0; i < teamsCount; i++)
|
|
327 |
{
|
|
328 |
teamnames += MAGIC_CHAR;
|
|
329 |
teamnames += teams[i].hhs[0];
|
|
330 |
}
|
|
331 |
QString tmsg = QString(MAGIC_CHAR"=%2").arg(teamnames);
|
|
332 |
RawSendNet(QString("PRIVMSG %1 :").arg(channel) + tmsg);
|
|
333 |
hwp_chanmsg(mynick, tmsg);
|
|
334 |
}
|
|
335 |
}
|
|
336 |
}
|
|
337 |
|
|
338 |
void HWNet::ConfigAsked()
|
|
339 |
{
|
|
340 |
configasks++;
|
|
341 |
if (configasks == playerscnt)
|
|
342 |
{
|
|
343 |
quint32 color = 65535;
|
|
344 |
#define ADD(a) { \
|
|
345 |
QByteArray strmsg; \
|
|
346 |
strmsg.append(a); \
|
|
347 |
quint8 sz = strmsg.size(); \
|
|
348 |
cache.append(QByteArray((char *)&sz, 1)); \
|
|
349 |
cache.append(strmsg); \
|
|
350 |
}
|
|
351 |
{
|
|
352 |
QByteArray cache;
|
|
353 |
ADD("eseed " + seed);
|
|
354 |
ADD("e$gmflags 0");
|
|
355 |
ADD(QString("etheme %1").arg(config->GetRandomTheme()));
|
|
356 |
QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(cache.toBase64());
|
|
357 |
RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, _msg));
|
|
358 |
hwp_chanmsg(mynick, _msg);
|
|
359 |
}
|
|
360 |
for (int i = 0; i < teamsCount; i++)
|
|
361 |
{
|
|
362 |
QString msg;
|
|
363 |
msg = MAGIC_CHAR "T" MAGIC_CHAR + teams[i].nick + MAGIC_CHAR + teams[i].hhs.join(MAGIC_CHAR);
|
|
364 |
RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, msg));
|
|
365 |
hwp_chanmsg(mynick, msg);
|
|
366 |
QByteArray cache;
|
|
367 |
ADD(QString("ecolor %1").arg(color));
|
|
368 |
ADD("eadd hh0 0");
|
|
369 |
ADD("eadd hh1 0");
|
|
370 |
ADD("eadd hh2 0");
|
|
371 |
ADD("eadd hh3 0");
|
|
372 |
ADD("eadd hh4 0");
|
|
373 |
#undef ADD
|
|
374 |
QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(cache.toBase64());
|
|
375 |
RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, _msg));
|
|
376 |
hwp_chanmsg(mynick, _msg);
|
|
377 |
color <<= 8;
|
|
378 |
}
|
|
379 |
SENDCFGSTRNET("!");
|
|
380 |
}
|
|
381 |
}
|
|
382 |
|
|
383 |
void HWNet::hwp_chanmsg(const QString & who, const QString & msg)
|
|
384 |
{
|
|
385 |
if ((state < nsJoined) || (state > nsGaming))
|
|
386 |
{
|
|
387 |
return ;
|
|
388 |
}
|
|
389 |
if (state == nsJoined)
|
|
390 |
{
|
|
391 |
if (msg.startsWith(MAGIC_CHAR"Start!") && (who == opnick))
|
|
392 |
{
|
|
393 |
state = nsStarting;
|
|
394 |
RunGame();
|
|
395 |
return ;
|
|
396 |
}
|
|
397 |
if (msg.startsWith(MAGIC_CHAR"="MAGIC_CHAR) && (who == opnick))
|
|
398 |
{
|
|
399 |
emit ChangeInTeams(msg.mid(3).split(MAGIC_CHAR));
|
|
400 |
}
|
|
401 |
}
|
|
402 |
if (state == nsStarting)
|
|
403 |
{
|
|
404 |
if (msg == MAGIC_CHAR MAGIC_CHAR "AUM=")
|
|
405 |
{
|
|
406 |
if (mynick == opnick) ConfigAsked();
|
|
407 |
return ;
|
|
408 |
}
|
|
409 |
if (msg == MAGIC_CHAR MAGIC_CHAR "ASE=")
|
|
410 |
{
|
|
411 |
state = nsGaming;
|
|
412 |
TimerFlusher = new QTimer();
|
|
413 |
connect(TimerFlusher, SIGNAL(timeout()), this, SLOT(FlushNetBuf()));
|
|
414 |
TimerFlusher->start(2000);
|
|
415 |
}
|
|
416 |
if (msg.startsWith(MAGIC_CHAR"T"MAGIC_CHAR))
|
|
417 |
{
|
|
418 |
NetTeamAdded(msg.mid(3));
|
|
419 |
}
|
|
420 |
}
|
|
421 |
if ((state < nsStarting) || (state > nsGaming))
|
|
422 |
{
|
|
423 |
return;
|
|
424 |
}
|
|
425 |
if (msg.startsWith(MAGIC_CHAR MAGIC_CHAR)) // HWP message
|
|
426 |
{
|
|
427 |
QByteArray em = QByteArray::fromBase64(msg.mid(2).toAscii());
|
|
428 |
emit FromNet(em);
|
|
429 |
} else // smth other
|
|
430 |
{
|
|
431 |
|
|
432 |
}
|
|
433 |
}
|
|
434 |
|
|
435 |
void HWNet::NetTeamAdded(const QString & msg)
|
|
436 |
{
|
|
437 |
QStringList list = msg.split(MAGIC_CHAR, QString::SkipEmptyParts);
|
|
438 |
if (list.size() != 10)
|
|
439 |
return ;
|
|
440 |
SENDCFGSTRLOC("eaddteam");
|
|
441 |
if (list[0] == mynick)
|
|
442 |
{
|
|
443 |
emit LocalCFG(list[1]);
|
|
444 |
} else
|
|
445 |
{
|
|
446 |
SENDCFGSTRLOC("erdriven");
|
|
447 |
SENDCFGSTRLOC(QString("ename team %1").arg(list[1]));
|
|
448 |
for (int i = 0; i < 8; i++)
|
|
449 |
{
|
|
450 |
SENDCFGSTRLOC(QString("ename hh%1 ").arg(i) + list[i + 2]);
|
|
451 |
}
|
|
452 |
}
|
|
453 |
}
|
|
454 |
|
|
455 |
void HWNet::AddTeam(const HWTeam & team)
|
|
456 |
{
|
|
457 |
if (state != nsJoined)
|
|
458 |
{
|
|
459 |
return ;
|
|
460 |
}
|
|
461 |
RawSendNet(QString("PRIVMSG %1 :").arg(opnick) + MAGIC_CHAR "A" MAGIC_CHAR +
|
|
462 |
team.TeamName + MAGIC_CHAR + team.HHName[0] + MAGIC_CHAR + team.HHName[1] + MAGIC_CHAR +
|
|
463 |
team.HHName[2] + MAGIC_CHAR + team.HHName[3] + MAGIC_CHAR + team.HHName[4] + MAGIC_CHAR +
|
|
464 |
team.HHName[5] + MAGIC_CHAR + team.HHName[6] + MAGIC_CHAR + team.HHName[7]);
|
|
465 |
}
|
|
466 |
|
|
467 |
void HWNet::StartGame()
|
|
468 |
{
|
|
469 |
if ((opnick != mynick) || (state != nsJoined))
|
|
470 |
{
|
|
471 |
return ;
|
|
472 |
}
|
|
473 |
QStringList players;
|
|
474 |
for (int i = 0; i < teamsCount; i++)
|
|
475 |
{
|
|
476 |
if (!players.contains(teams[i].nick))
|
|
477 |
{
|
|
478 |
players.append(teams[i].nick);
|
|
479 |
}
|
|
480 |
}
|
|
481 |
playerscnt = players.size();
|
|
482 |
configasks = 0;
|
|
483 |
|
|
484 |
seed = QUuid::createUuid().toString();
|
|
485 |
QString msg = QString(MAGIC_CHAR"Start!");
|
|
486 |
RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, msg));
|
|
487 |
hwp_chanmsg(mynick, msg);
|
|
488 |
}
|
|
489 |
|
|
490 |
void HWNet::RunGame()
|
|
491 |
{
|
|
492 |
HWGame * game = new HWGame(config, 0);
|
|
493 |
connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &)));
|
|
494 |
connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
|
|
495 |
connect(this, SIGNAL(LocalCFG(const QString &)), game, SLOT(LocalCFG(const QString &)));
|
|
496 |
game->StartNet();
|
|
497 |
}
|