'Proto' class to make things easier
authorunc0rr
Tue, 14 Nov 2006 20:39:30 +0000
changeset 239 f9adf3c73bed
parent 238 85949b945906
child 240 c7f0a4f7a54a
'Proto' class to make things easier
QTfrontend/CMakeLists.txt
QTfrontend/game.cpp
QTfrontend/game.h
QTfrontend/hedgewars.pro
QTfrontend/netclient.cpp
QTfrontend/proto.cpp
QTfrontend/proto.h
QTfrontend/team.cpp
QTfrontend/team.h
--- a/QTfrontend/CMakeLists.txt	Mon Nov 13 20:13:42 2006 +0000
+++ b/QTfrontend/CMakeLists.txt	Tue Nov 14 20:39:30 2006 +0000
@@ -30,7 +30,8 @@
 	hwmap.cpp
 	mapContainer.cpp
 	tcpBase.cpp
-	about.cpp)
+	about.cpp
+	proto.cpp)
 
 if (WIN32)
 	set(hwfr_src ${hwfr_src} res/hedgewars.rc)
@@ -56,7 +57,8 @@
 	mapContainer.h
 	tcpBase.h
 	about.h
-	KB.h)
+	KB.h
+	proto.h)
 
 
 set(hwfr_rez
--- a/QTfrontend/game.cpp	Mon Nov 13 20:13:42 2006 +0000
+++ b/QTfrontend/game.cpp	Tue Nov 14 20:39:30 2006 +0000
@@ -30,6 +30,7 @@
 #include "gameuiconfig.h"
 #include "gamecfgwidget.h"
 #include "KB.h"
+#include "proto.h"
 
 HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg) :
   TCPBase(true)
@@ -60,12 +61,14 @@
 
 	for (int i = 0; i < TeamCount; i++)
 	{
-		SendIPC("eaddteam");
-		LocalCFG(teams[i]);
-		QColor clr=m_teamsParams[teams[i]].teamColor;
-		SendIPC(QString("ecolor %1").arg(clr.rgb()&0xFFFFFF).toAscii());
-		for (int t = 0; t < m_teamsParams[teams[i]].numHedgehogs; t++)
-			SendIPC(QString("eadd hh%1 0").arg(t).toAscii());
+		HWTeam team(teams[i]);
+		team.LoadFromFile();
+
+		QColor clr = m_teamsParams[teams[i]].teamColor;
+		QByteArray buf;
+		QStringList sl = team.TeamGameConfig(clr.rgb()&0xFFFFFF, m_teamsParams[teams[i]].numHedgehogs);
+		HWProto::addStringListToBuffer(buf, sl);
+		RawSendIPC(buf);
 	}
 }
 
@@ -75,20 +78,15 @@
 	SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
 	SendIPC("TL");
 	SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii());
-	SendIPC("eaddteam");
-	LocalCFG(0);
-	SendIPC("ecolor 65535");
-	SendIPC("eadd hh0 0");
-	SendIPC("eadd hh1 0");
-	SendIPC("eadd hh2 0");
-	SendIPC("eadd hh3 0");
-	SendIPC("eaddteam");
-	LocalCFG(2);
-	SendIPC("ecolor 16776960");
-	SendIPC("eadd hh0 5");
-	SendIPC("eadd hh1 4");
-	SendIPC("eadd hh2 4");
-	SendIPC("eadd hh3 3");
+
+	QByteArray teamscfg;
+	HWTeam team1(0);
+	team1.difficulty = 0;
+	HWProto::addStringListToBuffer(teamscfg, team1.TeamGameConfig(65535, 4));
+
+	HWTeam team2(2);
+	team2.difficulty = 4;
+	RawSendIPC(HWProto::addStringListToBuffer(teamscfg, team2.TeamGameConfig(16776960, 4)));
 }
 
 void HWGame::ParseMessage(const QByteArray & msg)
@@ -261,7 +259,7 @@
 {
 	gameType = gtLocal;
 	if (TeamCount < 2) return;
-	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
+	seed = gamecfg->getCurrentSeed();
 	demo = new QByteArray;
 	Start();
 }
@@ -269,7 +267,7 @@
 void HWGame::StartQuick()
 {
 	gameType = gtQLocal;
-	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
+	seed = gamecfg->getCurrentSeed();
 	demo = new QByteArray;
 	Start();
 }
@@ -277,21 +275,8 @@
 
 void HWGame::LocalCFG(const QString & teamname)
 {
+	QByteArray teamcfg;
 	HWTeam team(teamname);
-	if (!team.LoadFromFile()) {
-		QMessageBox::critical(0,
-				"Error",
-				QString("Cannot load team config ""%1""").arg(teamname),
-				QMessageBox::Ok,
-				QMessageBox::NoButton,
-				QMessageBox::NoButton);
-		return;
-	}
-	RawSendIPC(team.IPCTeamInfo());
+	RawSendIPC(HWProto::addStringListToBuffer(teamcfg, team.TeamGameConfig(16776960, 4)));
 }
 
-void HWGame::LocalCFG(quint8 num)
-{
-	HWTeam team(num);
-	RawSendIPC(team.IPCTeamInfo());
-}
--- a/QTfrontend/game.h	Mon Nov 13 20:13:42 2006 +0000
+++ b/QTfrontend/game.h	Tue Nov 14 20:39:30 2006 +0000
@@ -53,7 +53,6 @@
 public slots:
 	void FromNet(const QByteArray & msg);
 	void LocalCFG(const QString & teamname);
-	void LocalCFG(quint8 num);
 
 private:
     enum GameType {
--- a/QTfrontend/hedgewars.pro	Mon Nov 13 20:13:42 2006 +0000
+++ b/QTfrontend/hedgewars.pro	Tue Nov 14 20:39:30 2006 +0000
@@ -31,7 +31,8 @@
            mapContainer.h \
            tcpBase.h \
            about.h \
-           KB.h
+           KB.h \
+           proto.h
            
 SOURCES += game.cpp \
            main.cpp \
@@ -51,7 +52,8 @@
            hwmap.cpp \
            mapContainer.cpp \
            tcpBase.cpp \
-           about.cpp
+           about.cpp \
+           proto.cpp
 
 TRANSLATIONS += translations/hedgewars_ru.ts
 
--- a/QTfrontend/netclient.cpp	Mon Nov 13 20:13:42 2006 +0000
+++ b/QTfrontend/netclient.cpp	Tue Nov 14 20:39:30 2006 +0000
@@ -21,6 +21,7 @@
 #include "netclient.h"
 #include "game.h"
 #include "gameuiconfig.h"
+#include "proto.h"
 
 HWNet::HWNet(GameUIConfig * config)
 	: QObject()
@@ -341,18 +342,11 @@
 	if (configasks == playerscnt)
 	{
 		quint32 color = 65535;
-			#define ADD(a) { \
-							QByteArray strmsg; \
-							strmsg.append(a); \
-							quint8 sz = strmsg.size(); \
-							cache.append(QByteArray((char *)&sz, 1)); \
-							cache.append(strmsg); \
-							}
 		{
 			QByteArray cache;
-			ADD("eseed " + seed);
-			ADD("e$gmflags 0");
-			ADD(QString("etheme %1").arg(config->GetRandomTheme()));
+			HWProto::addStringToBuffer(cache, "eseed " + seed);
+			HWProto::addStringToBuffer(cache, "e$gmflags 0");
+			HWProto::addStringToBuffer(cache, QString("etheme %1").arg(config->GetRandomTheme()));
 			QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(cache.toBase64());
 			RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, _msg));
 			hwp_chanmsg(mynick, _msg);
@@ -364,13 +358,12 @@
 			RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, msg));
 			hwp_chanmsg(mynick, msg);
 			QByteArray cache;
-			ADD(QString("ecolor %1").arg(color));
-			ADD("eadd hh0 0");
-			ADD("eadd hh1 0");
-			ADD("eadd hh2 0");
-			ADD("eadd hh3 0");
-			ADD("eadd hh4 0");
-			#undef ADD
+			HWProto::addStringToBuffer(cache, QString("ecolor %1").arg(color));
+			HWProto::addStringToBuffer(cache, "eadd hh0 0");
+			HWProto::addStringToBuffer(cache, "eadd hh1 0");
+			HWProto::addStringToBuffer(cache, "eadd hh2 0");
+			HWProto::addStringToBuffer(cache, "eadd hh3 0");
+			HWProto::addStringToBuffer(cache, "eadd hh4 0");
 			QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(cache.toBase64());
 			RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, _msg));
 			hwp_chanmsg(mynick, _msg);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/proto.cpp	Tue Nov 14 20:39:30 2006 +0000
@@ -0,0 +1,40 @@
+/*
+ * Hedgewars, a worms-like game
+ * Copyright (c) 2006 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include "proto.h"
+
+HWProto::HWProto()
+{
+
+}
+
+QByteArray & HWProto::addStringToBuffer(QByteArray & buf, const QString & string)
+{
+	QByteArray strmsg = string.toUtf8();
+	quint8 sz = strmsg.size();
+	buf.append(QByteArray((char *)&sz, 1));
+	buf.append(strmsg);
+	return buf;
+}
+
+QByteArray & HWProto::addStringListToBuffer(QByteArray & buf, const QStringList & strList)
+{
+	for (int i = 0; i < strList.size(); i++)
+		addStringToBuffer(buf, strList[i]);
+	return buf;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/proto.h	Tue Nov 14 20:39:30 2006 +0000
@@ -0,0 +1,37 @@
+/*
+ * Hedgewars, a worms-like game
+ * Copyright (c) 2006 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#ifndef _PROTO_H
+#define _PROTO_H
+
+#include <QByteArray>
+#include <QString>
+#include <QStringList>
+
+
+class HWProto : public QObject
+{
+	Q_OBJECT
+
+public:
+	HWProto();
+	static QByteArray & addStringToBuffer(QByteArray & buf, const QString & string);
+	static QByteArray & addStringListToBuffer(QByteArray & buf, const QStringList & strList);
+};
+
+#endif // _PROTO_H
--- a/QTfrontend/team.cpp	Mon Nov 13 20:13:42 2006 +0000
+++ b/QTfrontend/team.cpp	Tue Nov 14 20:39:30 2006 +0000
@@ -111,7 +111,7 @@
 					binds[i].strbind = str;
 					break;
 				}
-		} else 
+		} else
 		if (str.startsWith("difficulty "))
 		{
 		  str.remove(0, 11);
@@ -181,27 +181,24 @@
 	}
 }
 
-QByteArray HWTeam::IPCTeamInfo() const
+QStringList HWTeam::TeamGameConfig(quint32 color, int hedgehogs) const
 {
-	QByteArray buf;
-	#define ADD(a) { \
-					QByteArray strmsg = a.toUtf8(); \
-					quint8 sz = strmsg.size(); \
-					buf.append(QByteArray((char *)&sz, 1)); \
-					buf.append(strmsg); \
-					}
-
-	ADD(QString("ename team " + TeamName));
+	QStringList sl;
+	sl.push_back("eaddteam");
+	sl.push_back(QString("ecolor %1").arg(color));
+	sl.push_back("ename team " + TeamName);
 	for (int i = 0; i < 8; i++)
-		ADD(QString("ename hh%1 ").arg(i).append(HHName[i]));
-	ADD(QString("egrave " + Grave));
-	ADD(QString("efort " + Fort));
+		sl.push_back(QString("ename hh%1 ").arg(i).append(HHName[i]));
+	sl.push_back(QString("egrave " + Grave));
+	sl.push_back(QString("efort " + Fort));
 	for(int i = 0; i < BINDS_NUMBER; i++)
 	{
-		ADD(QString("ebind " + binds[i].strbind + " " + binds[i].action));
+		sl.push_back(QString("ebind " + binds[i].strbind + " " + binds[i].action));
 	}
-	#undef ADD
-	return buf;
+	for (int t = 0; t < hedgehogs; t++)
+		sl.push_back(QString("eadd hh%1 %2")
+				.arg(QString::number(t), QString::number(difficulty)));
+	return sl;
 }
 
 bool HWTeam::operator==(const HWTeam& t1) const {
--- a/QTfrontend/team.h	Mon Nov 13 20:13:42 2006 +0000
+++ b/QTfrontend/team.h	Tue Nov 14 20:39:30 2006 +0000
@@ -43,7 +43,7 @@
 		bool SaveToFile();
 		void SetToPage(HWForm * hwform);
 		void GetFromPage(HWForm * hwform);
-		QByteArray IPCTeamInfo() const;
+		QStringList HWTeam::TeamGameConfig(quint32 color, int hedgehogs) const;
 
 		bool operator==(const HWTeam& t1) const;
 		bool operator<(const HWTeam& t1) const;