summaryrefslogtreecommitdiff
path: root/src/model/CompetitorWithResults.cpp
blob: 70d9473ea1f2707ea4903869788d8e59898e6321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#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("m_mark")
        || !results.contains("m_medalType")) {
        return false;
    }

    this->m_mark = results["m_mark"].toString();
    this->m_medalType = results["m_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;
}

bool CompetitorWithResults::setCompetitorWithResults(const QJsonObject &competitor) {
    setCompetitor(competitor);

    if (!competitor.contains("results")) return false;
    QJsonObject results = competitor["results"].toObject();
    return setResults(results);
}

void CompetitorWithResults::setCompetitorWithResults(const CompetitorWithResults &competitor) {
    setCompetitor(competitor);
    setMark(competitor.m_mark);
    setMedalType(competitor.m_medalType);
    setStatistic(competitor.m_statistic);
}