forked from MrBesen/lolautoaccept
92 lines
2.2 KiB
C++
92 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include <condition_variable>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
#include <curl/curl.h>
|
|
#include <QJsonDocument>
|
|
#include <opencv2/opencv.hpp>
|
|
|
|
#include "datadragonimagecache.h"
|
|
#include "memoryimagecache.h"
|
|
|
|
class DataDragon {
|
|
public:
|
|
using notifyImgfunc_t = std::function<void(cv::Mat)>;
|
|
|
|
DataDragon(const std::string& locale);
|
|
~DataDragon();
|
|
DataDragon(const DataDragon&) = delete;
|
|
DataDragon& operator=(const DataDragon&) = delete;
|
|
|
|
struct ChampData {
|
|
public:
|
|
ChampData();
|
|
ChampData(const QJsonObject& source);
|
|
|
|
std::string name;
|
|
std::string id;
|
|
int key;
|
|
std::string partype;
|
|
std::string title;
|
|
};
|
|
|
|
enum class ImageType {
|
|
SQUARE = 0,
|
|
LOADING = 1,
|
|
SPLASH = 2
|
|
};
|
|
|
|
// might block until version is available
|
|
const std::string& getVersion();
|
|
// might block until champ data is available
|
|
const std::vector<ChampData>& getChamps();
|
|
// might block until image is downloaded
|
|
cv::Mat getImage(const std::string& champid, ImageType imgtype = ImageType::SQUARE);
|
|
void getImageAsnyc(const std::string& champid, notifyImgfunc_t func, ImageType imgtype = ImageType::SQUARE);
|
|
// might block until champ data is available
|
|
const ChampData& getBestMatchingChamp(const std::string& name, int* count = nullptr);
|
|
|
|
static const ChampData EMPTYCHAMP;
|
|
protected:
|
|
std::string getImageUrl(const std::string& champid, ImageType type);
|
|
std::string getCDNString() const;
|
|
QByteArray requestRaw(const std::string& url);
|
|
QJsonDocument request(const std::string& url);
|
|
|
|
void getVersionInternal();
|
|
void getChampsInternal();
|
|
void startThread();
|
|
void stopThread();
|
|
void stopAndJoinThread();
|
|
void threadLoop();
|
|
|
|
std::string locale;
|
|
std::string version;
|
|
std::vector<ChampData> champs;
|
|
std::mutex cachedatamutex;
|
|
std::condition_variable cachedatacv;
|
|
|
|
private:
|
|
struct Task {
|
|
std::string champid;
|
|
notifyImgfunc_t func;
|
|
ImageType type;
|
|
};
|
|
|
|
CURL* curl = nullptr; // the curl (does curling)
|
|
|
|
DataDragonImageCache cache[3];
|
|
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); |