diff options
author | Steru <jerrydream111@gmail.com> | 2024-08-15 23:27:54 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-08-26 11:19:38 +0200 |
commit | b6c86f02ff7ecb74cc96af61d8216b232bcecb3e (patch) | |
tree | 26def991cecebcaf8704a5194ae84d4bfe137951 /src/model/Sport.cpp | |
parent | db2d0c21c48698d336ac6e60c42cc2533648b51d (diff) |
Made Competitor a QObject and tidied up some code.
Diffstat (limited to 'src/model/Sport.cpp')
-rw-r--r-- | src/model/Sport.cpp | 28 |
1 files changed, 15 insertions, 13 deletions
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<CompetitorWithResults> 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<MedalWinner> Sport::getCompetitorsWithMedal() { map<QString, QJsonObject> competitors; @@ -239,7 +239,7 @@ QList<MedalWinner> 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<MedalWinner> 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<MedalWinner> 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<Competitor> &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<CompetitorWithResults> &competitors) { @@ -402,11 +404,11 @@ void Sport::addRelativeToFirst(QList<CompetitorWithResults> &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); |