summaryrefslogtreecommitdiff
path: root/src/model/SportModel.cpp
diff options
context:
space:
mode:
authorSteru <jerrydream111@gmail.com>2024-08-16 22:19:42 +0200
committerSteru <jerrydream111@gmail.com>2024-08-16 23:58:46 +0200
commit88b1b119dfa35f36b64d81dbe6c84f46d86455c5 (patch)
tree864ba9c747ef86d61b6aeb01e8985676f906bd3e /src/model/SportModel.cpp
parentf24b4dcbd11336dabfd146c656e2437e4393b225 (diff)
Compacted competitors into one object, deleted API class (now in sportmodel).
Diffstat (limited to 'src/model/SportModel.cpp')
-rw-r--r--src/model/SportModel.cpp161
1 files changed, 48 insertions, 113 deletions
diff --git a/src/model/SportModel.cpp b/src/model/SportModel.cpp
index be9d326..557affe 100644
--- a/src/model/SportModel.cpp
+++ b/src/model/SportModel.cpp
@@ -8,8 +8,7 @@
#include <set>
// sorting and filtering
-#include <map>
-#include <algorithm>
+//#include <algorithm>
#include <regex>
// float to string formatting
@@ -42,7 +41,7 @@ QVariant SportModel::data(const QModelIndex &index, int role) const {
return event->eventName();
case Competitors:
- return event->competitors();
+ return QVariant::fromValue(event->competitors());
}
}
@@ -82,14 +81,14 @@ void SportModel::parseData() {
qDeleteAll(m_sportList);
m_sportList.clear();
-
-
QByteArray strReply = m_reply->readAll();
//parse json
// qDebug() << "Response:" << strReply;
QJsonDocument jsonDocument = QJsonDocument::fromJson(strReply);
+ map<QString, map<QString, int>> medals = getMedalsOfCompetitors();
+
QJsonArray sports = jsonDocument["units"].toArray();
for (const auto &sport : sports) {
QJsonObject entry = sport.toObject();
@@ -97,15 +96,20 @@ void SportModel::parseData() {
EventInfo *event = new EventInfo(this);
event->setEventName(entry["eventUnitName"].toString());
- QList<QString> competitors;
+ QList<Competitor*> competitors;
for (const auto &competitor : entry["competitors"].toArray()) {
- competitors << competitor.toObject()["name"].toString();
+ Competitor *comp = new Competitor(this);
+ comp->setCompetitor(competitor.toObject());
+ if (medals.find(comp->name()) != medals.end()) comp->setMedals(medals.find(comp->name())->second);
+ if (!competitors.empty()) comp->setStatistic(competitors.first()->mark());
+ competitors << comp;
}
event->setCompetitors(competitors);
qDebug() << entry["eventUnitName"].toString();
m_sportList << event;
}
+
endResetModel();
}
}
@@ -134,7 +138,7 @@ void SportModel::lastName(QList<Competitor*> &competitors) {
for (int i = 0; i < competitors.size(); i++) {
Competitor* comp = competitors.value(i);
- string fullName = comp->getName().toUtf8().constData();
+ string fullName = comp->name().toUtf8().constData();
// regex to identify names, written in CAPS
regex r("[A-Z']{2,}");
@@ -185,43 +189,13 @@ set<QString> SportModel::getCategories() {
}
/**
- * @brief Sport::getCompetitorsByCategory Searches for all competitors, who took part in the given category.
- * @param category The category to search in.
- * @return An QJsonArray with all competitors as QJsonValueRef, which can be casted to QJsonObject.
- */
-QList<CompetitorWithResults*> SportModel::getCompetitorsByCategory(QString category) {
- QList<CompetitorWithResults*> competitors;
-
- if (!validateDiscipline()) return competitors;
-
- for (const QJsonValueRef &unitRef : this->o_discipline["units"].toArray()) {
- QJsonObject unit = unitRef.toObject();
-
- // validate unit
- if (!unit.contains("eventUnitName") || !unit.contains("competitors")) continue;
-
- // search all units with the same category
- if (unit["eventUnitName"].toString().compare(category, Qt::CaseSensitive) != 0) continue;
-
- // add all competitors from one unit
- for (const QJsonValueRef &compRef : unit["competitors"].toArray()) {
- CompetitorWithResults *comp = new CompetitorWithResults(); // TODO declare comp
- comp->setCompetitorWithResults(compRef.toObject());
- competitors.push_back(comp);
- }
- }
-
- return competitors;
-}
-
-/**
- * @brief Sport::getCompetitorsWithMedal Filters all competitors, who have at least one medal. These objects are different from getCompetitorsByCategory !!!
+ * @brief Sport::getMedalsOfCompetitor 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, m_noc, medals{ME_GOLD, ME_SILVER, ME_BRONZE}}
*/
-QList<MedalWinner*> SportModel::getCompetitorsWithMedal() {
- map<QString, QJsonObject> competitors;
+map<QString, map<QString, int>> SportModel::getMedalsOfCompetitors() {
+ map<QString, map<QString, int>> competitors;
- if (!validateDiscipline()) return QList<MedalWinner*>();
+ if (!validateDiscipline()) return competitors;
// filter all units, which have medal events
QJsonArray units = filter(this->o_discipline["units"].toArray(), [](QJsonObject unit){
@@ -242,7 +216,7 @@ QList<MedalWinner*> SportModel::getCompetitorsWithMedal() {
QJsonArray medalComps = filter(unit["competitors"].toArray(), [](QJsonObject comp) {
if (!comp.contains("results")) return false;
- QString medalType = comp["results"].toObject()["m_medalType"].toString();
+ QString medalType = comp["results"].toObject()["medalType"].toString();
return !medalType.isEmpty();
});
@@ -252,65 +226,26 @@ QList<MedalWinner*> SportModel::getCompetitorsWithMedal() {
// validate competitor (with medal)
if (!medalComp.contains("name")
|| !medalComp.contains("results")
- || !medalComp["results"].toObject().contains("m_medalType")) continue;
+ || !medalComp["results"].toObject().contains("medalType")) continue;
QString name = medalComp["name"].toString();
- QString medalType = medalComp["results"].toObject()["m_medalType"].toString();
+ QString medalType = medalComp["results"].toObject()["medalType"].toString();
// check if competitor has other medal(s)
if (competitors.find(name) == competitors.end()) {
- competitors.insert({name, createCompetitorWithMedals(medalComp)});
+ map<QString, int> emptyMedalObject = {
+ {"ME_GOLD", 0},
+ {"ME_SILVER", 0},
+ {"ME_BRONZE", 0}
+ };
+ competitors.insert({name, emptyMedalObject});
}
// update the medal count
- QJsonObject updatedMedalCount = QJsonObject(competitors.find(name)->second["medals"].toObject());
-
- int amount = updatedMedalCount[medalType].toInt() + 1;
- updatedMedalCount.remove(medalType);
- updatedMedalCount.insert(medalType, amount);
-
- // create new medals QJsonObject and set it in the map
- competitors.find(name)->second["medals"] = updatedMedalCount;
+ competitors.find(name)->second.find(medalType)->second++;
}
}
-
- // convert map to QJsonArray
- QList<MedalWinner*> output;
- for (const pair<QString, QJsonObject> &competitor : competitors) {
- MedalWinner *comp = new MedalWinner(); // TODO declare comp
- comp->setMedalWinner(competitor.second);
- output.append(comp);
- }
-
- return output;
-}
-
-/**
- * @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.
- */
-QJsonObject SportModel::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("m_noc")) comp.insert("code", "");
-
- // create new competitor QJsonObject and add it to the competitor map
- QJsonObject medals {
- {"ME_GOLD", 0},
- {"ME_SILVER", 0},
- {"ME_BRONZE", 0}
- };
-
- QJsonObject medalComp {
- {"code", comp["code"].toString()},
- {"name", comp["name"].toString()},
- {"m_noc", comp["m_noc"].toString()},
- {"medals", medals}
- };
-
- return medalComp;
+ return competitors;
}
/**
@@ -339,7 +274,7 @@ void SportModel::filterByCountry(QList<Competitor*> &competitors, QString nocSho
*/
void SportModel::filterCompetitors(QList<Competitor*> &competitors, QString filter) {
for (int i = 0; i < competitors.size(); i++) {
- if (!competitors.value(i)->getNOC().contains(filter)) {
+ if (!competitors.value(i)->noc().contains(filter)) {
competitors.remove(i);
i--;
}
@@ -350,37 +285,37 @@ void SportModel::filterCompetitors(QList<Competitor*> &competitors, QString filt
* @brief Sport::sortByName Sort the competitors by their name (alphabetical, ascending).
* @param competitors The competitors of one category.
*/
-void SportModel::sortByName(QList<Competitor*> &competitors) {
- if (competitors.isEmpty()) return;
- std::sort(competitors.begin(), competitors.end(), Competitor::compareName);
-}
+//void SportModel::sortByName(QList<Competitor*> &competitors) {
+// if (competitors.isEmpty()) return;
+// std::sort(competitors.begin(), competitors.end(), Competitor::compareName);
+//}
/**
* @brief Sport::sortByCountry Sort the competitors by their national olympic comittee short name (alphabetical, ascending).
* @param competitors The competitors of one category.
*/
-void SportModel::sortByCountry(QList<Competitor*> &competitors) {
- if (competitors.isEmpty()) return;
- std::sort(competitors.begin(), competitors.end(), Competitor::compareNOC);
-}
+//void SportModel::sortByCountry(QList<Competitor*> &competitors) {
+// if (competitors.isEmpty()) return;
+// std::sort(competitors.begin(), competitors.end(), Competitor::compareNOC);
+//}
/**
* @brief Sport::sortByResult Sort the competitors by their results in one specific category (numerical, ascending).
* @param competitors The competitors of one category.
*/
-void SportModel::sortByResult(QList<CompetitorWithResults*> &competitors) {
- if (competitors.isEmpty()) return;
- std::sort(competitors.begin(), competitors.end(), CompetitorWithResults::compare);
-}
+//void SportModel::sortByResult(QList<Competitor*> &competitors) {
+// if (competitors.isEmpty()) return;
+// std::sort(competitors.begin(), competitors.end(), Competitor::compareMark);
+//}
/**
* @brief Sport::sortByMedals Sort the competitors by their medal amounts in one specific category (numerical, ascending).
* @param competitors The competitors of one category.
*/
-void SportModel::sortByMedals(QList<MedalWinner*> &competitors) {
- if (competitors.isEmpty()) return;
- std::sort(competitors.begin(), competitors.end(), MedalWinner::compare);
-}
+//void SportModel::sortByMedals(QList<Competitor*> &competitors) {
+// if (competitors.isEmpty()) return;
+// std::sort(competitors.begin(), competitors.end(), Competitor::compareMedals);
+//}
/**
* @brief Sport::reverseOrder Reverses the order of the competitors.
@@ -403,15 +338,15 @@ void SportModel::reverseOrder(QList<Competitor*> &competitors) {
* Stores the m_statistic in obj->results->stat for each competitor.
* @param competitors The competitors of one category.
*/
-void SportModel::addRelativeToFirst(QList<CompetitorWithResults*> &competitors) {
+void SportModel::addRelativeToFirst(QList<Competitor*> &competitors) {
if (competitors.isEmpty()) return;
- QString reference = competitors.value(0)->getMark();
+ QString reference = competitors.value(0)->mark();
for (int i = 0; i < competitors.size(); i++) {
- CompetitorWithResults *comp = competitors.value(i);
+ Competitor *comp = competitors.value(i);
- QString result = comp->getMark();
+ QString result = comp->mark();
// format relative float value to string with 2 digits after decimal point and sign
stringstream sstream;