From c539242efa11538e5e3f91bf3ff2f0de1c526bed Mon Sep 17 00:00:00 2001 From: Steru Date: Mon, 5 Aug 2024 14:47:45 +0200 Subject: Added method to reverse order and statistic method. --- src/discipline/Sport.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++++++- src/discipline/Sport.h | 6 ++++ 2 files changed, 94 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/discipline/Sport.cpp b/src/discipline/Sport.cpp index 8d9a5c2..21b9b9f 100644 --- a/src/discipline/Sport.cpp +++ b/src/discipline/Sport.cpp @@ -1,10 +1,18 @@ #include "Sport.h" + +// categories #include + +// sorting and filtering #include #include #include +// float to string formatting +#include +#include + #include #include #include @@ -164,7 +172,6 @@ QJsonArray Sport::getCompetitorsByCategory(QString category) { * @brief Sport::getCompetitorsWithMedal Filters all competitors, who have at least one medal. These objects are different from getCompetitorsByCategory !!! * @return All competitors, who won at least one medal. Structure of one competitor: {code, name, noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE}} */ - QJsonArray Sport::getCompetitorsWithMedal() { map competitors; @@ -334,3 +341,83 @@ void Sport::sortCompetitors(QJsonArray &competitors, functionresults->stat for each competitor. + * @param competitors The competitors of one category. + */ +void Sport::addRelativeToFirst(QJsonArray &competitors) { + if (competitors.isEmpty()) return; + + QJsonObject reference = competitors[0].toObject(); + + // validate competitors + if (!reference.contains("results")) return; + + QString refVal = reference["results"].toObject()["mark"].toString(); + + for (int i = 0; i < competitors.size(); i++) { + QJsonObject competitor = competitors[i].toObject(); + QJsonObject results = competitor["results"].toObject(); + + if (results.contains("stat")) results.remove("stat"); + + // format relative float value to string with 2 digits after decimal point and sign + stringstream sstream; + sstream << fixed << setprecision(2) << calcRelativeStat(refVal, results["mark"].toString()); + QString stat(sstream.str().c_str()); + stat.append("%"); + if (stat.at(0).isNumber()) stat = QString("+").append(stat); + + results.insert("stat", stat); + + competitor.remove("results"); + competitor.insert("results", results); + + competitors.replace(i, competitor); + } + +} + +/** + * @brief Sport::calcRelativeStat Calculates the relative deviation of val from ref in percent. + * @param ref The reference value. + * @param val The value to calculate the deviation from. + * @return The deviation from ref to val in percent. + */ +float Sport::calcRelativeStat(QString ref, QString val) { + // check if the value is not a time + if (!ref.contains(":") && !val.contains(":")) { + float fRef = ref.toFloat(); + + if (fRef == 0) return 0.0; + return ((val.toFloat() * 100)/ fRef) - 100; + } + + regex r("\\d+"); + smatch mref, mval; + string sref = ref.toUtf8().constData(); + string sval = val.toUtf8().constData(); + + regex_search(sref, mref, r); + regex_search(sval, mval, r); + + float timeRef = stof(mref.str(1)) * 60 * 100 + stof(mref.str(2)) * 100 + stof(mref.str(3)); + float timeVal = stof(mval.str(1)) * 60 * 100 + stof(mval.str(2)) * 100 + stof(mval.str(3)); + + return ((timeVal * 100) / timeRef) - 100; +} diff --git a/src/discipline/Sport.h b/src/discipline/Sport.h index 9175ad0..147f6e8 100644 --- a/src/discipline/Sport.h +++ b/src/discipline/Sport.h @@ -33,6 +33,10 @@ public: void sortByName(QJsonArray &competitors); void sortByCountry(QJsonArray &competitors); void sortByResult(QJsonArray &competitors); + void reverseOrder(QJsonArray &competitors); + + // statistic function(s) + void addRelativeToFirst(QJsonArray &competitors); void setDiscipline(QJsonObject discipline) { this->discipline = QJsonObject(discipline); @@ -80,6 +84,8 @@ private: bool validateDiscipline(); QJsonObject createCompetitorWithMedals(QJsonObject medalComp); + float calcRelativeStat(QString ref, QString val); + }; -- cgit v1.2.3