summaryrefslogtreecommitdiff
path: root/src/model/Competitor.cpp
blob: c0ee01dbba3f34fbb14fc9a9b2d6579232c0df8b (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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("noc")) {
        return false;
    }

    setCode(competitor["code"].toInt());
    setName(competitor["name"].toString());
    setNOC(competitor["noc"].toString());

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

/**
 * 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)));
}