summaryrefslogtreecommitdiff
path: root/src/model/Competitor.cpp
diff options
context:
space:
mode:
authorSteru <jerrydream111@gmail.com>2024-08-16 22:19:42 +0200
committerSteru <jerrydream111@gmail.com>2024-08-16 22:19:42 +0200
commit100e73ec28dbc1f307c540cbd4b5a04dfe5922f8 (patch)
tree20ec2b68ae44f7e0e0cec62b605696301898072e /src/model/Competitor.cpp
parentb0063e96410ff0c407e2daead4cf53252568b542 (diff)
Compacted competitors into one object, deleted API class (now in sportmodel).
Diffstat (limited to 'src/model/Competitor.cpp')
-rw-r--r--src/model/Competitor.cpp114
1 files changed, 109 insertions, 5 deletions
diff --git a/src/model/Competitor.cpp b/src/model/Competitor.cpp
index a584689..c0ee01d 100644
--- a/src/model/Competitor.cpp
+++ b/src/model/Competitor.cpp
@@ -1,22 +1,126 @@
#include "Competitor.h"
+/**
+ * Reads certain properties from a competitor json object.
+ * These are: code, name, noc, results
+ *
+ * For further information on 'results' see [Competitor::setResult].
+ *
+ * Does not set the amounts of medals. For this, call [Competitor::setMedals].
+ *
+ * @param competitor The competitor as a QJsonObject.
+ * @return True if successful.
+ */
bool Competitor::setCompetitor(const QJsonObject &competitor) {
if (!competitor.contains("code")
|| !competitor.contains("name")
- || !competitor.contains("m_noc")) {
- throw invalid_argument("Not a competitor object.");
+ || !competitor.contains("noc")) {
+ return false;
}
setCode(competitor["code"].toInt());
setName(competitor["name"].toString());
- setNOC(competitor["m_noc"].toString());
- return true;
+ setNOC(competitor["noc"].toString());
+
+ if (!competitor.contains("results")) return false;
+ QJsonObject results = competitor["results"].toObject();
+ return setResults(results);
}
-bool Competitor::setCompetitor(const Competitor &competitor) {
+/**
+ * Copies all values of a given competitor.
+ *
+ * @param competitor The competitor to copy.
+ */
+void Competitor::setCompetitor(const Competitor &competitor) {
setCode(competitor.m_code);
setName(competitor.m_name);
setNOC(competitor.m_noc);
+ setMark(competitor.m_mark);
+ setMedalType(competitor.m_medalType);
+ setStatistic(competitor.m_statistic);
+ setGold(competitor.m_gold);
+ setSilver(competitor.m_silver);
+ setBronze(competitor.m_bronze);
+}
+
+/**
+ * Replaces/sets the results of a competitor.
+ *
+ * @param results The results of the competitor.
+ * @return True, if successful.
+ */
+bool Competitor::setResults(const QJsonObject &results) {
+ if (!results.contains("mark")
+ || !results.contains("medalType")) {
+ return false;
+ }
+
+ setMark(results["mark"].toString());
+ setMedalType(results["medalType"].toString());
+
+ return true;
+}
+
+/**
+ * Replaces/sets the won medals of a competitor.
+ *
+ * @param medals The won medals with their amount.
+ * @return True, if successful.
+ */
+bool Competitor::setMedals(const map<QString, int> &medals) {
+ if (medals.find("ME_GOLD") == medals.end()
+ || medals.find("ME_SILVER") == medals.end()
+ || medals.find("ME_BRONZE") == medals.end()) return false;
+
+ setGold(medals.find("ME_GOLD")->second);
+ setSilver(medals.find("ME_SILVER")->second);
+ setBronze(medals.find("ME_BRONZE")->second);
+
return true;
}
+
+/**
+ * Static compare method, which can compare the result times or points of two competitors.
+ * 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 Competitor::compareMark(Competitor lComp, Competitor rComp) {
+ QString l = lComp.mark();
+ QString r = rComp.mark();
+
+ // 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;
+}
+
+/**
+ * Static compare method, which can compare the amount of medals of two competitors.
+ * Gold has the highest priority, then m_silver and finally m_bronze.
+ *
+ * @param lComp First competitor to compare.
+ * @param rComp Second competitor to compare.
+ * @return True, if the second competitor got more or higher medals.
+ */
+bool Competitor::compareMedals(Competitor lComp, Competitor rComp) {
+ // create difference between medal amounts
+ int gold = lComp.gold() - rComp.gold();
+ int silver = lComp.silver() - rComp.silver();
+ int bronze = lComp.bronze() - rComp.bronze();
+
+ // compare medal differences
+ return gold < 0 || (gold == 0 && (silver < 0 || (silver == 0 && bronze < 0)));
+}