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