- Simplify net game objects relationship
authorunc0rr
Fri, 14 Dec 2007 16:13:35 +0000
changeset 660 7c155e849602
parent 659 5eafb20566f1
child 661 0523b15353a2
- Simplify net game objects relationship - Fix statistics page bug caused by recent protocol changes
QTfrontend/game.cpp
QTfrontend/hwform.cpp
QTfrontend/hwform.h
QTfrontend/netwwwserver.cpp
QTfrontend/newnetclient.cpp
QTfrontend/newnetclient.h
--- a/QTfrontend/game.cpp	Fri Dec 14 15:36:00 2007 +0000
+++ b/QTfrontend/game.cpp	Fri Dec 14 16:13:35 2007 +0000
@@ -183,7 +183,8 @@
 			break;
 		}
 		case 'E': {
-			emit ErrorMessage(QString().append(msg.mid(2)).left(msg.size() - 6));
+			int size = msg.size();
+			emit ErrorMessage(QString().append(msg.mid(2)).left(size - 4));
 			return;
 		}
 		case 'K': {
@@ -206,7 +207,8 @@
 			break;
 		}
 		case 'i': {
-			emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3)));
+			int size = msg.size();
+			emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3).left(size - 5)));
 			break;
 		}
 		case 'Q': {
--- a/QTfrontend/hwform.cpp	Fri Dec 14 15:36:00 2007 +0000
+++ b/QTfrontend/hwform.cpp	Fri Dec 14 16:13:35 2007 +0000
@@ -365,7 +365,7 @@
 	ui.pageNetGame->pChatWidget->clear();
 	hwnet = new HWNewNet(config, ui.pageNetGame->pGameCFG, ui.pageNetGame->pNetTeamsWidget);
 
-	connect(hwnet, SIGNAL(GameStateChanged(GameState)), this, SLOT(NetGameStateChanged(GameState)));
+	connect(hwnet, SIGNAL(AskForRunGame()), this, SLOT(CreateNetGame()));
 	connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
 	connect(hwnet, SIGNAL(AddNetTeam(const HWTeam&)), this, SLOT(AddNetTeam(const HWTeam&)));
 
@@ -445,8 +445,11 @@
     hwnet=0;
   }
   if(pnetserver) {
-    pRegisterServer->unregister();
-    pRegisterServer = 0;
+    if (pRegisterServer)
+    {
+      pRegisterServer->unregister();
+      pRegisterServer = 0;
+    }
 
     pnetserver->StopServer();
     delete pnetserver;
@@ -490,12 +493,6 @@
 	game->StartLocal();
 }
 
-void HWForm::NetGameStateChanged(GameState __attribute__((unused)) gameState)
-{
-  ui.pageNetGame->BtnGo->setText(QPushButton::tr("Go!"));
-  ui.pageNetGame->BtnGo->setEnabled(true);
-}
-
 void HWForm::GameStateChanged(GameState gameState)
 {
 	switch(gameState) {
@@ -510,6 +507,11 @@
 		default: ;
 	}
 
+	if (hwnet)
+	{
+		ui.pageNetGame->BtnGo->setText(QPushButton::tr("Go!"));
+		ui.pageNetGame->BtnGo->setEnabled(true);
+	}
 }
 
 void HWForm::AddStatText(const QString & msg)
@@ -589,3 +591,13 @@
 
 	game->StartTraining();
 }
+
+void HWForm::CreateNetGame()
+{
+	CreateGame(ui.pageNetGame->pGameCFG, ui.pageNetGame->pNetTeamsWidget);
+
+	connect(game, SIGNAL(SendNet(const QByteArray &)), hwnet, SLOT(SendNet(const QByteArray &)));
+	connect(hwnet, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
+
+	game->StartNet();
+}
--- a/QTfrontend/hwform.h	Fri Dec 14 15:36:00 2007 +0000
+++ b/QTfrontend/hwform.h	Fri Dec 14 16:13:35 2007 +0000
@@ -74,13 +74,15 @@
 	void NetStartGame();
 	void AddNetTeam(const HWTeam& team);
 	void StartMPGame();
-	void NetGameStateChanged(GameState gameState);
 	void GameStateChanged(GameState gameState);
 	void GameStats(char type, const QString & info);
 	void ForcedDisconnect();
 	void ShowErrorMessage(const QString &);
 	void GetRecord(bool isDemo, const QByteArray & record);
 
+public slots:
+	void CreateNetGame();
+
 private:
 	void _NetConnect(const QString & hostName, quint16 port, const QString & nick);
 	void UpdateTeamsLists(const QStringList* editable_teams=0);
--- a/QTfrontend/netwwwserver.cpp	Fri Dec 14 15:36:00 2007 +0000
+++ b/QTfrontend/netwwwserver.cpp	Fri Dec 14 16:13:35 2007 +0000
@@ -86,6 +86,7 @@
 
 void HWNetWwwServer::unregister()
 {
+	qDebug("delete server");
 	QString request = QString("id=%1&key=%2")
 			.arg(servid)
 			.arg(servkey);
--- a/QTfrontend/newnetclient.cpp	Fri Dec 14 15:36:00 2007 +0000
+++ b/QTfrontend/newnetclient.cpp	Fri Dec 14 16:13:35 2007 +0000
@@ -338,19 +338,7 @@
 
 void HWNewNet::RunGame()
 {
-  HWGame* game = new HWGame(config, m_pGameCFGWidget, m_pTeamSelWidget);
-  connect(game, SIGNAL(GameStateChanged(GameState)), this, SIGNAL(GameStateChanged(GameState)));
-  connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &)));
-  connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
-  connect(game, SIGNAL(ErrorMessage(const QString &)), this, SLOT(ShowErrorMessage(const QString &)), Qt::QueuedConnection);
-  game->StartNet();
-}
-
-void HWNewNet::ShowErrorMessage(const QString & msg)
-{
-	QMessageBox::warning(0,
-			"Hedgewars",
-			msg);
+  emit AskForRunGame();
 }
 
 void HWNewNet::onHedgehogsNumChanged(const HWTeam& team)
--- a/QTfrontend/newnetclient.h	Fri Dec 14 15:36:00 2007 +0000
+++ b/QTfrontend/newnetclient.h	Fri Dec 14 16:13:35 2007 +0000
@@ -84,6 +84,7 @@
   void ParseLine(const QByteArray & line);
 
  signals:
+  void AskForRunGame();
   void Connected();
   void Disconnected();
   void EnteredGame();
@@ -91,7 +92,6 @@
   void nickRemoved(const QString& nick);
   void FromNet(const QByteArray & buf);
   void AddNetTeam(const HWTeam&);
-  void GameStateChanged(GameState gameState);
 
   void seedChanged(const QString & seed);
   void mapChanged(const QString & map);
@@ -123,7 +123,6 @@
   void OnDisconnect();
   //void Perform();
   void displayError(QAbstractSocket::SocketError socketError);
-  void ShowErrorMessage(const QString &);
   //void FlushNetBuf();
 };