QTfrontend/netclient.cpp
changeset 31 99888245a4e8
parent 29 9311259d5242
child 32 78bff13b11c0
--- a/QTfrontend/netclient.cpp	Tue Dec 27 10:19:21 2005 +0000
+++ b/QTfrontend/netclient.cpp	Tue Dec 27 10:20:55 2005 +0000
@@ -33,16 +33,25 @@
 
 #include <QMessageBox>
 #include "netclient.h"
+#include "game.h"
 
-HWNet::HWNet()
+#include <QtDebug>
+
+#define chkp qDebug() << "hw chkp in " << __FILE__ << ":" << __LINE__
+
+HWNet::HWNet(int Resolution, bool Fullscreen)
 	: QObject()
 {
+	gameResolution = Resolution;
+	gameFullscreen = Fullscreen;
 	state = nsDisconnected;
-	IRCmsg_cmd_param = new QRegExp("^[A-Z]+ :.+$");
+	IRCmsg_cmd_text = new QRegExp("^[A-Z]+ :.+$");
 	IRCmsg_number_param = new QRegExp("^:\\S+ [0-9]{3} .+$");
-	IRCmsg_who_cmd_param = new QRegExp("^:\\S+ [A-Z]+ \\S+");
-	IRCmsg_who_cmd_param_text = new QRegExp("^:\\S+ [A-Z]+ \\S+ :.+$");
+	IRCmsg_who_cmd_target = new QRegExp("^:\\S+ [A-Z]+ \\S+$"); // last \\S should mean 'the 1st char is not ":"'
+	IRCmsg_who_cmd_text = new QRegExp("^:\\S+ [A-Z]+ :.+$");
+	IRCmsg_who_cmd_target_text = new QRegExp("^:\\S+ [A-Z]+ \\S+ :.+$");
 	isOp = false;
+	teamsCount = 0;
 
 	connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
 	connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect()));
@@ -92,8 +101,8 @@
 void HWNet::OnConnect()
 {
 	state = nsConnected;
-	SendNet(QString("USER hwgame 1 2 Hedgewars game"));
-	SendNet(QString("NICK %1").arg(mynick));
+	RawSendNet(QString("USER hwgame 1 2 Hedgewars game"));
+	RawSendNet(QString("NICK %1").arg(mynick));
 }
 
 void HWNet::OnDisconnect()
@@ -103,8 +112,13 @@
 
 void HWNet::Perform()
 {
-//	SendNet(QString("LIST"));
-	SendNet(QString("JOIN #alex"));
+
+}
+
+void HWNet::JoinGame(const QString & game)
+{
+	state = nsJoining;
+	RawSendNet(QString("JOIN %1").arg(game));
 }
 
 void HWNet::Disconnect()
@@ -124,50 +138,69 @@
 		default:
 		{
 			state = nsQuitting;
-			SendNet(QString("QUIT :oops"));
+			RawSendNet(QString("QUIT :oops"));
 		}
 	}
 }
 
-void HWNet::SendNet(const QString & str)
+void HWNet::RawSendNet(const QString & str)
 {
-	SendNet(str.toLatin1());
+	RawSendNet(str.toLatin1());
 }
 
-void HWNet::SendNet(const QByteArray & buf)
+void HWNet::RawSendNet(const QByteArray & buf)
 {
 	if (buf.size() > 510) return;
 	NetSocket.write(buf);
 	NetSocket.write("\x0d\x0a", 2);
 }
 
+void HWNet::SendNet(const QByteArray & buf)
+{
+	if ((state == nsGaming) || (state == nsStarting))
+	{
+		QString msg = QString(buf.toBase64());
+		if (msg == "AUM=")
+		{
+			ConfigAsked();
+		} else
+		{
+			RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, msg));
+		}
+	}
+}
+
 void HWNet::ParseLine(const QString & msg)
 {
 	//QMessageBox::information(0, "", msg);
-	if (IRCmsg_cmd_param->exactMatch(msg))
+	if (IRCmsg_cmd_text->exactMatch(msg))
 	{
-		msgcmd_paramHandler(msg);
+		msgcmd_textHandler(msg);
 	} else
 	if (IRCmsg_number_param->exactMatch(msg))
 	{
 		msgnumber_paramHandler(msg);
 	} else
-	if (IRCmsg_who_cmd_param->exactMatch(msg))
+	if (IRCmsg_who_cmd_text->exactMatch(msg))
 	{
-		msgwho_cmd_paramHandler(msg);
+		msgwho_cmd_textHandler(msg);
 	} else
-	if (IRCmsg_who_cmd_param_text->exactMatch(msg))
+	if (IRCmsg_who_cmd_target->exactMatch(msg))
 	{
-		msgwho_cmd_param_textHandler(msg);
+		msgwho_cmd_targetHandler(msg);
+	} else
+	if (IRCmsg_who_cmd_target_text->exactMatch(msg))
+	{
+		msgwho_cmd_target_textHandler(msg);
 	}
 }
 
-void HWNet::msgcmd_paramHandler(const QString & msg)
+void HWNet::msgcmd_textHandler(const QString & msg)
 {
 	QStringList list = msg.split(" :");
 	if (list[0] == "PING")
 	{
-		SendNet(QString("PONG %1").arg(list[1]));
+		RawSendNet(QString("PONG %1").arg(list[1]));
 	}
 }
 
@@ -209,7 +242,6 @@
 			{
 				opnick = "";
 			}
-			SendNet(QString("PRIVMSG #alex :%1 ops here").arg(opCount));
 			opCount = 0;
 			break;
 		}
@@ -221,7 +253,7 @@
 	}
 }
 
-void HWNet::msgwho_cmd_paramHandler(const QString & msg)
+void HWNet::msgwho_cmd_targetHandler(const QString & msg)
 {
 	QStringList list = msg.split(" ");
 	QString who = list[0].mid(1).split("!")[0];
@@ -234,7 +266,24 @@
 	}
 }
 
-void HWNet::msgwho_cmd_param_textHandler(const QString & msg)
+void HWNet::msgwho_cmd_textHandler(const QString & msg)
+{
+	int pos = msg.indexOf(" :");
+	QString text = msg.mid(pos + 2);
+	QStringList list = msg.mid(0, pos).split(" ");
+	QString who = list[0].mid(1).split("!")[0];
+	if (list[1] == "JOIN")
+	{
+		if (who == mynick)
+		{
+			channel = text;
+			state = nsJoined;
+			emit EnteredGame();
+			RawSendNet(QString("PRIVMSG %1 :Hello!").arg(channel));
+		}
+	}
+}
+void HWNet::msgwho_cmd_target_textHandler(const QString & msg)
 {
 	int pos = msg.indexOf(" :");
 	QString text = msg.mid(pos + 2);
@@ -242,6 +291,162 @@
 	QString who = list[0].mid(1).split("!")[0];
 	if (list[1] == "PRIVMSG")
 	{
-		SendNet(QString("PRIVMSG #alex :%1 said \"%2\" to %3").arg(who, text, list[2]));
+		if (list[2] == opnick)
+		{
+			hwp_opmsg(who, text);
+		} else
+		if (list[2] == channel)
+		{
+			hwp_chanmsg(who, text);
+		}
+	}
+}
+
+void HWNet::hwp_opmsg(const QString & who, const QString & msg)
+{
+	if (state != nsJoined)
+		return ;
+	if (!msg.startsWith(MAGIC_CHAR))
+		return ;
+	QStringList list = msg.split(MAGIC_CHAR, QString::SkipEmptyParts);
+	if (list[0] == "A")
+	{
+		list.removeFirst();
+		if (list.size() != 9)
+			return ;
+		if (teamsCount < 5)
+		{
+			teams[teamsCount].nick = who;
+			teams[teamsCount].hhs = list;
+			teamsCount++;
+			QString teamnames;
+			for(int i = 0; i < teamsCount; i++)
+			{
+				teamnames += MAGIC_CHAR;
+				teamnames += teams[i].hhs[0];
+			}
+			RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR"Teams%2").arg(channel, teamnames));
+		}
+	}
+}
+
+void HWNet::ConfigAsked()
+{
+	configasks++;
+	if (configasks == playerscnt)
+	{
+		quint32 color = 65535;
+		for (int i = 0; i < teamsCount; i++)
+		{
+chkp;			SENDCFGSTRNET("eaddteam");
+			QString msg;
+			msg = MAGIC_CHAR "T" MAGIC_CHAR + teams[i].nick + MAGIC_CHAR + teams[i].hhs.join(MAGIC_CHAR);
+			RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, msg));
+			hwp_chanmsg(mynick, msg);
+			SENDCFGSTRNET(QString("ecolor %1").arg(color));
+			SENDCFGSTRNET("eadd hh0 0");
+			SENDCFGSTRNET("eadd hh1 0");
+			SENDCFGSTRNET("eadd hh2 0");
+			color <<= 8;
+		}
+chkp;		SENDCFGSTRNET("!");
 	}
 }
+
+void HWNet::hwp_chanmsg(const QString & who, const QString & msg)
+{
+	if ((state < nsJoined) || (state > nsGaming))
+	{
+		return ;
+	}
+	if ((state == nsJoined) && (msg.startsWith(MAGIC_CHAR"Start!"MAGIC_CHAR)) && (who == opnick))
+	{
+		state = nsStarting;
+		RunGame(msg.mid(7));
+		return ;
+	}
+	if ((state == nsStarting) && (msg == MAGIC_CHAR MAGIC_CHAR "AUM="))
+	{
+		if (mynick == opnick) ConfigAsked();
+		return ;
+	}
+	if ((state == nsStarting) && (msg.startsWith(MAGIC_CHAR"T"MAGIC_CHAR)))
+	{
+		NetTeamAdded(msg.mid(3));
+	}
+	if (state != nsGaming)
+	{
+		return;
+	}
+	if (msg.startsWith(MAGIC_CHAR MAGIC_CHAR)) // HWP message
+	{
+		emit FromNet(QByteArray::fromBase64(msg.mid(2).toLocal8Bit()));
+	} else // smth other
+	{
+
+	}
+}
+
+void HWNet::NetTeamAdded(const QString & msg)
+{
+	QStringList list = msg.split(MAGIC_CHAR, QString::SkipEmptyParts);
+	if (list.size() != 10)
+		return ;
+	if (list[0] == mynick)
+	{
+chkp;		emit LocalCFG(list[1]);
+	} else
+	{
+chkp;		SENDCFGSTRLOC("erdriven");
+		SENDCFGSTRLOC(QString("ename team %1").arg(list[2]));
+		for (int i = 0; i < 8; i++)
+		{
+			SENDCFGSTRLOC(QString("ename hh%1").arg(i) + list[i + 3]);
+		}
+chkp;	}
+}
+
+void HWNet::AddTeam(const HWTeam & team)
+{
+	if (state != nsJoined)
+	{
+		return ;
+	}
+	RawSendNet(QString("PRIVMSG %1 :").arg(opnick) + MAGIC_CHAR "A" MAGIC_CHAR +
+			team.TeamName + MAGIC_CHAR + team.HHName[0] + MAGIC_CHAR + team.HHName[1] + MAGIC_CHAR +
+			team.HHName[2] + MAGIC_CHAR + team.HHName[3] + MAGIC_CHAR + team.HHName[4] + MAGIC_CHAR +
+			team.HHName[5] + MAGIC_CHAR + team.HHName[6] + MAGIC_CHAR + team.HHName[7]);
+}
+
+void HWNet::StartGame()
+{
+	if ((opnick != mynick) || (state != nsJoined))
+	{
+		return ;
+	}
+	QStringList players;
+	for (int i = 0; i < teamsCount; i++)
+	{
+		if (!players.contains(teams[i].nick))
+		{
+			players.append(teams[i].nick);
+		}
+	}
+	playerscnt = players.size();
+	configasks = 0;
+
+	QString seed;
+	seedgen.GenRNDStr(seed, 10);
+	QString msg = QString(MAGIC_CHAR"Start!"MAGIC_CHAR"%1").arg(seed);
+	RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, msg));
+	hwp_chanmsg(mynick, msg);
+}
+
+void HWNet::RunGame(const QString & seed)
+{
+	HWGame * game = new HWGame(gameResolution, gameFullscreen);
+	connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &)));
+	connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
+	connect(this, SIGNAL(LocalCFG(const QString &)), game, SLOT(LocalCFG(const QString &)));
+	game->StartNet(seed);
+}