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