QTfrontend/campaign.cpp
author Periklis Ntanasis <pntanasis@gmail.com>
Mon, 23 Dec 2013 02:59:44 +0200
changeset 9821 7501208fd025
parent 9308 4bddcc1a1450
child 9822 f3b124352c3a
permissions -rw-r--r--
check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue #722
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7201
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
     1
/*
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
9080
9b42757d7e71 bump copyright year for Andrey entries
unc0rr
parents: 9001
diff changeset
     3
 * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
7201
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
     4
 *
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
     8
 *
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    12
 * GNU General Public License for more details.
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    13
 *
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    17
 */
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    18
9000
af202eff659c remove about 45 useless #include statements from campain.* - copypasta ftw, eh? :p
sheepluva
parents: 8434
diff changeset
    19
#include "campaign.h"
af202eff659c remove about 45 useless #include statements from campain.* - copypasta ftw, eh? :p
sheepluva
parents: 8434
diff changeset
    20
#include "hwconsts.h"
9278
33a30ce9782f this seems to works, still wip
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9275
diff changeset
    21
#include "DataManager.h"
7201
dc17ffdf0702 The first campaign commit with a lot of changes...
belphegorr <szabibibi@gmail.com>
parents:
diff changeset
    22
#include <QSettings>
9278
33a30ce9782f this seems to works, still wip
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9275
diff changeset
    23
#include <QObject>
9292
187ce630dddc y! code clean up :)
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9290
diff changeset
    24
#include <QLocale>
8434
4821897a0f10 big indentation/whitespaces cleanup
sheepluva
parents: 8049
diff changeset
    25
9292
187ce630dddc y! code clean up :)
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9290
diff changeset
    26
QList<MissionInfo> getCampMissionList(QString & campaignName, QString & teamName)
9294
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
    27
{
9290
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    28
    QList<MissionInfo> missionInfoList;
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    29
	QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + teamName + ".hwt", QSettings::IniFormat, 0);
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    30
    teamfile.setIniCodec("UTF-8");
9821
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    31
    
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    32
    // if entry not found check if there is written without _
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    33
    // if then is found rename it to use _
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    34
    QString spaceCampName = campaignName;
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    35
    spaceCampName = spaceCampName.replace(QString("_"),QString(" "));
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    36
    if (teamfile.childGroups().contains("Campaign " + campaignName) == false and 
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    37
			teamfile.childGroups().contains("Campaign " + spaceCampName) == true){
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    38
		qDebug("CAMP NAME FOUND");
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    39
		teamfile.beginGroup("Campaign " + spaceCampName);
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    40
		QStringList keys = teamfile.childKeys();
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    41
		teamfile.endGroup();
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    42
		for (int i=0;i<keys.size();i++) {			
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    43
			QVariant value = teamfile.value("Campaign " + spaceCampName + "/" + keys[i]);
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    44
			teamfile.setValue("Campaign " + campaignName + "/" + keys[i], value);
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    45
		}
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    46
		teamfile.remove("Campaign " + spaceCampName);
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    47
	}
7501208fd025 check if save from old campaign name scheme exists and if so rename it to use _ instead of spaces -see issue 722
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9308
diff changeset
    48
	
9308
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
    49
    int progress = teamfile.value("Campaign " + campaignName + "/Progress", 0).toInt();
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
    50
    int unlockedMissions = teamfile.value("Campaign " + campaignName + "/UnlockedMissions", 0).toInt();
9290
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    51
    
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    52
    QSettings campfile("physfs://Missions/Campaign/" + campaignName + "/campaign.ini", QSettings::IniFormat, 0);
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    53
    campfile.setIniCodec("UTF-8");
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    54
    
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    55
    DataManager & dataMgr = DataManager::instance();
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    56
        // get locale
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    57
        QSettings settings(dataMgr.settingsFileName(),
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    58
                           QSettings::IniFormat);
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    59
        QString loc = settings.value("misc/locale", "").toString();
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    60
        if (loc.isEmpty())
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    61
            loc = QLocale::system().name();
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    62
        QString campaignDescFile = QString("physfs://Locale/campaigns_" + loc + ".txt");
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    63
        // if file is non-existant try with language only
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    64
        if (!QFile::exists(campaignDescFile))
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    65
            campaignDescFile = QString("physfs://Locale/campaigns_" + loc.remove(QRegExp("_.*$")) + ".txt");
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    66
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    67
        // fallback if file for current locale is non-existant
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    68
        if (!QFile::exists(campaignDescFile))
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    69
            campaignDescFile = QString("physfs://Locale/campaigns_en.txt");
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    70
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    71
        QSettings m_info(campaignDescFile, QSettings::IniFormat, 0);
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    72
        m_info.setIniCodec("UTF-8");
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    73
    
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    74
    if(progress>=0 and unlockedMissions==0)
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    75
    {
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    76
		for(unsigned int i=progress+1;i>0;i--)
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    77
		{
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    78
			MissionInfo missionInfo;
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    79
			missionInfo.name = campfile.value(QString("Mission %1/Name").arg(i)).toString();
9308
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
    80
			QString script = campfile.value(QString("Mission %1/Script").arg(i)).toString();
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
    81
            missionInfo.script = script;
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
    82
			missionInfo.description = m_info.value(campaignName+"-"+ script.replace(QString(".lua"),QString("")) + ".desc",
9290
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    83
                                            QObject::tr("No description available")).toString();
9294
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
    84
            QString image = campfile.value(QString("Mission %1/Script").arg(i)).toString().replace(QString(".lua"),QString(".png"));
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
    85
            missionInfo.image = ":/res/campaign/"+campaignName+"/"+image;
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
    86
            if (!QFile::exists(missionInfo.image))
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
    87
				missionInfo.image = ":/res/CampaignDefault.png";
9290
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    88
			missionInfoList.append(missionInfo);
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    89
		}
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    90
	} 
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    91
	else if(unlockedMissions>0)
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    92
	{
9308
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
    93
		for(int i=1;i<=unlockedMissions;i++)
9290
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    94
		{
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    95
			QString missionNum = QString("%1").arg(i);
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    96
			int missionNumber = teamfile.value("Campaign " + campaignName + "/Mission"+missionNum, -1).toInt();
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    97
			MissionInfo missionInfo;
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
    98
			missionInfo.name = campfile.value(QString("Mission %1/Name").arg(missionNumber)).toString();
9308
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
    99
			QString script = campfile.value(QString("Mission %1/Script").arg(missionNumber)).toString();
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
   100
            missionInfo.script = script;
4bddcc1a1450 merge and changed also spacecampaign name
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9294
diff changeset
   101
			missionInfo.description = m_info.value(campaignName+"-"+ script.replace(QString(".lua"),QString("")) + ".desc",
9290
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
   102
                                            QObject::tr("No description available")).toString();
9294
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
   103
            QString image = campfile.value(QString("Mission %1/Script").arg(missionNumber)).toString().replace(QString(".lua"),QString(".png"));
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
   104
            missionInfo.image = ":/res/campaign/"+campaignName+"/"+image;
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
   105
            if (!QFile::exists(missionInfo.image))
f9b31c3efd35 added deafult image if not one available in campaign
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9292
diff changeset
   106
				missionInfo.image = ":/res/CampaignDefault.png";
9290
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
   107
			missionInfoList.append(missionInfo);
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
   108
		}
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
   109
	}
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
   110
	return missionInfoList;
478aa681e702 this seems to work ok, but I have to clean up the code
Periklis Ntanasis <pntanasis@gmail.com>
parents: 9286
diff changeset
   111
}