Skip to content

Commit

Permalink
Add ChartTracked, give JSON response as lua table to skin
Browse files Browse the repository at this point in the history
  • Loading branch information
ereti committed Dec 13, 2020
1 parent e87fe05 commit f70a442
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 30 deletions.
1 change: 1 addition & 0 deletions Main/include/IR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace IR

cpr::AsyncResponse PostScore(ScoreIndex& score, BeatmapSettings& map);
cpr::AsyncResponse Heartbeat();
cpr::AsyncResponse ChartTracked(String chartHash);

bool ValidatePostScoreReturn(nlohmann::json& json);
}
4 changes: 4 additions & 0 deletions Main/include/SkinIR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "stdafx.h"
#include "Shared/Thread.hpp"
#include "LuaRequests.hpp"
#include "json.hpp"

//mostly based on SkinHttp, explained in .cpp

Expand All @@ -28,4 +29,7 @@ class SkinIR

void m_requestLoop();
void m_PushResponse(struct lua_State* L, const cpr::Response& r);
void m_PushJSON(struct lua_State* L, nlohmann::json& json);
void m_PushArray(struct lua_State* L, nlohmann::json& json);
void m_PushObject(struct lua_State* L, nlohmann::json& json);
};
8 changes: 8 additions & 0 deletions Main/src/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ namespace IR {
CommonHeader());
}

cpr::AsyncResponse ChartTracked(String chartHash)
{
String host = g_gameConfig.GetString(GameConfigKeys::IRBaseURL) + "/charts/" + chartHash;

return cpr::GetAsync(cpr::Url{host},
CommonHeader());
}

//note: this only confirms that the response is well-formed - responses other than 20 will not have most information, so this needs to be beared in mind too
//e.g., this function returning true does not mean that body will exist, because status may not be 20.
//it also does not validate each score's structure at this time - possible todo?
Expand Down
84 changes: 54 additions & 30 deletions Main/src/SkinIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@
//especially since that'd then require the skin to specify the authorization headers, json Content-Type, etc.
//this approach makes it easier for the skin creator

void SkinIR::m_PushArray(lua_State* L, nlohmann::json& json)
{
lua_newtable(L);
int index = 1;

for(auto& el : json.items())
{
lua_pushinteger(L, index++);
m_PushJSON(L, el.value());
lua_settable(L, -3);
}
}

void SkinIR::m_PushObject(lua_State* L, nlohmann::json& json)
{
lua_newtable(L);


for(auto& el : json.items())
{
lua_pushstring(L, el.key().c_str());
m_PushJSON(L, el.value());
lua_settable(L, -3);
}
}

void SkinIR::m_PushJSON(lua_State* L, nlohmann::json& json)
{
if(json.is_array()) m_PushArray(L, json);
else if(json.is_object()) m_PushObject(L, json);
else if(json.is_boolean()) lua_pushboolean(L, json);
else if(json.is_string()) lua_pushstring(L, json.get_ref<const std::string&>().c_str());
else if(json.is_number()) lua_pushnumber(L, json);
else if(json.is_null()) lua_pushnil(L);
}

void SkinIR::m_requestLoop()
{
while (m_running)
Expand Down Expand Up @@ -40,37 +76,16 @@ void SkinIR::m_requestLoop()
}
}

//TODO: change
void SkinIR::m_PushResponse(lua_State * L, const cpr::Response & r)
{
auto pushString = [L](String key, String value)
{
lua_pushstring(L, *key);
lua_pushstring(L, *value);
lua_settable(L, -3);
};
auto pushNumber = [L](String key, double value)
{
lua_pushstring(L, *key);
lua_pushnumber(L, value);
lua_settable(L, -3);
};

lua_newtable(L);
pushString("url", r.url);
pushString("text", r.text);
pushNumber("status", r.status_code);
pushNumber("elapsed", r.elapsed);
pushString("error", r.error.message.c_str());
pushString("cookies", r.cookies.GetEncoded().c_str());
lua_pushstring(L, "header");
lua_newtable(L);
for (auto& i : r.header)
{
pushString(i.first, i.second);
}
lua_settable(L, -3);
try {
nlohmann::json json = nlohmann::json::parse(r.text);

m_PushJSON(L, json);

} catch(nlohmann::json::parse_error& e) {
Log("Parsing JSON returned from IR failed.", Logger::Severity::Error);
}
}


Expand Down Expand Up @@ -114,8 +129,16 @@ int SkinIR::lHeartbeat(struct lua_State* L)

int SkinIR::lChartTracked(struct lua_State* L)
{
//todo
return 0;
String hash = luaL_checkstring(L, 2);
int callback = luaL_ref(L, LUA_REGISTRYINDEX);
AsyncRequest* r = new AsyncRequest();
r->r = IR::ChartTracked(hash);
r->callback = callback;
r->L = L;
m_mutex.lock();
m_requests.push(r);
m_mutex.unlock();
return 0;
}


Expand Down Expand Up @@ -151,6 +174,7 @@ void SkinIR::PushFunctions(lua_State * L)
{
auto bindable = new LuaBindable(L, "IR");
bindable->AddFunction("Heartbeat", this, &SkinIR::lHeartbeat);
bindable->AddFunction("ChartTracked", this, &SkinIR::lChartTracked);
bindable->Push();
lua_settop(L, 0);
m_boundStates.Add(L, bindable);
Expand Down

0 comments on commit f70a442

Please sign in to comment.