Display team records in training menu
authorWuzzy <Wuzzy2@mail.ru>
Sun, 06 Jan 2019 01:21:16 +0100
changeset 14546 029f40c609b4
parent 14545 b5618fa33a49
child 14547 797e3f441c14
Display team records in training menu
QTfrontend/hwform.cpp
QTfrontend/mission.cpp
QTfrontend/mission.h
QTfrontend/ui/page/pagetraining.cpp
QTfrontend/ui/page/pagetraining.h
share/hedgewars/Data/Scripts/Utils.lua
--- a/QTfrontend/hwform.cpp	Sat Jan 05 23:15:13 2019 +0100
+++ b/QTfrontend/hwform.cpp	Sun Jan 06 01:21:16 2019 +0100
@@ -2039,6 +2039,7 @@
                 item->setIcon(notFinishedIcon);
         }
     }
+    ui.pageTraining->updateInfo();
 }
 
 void HWForm::InitCampaignPage()
--- a/QTfrontend/mission.cpp	Sat Jan 05 23:15:13 2019 +0100
+++ b/QTfrontend/mission.cpp	Sun Jan 06 01:21:16 2019 +0100
@@ -53,3 +53,27 @@
     bool won = teamfile->value("Mission " + missionName + "/Won", false).toBool();
     return won;
 }
+
+/**
+    Returns true if the mission value adressed with the provided
+    missionName: Name of the mission in question
+    teamName: Name of the playing team
+    key: name of key to check
+*/
+bool missionValueExists(QString & missionName, QString & teamName, QString key)
+{
+    QSettings* teamfile = getMissionTeamFile(missionName, teamName);
+    return teamfile->contains("Mission " + missionName + "/" + key);
+}
+/**
+    Returns a mission value.
+    NOTE: Check whether the mission value exists first, using missionValueExists.
+    missionName: Name of the mission in question
+    teamName: Name of the playing team
+    key: name of key to read its value from
+*/
+QVariant getMissionValue(QString & missionName, QString & teamName, QString key)
+{
+    QSettings* teamfile = getMissionTeamFile(missionName, teamName);
+    return teamfile->value("Mission " + missionName + "/" + key);
+}
--- a/QTfrontend/mission.h	Sat Jan 05 23:15:13 2019 +0100
+++ b/QTfrontend/mission.h	Sun Jan 06 01:21:16 2019 +0100
@@ -24,5 +24,7 @@
 
 QSettings* getMissionTeamFile(QString & missionName, QString & teamName);
 bool isMissionWon(QString & missionName, QString & teamName);
+bool missionValueExists(QString & missionName, QString & teamName, QString key);
+QVariant getMissionValue(QString & missionName, QString & teamName, QString key);
 
 #endif
--- a/QTfrontend/ui/page/pagetraining.cpp	Sat Jan 05 23:15:13 2019 +0100
+++ b/QTfrontend/ui/page/pagetraining.cpp	Sun Jan 06 01:21:16 2019 +0100
@@ -28,6 +28,7 @@
 #include <QLocale>
 #include <QSettings>
 
+#include "mission.h"
 #include "hwconsts.h"
 #include "DataManager.h"
 
@@ -72,9 +73,13 @@
     lblDescription->setMinimumWidth(360);
     lblDescription->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
     lblDescription->setWordWrap(true);
+    lblHighscores = new QLabel();
+    lblHighscores->setMinimumWidth(360);
+    lblHighscores->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
 
     infoTextLayout->addWidget(lblCaption);
     infoTextLayout->addWidget(lblDescription);
+    infoTextLayout->addWidget(lblHighscores);
 
     infoLayout->addWidget(infoTextWidget);
 
@@ -88,7 +93,7 @@
     // let's not make the tab widget use more space than needed
     tbw->setFixedWidth(400);
     pageLayout->setAlignment(tbw, Qt::AlignHCenter);
-    
+ 
     tbw->setStyleSheet("QListWidget { border-style: none; padding-top: 6px; }");
 
     // training/challenge/scenario lists
@@ -314,9 +319,10 @@
         list = (QListWidget*) tbw->currentWidget();
         if (list->currentItem())
         {
+            QString missionName = list->currentItem()->data(Qt::UserRole).toString();
             QString thumbFile =     "physfs://Graphics/Missions/" +
                                     subFolder + "/" +
-                                    list->currentItem()->data(Qt::UserRole).toString() +
+                                    missionName +
                                     "@2x.png";
 
             if (QFile::exists(thumbFile))
@@ -331,24 +337,43 @@
 
             btnPreview->setWhatsThis(tr("Start fighting"));
 
-            QString realName = list->currentItem()->data(
-                                    Qt::UserRole).toString();
-
-            QString caption = m_info->value(realName + ".name",
+            QString caption = m_info->value(missionName + ".name",
                                             list->currentItem()->text()).toString();
 
-            QString description = m_info->value(realName + ".desc",
+            QString description = m_info->value(missionName + ".desc",
                                                 tr("No description available")).toString();
 
             lblCaption->setText("<h2>" + caption +"</h2>");
             lblDescription->setText(description);
+
+            // Challenge highscores
+            QString highscoreText = QString("");
+            QString teamName = CBTeam->currentText();
+            if (missionValueExists(missionName, teamName, "Highscore"))
+                //: Highest score of a team
+                highscoreText = highscoreText + tr("Team highscore: %1").arg(getMissionValue(missionName, teamName, "Highscore").toString()) + "\n";
+            if (missionValueExists(missionName, teamName, "Lowscore"))
+                //: Lowest score of a team
+                highscoreText = highscoreText + tr("Team lowscore: %1").arg(getMissionValue(missionName, teamName, "Lowscore").toString()) + "\n";
+            if (missionValueExists(missionName, teamName, "TimeRecord"))
+            {
+                double time = ((double) getMissionValue(missionName, teamName, "TimeRecord").toInt()) / 1000.0;
+                highscoreText = highscoreText + tr("Team's best time: %L1 s").arg(time, 0, 'f', 3) + "\n";
+            }
+            if (missionValueExists(missionName, teamName, "TimeRecordHigh"))
+            {
+                double time = ((double) getMissionValue(missionName, teamName, "TimeRecordHigh").toInt()) / 1000.0;
+                highscoreText = highscoreText + tr("Team's longest time: %L1 s").arg(time, 0, 'f', 3) + "\n";
+            }
+
+            lblHighscores->setText(highscoreText);
         }
         else
         {
             btnPreview->setIcon(QIcon(":/res/Trainings.png"));
             lblCaption->setText(tr("Select a mission!"));
-            // TODO better text and tr()
             lblDescription->setText("");
+            lblHighscores->setText("");
         }
     }
 }
--- a/QTfrontend/ui/page/pagetraining.h	Sat Jan 05 23:15:13 2019 +0100
+++ b/QTfrontend/ui/page/pagetraining.h	Sun Jan 06 01:21:16 2019 +0100
@@ -32,6 +32,9 @@
         QListWidget * lstScenarios;
         QComboBox * CBTeam;
 
+    public slots:
+        void updateInfo();
+
     signals:
         void startMission(const QString & scriptName, const QString & subFolder);
 
@@ -47,6 +50,7 @@
         QPushButton * btnStart;
         QLabel * lblCaption;
         QLabel * lblDescription;
+        QLabel * lblHighscores;
         QTabWidget * tbw;
         QSettings * m_info;
         QString getSubFolderOfSelected();
@@ -54,7 +58,6 @@
 
     private slots:
         void startSelected();
-        void updateInfo();
 
 };
 
--- a/share/hedgewars/Data/Scripts/Utils.lua	Sat Jan 05 23:15:13 2019 +0100
+++ b/share/hedgewars/Data/Scripts/Utils.lua	Sun Jan 06 01:21:16 2019 +0100
@@ -36,12 +36,14 @@
 end
 
 local function challengeRecordToString(recordType, value)
-	if recordType == "TimeRecord" or recordType == "TimeRecordHigh" then
-		return string.format(loc("Team record: %.3fs"), value/1000)
+	if recordType == "TimeRecord" then
+		return string.format(loc("Team's best time: %.3fs"), value/1000)
+	elseif recordType == "TimeRecordHigh" then
+		return string.format(loc("Team's longest time: %.3fs"), value/1000)
 	elseif recordType == "Highscore" then
-		return string.format(loc("Team high score: %d"), value)
-	else
-		return string.format(loc("Team record: %d"), value)
+		return string.format(loc("Team highscore: %d"), value)
+	elseif recordType == "Lowscore" then
+		return string.format(loc("Team lowscore: %d"), value)
 	end
 end