lolautoaccept/include/datadragon.h

114 lines
2.7 KiB
C
Raw Permalink Normal View History

2022-04-21 23:42:53 +02:00
#pragma once
2022-04-24 12:15:58 +02:00
#include <condition_variable>
#include <functional>
#include <mutex>
2022-07-05 19:39:16 +02:00
#include <set>
2023-05-31 22:22:23 +02:00
#include <QString>
2022-04-21 23:42:53 +02:00
#include <vector>
2022-06-27 20:45:01 +02:00
2022-07-02 17:44:29 +02:00
#include <QPixmap>
2022-04-21 23:42:53 +02:00
2022-04-23 20:33:21 +02:00
#include "datadragonimagecache.h"
2022-07-05 23:45:28 +02:00
#include "champcache.h"
2022-04-23 22:28:01 +02:00
#include "memoryimagecache.h"
2022-06-27 20:45:01 +02:00
#include "restclient.h"
2022-04-23 20:33:21 +02:00
class QThread;
2022-06-27 20:45:01 +02:00
class DataDragon : public RestClient {
Q_OBJECT
2022-04-21 23:42:53 +02:00
public:
2022-07-02 17:44:29 +02:00
using notifyImgfunc_t = std::function<void(QPixmap)>;
2022-04-24 12:15:58 +02:00
2023-05-31 22:22:23 +02:00
DataDragon(const QString& locale);
2022-04-24 12:15:58 +02:00
~DataDragon();
2022-04-21 23:42:53 +02:00
DataDragon(const DataDragon&) = delete;
DataDragon& operator=(const DataDragon&) = delete;
struct ChampData {
public:
2022-04-23 00:39:45 +02:00
ChampData();
2022-04-21 23:42:53 +02:00
ChampData(const QJsonObject& source);
2023-05-31 22:22:23 +02:00
QString name;
QString id;
2022-07-04 22:59:48 +02:00
int key = 0;
2023-05-31 22:22:23 +02:00
QString partype;
QString title;
2022-04-21 23:42:53 +02:00
};
2022-04-22 00:26:10 +02:00
enum class ImageType {
2022-04-23 20:33:21 +02:00
SQUARE = 0,
LOADING = 1,
SPLASH = 2
2022-04-22 00:26:10 +02:00
};
2022-04-24 12:15:58 +02:00
// might block until version is available
2023-05-31 22:22:23 +02:00
const QString& getVersion();
2022-04-24 12:15:58 +02:00
// might block until champ data is available
2022-04-21 23:42:53 +02:00
const std::vector<ChampData>& getChamps();
2022-04-24 12:15:58 +02:00
// might block until image is downloaded
2023-05-31 22:22:23 +02:00
QPixmap getImage(const QString& champid, ImageType imgtype = ImageType::SQUARE, bool writeMemcache = true);
void getImageAsnyc(const QString& champid, notifyImgfunc_t func, ImageType imgtype = ImageType::SQUARE);
2022-04-24 12:15:58 +02:00
// might block until champ data is available
2023-05-31 22:22:23 +02:00
const ChampData& getBestMatchingChamp(const QString& name, int* count = nullptr);
std::vector<const ChampData*> getMatchingChamp(const QString& name, uint32_t limit = 25);
2022-07-10 15:19:25 +02:00
const ChampData* getChampByID(uint32_t id);
2022-04-21 23:42:53 +02:00
2023-05-31 22:22:23 +02:00
std::vector<uint32_t> resolveChampIDs(const std::vector<QString>& champnames);
2022-07-04 22:59:48 +02:00
void startThread();
2022-04-23 00:39:45 +02:00
static const ChampData EMPTYCHAMP;
public slots:
void stop();
signals:
// loading progress in 0.0 - 1.0
void loading(float);
// which champion is currently prefretched
void fetchingChamp(QString);
2022-04-21 23:42:53 +02:00
protected:
2023-05-31 22:22:23 +02:00
QString getImageUrl(const QString& champid, ImageType type);
QString getCDNString() const;
2022-04-21 23:42:53 +02:00
2023-05-31 22:22:23 +02:00
void prefetchChampImage(const QString& champid, ImageType imgtype = ImageType::SQUARE);
2022-04-24 12:15:58 +02:00
void getVersionInternal();
void getChampsInternal();
void stopThread();
void stopAndJoinThread();
void threadLoop();
2023-05-31 22:22:23 +02:00
QString locale;
QString version;
2022-04-21 23:42:53 +02:00
std::vector<ChampData> champs;
2022-04-24 12:15:58 +02:00
std::mutex cachedatamutex;
std::condition_variable cachedatacv;
2023-05-31 22:22:23 +02:00
std::set<QString> notDownloadedImages; // the champions of which the square image is not downloaded yet. Is used to download them on idle
2022-04-21 23:42:53 +02:00
private:
2022-04-24 12:15:58 +02:00
struct Task {
2023-05-31 22:22:23 +02:00
QString champid;
2022-04-24 12:15:58 +02:00
notifyImgfunc_t func;
ImageType type;
};
2022-04-23 20:33:21 +02:00
DataDragonImageCache cache[3];
2022-07-05 23:45:28 +02:00
ChampCache champCache;
2022-04-23 22:28:01 +02:00
MemoryImageCache memcache;
2022-04-24 12:15:58 +02:00
std::list<Task> tasks;
std::mutex tasksmutex;
std::condition_variable tasksnotemptycv;
QThread* bgthread;
2022-04-24 12:15:58 +02:00
bool shouldrun = true;
2022-04-21 23:42:53 +02:00
};
2023-05-01 01:23:08 +02:00
std::ostream& operator<<(std::ostream& str, const DataDragon::ChampData& cd);