QTfrontend/pagegamestats.cpp
branchhedgeroid
changeset 6224 42b256eca362
parent 6055 88cfcd9161d3
parent 6223 cc3eb9b7230f
child 6226 3106add9a5bf
equal deleted inserted replaced
6055:88cfcd9161d3 6224:42b256eca362
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2010-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include <QLabel>
       
    20 #include <QGridLayout>
       
    21 #include <QGraphicsScene>
       
    22 #include <QGroupBox>
       
    23 #include <QSizePolicy>
       
    24 
       
    25 #include "pagegamestats.h"
       
    26 #include "team.h"
       
    27 
       
    28 FitGraphicsView::FitGraphicsView(QWidget* parent) : QGraphicsView(parent)
       
    29 {
       
    30 
       
    31 }
       
    32 
       
    33 void FitGraphicsView::resizeEvent(QResizeEvent * event)
       
    34 {
       
    35     Q_UNUSED(event);
       
    36 
       
    37     fitInView(sceneRect());
       
    38 }
       
    39 
       
    40 PageGameStats::PageGameStats(QWidget* parent) : AbstractPage(parent)
       
    41 {
       
    42     QGridLayout * pageLayout = new QGridLayout(this);
       
    43     pageLayout->setSpacing(20);
       
    44     pageLayout->setColumnStretch(0, 1);
       
    45     pageLayout->setColumnStretch(1, 1);
       
    46     pageLayout->setContentsMargins(7, 7, 7, 0);
       
    47 
       
    48     BtnSave = addButton(":/res/Save.png", pageLayout, 3, 2, true);
       
    49     BtnSave->setStyleSheet("QPushButton{margin: 12px 0px 12px 0px;}");
       
    50     connect(BtnSave, SIGNAL(clicked()), this, SIGNAL(saveDemoRequested()));
       
    51 
       
    52 
       
    53     BtnBack = addButton(":/res/Exit.png", pageLayout, 3, 0, true);
       
    54     BtnBack->setFixedHeight(BtnSave->height());
       
    55     BtnBack->setFixedWidth(BtnBack->width()+2);
       
    56     BtnBack->setStyleSheet("QPushButton{margin: 22px 0 9px 2px;}");
       
    57     connect(BtnBack, SIGNAL(clicked()), this, SIGNAL(goBack()));
       
    58 
       
    59 
       
    60     QGroupBox * gb = new QGroupBox(this);
       
    61     QVBoxLayout * gbl = new QVBoxLayout;
       
    62 
       
    63     // details
       
    64     labelGameStats = new QLabel(this);
       
    65     QLabel * l = new QLabel(this);
       
    66     l->setTextFormat(Qt::RichText);
       
    67     l->setText("<h1><img src=\":/res/StatsD.png\"> " + PageGameStats::tr("Details") + "</h1>");
       
    68     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    69     labelGameStats->setTextFormat(Qt::RichText);
       
    70     labelGameStats->setAlignment(Qt::AlignTop);
       
    71     labelGameStats->setWordWrap(true);
       
    72     gbl->addWidget(l);
       
    73     gbl->addWidget(labelGameStats);
       
    74     gb->setLayout(gbl);
       
    75     pageLayout->addWidget(gb, 1, 1, 1, 2);
       
    76     
       
    77     // graph
       
    78     graphic = new FitGraphicsView(gb);
       
    79     l = new QLabel(this);
       
    80     l->setTextFormat(Qt::RichText);
       
    81     l->setText("<br><h1><img src=\":/res/StatsH.png\"> " + PageGameStats::tr("Health graph") + "</h1>");
       
    82     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    83     gbl->addWidget(l);
       
    84     gbl->addWidget(graphic);
       
    85     graphic->scale(1.0, -1.0);
       
    86     graphic->setBackgroundBrush(QBrush(Qt::black));
       
    87     
       
    88     labelGameWin = new QLabel(this);
       
    89     labelGameWin->setTextFormat(Qt::RichText);
       
    90     pageLayout->addWidget(labelGameWin, 0, 0, 1, 2);
       
    91 
       
    92     // ranking box
       
    93     gb = new QGroupBox(this);
       
    94     gbl = new QVBoxLayout;
       
    95     labelGameRank = new QLabel(gb);
       
    96     l = new QLabel(this);
       
    97     l->setTextFormat(Qt::RichText);
       
    98     l->setText("<h1><img src=\":/res/StatsR.png\"> " + PageGameStats::tr("Ranking") + "</h1>");
       
    99     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   100     gbl->addWidget(l);
       
   101     gbl->addWidget(labelGameRank);
       
   102     gb->setLayout(gbl);
       
   103 
       
   104     labelGameRank->setTextFormat(Qt::RichText);
       
   105     labelGameRank->setAlignment(Qt::AlignTop);
       
   106     pageLayout->addWidget(gb, 1, 0);
       
   107 }
       
   108 
       
   109 void PageGameStats::AddStatText(const QString & msg)
       
   110 {
       
   111     labelGameStats->setText(labelGameStats->text() + msg);
       
   112 }
       
   113 
       
   114 void PageGameStats::clear()
       
   115 {
       
   116     labelGameStats->setText("");
       
   117     healthPoints.clear();
       
   118     labelGameRank->setText("");
       
   119     playerPosition = 0;
       
   120     lastColor = 0;
       
   121 }
       
   122 
       
   123 void PageGameStats::renderStats()
       
   124 {
       
   125     QGraphicsScene * scene = new QGraphicsScene();
       
   126 
       
   127     QMap<quint32, QVector<quint32> >::const_iterator i = healthPoints.constBegin();
       
   128     while (i != healthPoints.constEnd())
       
   129     {
       
   130         quint32 c = i.key();
       
   131         QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
       
   132         QVector<quint32> hps = i.value();
       
   133 
       
   134         QPainterPath path;
       
   135         if (hps.size())
       
   136             path.moveTo(0, hps[0]);
       
   137 
       
   138         for(int t = 1; t < hps.size(); ++t)
       
   139             path.lineTo(t, hps[t]);
       
   140 
       
   141         scene->addPath(path, QPen(c));
       
   142         ++i;
       
   143     }
       
   144 
       
   145     graphic->setScene(scene);
       
   146     graphic->fitInView(graphic->sceneRect());
       
   147 }
       
   148 
       
   149 void PageGameStats::GameStats(char type, const QString & info)
       
   150 {
       
   151     switch(type) {
       
   152         case 'r' : {
       
   153             labelGameWin->setText(QString("<h1 align=\"center\">%1</h1>").arg(info));
       
   154             break;
       
   155         }
       
   156         case 'D' : {
       
   157             int i = info.indexOf(' ');
       
   158             QString message = "<p><img src=\":/res/StatsBestShot.png\"> " + PageGameStats::tr("The best shot award was won by <b>%1</b> with <b>%2</b> pts.").arg(info.mid(i + 1), info.left(i)) + "</p>";
       
   159             AddStatText(message);
       
   160             break;
       
   161         }
       
   162         case 'k' : {
       
   163             int i = info.indexOf(' ');
       
   164             int num = info.left(i).toInt();
       
   165             QString message = "<p><img src=\":/res/StatsBestKiller.png\"> " + PageGameStats::tr("The best killer is <b>%1</b> with <b>%2</b> kills in a turn.", "", num).arg(info.mid(i + 1), info.left(i)) + "</p>";
       
   166             AddStatText(message);
       
   167             break;
       
   168         }
       
   169         case 'K' : {
       
   170             int num = info.toInt();
       
   171             QString message = "<p><img src=\":/res/StatsHedgehogsKilled.png\"> " +  PageGameStats::tr("A total of <b>%1</b> hedgehog(s) were killed during this round.", "", num).arg(num) + "</p>";
       
   172             AddStatText(message);
       
   173             break;
       
   174         }
       
   175         case 'H' : {
       
   176             int i = info.indexOf(' ');
       
   177             quint32 clan = info.left(i).toInt();
       
   178             quint32 hp = info.mid(i + 1).toUInt();
       
   179             healthPoints[clan].append(hp);
       
   180             break;
       
   181         }
       
   182         case 'T': { // local team stats
       
   183             //AddStatText("<p>local team: " + info + "</p>");
       
   184             QStringList infol = info.split(":");
       
   185             HWTeam team(infol[0]);
       
   186             if(team.fileExists()) // do some better test to avoid influence from scripted/predefined teams?
       
   187             {
       
   188                 team.loadFromFile();
       
   189                 team.incRounds();
       
   190                 if(infol[1].toInt() > 0) // might require some better test for winning condition (or changed flag) ... WIP!
       
   191                     team.incWins(); // should draws count as wins?
       
   192                 //team.SaveToFile(); // don't save yet
       
   193             }
       
   194             break;
       
   195             }
       
   196 
       
   197         case 'P' : {
       
   198             int i = info.indexOf(' ');
       
   199             playerPosition++;
       
   200             QString color = info.left(i);
       
   201             quint32 c = color.toInt();
       
   202             QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
       
   203 
       
   204             QString playerinfo = info.mid(i + 1);
       
   205 
       
   206             i = playerinfo.indexOf(' ');
       
   207 
       
   208             int kills = playerinfo.left(i).toInt();
       
   209             QString playername = playerinfo.mid(i + 1);
       
   210             QString image;
       
   211 
       
   212             if (lastColor == c) playerPosition--;
       
   213             lastColor = c;
       
   214 
       
   215             switch (playerPosition)
       
   216             {
       
   217                 case 1:
       
   218                 image = "<img src=\":/res/StatsMedal1.png\">";
       
   219                 break;
       
   220             case 2:
       
   221                 image = "<img src=\":/res/StatsMedal2.png\">";
       
   222                 break;
       
   223             case 3:
       
   224                 image = "<img src=\":/res/StatsMedal3.png\">";
       
   225                 break;
       
   226             default:
       
   227                 image = "<img src=\":/res/StatsMedal4.png\">";
       
   228                 break;
       
   229             }
       
   230 
       
   231             QString message;
       
   232             QString killstring = PageGameStats::tr("(%1 kill)", "", kills).arg(kills);
       
   233 
       
   234             message = QString("<p><h2>%1 %2. <font color=\"%4\">%3</font> ").arg(image, QString::number(playerPosition), playername, clanColor.name()) + killstring + "</h2></p>";
       
   235 
       
   236             labelGameRank->setText(labelGameRank->text() + message);
       
   237                 break;
       
   238         }
       
   239         case 's' : {
       
   240             int i = info.indexOf(' ');
       
   241             int num = info.left(i).toInt();
       
   242             QString message = "<p><img src=\":/res/StatsMostSelfDamage.png\"> " + PageGameStats::tr("<b>%1</b> thought it's good to shoot his own hedgehogs with <b>%2</b> pts.", "", num).arg(info.mid(i + 1)).arg(num) + "</p>";
       
   243             AddStatText(message);
       
   244             break;
       
   245         }
       
   246         case 'S' : {
       
   247             int i = info.indexOf(' ');
       
   248             int num = info.left(i).toInt();
       
   249             QString message = "<p><img src=\":/res/StatsSelfKilled.png\"> " + PageGameStats::tr("<b>%1</b> killed <b>%2</b> of his own hedgehogs.", "", num).arg(info.mid(i + 1)).arg(num) + "</p>";
       
   250             AddStatText(message);
       
   251             break;
       
   252         }
       
   253         case 'B' : {
       
   254             int i = info.indexOf(' ');
       
   255             int num = info.left(i).toInt();
       
   256             QString message = "<p><img src=\":/res/StatsSkipped.png\"> " + PageGameStats::tr("<b>%1</b> was scared and skipped turn <b>%2</b> times.", "", num).arg(info.mid(i + 1)).arg(num) + "</p>";
       
   257             AddStatText(message);
       
   258             break;
       
   259         }
       
   260 
       
   261     }
       
   262 }