Add team selection in training page and basic check mark support
authorWuzzy <Wuzzy2@mail.ru>
Tue, 18 Dec 2018 14:24:23 +0100
changeset 14483 4c743ef80b1b
parent 14482 9844450389a4
child 14484 bd1db668b7c0
Add team selection in training page and basic check mark support
QTfrontend/CMakeLists.txt
QTfrontend/campaign.cpp
QTfrontend/campaign.h
QTfrontend/game.cpp
QTfrontend/game.h
QTfrontend/hwform.cpp
QTfrontend/hwform.h
QTfrontend/mission.cpp
QTfrontend/mission.h
QTfrontend/ui/page/pagetraining.cpp
QTfrontend/ui/page/pagetraining.h
--- a/QTfrontend/CMakeLists.txt	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/CMakeLists.txt	Tue Dec 18 14:24:23 2018 +0100
@@ -115,6 +115,7 @@
     main.cpp
     team.cpp
     campaign.cpp
+    mission.cpp
     ui_hwform.cpp
     ${CMAKE_CURRENT_BINARY_DIR}/hwconsts.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/sdlkeys.cpp
@@ -160,7 +161,9 @@
     hwconsts.h
     sdlkeys.h
     campaign.h
+    mission.h
     ${CMAKE_CURRENT_BINARY_DIR}/servermessages.h
+    ${CMAKE_CURRENT_BINARY_DIR}/creditsmessages.h
     )
 
 set(hwfr_rez hedgewars.qrc)
--- a/QTfrontend/campaign.cpp	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/campaign.cpp	Tue Dec 18 14:24:23 2018 +0100
@@ -52,7 +52,7 @@
     missionInList: QComboBox index of the mission as selected in the mission widget
     teamName: Name of the playing team
 */
-bool isMissionWon(QString & campaignName, int missionInList, QString & teamName)
+bool isCampMissionWon(QString & campaignName, int missionInList, QString & teamName)
 {
     QSettings* teamfile = getCampTeamFile(campaignName, teamName);
     int progress = teamfile->value("Campaign " + campaignName + "/Progress", 0).toInt();
--- a/QTfrontend/campaign.h	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/campaign.h	Tue Dec 18 14:24:23 2018 +0100
@@ -36,7 +36,7 @@
 QSettings* getCampTeamFile(QString & campaignName, QString & teamName);
 QSettings* getCampMetaInfo();
 bool isCampWon(QString & campaignName, QString & teamName);
-bool isMissionWon(QString & campaignName, int missionInList, QString & teamName);
+bool isCampMissionWon(QString & campaignName, int missionInList, QString & teamName);
 QString getRealCampName(const QString & campaignName);
 QList<MissionInfo> getCampMissionList(QString & campaignName, QString & teamName);
 
--- a/QTfrontend/game.cpp	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/game.cpp	Tue Dec 18 14:24:23 2018 +0100
@@ -44,12 +44,11 @@
 // last game info
 QList<QVariant> lastGameStartArgs = QList<QVariant>();
 GameType lastGameType = gtNone;
-QString lastTrainingSubFolder = NULL;
 GameCFGWidget * lastGameCfg = NULL;
 QString lastGameAmmo = NULL;
 TeamSelWidget * lastGameTeamSel = NULL;
 
-QString training, campaign, campaignScript, campaignTeam; // TODO: Cleaner solution?
+QString training, trainingTeam, campaign, campaignScript, campaignTeam; // TODO: Cleaner solution?
 
 HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg, QString ammo, TeamSelWidget* pTeamSelWidget) :
     TCPBase(true, 0),
@@ -307,6 +306,14 @@
                 writeCampaignVar(msg.right(msg.size() - 3));
             break;
         }
+        case 'v':
+        {
+            if (msg.at(2) == '?')
+                sendMissionVar(msg.right(msg.size() - 3));
+            else if (msg.at(2) == '!')
+                writeMissionVar(msg.right(msg.size() - 3));
+            break;
+        }
         case 'W':
         {
             // fetch new window resolution via IPC and save it in the settings
@@ -500,16 +507,18 @@
     SetGameState(gsStarted);
 }
 
-void HWGame::StartTraining(const QString & file, const QString & subFolder)
+void HWGame::StartTraining(const QString & file, const QString & subFolder, const QString & trainTeam)
 {
     lastGameStartArgs.clear();
     lastGameStartArgs.append(file);
+    lastGameStartArgs.append(subFolder);
+    lastGameStartArgs.append(trainTeam);
     lastGameType = gtTraining;
-    lastTrainingSubFolder = subFolder;
 
     gameType = gtTraining;
 
     training = "Missions/" + subFolder + "/" + file + ".lua";
+    trainingTeam = trainTeam;
     demo.clear();
     Start(false);
     SetGameState(gsStarted);
@@ -574,3 +583,28 @@
     teamfile.setValue("Campaign " + campaign + "/" + varToWrite, varValue);
 }
 
+void HWGame::sendMissionVar(const QByteArray &varToSend)
+{
+    QString varToFind = QString::fromUtf8(varToSend);
+    QSettings teamfile(QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(trainingTeam), QSettings::IniFormat, 0);
+    teamfile.setIniCodec("UTF-8");
+    QString varValue = teamfile.value("Mission " + training + "/" + varToFind, "").toString();
+    QByteArray command;
+    HWProto::addStringToBuffer(command, "v." + varValue);
+    RawSendIPC(command);
+}
+
+void HWGame::writeMissionVar(const QByteArray & varVal)
+{
+    int i = varVal.indexOf(" ");
+    if(i < 0)
+        return;
+
+    QString varToWrite = QString::fromUtf8(varVal.left(i));
+    QString varValue = QString::fromUtf8(varVal.mid(i + 1));
+
+    QSettings teamfile(QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(trainingTeam), QSettings::IniFormat, 0);
+    teamfile.setIniCodec("UTF-8");
+    teamfile.setValue("Mission " + training + "/" + varToWrite, varValue);
+}
+
--- a/QTfrontend/game.h	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/game.h	Tue Dec 18 14:24:23 2018 +0100
@@ -64,7 +64,6 @@
 // last game info
 extern QList<QVariant> lastGameStartArgs;
 extern GameType lastGameType;
-extern QString lastTrainingSubFolder;
 extern GameCFGWidget * lastGameCfg;
 extern QString lastGameAmmo;
 extern TeamSelWidget * lastGameTeamSel;
@@ -80,7 +79,7 @@
         void StartLocal();
         void StartQuick();
         void StartNet();
-        void StartTraining(const QString & file, const QString & subFolder);
+        void StartTraining(const QString & file, const QString & subFolder, const QString & trainTeam);
         void StartCampaign(const QString & camp, const QString & campScript, const QString & campTeam);
         void abort();
         GameState gameState;
@@ -127,6 +126,8 @@
         void SetGameState(GameState state);
         void sendCampaignVar(const QByteArray & varToSend);
         void writeCampaignVar(const QByteArray &varVal);
+        void sendMissionVar(const QByteArray & varToSend);
+        void writeMissionVar(const QByteArray &varVal);
         void flushNetBuffer();
 };
 
--- a/QTfrontend/hwform.cpp	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/hwform.cpp	Tue Dec 18 14:24:23 2018 +0100
@@ -57,6 +57,7 @@
 #include "hwform.h"
 #include "game.h"
 #include "team.h"
+#include "mission.h"
 #include "campaign.h"
 #include "teamselect.h"
 #include "selectWeapon.h"
@@ -330,6 +331,7 @@
     connect(ui.pageCampaign->CBTeam, SIGNAL(currentIndexChanged(int)), this, SLOT(UpdateCampaignPageTeam(int)));
     connect(ui.pageCampaign->CBCampaign, SIGNAL(currentIndexChanged(int)), this, SLOT(UpdateCampaignPage(int)));
     connect(ui.pageCampaign->CBMission, SIGNAL(currentIndexChanged(int)), this, SLOT(UpdateCampaignPageMission(int)));
+    connect(ui.pageTraining->CBTeam, SIGNAL(currentIndexChanged(int)), this, SLOT(UpdateTrainingPageTeam(int)));
 
     connect(ui.pageSelectWeapon->pWeapons, SIGNAL(weaponsDeleted(QString)),
              this, SLOT(DeleteWeapons(QString)));
@@ -572,7 +574,8 @@
     ui.pageOptions->CBTeamName->clear();
     ui.pageOptions->CBTeamName->addItems(teamslist);
     ui.pageCampaign->CBTeam->clear();
-    /* Only show human teams in campaign page */
+    ui.pageTraining->CBTeam->clear();
+    /* Only show human teams in campaign/training page */
     for(int i=0; i<teamslist.length(); i++)
     {
         HWTeam testTeam = HWTeam(teamslist[i]);
@@ -580,8 +583,10 @@
         if(testTeam.difficulty() == 0)
         {
             ui.pageCampaign->CBTeam->addItem(teamslist[i]);
+            ui.pageTraining->CBTeam->addItem(teamslist[i]);
         }
     }
+    UpdateTrainingPageTeam(0);
 }
 
 void HWForm::GoToNewWeapons()
@@ -1842,7 +1847,8 @@
 {
     CreateGame(0, 0, 0);
 
-    game->StartTraining(scriptName, subFolder);
+    QString trainTeam = ui.pageTraining->CBTeam->currentText();
+    game->StartTraining(scriptName, subFolder, trainTeam);
 }
 
 void HWForm::StartCampaign()
@@ -1997,6 +2003,36 @@
     }
 }
 
+void HWForm::UpdateTrainingPageTeam(int index)
+{
+    Q_UNUSED(index);
+    HWTeam team(ui.pageTraining->CBTeam->currentText());
+    QString tName = team.name();
+
+    QListWidget* listWidget;
+    for(int w = 0; w < 3; w++)
+    {
+        switch(w) {
+            case 0: listWidget = ui.pageTraining->lstTrainings; break;
+            case 1: listWidget = ui.pageTraining->lstChallenges; break;
+            case 2: listWidget = ui.pageTraining->lstScenarios; break;
+            default: listWidget = ui.pageTraining->lstTrainings; break;
+        }
+        unsigned int n = listWidget->count();
+
+        for(unsigned int i = 0; i < n; i++)
+        {
+            QListWidgetItem* item = listWidget->takeItem(i);
+            QString missionName = QString(item->data(Qt::UserRole).toString()).replace(QString(" "),QString("_"));
+            if(isMissionWon(missionName, tName))
+                item->setIcon(finishedIcon);
+            else
+                item->setIcon(notFinishedIcon);
+            listWidget->insertItem(i, item);
+        }
+    }
+}
+
 void HWForm::InitCampaignPage()
 {
     ui.pageCampaign->CBCampaign->clear();
@@ -2031,7 +2067,7 @@
     for(int i=0;i<campaignMissionInfo.size();i++)
     {
         ui.pageCampaign->CBMission->addItem(QString(campaignMissionInfo[i].realName), QString(campaignMissionInfo[i].name));
-        if(isMissionWon(campaignName, i, tName))
+        if(isCampMissionWon(campaignName, i, tName))
             ui.pageCampaign->CBMission->setItemIcon(i, finishedIcon);
         else
             ui.pageCampaign->CBMission->setItemIcon(i, notFinishedIcon);
@@ -2235,7 +2271,7 @@
 
     switch(lastGameType) {
     case gtTraining:
-        game->StartTraining(lastGameStartArgs.at(0).toString(), lastTrainingSubFolder);
+        game->StartTraining(lastGameStartArgs.at(0).toString(), lastGameStartArgs.at(1).toString(), lastGameStartArgs.at(2).toString());
         break;
     case gtQLocal:
         game->StartQuick();
--- a/QTfrontend/hwform.h	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/hwform.h	Tue Dec 18 14:24:23 2018 +0100
@@ -135,6 +135,7 @@
         void UpdateCampaignPageTeam(int index);
         void UpdateCampaignPageProgress(int index);
         void UpdateCampaignPageMission(int index);
+        void UpdateTrainingPageTeam(int index);
         void InitCampaignPage();
         void showFeedbackDialog();
         void showFeedbackDialogNetChecked();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/mission.cpp	Tue Dec 18 14:24:23 2018 +0100
@@ -0,0 +1,59 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2004-2018 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "mission.h"
+#include "hwconsts.h"
+#include "DataManager.h"
+#include <QSettings>
+#include <QObject>
+#include <QLocale>
+
+QSettings* getMissionTeamFile(QString & missionName, QString & teamName)
+{
+    QSettings* teamfile = new QSettings(cfgdir->absolutePath() + "/Teams/" + teamName + ".hwt", QSettings::IniFormat, 0);
+    teamfile->setIniCodec("UTF-8");
+    // if entry not found check if there is written without _
+    // if then is found rename it to use _
+    QString cleanedMissionName = missionName;
+    cleanedMissionName = cleanedMissionName.replace(QString("_"),QString(" "));
+    if (!teamfile->childGroups().contains("Mission " + cleanedMissionName) &&
+            teamfile->childGroups().contains("Mission " + cleanedMissionName)){
+        teamfile->beginGroup("Mission " + cleanedMissionName);
+        QStringList keys = teamfile->childKeys();
+        teamfile->endGroup();
+        for (int i=0;i<keys.size();i++) {
+            QVariant value = teamfile->value("Mission " + cleanedMissionName + "/" + keys[i]);
+            teamfile->setValue("Mission " + missionName + "/" + keys[i], value);
+        }
+        teamfile->remove("Mission " + cleanedMissionName);
+    }
+
+    return teamfile;
+}
+
+/**
+    Returns true if the specified mission has been completed
+    missionName: Name of the mission in question
+    teamName: Name of the playing team
+*/
+bool isMissionWon(QString & missionName, QString & teamName)
+{
+    QSettings* teamfile = getMissionTeamFile(missionName, teamName);
+    bool won = teamfile->value("Mission " + missionName + "/Won", false).toBool();
+    return won;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/mission.h	Tue Dec 18 14:24:23 2018 +0100
@@ -0,0 +1,28 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2004-2018 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef MISSION_H
+#define MISSION_H
+
+#include <QString>
+#include <QSettings>
+
+QSettings* getMissionTeamFile(QString & missionName, QString & teamName);
+bool isMissionWon(QString & missionName, QString & teamName);
+
+#endif
--- a/QTfrontend/ui/page/pagetraining.cpp	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/ui/page/pagetraining.cpp	Tue Dec 18 14:24:23 2018 +0100
@@ -101,6 +101,12 @@
     tbw->addTab(lstScenarios, tr("Scenarios"));
     tbw->setCurrentWidget(lstTrainings);
 
+    QLabel* lblteam = new QLabel(tr("Team"));
+    CBTeam = new QComboBox(this);
+    CBTeam->setMaxVisibleItems(30);
+    pageLayout->addWidget(lblteam, 2, 0);
+    pageLayout->addWidget(CBTeam, 2, 1);
+
     return pageLayout;
 }
 
@@ -200,6 +206,7 @@
         // first, load scripts in order specified in order.cfg (if present)
         QFile orderFile(QString("physfs://Missions/%1/order.cfg").arg(subFolder));
         QStringList orderedMissions;
+
         if (orderFile.open(QFile::ReadOnly))
         {
             QString m_id;
--- a/QTfrontend/ui/page/pagetraining.h	Sun Dec 16 18:40:25 2018 +0100
+++ b/QTfrontend/ui/page/pagetraining.h	Tue Dec 18 14:24:23 2018 +0100
@@ -27,7 +27,10 @@
 
     public:
         PageTraining(QWidget* parent = 0);
-
+        QListWidget * lstTrainings;
+        QListWidget * lstChallenges;
+        QListWidget * lstScenarios;
+        QComboBox * CBTeam;
 
     signals:
         void startMission(const QString & scriptName, const QString & subFolder);
@@ -45,9 +48,6 @@
         QLabel * lblCaption;
         QLabel * lblDescription;
         QTabWidget * tbw;
-        QListWidget * lstTrainings;
-        QListWidget * lstChallenges;
-        QListWidget * lstScenarios;
         QSettings * m_info;
         QString getSubFolderOfSelected();