- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
authorunc0rr
Wed, 08 Oct 2008 13:40:29 +0000
changeset 1313 f4c54e9e1b8c
parent 1312 0078e4f72d40
child 1314 e56b178d6d62
- Introduce a bit of state miachine in client code (should be more robust and verbosive now) - Prepare for creating and joining rooms code
QTfrontend/hwform.cpp
QTfrontend/newnetclient.cpp
QTfrontend/newnetclient.h
QTfrontend/pages.cpp
QTfrontend/pages.h
--- a/QTfrontend/hwform.cpp	Wed Oct 08 10:58:21 2008 +0000
+++ b/QTfrontend/hwform.cpp	Wed Oct 08 13:40:29 2008 +0000
@@ -412,6 +412,14 @@
 	connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
 	connect(hwnet, SIGNAL(AddNetTeam(const HWTeam&)), this, SLOT(AddNetTeam(const HWTeam&)));
 
+	connect(hwnet, SIGNAL(roomsList(const QStringList&)),
+		ui.pageRoomsList, SLOT(setRoomsList(const QStringList&)));
+	
+	connect(ui.pageRoomsList, SIGNAL(askForCreateRoom(const QString &)),
+		hwnet, SLOT(CreateRoom(const QString&)));
+	connect(ui.pageRoomsList, SIGNAL(askForJoinRoom(const QString &)),
+		hwnet, SLOT(JoinRoom(const QString&)));
+
 	connect(hwnet, SIGNAL(chatStringFromNet(const QStringList&)),
 		ui.pageNetGame->pChatWidget, SLOT(onChatStringFromNet(const QStringList&)));
 	connect(ui.pageNetGame->pChatWidget, SIGNAL(chatLine(const QString&)),
--- a/QTfrontend/newnetclient.cpp	Wed Oct 08 10:58:21 2008 +0000
+++ b/QTfrontend/newnetclient.cpp	Wed Oct 08 13:40:29 2008 +0000
@@ -34,7 +34,9 @@
   m_pGameCFGWidget(pGameCFGWidget),
   m_pTeamSelWidget(pTeamSelWidget),
   isChief(false),
-  m_game_connected(false)
+  m_game_connected(false),
+  loginStep(0),
+  netClientState(0)
 {
   connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
   connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect()));
@@ -57,12 +59,30 @@
 
 void HWNewNet::CreateRoom(const QString & room)
 {
+	if(netClientState != 2)
+	{
+		qDebug("Illegal try to create room!");
+		return;
+	}
+	
 	RawSendNet(QString("CREATE%1%2").arg(delimeter).arg(room));
+	isChief = true;
 }
 
 void HWNewNet::JoinRoom(const QString & room)
 {
+	if(netClientState != 2)
+	{
+		qDebug("Illegal try to join room!");
+		return;
+	}
+	
+	loginStep++;
+
 	RawSendNet(QString("JOIN%1%2").arg(delimeter).arg(room));
+	m_pGameCFGWidget->setEnabled(false);
+	m_pTeamSelWidget->setNonInteractive();
+	isChief = false;
 }
 
 void HWNewNet::AddTeam(const HWTeam & team)
@@ -131,8 +151,6 @@
 {
 	RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
 	RawSendNet(QString("PROTO%1%2").arg(delimeter).arg(*cProtoVer));
-	RawSendNet(QString("CREATE%1%2").arg(delimeter).arg("myroom"));
-	RawSendNet(QString("JOIN%1%2").arg(delimeter).arg("myroom"));
 }
 
 void HWNewNet::OnDisconnect()
@@ -172,7 +190,15 @@
 	}
 
 	if ((lst[0] == "NICK") || (lst[0] == "PROTO"))
+	{
+		loginStep++;
+		if (loginStep == 2)
+		{
+			netClientState = 2;
+			RawSendNet(QString("LIST"));
+		}
 		return ;
+	}
 
 	if (lst[0] == "ERROR") {
 		if (lst.size() == 2)
@@ -191,11 +217,19 @@
 	}
 
 	if (lst[0] == "CONNECTED") {
-		m_game_connected=true;
+		netClientState = 1;
+		m_game_connected = true;
 		emit Connected();
 		return;
 	}
 
+	if (lst[0] == "ROOMS") {
+		QStringList tmp = lst;
+		tmp.removeFirst();
+		emit roomsList(tmp);
+		return;
+	}
+
 	if (lst[0] == "CHAT_STRING") {
 		if(lst.size() < 3)
 		{
@@ -230,12 +264,6 @@
     return;
   }
 
-/*	if(lst[0] == "SLAVE") { // клиент знает CREATE он делал или JOIN
-		m_pGameCFGWidget->setEnabled(false);
-		m_pTeamSelWidget->setNonInteractive();
-		return;
-	}*/
-
 	if(lst[0]=="JOINED") {
 		if(lst.size() < 2)
 		{
@@ -245,7 +273,12 @@
 		
 		for(int i = 1; i < lst.size(); ++i)
 		{
-			if (lst[i] == mynick) emit EnteredGame();
+			if (lst[i] == mynick)
+			{
+				netClientState = 3;
+				emit EnteredGame();
+				ConfigAsked();
+			}
 			emit nickAdded(lst[i]);
 		}
 		return;
@@ -261,12 +294,6 @@
 		return;
 	}
 
-	if (lst[0] == "CONFIGASKED") { // клиент знает CREATE он делал или JOIN
-		isChief=true;
-		ConfigAsked();
-		return;
-	}
-
   if (lst[0] == "RUNGAME") {
     RunGame();
     return;
--- a/QTfrontend/newnetclient.h	Wed Oct 08 10:58:21 2008 +0000
+++ b/QTfrontend/newnetclient.h	Wed Oct 08 13:40:29 2008 +0000
@@ -41,8 +41,6 @@
   HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget);
   void Connect(const QString & hostName, quint16 port, const QString & nick);
   void Disconnect();
-  void JoinRoom(const QString & room);
-  void CreateRoom(const QString & room);
   void Ready();
 
  private:
@@ -86,6 +84,9 @@
   void RawSendNet(const QByteArray & buf);
   void ParseCmd(const QStringList & lst);
 
+  int loginStep;
+  int netClientState;
+
  signals:
   void AskForRunGame();
   void Connected();
@@ -106,6 +107,7 @@
   void teamColorChanged(const HWTeam&);
   void chatStringFromNet(const QStringList&);
   void ammoChanged(const QString& name, const QString& ammo);
+  void roomsList(const QStringList&);
 
  public slots:
   void chatLineToNet(const QString& str);
@@ -121,6 +123,8 @@
   void onHedgehogsNumChanged(const HWTeam& team);
   void onTeamColorChanged(const HWTeam& team);
   void onWeaponsNameChanged(const QString& name, const QString& ammo);
+  void JoinRoom(const QString & room);
+  void CreateRoom(const QString & room);
 
  private slots:
   void ClientRead();
--- a/QTfrontend/pages.cpp	Wed Oct 08 10:58:21 2008 +0000
+++ b/QTfrontend/pages.cpp	Wed Oct 08 13:40:29 2008 +0000
@@ -632,3 +632,10 @@
 	BtnJoin = addButton(tr("Join"), pageLayout, 1, 1);
 	BtnRefresh = addButton(tr("Refresh"), pageLayout, 2, 1);
 }
+
+void PageRoomsList::setRoomsList(const QStringList & list)
+{
+	roomsList->clear();
+	roomsList->addItems(list);
+	roomsList->sortItems();
+}
--- a/QTfrontend/pages.h	Wed Oct 08 10:58:21 2008 +0000
+++ b/QTfrontend/pages.h	Wed Oct 08 13:40:29 2008 +0000
@@ -348,6 +348,13 @@
 	QPushButton * BtnCreate;
 	QPushButton * BtnJoin;
 	QPushButton * BtnRefresh;
+	
+public slots:
+	void setRoomsList(const QStringList & list);
+	
+signals:
+	void askForCreateRoom(const QString &);
+	void askForJoinRoom(const QString &);
 };
 
 #endif // PAGES_H