Fix critical failure to cleanup teams list after rejoining game after a force-quit. Fixes bug 597
authorWuzzy <Wuzzy2@mail.ru>
Wed, 22 Aug 2018 15:00:04 +0200
changeset 13689 27e5e311c7a3
parent 13687 a8b2a5e7e9db
child 13691 b5a7f83fa607
Fix critical failure to cleanup teams list after rejoining game after a force-quit. Fixes bug #597
ChangeLog.txt
QTfrontend/hwform.cpp
QTfrontend/ui/page/pagenetgame.cpp
QTfrontend/ui/page/pagenetgame.h
QTfrontend/ui/widget/teamselect.cpp
QTfrontend/ui/widget/teamselect.h
--- a/ChangeLog.txt	Tue Aug 21 23:56:49 2018 +0200
+++ b/ChangeLog.txt	Wed Aug 22 15:00:04 2018 +0200
@@ -34,6 +34,7 @@
 Frontend:
  + Add setting to disable audio dampening when losing window focus
  * Fix rare crash when aborting video encoding in progress
+ * Fix critical failure to cleanup teams list after rejoining game under certain conditions
  * Controllers are detected again
  * Fix failure to shutdown game window properly after player got kicked
  * No longer allow having schemes with equal names (case-insensitive)
--- a/QTfrontend/hwform.cpp	Tue Aug 21 23:56:49 2018 +0200
+++ b/QTfrontend/hwform.cpp	Wed Aug 22 15:00:04 2018 +0200
@@ -1746,7 +1746,15 @@
             Music(ui.pageOptions->CBFrontendMusic->isChecked());
             if (wBackground) wBackground->startAnimation();
             GoToPage(ID_PAGE_GAMESTATS);
-            if (hwnet && (!game || !game->netSuspend)) hwnet->gameFinished(true);
+            if (hwnet)
+            {
+                if (!game || !game->netSuspend)
+                    hwnet->gameFinished(true);
+                // After a game, the local player might have pseudo-teams left
+                // when rejoining a previously left game. This makes sure the
+                // teams list is in a consistent state.
+                ui.pageNetGame->cleanupFakeNetTeams();
+            }
             if (game) game->netSuspend = false;
             break;
         }
--- a/QTfrontend/ui/page/pagenetgame.cpp	Tue Aug 21 23:56:49 2018 +0200
+++ b/QTfrontend/ui/page/pagenetgame.cpp	Wed Aug 22 15:00:04 2018 +0200
@@ -218,6 +218,10 @@
     chatWidget->displayWarning(message);
 }
 
+void PageNetGame::cleanupFakeNetTeams()
+{
+    pNetTeamsWidget->cleanupFakeNetTeams();
+}
 
 void PageNetGame::setReadyStatus(bool isReady)
 {
@@ -280,6 +284,7 @@
 
 void PageNetGame::setUser(const QString & nickname)
 {
+    pNetTeamsWidget->setUser(nickname);
     chatWidget->setUser(nickname);
 }
 
--- a/QTfrontend/ui/page/pagenetgame.h	Tue Aug 21 23:56:49 2018 +0200
+++ b/QTfrontend/ui/page/pagenetgame.h	Wed Aug 22 15:00:04 2018 +0200
@@ -40,6 +40,7 @@
         void displayError(const QString & message);
         void displayNotice(const QString & message);
         void displayWarning(const QString & message);
+        void cleanupFakeNetTeams();
 
         QPushButton *BtnGo;
         QPushButton *BtnMaster;
--- a/QTfrontend/ui/widget/teamselect.cpp	Tue Aug 21 23:56:49 2018 +0200
+++ b/QTfrontend/ui/widget/teamselect.cpp	Wed Aug 22 15:00:04 2018 +0200
@@ -74,6 +74,11 @@
     framePlaying->setInteractivity(interactive);
 }
 
+void TeamSelWidget::setUser(const QString& nickname)
+{
+    m_curUser = nickname;
+}
+
 void TeamSelWidget::hhNumChanged(const HWTeam& team)
 {
     QList<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
@@ -152,6 +157,37 @@
     emit setEnabledGameStart(curPlayingTeams.size()>1);
 }
 
+// Removes teams classified as net teams but which are owned by the local user.
+// Those teams don't make sense and might be leftovers from a finished game
+// after rejoining. See also: Bugzilla bug 597.
+void TeamSelWidget::cleanupFakeNetTeams()
+{
+    // m_curUser is not set for offline games. No cleanup is needed when offline.
+    if(m_curUser.isNull())
+        return;
+
+    QList<HWTeam>::iterator itPlay = curPlayingTeams.begin();
+    while(itPlay != curPlayingTeams.end())
+    {
+        if(itPlay->isNetTeam() && itPlay->owner() == m_curUser)
+        {
+            qDebug() << QString("cleanupFakeNetTeams: team '%1' removed").arg(itPlay->name());
+            QObject::disconnect(framePlaying->getTeamWidget(*itPlay), SIGNAL(teamStatusChanged(HWTeam)));
+            framePlaying->removeTeam(*itPlay);
+            itPlay = curPlayingTeams.erase(itPlay);
+        }
+        else
+            itPlay++;
+    }
+
+    // Show team notice if less than two teams.
+    if (curPlayingTeams.size() < 2)
+    {
+        numTeamNotice->show();
+    }
+    emit setEnabledGameStart(curPlayingTeams.size()>1);
+}
+
 void TeamSelWidget::changeTeamStatus(HWTeam team)
 {
     QList<HWTeam>::iterator itDontPlay=std::find(m_curNotPlayingTeams.begin(), m_curNotPlayingTeams.end(), team);
--- a/QTfrontend/ui/widget/teamselect.h	Tue Aug 21 23:56:49 2018 +0200
+++ b/QTfrontend/ui/widget/teamselect.h	Wed Aug 22 15:00:04 2018 +0200
@@ -49,6 +49,8 @@
         QList<HWTeam> getNotPlayingTeams() const;
 	unsigned short getNumHedgehogs() const;
         void setInteractivity(bool interactive);
+        void setUser(const QString& nickname);
+        void cleanupFakeNetTeams();
 
     public slots:
         void addTeam(HWTeam team);
@@ -78,6 +80,7 @@
         QLabel *numTeamNotice;
         bool m_acceptOuter;
         void repaint();
+        QString m_curUser;
 
         QList<HWTeam> curPlayingTeams;
         QList<HWTeam> m_curNotPlayingTeams;