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