summaryrefslogtreecommitdiff
path: root/src/model/MedalWinner.cpp
diff options
context:
space:
mode:
authorSteru <jerrydream111@gmail.com>2024-08-15 19:21:02 +0200
committerSteru <jerrydream111@gmail.com>2024-08-16 23:50:34 +0200
commit70d5f014e50b9255dcb5c36122a109cc049c8936 (patch)
treef14ab291bec1a27457166ab1c4e0e70920b2ffa9 /src/model/MedalWinner.cpp
parent173ce00cf29822294137d3ebacc5b33ad01a3d25 (diff)
Added Q Object macros.
Diffstat (limited to 'src/model/MedalWinner.cpp')
-rw-r--r--src/model/MedalWinner.cpp32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/model/MedalWinner.cpp b/src/model/MedalWinner.cpp
index c4cb663..4eca811 100644
--- a/src/model/MedalWinner.cpp
+++ b/src/model/MedalWinner.cpp
@@ -1,6 +1,12 @@
#include "MedalWinner.h"
+/**
+ * Replaces/sets the won medals of a competitor.
+ *
+ * @param medals The won medals with their amount.
+ * @return True, if successful.
+ */
bool MedalWinner::setMedals(const QJsonObject &medals) {
if (!medals.contains("ME_GOLD")
|| !medals.contains("ME_SILVER")
@@ -8,11 +14,27 @@ bool MedalWinner::setMedals(const QJsonObject &medals) {
throw invalid_argument("Medal object of competitor is incomplete.");
}
- this->wonMedals = {
- {QString("ME_GOLD"), medals["ME_GOLD"].toString()},
- {QString("ME_SILVER"), medals["ME_SILVER"].toString()},
- {QString("ME_BRONZE"), medals["ME_BRONZE"].toString()}
- };
+ this->gold = medals["ME_GOLD"].toInt();
+ this->silver = medals["ME_SILVER"].toInt();
+ this->bronze = medals["ME_BRONZE"].toInt();
return true;
}
+
+/**
+ * Static compare method, which can compare the amount of medals of two MedalWinners.
+ * Gold has the highest priority, then silver and finally 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 MedalWinner::compare(MedalWinner lComp, MedalWinner rComp) {
+ // create difference between medal amounts
+ int gold = lComp.getGold() - rComp.getGold();
+ int silver = lComp.getSilver() - rComp.getSilver();
+ int bronze = lComp.getBronze() - rComp.getBronze();
+
+ // compare medal differences
+ return gold < 0 || (gold == 0 && (silver < 0 || (silver == 0 && bronze < 0)));
+}