app/sources/brprovidervl.cpp

343 lines
12 KiB
C++

#include "headers/brprovidervl.h"
BRProviderVl::BRProviderVl(QObject *parent) : BRProvider(parent)
{
}
BRWidget::BRWidgetStatusCode BRProviderVl::getWidgetData(BRCalendar::BRCalendarData* calendarData)
{
// load some data
QString requestUrl = "https://ifsc.results.info/api/v1/";
QVariantMap ret = this->serverRequest(QUrl(requestUrl), {{"x-auth-token", "cc7375f680648e7e6171e035e70351eb"}});
if(ret["status"] != 200){
// request was a failure
return BRWidget::BRWidgetStatusCode(ret["status"].toInt());
}
QVariantMap data = QJsonDocument::fromJson(ret["text"].toString().toUtf8()).toVariant().toMap();
// create seasons
QVariantList seasons = data["seasons"].toList();
for(QVariant seasonVar : seasons) {
QVariantMap seasonMap = seasonVar.toMap();
int seasonId = seasonMap["name"].toInt();
BRSeason* season = this->getSeason(
calendarData->calendar->getFederation(),
seasonId,
this->parseSeasonData(seasonMap)
);
calendarData->seasons.append(season);
qDebug() << "Inserted season:" << season->getName();
if(seasonId == data["current"].toMap()["season"].toInt())
calendarData->currentSeason = season;
}
// load default league of current season
calendarData->currentSeason->getLeagues()[0]->load();
return BRWidget::Success;
}
BRWidget::BRWidgetStatusCode BRProviderVl::getWidgetData(BRSeason::BRSeasonData* seasonData) {
if(seasonData->season->getLeagues().length() > 0) {
// leagues are already loaded
// load default league
seasonData->season->getLeagues()[0]->load();
return BRWidget::Success;
}
else {
// should never happen
return BRWidget::NotImplementedError;
}
}
BRSeason::BRSeasonData BRProviderVl::parseSeasonData(QVariantMap rawData) {
BRSeason::BRSeasonData data;
data.year = rawData["name"].toInt();
data.name = rawData["name"].toString();
data.nativeMultiLeagueSupport = false;
// insert the leagues
for(QVariant leagueVar : rawData["leagues"].toList()) {
QVariantMap leagueMap = leagueVar.toMap();
BRLeague::BRLeagueData leagueData;
leagueData.name = leagueMap["name"].toString();
int leagueId = leagueMap["url"].toString().split("/").last().toInt();
BRLeague* league = this->getLeague(BRWidget::IFSC, leagueId, leagueData);
data.leagues.append(league);
}
return data;
}
BRWidget::BRWidgetStatusCode BRProviderVl::getWidgetData(BRLeague::BRLeagueData* leagueData) {
// load some data
QString requestUrl = "https://ifsc.results.info/api/v1/season_leagues/" + QString::number(leagueData->league->getId());
QVariantMap ret = this->serverRequest(QUrl(requestUrl), {{"x-auth-token", "cc7375f680648e7e6171e035e70351eb"}});
if(ret["status"] != 200){
// request was a failure
return BRWidget::BRWidgetStatusCode(ret["status"].toInt());
}
QVariantMap data = QJsonDocument::fromJson(ret["text"].toString().toUtf8()).toVariant().toMap();
this->parseLeagueData(leagueData, data);
return BRWidget::Success;
}
void BRProviderVl::parseLeagueData(BRLeague::BRLeagueData* data, QVariantMap rawData) {
BRWidget::BRFederation federation = BRWidget::BRFederation(rawData["federation"].toInt(0));
data->name = rawData["league"].toString();
data->color = QColor("#ffffff"); // TODO
qDebug() << "Adding league: " << data->name;
// parse competitions
QVariantList competitionsVar = rawData["events"].toList();
for(QVariant competitionVar : competitionsVar) {
QVariantMap competitionMap = competitionVar.toMap();
qDebug() << "- Adding competition: " << competitionMap["event"].toString();
BRCompetition* competition = this->getCompetition(federation, competitionVar.toMap()["event_id"].toInt(), this->parseCompetitionData(competitionMap, federation));
data->competitions.append(competition);
}
// parse cups
QVariantList cupsVar = rawData["cups"].toList();
for(QVariant cupVar : cupsVar) {
QVariantMap cupMap = cupVar.toMap();
BRCup* cup = this->getCup(federation, cupMap["cup_id"].toInt(), this->parseCupData(cupMap));
data->cups.append(cup);
}
}
BRWidget::BRWidgetStatusCode BRProviderVl::getWidgetData(BRCompetition::BRCompetitionData* competitionData) {
if(competitionData->competition->getCurrentCategory() == nullptr)
return BRWidget::OpeationNotSupportedError;
BRCategory* currentCategory = competitionData->competition->getCurrentCategory();
if(currentCategory->getCurrentRound() == nullptr)
return BRWidget::OpeationNotSupportedError;
BRRound* currentRound = currentCategory->getCurrentRound();
BRRound::BRRoundData roundData = currentRound->getData();
qDebug() << "LOADING ROUND DATA";
// load round data
QString competitionId = QString::number(roundData.round->getCategory()->getCompetition()->getId());
QString categoryId = QString::number(roundData.round->getCategory()->getId());
QString roundId = QString::number(roundData.round->getId());
QString requestUrl;
if(roundId == "-1") {
requestUrl = "https://ifsc.results.info/api/v1/events/" + competitionId + "/result/" + categoryId;
}
else {
requestUrl = "https://ifsc.results.info/api/v1/category_rounds/" + roundId + "/results";
}
QVariantMap ret = this->serverRequest(QUrl(requestUrl), {{"x-auth-token", "cc7375f680648e7e6171e035e70351eb"}});
if(ret["status"] != 200){
// request was a failure
return BRWidget::BRWidgetStatusCode(ret["status"].toInt());
}
QVariantMap data = QJsonDocument::fromJson(ret["text"].toString().toUtf8()).toVariant().toMap();
this->parseRoundData(&roundData, data);
this->setRoundData(currentRound, roundData);
return BRWidget::Success;
}
BRCompetition::BRCompetitionData BRProviderVl::parseCompetitionData(QVariantMap rawData, BRWidget::BRFederation federation) {
QMap<QString, BRCompetition::BRDiscipline> disciplineTranslations = {
{"boulder", BRCompetition::Boulder},
{"lead", BRCompetition::Lead},
{"speed", BRCompetition::Speed}
};
BRCompetition::BRCompetitionData data;
data.name = rawData["event"].toString();
data.discipline = BRCompetition::Unknown;
data.startDate = QDate::fromString(rawData["starts_at"].toString().split(" ")[0], "yyyy-MM-dd");
data.endDate = QDate::fromString(rawData["ends_at"].toString().split(" ")[0], "yyyy-MM-dd");
data.eventWebsiteUrl = "";
for(int i = 0; i < 2; i++) {
QString infosheetName = QStringList({"infosheet_url", "additional_info_url"})[i];
if(rawData.contains(infosheetName) && !rawData[infosheetName].isNull()) {
data.infosheetUrls.append(rawData[infosheetName].toString());
}
}
QVariantList categoriesList = rawData["d_cats"].toList();
for(QVariant categoryVar : categoriesList) {
QVariantMap categoryMap = categoryVar.toMap();
qDebug() << "-- Adding category: " << categoryMap["name"].toString();
BRCategory* category = this->getCategory(federation, categoryMap["id"].toInt(), this->parseCategoryData(categoryMap));
data.categories.append(category);
}
data.currentCategory = nullptr;
return data;
}
BRCup::BRCupData BRProviderVl::parseCupData(QVariantMap rawData) {
BRCup::BRCupData data;
data.name = rawData["cup"].toString();
// parse categories
for(QVariant categoryVar : rawData["d_cats"].toList()) {
// find category name in cats:
QVariantMap categoryMap = categoryVar.toMap();
int categoryId = categoryMap["ranking_url"].toString().split("/").last().toInt();
BRCategory* category = this->getCategory(BRWidget::IFSC, categoryId, this->parseCategoryData(categoryMap));
data.categories.append(category);
}
return data;
}
BRCategory::BRCategoryData BRProviderVl::parseCategoryData(QVariantMap rawData) {
// TODO: status
// TODO: sex
BRCategory::BRCategoryData data;
data.name = rawData["name"].toString();
data.currentRound = nullptr;
// load rounds
QVariantList roundsList = rawData["category_rounds"].toList();
// insert general result
roundsList.insert(0, QVariantMap({{"name", "General result"}, {"category_round_id", -1}}));
for(QVariant roundVar : roundsList) {
QVariantMap roundMap = roundVar.toMap();
BRRound::BRRoundData roundData;
roundData.name = roundMap["name"].toString();
qDebug() << "--- Adding round: " << roundData.name;
BRRound* round = this->getRound(BRWidget::IFSC, roundMap["category_round_id"].toInt(), roundData);
data.rounds.append(round);
}
data.currentRound = data.rounds[0];
return data;
}
void BRProviderVl::parseCategoryData(BRCategory::BRCategoryData* categoryData, QVariantMap rawData) {
// no need to load rounds, they were already loaded when the whole competition was loaded!
qDebug() << "INITIALLY PARSING CATEGORY";
// load athletes
QVariantList atheletList = rawData["ranking"].toList();
QMap<int, BRAthlete*> tmpAthletes;
// load general result
BRRound::BRRoundData roundData;
// TODO status
roundData.name = tr("General result");
// parse Ranking
QVariantList resultList = rawData["ranking"].toList();
for(QVariant resultVar : resultList) {
QVariantMap resultMap = resultVar.toMap();
int athleteId = resultMap["athlete_id"].toInt();
if(!tmpAthletes.contains(athleteId))
continue;
qDebug() << "Adding result: rank: " << resultMap["rank"];
BRResult::BRResultData resultData;
resultData.rank = resultMap["rank"].toInt();
resultData.athlete = tmpAthletes[athleteId];
BRResult* result = this->getResult(BRWidget::IFSC, -1, resultData);
roundData.results.append(result);
}
BRRound* generalResultRound = this->getRound(BRWidget::IFSC, -1, roundData);
qDebug() << "General result round: " << generalResultRound;
categoryData->rounds.insert(0, generalResultRound);
categoryData->currentRound = generalResultRound;
}
BRRound::BRRoundData BRProviderVl::parseRoundData(QVariantMap rawData) {
BRRound::BRRoundData data;
this->parseRoundData(&data, rawData);
return data;
}
void BRProviderVl::parseRoundData(BRRound::BRRoundData* roundData, QVariantMap rawData) {
// TODO status
// clear up
for(BRResult* result : roundData->results)
result->deleteLater();
roundData->results.clear();
// parse RankingQVariantList resultList = rawData["ranking"].toList();
QVariantList resultList = rawData["ranking"].toList();
for(QVariant resultVar : resultList) {
QVariantMap resultMap = resultVar.toMap();
// load athlete
BRAthlete::BRAthleteData athleteData;
// TODO: start number
athleteData.firstName = resultMap["firstname"].toString();
athleteData.lastName = resultMap["lastname"].toString();
athleteData.federation = resultMap["country"].toString();
athleteData.nation = resultMap["country"].toString();
qDebug() << "--- Adding athlete: " << athleteData.firstName << " " << athleteData.lastName;
BRAthlete* athlete = this->getAthlete(BRWidget::IFSC, resultMap["athlete_id"].toInt(), athleteData);
// load result
qDebug() << "Adding result: rank: " << resultMap["rank"];
BRResult::BRResultData resultData;
resultData.rank = resultMap["rank"].toInt();
resultData.athlete = athlete;
BRResult* result = this->getResult(BRWidget::IFSC, -1, resultData);
roundData->results.append(result);
}
}