File indexing completed on 2024-10-13 03:44:24

0001 /*
0002     SPDX-FileCopyrightText: 2000-2001 Nikolas Zimmermann <wildfox@kde.org>
0003     SPDX-FileCopyrightText: 2000-2001 Daniel Molkentin <molkentin@kde.org>
0004     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "stats.h"
0010 #include "hitinfo.h"
0011 
0012 Stats::Stats()
0013 : m_hits(0)
0014 , m_misses(0)
0015 {
0016 }
0017 
0018 int Stats::score() const
0019 {
0020     // Balancing factors
0021     // a = shot-balance
0022     // b = water-balance
0023     double a = 3;
0024     double b = 0.5;
0025     double score = (a * hits() - b * misses()) / (shots() + misses()) * 1000;
0026     int res = static_cast<int>(score);
0027     if (res <= 0) {
0028         res = 1;
0029     }
0030     return res;
0031 }
0032 
0033 void Stats::addHit()
0034 {
0035     m_hits++;
0036     Q_EMIT hitsChanged();
0037 }
0038 
0039 void Stats::addMiss()
0040 {
0041     m_misses++;
0042     Q_EMIT missesChanged();
0043 }
0044 
0045 void Stats::addInfo(const HitInfo& info)
0046 {
0047     switch (info.type)
0048     {
0049     case HitInfo::HIT:
0050         addHit();
0051         break;
0052     case HitInfo::MISS:
0053         addMiss();
0054         break;
0055     default:
0056         break;
0057     }
0058 }
0059 
0060 void Stats::reset()
0061 {
0062     m_hits = 0;
0063     m_misses = 0;
0064     Q_EMIT hitsChanged();
0065     Q_EMIT missesChanged();
0066 }
0067 
0068 #include "moc_stats.cpp"