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