app/sources/provider/brprovidervl.cpp

431 lines
16 KiB
C++
Raw Permalink Normal View History

#include "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/";
2022-10-24 10:07:39 +02:00
QVariantMap ret = this->serverRequest(QUrl(requestUrl), {{"x-auth-token", "cc7375f680648e7e6171e035e70351eb"}, {"Referer", "https://ifsc.results.info/"}});
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());
2022-10-24 10:07:39 +02:00
QVariantMap ret = this->serverRequest(QUrl(requestUrl), {{"x-auth-token", "cc7375f680648e7e6171e035e70351eb"}, {"Referer", "https://ifsc.results.info/"}});
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
data->enabled = true;
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, rawData["d_cats"].toList(), 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, rawData["d_cats"].toList()));
data->cups.append(cup);
}
}
BRWidget::BRWidgetStatusCode BRProviderVl::getWidgetData(BRCompetition::BRCompetitionData* competitionData) {
if(competitionData->competition->getCurrentCategory() == nullptr)
2020-11-04 17:25:28 +01:00
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;
2020-11-04 17:25:28 +01:00
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";
}
2022-10-24 10:07:39 +02:00
QVariantMap ret = this->serverRequest(QUrl(requestUrl), {{"x-auth-token", "cc7375f680648e7e6171e035e70351eb"}, {"Referer", "https://ifsc.results.info/"}});
2020-11-04 17:25:28 +01:00
if(ret["status"] != 200){
// request was a failure
return BRWidget::BRWidgetStatusCode(ret["status"].toInt());
}
QVariantMap data = QJsonDocument::fromJson(ret["text"].toString().toUtf8()).toVariant().toMap();
// TODO: reload all rounds, as they may change!
// (load category instead of competition)
2020-11-04 17:25:28 +01:00
this->parseRoundData(&roundData, data);
this->setRoundData(currentRound, roundData);
return BRWidget::Success;
}
BRCompetition::BRCompetitionData BRProviderVl::parseCompetitionData(QVariantMap rawData, QVariantList globalCategoriesList, BRWidget::BRFederation federation) {
BRCompetition::BRCompetitionData data;
data.name = rawData["event"].toString();
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("https://ifsc.results.info" + rawData[infosheetName].toString());
}
}
QVariantList categoriesList = rawData["d_cats"].toList();
for(QVariant categoryVar : categoriesList) {
QVariantMap categoryMap = categoryVar.toMap();
// search category in global categories list
for(QVariant globalCategoryVar : globalCategoriesList) {
QVariantMap globalCategoryMap = globalCategoryVar.toMap();
if(categoryMap["id"].toInt() == globalCategoryMap["id"].toInt()) {
categoryMap.insert("discipline", globalCategoryMap["discipline"].toString());
break;
}
}
BRCategory* category = this->getCategory(federation, categoryMap["id"].toInt(), this->parseCategoryData(categoryMap));
data.categories.append(category);
}
2020-11-04 17:25:28 +01:00
data.currentCategory = nullptr;
return data;
}
BRCup::BRCupData BRProviderVl::parseCupData(QVariantMap rawData, QVariantList globalCategoriesList) {
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();
// search category in global categories list
for(QVariant globalCategoryVar : globalCategoriesList) {
QVariantMap globalCategoryMap = globalCategoryVar.toMap();
if(categoryId == globalCategoryMap["id"].toInt()) {
categoryMap.insert("discipline", globalCategoryMap["discipline"].toString());
break;
}
}
BRCategory* category = this->getCategory(BRWidget::IFSC, categoryId, this->parseCategoryData(categoryMap));
data.categories.append(category);
}
return data;
}
BRCategory::BRCategoryData BRProviderVl::parseCategoryData(QVariantMap rawData) {
BRCategory::BRCategoryData data;
data.name = rawData["name"].toString();
// TODO: gender
// parse status and discipline
QMap<QString, BRCategory::BRCategoryStatus> statusTranslations = {
{"registration_pending", BRCategory::Registration},
{"registration_active", BRCategory::Registration},
// TODO {2, BRCategory::Startlist},
{"active", BRCategory::Result},
{"finished", BRCategory::Result}
};
QMap<QString, BRCategory::BRDiscipline> disciplineTranslations = {
{"Boulder", BRCategory::Boulder},
{"Lead", BRCategory::Lead},
{"Speed", BRCategory::Speed}
};
if(disciplineTranslations.contains(rawData["discipline"].toString()))
data.discipline = disciplineTranslations[rawData["discipline"].toString()];
else
data.discipline = BRCategory::UnknownDiscipline;
if(statusTranslations.contains(rawData["status"].toString()))
data.status = statusTranslations[rawData["status"].toString()];
else
data.status = BRCategory::UnknownStatus;
qDebug() << "-- Parsing category: " << data.name << " with discipline: " << data.discipline << " and status: " << data.status;
data.currentRound = nullptr;
BRRound::BRRoundData roundData;
roundData.name = "General result";
qDebug() << "--- Adding round: " << roundData.name;
data.generalResult = this->getRound(BRWidget::IFSC, -1, roundData, true);
// load rounds
QVariantList roundsList = rawData["category_rounds"].toList();
// insert general result
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.generalResult;
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(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
qDebug() << "clearing";
QList<BRRound*> clearRounds;
clearRounds.append(roundData->round);
if(roundData->round->isGeneralResult()) {
// if we are parsing a general result -> clear all rounds
clearRounds.append(roundData->round->getCategory()->getRounds());
}
for(BRRound* round : clearRounds) {
for(BRResult* result : round->getResults())
result->deleteLater();
BRRound::BRRoundData roundData = round->getData();
roundData.results.clear();
this->setRoundData(round, roundData);
}
roundData->results.clear();
qDebug() << "finished clearing";
// parse Ranking
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;
// if this is the general result -> parse all other rounds as well
if(roundData->round->isGeneralResult()) {
qDebug() << "THIS is general result!!";
resultData.details = new BRResultDetailsGeneralResult();
// add results of other rounds
for(QVariant subRoundVar : resultMap["rounds"].toList()) {
QVariantMap subRoundMap = subRoundVar.toMap();
for(BRRound* subRound : roundData->round->getCategory()->getRounds(false)) {
if(subRound->getId() == subRoundMap["category_round_id"].toInt()) {
BRResult::BRResultData subResultData;
subResultData.rank = subRoundMap["rank"].toInt();
subResultData.athlete = athlete;
subResultData.details = new BRResultDetailsUnknown(subRoundMap["score"].toString());
qDebug() << "Adding subResult: rank: " << resultMap["rank"];
BRRound::BRRoundData subRoundData = subRound->getData();
subRoundData.results.append(this->getResult(subResultData));
this->setRoundData(subRound, subRoundData);
}
}
}
}
else
resultData.details = new BRResultDetailsUnknown(resultMap["score"].toString());
BRResult* result = this->getResult(resultData);
roundData->results.append(result);
}
}