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