lolautoaccept/include/datadragon.h

99 lines
2.6 KiB
C
Raw 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>
2022-04-21 23:42:53 +02:00
#include <string>
2022-04-24 12:15:58 +02:00
#include <thread>
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
2022-06-27 20:45:01 +02:00
class DataDragon : public RestClient {
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
2022-04-24 13:09:28 +02:00
DataDragon(const std::string& 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);
std::string name;
std::string id;
2022-07-04 22:59:48 +02:00
int key = 0;
2022-04-21 23:42:53 +02:00
std::string partype;
std::string title;
};
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
2022-04-21 23:42:53 +02:00
const std::string& 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
2022-07-05 19:39:16 +02:00
QPixmap getImage(const std::string& champid, ImageType imgtype = ImageType::SQUARE, bool writeMemcache = true);
2022-04-24 12:15:58 +02:00
void getImageAsnyc(const std::string& champid, notifyImgfunc_t func, ImageType imgtype = ImageType::SQUARE);
// might block until champ data is available
2022-04-23 01:53:19 +02:00
const ChampData& getBestMatchingChamp(const std::string& name, int* count = nullptr);
std::vector<const ChampData*> getMatchingChamp(const std::string& 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
2022-07-04 22:59:48 +02:00
std::vector<uint32_t> resolveChampIDs(const std::vector<std::string>& champnames);
2022-04-23 00:39:45 +02:00
static const ChampData EMPTYCHAMP;
2022-04-21 23:42:53 +02:00
protected:
2022-04-24 12:15:58 +02:00
std::string getImageUrl(const std::string& champid, ImageType type);
2022-04-22 00:26:10 +02:00
std::string getCDNString() const;
2022-04-21 23:42:53 +02:00
2022-07-05 19:39:16 +02:00
void prefetchChampImage(const std::string& champid, ImageType imgtype = ImageType::SQUARE);
2022-04-24 12:15:58 +02:00
void getVersionInternal();
void getChampsInternal();
void startThread();
void stopThread();
void stopAndJoinThread();
void threadLoop();
2022-04-24 13:09:28 +02:00
std::string locale;
2022-04-21 23:42:53 +02:00
std::string version;
std::vector<ChampData> champs;
2022-04-24 12:15:58 +02:00
std::mutex cachedatamutex;
std::condition_variable cachedatacv;
2022-07-05 19:39:16 +02:00
std::set<std::string> 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 {
std::string champid;
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;
std::thread bgthread;
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);