QTfrontend/pagegamestats.cpp
changeset 6101 5a4ea2c7b9df
parent 5801 531f64292489
parent 6100 e6426c6b2882
child 6102 97565ab4afe9
equal deleted inserted replaced
5801:531f64292489 6101:5a4ea2c7b9df
     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 
       
    47     BtnBack = addButton(":/res/Exit.png", pageLayout, 3, 0, true);
       
    48     BtnBack->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    49 
       
    50     BtnSave = addButton(":/res/Save.png", pageLayout, 3, 2, true);
       
    51     BtnSave->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    52     BtnSave->setStyleSheet("QPushButton{margin: 12px 0px 12px 0px;}");
       
    53     connect(BtnSave, SIGNAL(clicked()), this, SIGNAL(saveDemoRequested()));
       
    54 
       
    55     QGroupBox * gb = new QGroupBox(this);
       
    56     QVBoxLayout * gbl = new QVBoxLayout;
       
    57 
       
    58     // details
       
    59     labelGameStats = new QLabel(this);
       
    60     QLabel * l = new QLabel(this);
       
    61     l->setTextFormat(Qt::RichText);
       
    62     l->setText("<h1><img src=\":/res/StatsD.png\"> " + PageGameStats::tr("Details") + "</h1>");
       
    63     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    64     labelGameStats->setTextFormat(Qt::RichText);
       
    65     labelGameStats->setAlignment(Qt::AlignTop);
       
    66     labelGameStats->setWordWrap(true);
       
    67     gbl->addWidget(l);
       
    68     gbl->addWidget(labelGameStats);
       
    69     gb->setLayout(gbl);
       
    70     pageLayout->addWidget(gb, 1, 1, 1, 2);
       
    71     
       
    72     // graph
       
    73     graphic = new FitGraphicsView(gb);
       
    74     l = new QLabel(this);
       
    75     l->setTextFormat(Qt::RichText);
       
    76     l->setText("<br><h1><img src=\":/res/StatsH.png\"> " + PageGameStats::tr("Health graph") + "</h1>");
       
    77     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    78     gbl->addWidget(l);
       
    79     gbl->addWidget(graphic);
       
    80     graphic->scale(1.0, -1.0);
       
    81     graphic->setBackgroundBrush(QBrush(Qt::black));
       
    82     
       
    83     labelGameWin = new QLabel(this);
       
    84     labelGameWin->setTextFormat(Qt::RichText);
       
    85     pageLayout->addWidget(labelGameWin, 0, 0, 1, 2);
       
    86 
       
    87     // ranking box
       
    88     gb = new QGroupBox(this);
       
    89     gbl = new QVBoxLayout;
       
    90     labelGameRank = new QLabel(gb);
       
    91     l = new QLabel(this);
       
    92     l->setTextFormat(Qt::RichText);
       
    93     l->setText("<h1><img src=\":/res/StatsR.png\"> " + PageGameStats::tr("Ranking") + "</h1>");
       
    94     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    95     gbl->addWidget(l);
       
    96     gbl->addWidget(labelGameRank);
       
    97     gb->setLayout(gbl);
       
    98 
       
    99     labelGameRank->setTextFormat(Qt::RichText);
       
   100     labelGameRank->setAlignment(Qt::AlignTop);
       
   101     pageLayout->addWidget(gb, 1, 0);
       
   102 }
       
   103 
       
   104 void PageGameStats::AddStatText(const QString & msg)
       
   105 {
       
   106     labelGameStats->setText(labelGameStats->text() + msg);
       
   107 }
       
   108 
       
   109 void PageGameStats::clear()
       
   110 {
       
   111     labelGameStats->setText("");
       
   112     healthPoints.clear();
       
   113     labelGameRank->setText("");
       
   114     playerPosition = 0;
       
   115     lastColor = 0;
       
   116 }
       
   117 
       
   118 void PageGameStats::renderStats()
       
   119 {
       
   120     QGraphicsScene * scene = new QGraphicsScene();
       
   121 
       
   122     QMap<quint32, QVector<quint32> >::const_iterator i = healthPoints.constBegin();
       
   123     while (i != healthPoints.constEnd())
       
   124     {
       
   125         quint32 c = i.key();
       
   126         QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
       
   127         QVector<quint32> hps = i.value();
       
   128 
       
   129         QPainterPath path;
       
   130         if (hps.size())
       
   131             path.moveTo(0, hps[0]);
       
   132 
       
   133         for(int t = 1; t < hps.size(); ++t)
       
   134             path.lineTo(t, hps[t]);
       
   135 
       
   136         scene->addPath(path, QPen(c));
       
   137         ++i;
       
   138     }
       
   139 
       
   140     graphic->setScene(scene);
       
   141     graphic->fitInView(graphic->sceneRect());
       
   142 }
       
   143 
       
   144 void PageGameStats::GameStats(char type, const QString & info)
       
   145 {
       
   146     switch(type) {
       
   147         case 'r' : {
       
   148             labelGameWin->setText(QString("<h1 align=\"center\">%1</h1>").arg(info));
       
   149             break;
       
   150         }
       
   151         case 'D' : {
       
   152             int i = info.indexOf(' ');
       
   153             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>";
       
   154             AddStatText(message);
       
   155             break;
       
   156         }
       
   157         case 'k' : {
       
   158             int i = info.indexOf(' ');
       
   159             int num = info.left(i).toInt();
       
   160             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>";
       
   161             AddStatText(message);
       
   162             break;
       
   163         }
       
   164         case 'K' : {
       
   165             int num = info.toInt();
       
   166             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>";
       
   167             AddStatText(message);
       
   168             break;
       
   169         }
       
   170         case 'H' : {
       
   171             int i = info.indexOf(' ');
       
   172             quint32 clan = info.left(i).toInt();
       
   173             quint32 hp = info.mid(i + 1).toUInt();
       
   174             healthPoints[clan].append(hp);
       
   175             break;
       
   176         }
       
   177         case 'T': { // local team stats
       
   178             //AddStatText("<p>local team: " + info + "</p>");
       
   179             QStringList infol = info.split(":");
       
   180             HWTeam team(infol[0]);
       
   181             if(team.FileExists()) // do some better test to avoid influence from scripted/predefined teams?
       
   182             {
       
   183                 team.LoadFromFile();
       
   184                 team.Rounds++;
       
   185                 if(infol[1].toInt() > 0) // might require some better test for winning condition (or changed flag) ... WIP!
       
   186                     team.Wins++; // should draws count as wins?
       
   187                 //team.SaveToFile(); // don't save yet
       
   188             }
       
   189             break;
       
   190             }
       
   191 
       
   192         case 'P' : {
       
   193             int i = info.indexOf(' ');
       
   194             playerPosition++;
       
   195             QString color = info.left(i);
       
   196             quint32 c = color.toInt();
       
   197             QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
       
   198 
       
   199             QString playerinfo = info.mid(i + 1);
       
   200 
       
   201             i = playerinfo.indexOf(' ');
       
   202 
       
   203             int kills = playerinfo.left(i).toInt();
       
   204             QString playername = playerinfo.mid(i + 1);
       
   205             QString image;
       
   206 
       
   207             if (lastColor == c) playerPosition--;
       
   208             lastColor = c;
       
   209 
       
   210             switch (playerPosition)
       
   211             {
       
   212                 case 1:
       
   213                 image = "<img src=\":/res/StatsMedal1.png\">";
       
   214                 break;
       
   215             case 2:
       
   216                 image = "<img src=\":/res/StatsMedal2.png\">";
       
   217                 break;
       
   218             case 3:
       
   219                 image = "<img src=\":/res/StatsMedal3.png\">";
       
   220                 break;
       
   221             default:
       
   222                 image = "<img src=\":/res/StatsMedal4.png\">";
       
   223                 break;
       
   224             }
       
   225 
       
   226             QString message;
       
   227             QString killstring = PageGameStats::tr("(%1 kill)", "", kills).arg(kills);
       
   228 
       
   229             message = QString("<p><h2>%1 %2. <font color=\"%4\">%3</font> ").arg(image, QString::number(playerPosition), playername, clanColor.name()) + killstring + "</h2></p>";
       
   230 
       
   231             labelGameRank->setText(labelGameRank->text() + message);
       
   232                 break;
       
   233         }
       
   234         case 's' : {
       
   235             int i = info.indexOf(' ');
       
   236             int num = info.left(i).toInt();
       
   237             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>";
       
   238             AddStatText(message);
       
   239             break;
       
   240         }
       
   241         case 'S' : {
       
   242             int i = info.indexOf(' ');
       
   243             int num = info.left(i).toInt();
       
   244             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>";
       
   245             AddStatText(message);
       
   246             break;
       
   247         }
       
   248         case 'B' : {
       
   249             int i = info.indexOf(' ');
       
   250             int num = info.left(i).toInt();
       
   251             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>";
       
   252             AddStatText(message);
       
   253             break;
       
   254         }
       
   255 
       
   256     }
       
   257 }