summaryrefslogtreecommitdiff
path: root/src/model/MedalWinner.cpp
blob: 56cfb228a3de8b7ffad22744be8024033e3d6017 (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

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

    setGold(medals["ME_GOLD"].toInt());
    setSilver(medals["ME_SILVER"].toInt());
    setBronze(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 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 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)));
}

bool MedalWinner::setMedalWinner(const QJsonObject &competitor) {
    setCompetitor(competitor);

    if (!competitor.contains("medals")) return false;
    QJsonObject medals = competitor["medals"].toObject();
    setMedals(medals);
    return true;
}

void MedalWinner::setMedalWinner(const MedalWinner &competitor) {
    setCompetitor(competitor);

    setGold(competitor.m_gold);
    setSilver(competitor.m_silver);
    setBronze(competitor.m_bronze);
}