forked from MrBesen/lolautoaccept
99 lines
2.5 KiB
C++
99 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <condition_variable>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <set>
|
|
#include <QString>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include "datadragonimagecache.h"
|
|
#include "champcache.h"
|
|
#include "memoryimagecache.h"
|
|
#include "restclient.h"
|
|
|
|
class DataDragon : public RestClient {
|
|
public:
|
|
using notifyImgfunc_t = std::function<void(QPixmap)>;
|
|
|
|
DataDragon(const QString& locale);
|
|
~DataDragon();
|
|
DataDragon(const DataDragon&) = delete;
|
|
DataDragon& operator=(const DataDragon&) = delete;
|
|
|
|
struct ChampData {
|
|
public:
|
|
ChampData();
|
|
ChampData(const QJsonObject& source);
|
|
|
|
QString name;
|
|
QString id;
|
|
int key = 0;
|
|
QString partype;
|
|
QString title;
|
|
};
|
|
|
|
enum class ImageType {
|
|
SQUARE = 0,
|
|
LOADING = 1,
|
|
SPLASH = 2
|
|
};
|
|
|
|
// might block until version is available
|
|
const QString& getVersion();
|
|
// might block until champ data is available
|
|
const std::vector<ChampData>& getChamps();
|
|
// might block until image is downloaded
|
|
QPixmap getImage(const QString& champid, ImageType imgtype = ImageType::SQUARE, bool writeMemcache = true);
|
|
void getImageAsnyc(const QString& champid, notifyImgfunc_t func, ImageType imgtype = ImageType::SQUARE);
|
|
// might block until champ data is available
|
|
const ChampData& getBestMatchingChamp(const QString& name, int* count = nullptr);
|
|
std::vector<const ChampData*> getMatchingChamp(const QString& name, uint32_t limit = 25);
|
|
const ChampData* getChampByID(uint32_t id);
|
|
|
|
std::vector<uint32_t> resolveChampIDs(const std::vector<QString>& champnames);
|
|
|
|
static const ChampData EMPTYCHAMP;
|
|
protected:
|
|
QString getImageUrl(const QString& champid, ImageType type);
|
|
QString getCDNString() const;
|
|
|
|
void prefetchChampImage(const QString& champid, ImageType imgtype = ImageType::SQUARE);
|
|
void getVersionInternal();
|
|
void getChampsInternal();
|
|
void startThread();
|
|
void stopThread();
|
|
void stopAndJoinThread();
|
|
void threadLoop();
|
|
|
|
QString locale;
|
|
QString version;
|
|
std::vector<ChampData> champs;
|
|
std::mutex cachedatamutex;
|
|
std::condition_variable cachedatacv;
|
|
std::set<QString> notDownloadedImages; // the champions of which the square image is not downloaded yet. Is used to download them on idle
|
|
|
|
private:
|
|
struct Task {
|
|
QString champid;
|
|
notifyImgfunc_t func;
|
|
ImageType type;
|
|
};
|
|
|
|
|
|
DataDragonImageCache cache[3];
|
|
ChampCache champCache;
|
|
MemoryImageCache memcache;
|
|
std::list<Task> tasks;
|
|
std::mutex tasksmutex;
|
|
std::condition_variable tasksnotemptycv;
|
|
std::thread bgthread;
|
|
bool shouldrun = true;
|
|
};
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& str, const DataDragon::ChampData& cd);
|