summaryrefslogtreecommitdiff
path: root/src/model/Sport.h
blob: 01798fb88771f3f39d229639da8854baf9de7951 (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
#ifndef ITAT_CHALLANGE_OLYMPICS_SPORT_H
#define ITAT_CHALLANGE_OLYMPICS_SPORT_H

#include <QAbstractListModel>
#include <QNetworkAccessManager>
#include <qcontainerfwd.h>
#include <set>

#include <QJsonObject>
#include <QJsonDocument>
#include <QString>
#include "EventInfo.h"

using namespace std;

class SportModel : public QAbstractListModel {
  Q_OBJECT

  Q_PROPERTY(QString discipline READ discipline WRITE setDiscipline);

  public: 
    enum Role {
      EventName = Qt::UserRole + 1,
      Competitors
    };

    explicit SportModel(QObject *parent = nullptr);

    virtual int rowCount(const QModelIndex &parent) const override;
    virtual QVariant data(const QModelIndex &index, int role) const override;
    virtual QHash<int, QByteArray> roleNames() const override;


    QString discipline() const;
    void setDiscipline(const QString &discipline);
  public slots:
    void request(QString discipline);
    void parseData();

  private:
    QList<EventInfo*> m_sportList;
    QString m_discipline;
    QNetworkAccessManager m_networkManager;
    QNetworkReply *m_reply = nullptr;
};

class Sport {

public:

    Sport(QJsonObject discipline) {
        this->discipline = discipline;
    }

    set<QString> getCategories();
    QJsonArray getCompetitorsByCategory(QString category);
    QJsonArray getCompetitorsWithMedal();

    // filter to change the current competitor array
    void lastName(QJsonArray &competitors);
    void filterByName(QJsonArray &competitors, QString name);
    void filterByCountry(QJsonArray &competitors, QString nocShort);

    // sort functions to change the order of the current competitor array
    void sortByName(QJsonArray &competitors);
    void sortByCountry(QJsonArray &competitors);
    void sortByResult(QJsonArray &competitors);
    void reverseOrder(QJsonArray &competitors);

    // statistic function(s)
    void addRelativeToFirst(QJsonArray &competitors);

    void setDiscipline(QJsonObject discipline) {
        this->discipline = QJsonObject(discipline);
    }

private:

    /*
     * Analysis of provided competitor objects:
     *
     * Attributes:
     * - code
     * - noc (national olympics comittee)
     * - name (sometimes the country name? mostly the competitors name)
     * - order
     * [- results] (only if the results are out!)
     *
     *
     * Analysis of provided result objects:
     *
     * Attributes:
     * - position
     * - mark
     * - medalType
     * - irk
     * [- winnerLoserTie] (only if provided in the discipline?)
     *
     * Analysis of where to find the medal winners:
     *
     * Search for ... in category name.
     * - "Bronze"
     * - "Gold"
     * - "Final"
     *
     * ! ATTENTION !
     * When searching for "Final" there might be "Final A", "Final B", etc.
     * The results will only be in ONE of these categories!
     * -> which is good... cause then we can count occurences.
     */
    QJsonObject discipline;

    void filterCompetitors(QJsonArray &competitors, QString attribute, QString filter);
    void sortCompetitors(QJsonArray &competitors,  function<bool (const QJsonValue &left, const QJsonValue &right)> compare);

    bool validateDiscipline();
    QJsonObject createCompetitorWithMedals(QJsonObject medalComp);

    float calcRelativeStat(QString ref, QString val);

};


#endif