From b6c86f02ff7ecb74cc96af61d8216b232bcecb3e Mon Sep 17 00:00:00 2001 From: Steru Date: Thu, 15 Aug 2024 23:27:54 +0200 Subject: Made Competitor a QObject and tidied up some code. --- src/model/Sport.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'src/model/Sport.cpp') diff --git a/src/model/Sport.cpp b/src/model/Sport.cpp index 4b822fa..4b341ed 100644 --- a/src/model/Sport.cpp +++ b/src/model/Sport.cpp @@ -213,7 +213,7 @@ QList 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}} + * @return All competitors, who won at least one medal. Structure of one competitor: {code, name, m_noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE}} */ QList Sport::getCompetitorsWithMedal() { map competitors; @@ -239,7 +239,7 @@ QList Sport::getCompetitorsWithMedal() { QJsonArray medalComps = filter(unit["competitors"].toArray(), [](QJsonObject comp) { if (!comp.contains("results")) return false; - QString medalType = comp["results"].toObject()["medalType"].toString(); + QString medalType = comp["results"].toObject()["m_medalType"].toString(); return !medalType.isEmpty(); }); @@ -249,10 +249,10 @@ QList Sport::getCompetitorsWithMedal() { // validate competitor (with medal) if (!medalComp.contains("name") || !medalComp.contains("results") - || !medalComp["results"].toObject().contains("medalType")) continue; + || !medalComp["results"].toObject().contains("m_medalType")) continue; QString name = medalComp["name"].toString(); - QString medalType = medalComp["results"].toObject()["medalType"].toString(); + QString medalType = medalComp["results"].toObject()["m_medalType"].toString(); // check if competitor has other medal(s) if (competitors.find(name) == competitors.end()) { @@ -281,7 +281,7 @@ QList Sport::getCompetitorsWithMedal() { } /** - * @brief Sport::createCompetitorWithMedals Creates a competitor QJsonObject with the following attributes: code, name, noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE} + * @brief Sport::createCompetitorWithMedals Creates a competitor QJsonObject with the following attributes: code, name, m_noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE} * @param comp The original competitor object. * @return A competitor object with medal counts. */ @@ -289,7 +289,7 @@ QJsonObject Sport::createCompetitorWithMedals(QJsonObject comp) { // repair competitor if something is missing if (!comp.contains("code")) comp.insert("code", "0"); if (!comp.contains("name")) comp.insert("code", ""); - if (!comp.contains("noc")) comp.insert("code", ""); + if (!comp.contains("m_noc")) comp.insert("code", ""); // create new competitor QJsonObject and add it to the competitor map QJsonObject medals { @@ -301,7 +301,7 @@ QJsonObject Sport::createCompetitorWithMedals(QJsonObject comp) { QJsonObject medalComp { {"code", comp["code"].toString()}, {"name", comp["name"].toString()}, - {"noc", comp["noc"].toString()}, + {"m_noc", comp["m_noc"].toString()}, {"medals", medals} }; @@ -385,15 +385,17 @@ void Sport::reverseOrder(QList &competitors) { int iterations = competitors.size() / 2; // automatically rounds down for (int i = 0; i < iterations; i++) { - Competitor temp = competitors.value(i); - competitors.replace(i, competitors.value(competitors.size() - 1 - i)); - competitors.replace(competitors.size() - 1 - i, temp); + Competitor left = Competitor(competitors.value(i)); + Competitor right = Competitor(competitors.value(competitors.size() - 1 - i)); + + competitors.replace(i, right); + competitors.replace(competitors.size() - 1 - i, left); } } /** * @brief Sport::addRelativeToFirst Adds a relative value to the result of all competitors. Relative to the first competitor in the QJsonArray. - * Stores the statistic in obj->results->stat for each competitor. + * Stores the m_statistic in obj->results->stat for each competitor. * @param competitors The competitors of one category. */ void Sport::addRelativeToFirst(QList &competitors) { @@ -402,11 +404,11 @@ void Sport::addRelativeToFirst(QList &competitors) { QString reference = competitors.value(0).getMark(); for (CompetitorWithResults comp : competitors) { - QString results = comp.getMark(); + QString result = comp.getMark(); // format relative float value to string with 2 digits after decimal point and sign stringstream sstream; - sstream << fixed << setprecision(2) << calcRelativeStat(reference, results["mark"].toString()); + sstream << fixed << setprecision(2) << calcRelativeStat(reference, result); QString stat(sstream.str().c_str()); stat.append("%"); if (stat.at(0).isNumber()) stat = QString("+").append(stat); -- cgit v1.2.3