diff options
author | Steru <jerrydream111@gmail.com> | 2024-08-15 19:21:02 +0200 |
---|---|---|
committer | Steru <jerrydream111@gmail.com> | 2024-08-16 15:48:53 +0200 |
commit | 5f4431e19c0b6c50c7f24ce040bcbcdd40b7a13c (patch) | |
tree | a769254e53716ca2c0a1fde64b2842bc3a6bac13 /src/model/CompetitorWithResults.cpp | |
parent | 3d19fd72716561a0322ba0acc5c94aac9f161e9d (diff) |
Added Q Object macros.
Diffstat (limited to 'src/model/CompetitorWithResults.cpp')
-rw-r--r-- | src/model/CompetitorWithResults.cpp | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/model/CompetitorWithResults.cpp b/src/model/CompetitorWithResults.cpp index d29441d..1601a4a 100644 --- a/src/model/CompetitorWithResults.cpp +++ b/src/model/CompetitorWithResults.cpp @@ -1,15 +1,46 @@ #include "CompetitorWithResults.h" +/** + * Replaces/sets the results of a competitor. + * + * @param results The results of the competitor. + * @return True, if successful. + */ bool CompetitorWithResults::setResults(const QJsonObject &results) { if (!results.contains("mark") || !results.contains("medalType")) { throw invalid_argument("Results object of competitor is incomplete."); } - this->results = { - {QString("mark"), results["mark"].toString()}, - {QString("medalType"), results["medalType"].toString()} - }; + this->mark = results["mark"].toString(); + this->medalType = results["medalType"].toString(); + return true; } + +/** + * Static compare method, which can compare the result times or points of two CompetitorsWithResult. + * Returns true, if the left competitor (lComp) got a real lesser score than the right competitor (rComp). + * + * @param lComp First competitor to compare. + * @param rComp Second competitor to compare. + * @return True, if the second competitor got a higher score. + */ +bool CompetitorWithResults::compare(CompetitorWithResults lComp, CompetitorWithResults rComp) { + QString l = lComp.getMark(); + QString r = rComp.getMark(); + + // check if values are numerical (-> not time values) + if (!l.contains(":") || !r.contains(":")) { + return l.toFloat() < r.toFloat(); + } + + // compare time values if not numerical + QString lTime(""), rTime(""); + + for (QChar c : l) if (c.isDigit()) lTime.append(c); + for (QChar c : r) if (c.isDigit()) rTime.append(c); + + return lTime.compare(rTime) < 0; +} |