Permalink
Browse files

get all mod stats implemented on c

  • Loading branch information...
Turupawn committed Aug 11, 2018
1 parent bdcaa03 commit 58d4ccacc29cf66a4f69ba055fb559abe6cbeb31
View
@@ -71,6 +71,7 @@
"string_view": "cpp",
"*.rh": "cpp",
"cstdarg": "cpp",
"deque": "cpp"
"deque": "cpp",
"cinttypes": "cpp"
}
}
@@ -6,7 +6,7 @@ void onGetMods(void *object, ModioResponse response, ModioMod *mods, u32 mods_si
printf("Get mods response: %i\n", response.code);
if (response.code == 200)
{
printf("Listing mod\n");
printf("Listing mods\n");
printf("============\n");
for (u32 i = 0; i < mods_size; i++)
{
@@ -0,0 +1,57 @@
#include "modio_c.h"
void onGetAllModStats(void *object, ModioResponse response, ModioModStats *mods_stats, u32 mods_stats_size)
{
bool *wait = object;
printf("Get mods response: %i\n", response.code);
if (response.code == 200)
{
printf("Listing mods stats\n");
printf("==================\n");
for (u32 i = 0; i < mods_stats_size; i++)
{
printf("Mod[%i]\n", i);
printf("Mod id: %i\n", mods_stats[i].mod_id);
printf("Popularity rank position: %i\n", mods_stats[i].popularity_rank_position);
printf("Popularity rank total mods: %i\n", mods_stats[i].popularity_rank_total_mods);
printf("Downloads total: %i\n", mods_stats[i].downloads_total);
printf("Subscribers total: %i\n", mods_stats[i].subscribers_total);
printf("Ratings total: %i\n", mods_stats[i].ratings_total);
printf("Ratings positive: %i\n", mods_stats[i].ratings_positive);
printf("Ratings negative: %i\n", mods_stats[i].ratings_negative);
printf("Ratings percentage positive: %i\n", mods_stats[i].ratings_percentage_positive);
printf("Ratings weighted aggregate: %f\n", mods_stats[i].ratings_weighted_aggregate);
printf("Ratings display text: %s\n", mods_stats[i].ratings_display_text);
printf("Date expires: %i\n", mods_stats[i].date_expires);
}
}
*wait = false;
}
int main(void)
{
modioInit(MODIO_ENVIRONMENT_TEST, 7, (char *)"e91c01b8882f4affeddd56c96111977b", NULL);
bool wait = true;
// Before requesting mods, let's define the query filters
ModioFilterCreator filter;
modioInitFilter(&filter);
modioSetFilterLimit(&filter, 3);
printf("Getting mods stats...\n");
// Now we finished setting up the filters we are ready to request the mods
modioGetAllModStats(&wait, filter, &onGetAllModStats);
while (wait)
{
modioProcess();
}
modioShutdown();
printf("Process finished\n");
return 0;
}
@@ -632,4 +632,5 @@ void modioDeleteModComment(void* object, u32 mod_id, u32 comment_id, void(*callb
void modioSubmitReport(void* object, char* resource, u32 id, u32 type, char* name, char* summary, void(*callback)(void* object, ModioResponse response));
//Stats Methods
void modioGetModStats(void* object, u32 mod_id, void (*callback)(void* object, ModioResponse response, ModioModStats mod));
void modioGetModStats(void* object, u32 mod_id, void (*callback)(void* object, ModioResponse response, ModioModStats mod));
void modioGetAllModStats(void* object, ModioFilterCreator filter, void (*callback)(void* object, ModioResponse response, ModioModStats mods_stats[], u32 mods_stats_size));
View
@@ -633,6 +633,7 @@ extern "C"
//Stats Methods
void MODIO_DLL modioGetModStats(void* object, u32 mod_id, void (*callback)(void* object, ModioResponse response, ModioModStats mod));
void MODIO_DLL modioGetAllModStats(void* object, ModioFilterCreator filter, void (*callback)(void* object, ModioResponse response, ModioModStats mods_stats[], u32 mods_stats_size));
}
#endif
@@ -14,8 +14,18 @@ struct GetModStatsParams
void (*callback)(void* object, ModioResponse response, ModioModStats mod_stats);
};
struct GetAllModStatsParams
{
void* object;
std::string url;
bool is_cache;
void (*callback)(void* object, ModioResponse response, ModioModStats mods_stats[], u32 mods_stats_size);
};
extern std::map< u32,GetModStatsParams* > get_mod_stats_callbacks;
extern std::map< u32,GetAllModStatsParams* > get_all_mod_stats_callbacks;
void modioOnGetModStats(u32 call_number, u32 response_code, json response_json);
void modioOnGetAllModStats(u32 call_number, u32 response_code, json response_json);
#endif
@@ -14,4 +14,32 @@ extern "C"
modio::curlwrapper::get(call_number, url, modio::getHeaders(), &modioOnGetModStats);
}
void modioGetAllModStats(void* object, ModioFilterCreator filter, void (*callback)(void* object, ModioResponse response, ModioModStats mods_stats[], u32 mods_stats_size))
{
std::string filter_string = modio::getFilterString(&filter);
std::string url = modio::MODIO_URL + modio::MODIO_VERSION_PATH + "games/" + modio::toString(modio::GAME_ID) + "/mods/stats?" + filter_string + "&api_key=" + modio::API_KEY;
u32 call_number = modio::curlwrapper::getCallNumber();
get_all_mod_stats_callbacks[call_number] = new GetAllModStatsParams;
get_all_mod_stats_callbacks[call_number]->callback = callback;
get_all_mod_stats_callbacks[call_number]->object = object;
get_all_mod_stats_callbacks[call_number]->url = url;
get_all_mod_stats_callbacks[call_number]->is_cache = false;
std::string cache_filename = modio::getCallFileFromCache(url, filter.cache_max_age_seconds);
if(cache_filename != "")
{
json cache_file_json = modio::openJson(modio::getModIODirectory() + "cache/" + cache_filename);
json empty_json = {};
if(!cache_file_json.empty())
{
get_all_mod_stats_callbacks[call_number]->is_cache = true;
modioOnGetAllModStats(call_number, 200, cache_file_json);
return;
}
}
modio::curlwrapper::get(call_number, url, modio::getHeaders(), &modioOnGetAllModStats);
}
}
@@ -1,6 +1,7 @@
#include "c/methods/callbacks/ModStatsCallbacks.h"
std::map< u32,GetModStatsParams* > get_mod_stats_callbacks;
std::map< u32,GetAllModStatsParams* > get_all_mod_stats_callbacks;
void modioOnGetModStats(u32 call_number, u32 response_code, json response_json)
{
@@ -19,4 +20,39 @@ void modioOnGetModStats(u32 call_number, u32 response_code, json response_json)
modioFreeResponse(&response);
modioFreeModStats(&mod_stats);
}
void modioOnGetAllModStats(u32 call_number, u32 response_code, json response_json)
{
ModioResponse response;
modioInitResponse(&response, response_json);
response.code = response_code;
if(response.code == 200)
{
if(!get_all_mod_stats_callbacks[call_number]->is_cache)
modio::addCallToCache(get_all_mod_stats_callbacks[call_number]->url, response_json);
u32 mods_stats_size = (u32)response_json["data"].size();
ModioModStats* mods_stats = new ModioModStats[mods_stats_size];
for(u32 i=0; i<mods_stats_size; i++)
{
modioInitModStats(&mods_stats[i], response_json["data"][i]);
}
get_all_mod_stats_callbacks[call_number]->callback(get_all_mod_stats_callbacks[call_number]->object, response, mods_stats, mods_stats_size);
for(u32 i=0; i<mods_stats_size; i++)
{
modioFreeModStats(&mods_stats[i]);
}
delete[] mods_stats;
}else
{
get_all_mod_stats_callbacks[call_number]->callback(get_all_mod_stats_callbacks[call_number]->object, response, NULL, 0);
}
delete get_all_mod_stats_callbacks[call_number];
get_all_mod_stats_callbacks.erase(call_number);
modioFreeResponse(&response);
}

0 comments on commit 58d4cca

Please sign in to comment.