76 lines
2.9 KiB
C++
76 lines
2.9 KiB
C++
#include <stdinc.hpp>
|
|
#include "loader/component_loader.hpp"
|
|
#include "gsc.hpp"
|
|
#include "game/scripting/array.hpp"
|
|
#include <utils/telemetry.hpp>
|
|
|
|
namespace telemetry
|
|
{
|
|
namespace
|
|
{
|
|
utils::telemetry::outbox spool(const std::string& server, const std::string& endpoint)
|
|
{
|
|
return {"tws-telemetry/t6", server, endpoint};
|
|
}
|
|
|
|
void storage_error()
|
|
{
|
|
// Do not echo parse exceptions: their text can contain player data.
|
|
printf("^1[tws_telemetry] Outbox operation failed; check tws-telemetry/t6 files, disk space and permissions. Existing reports retained.\n");
|
|
}
|
|
}
|
|
|
|
class component final : public component_interface
|
|
{
|
|
public:
|
|
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
|
{
|
|
gsc::function::add("telemetrynewmatch", []() -> scripting::script_value
|
|
{
|
|
try { return utils::telemetry::new_match_key(); }
|
|
catch (...) { return {}; }
|
|
});
|
|
gsc::function::add("telemetryload", [](const std::string& server, const std::string& endpoint) -> scripting::script_value
|
|
{
|
|
try
|
|
{
|
|
scripting::array entries;
|
|
for (const auto& value : spool(server, endpoint).load())
|
|
{
|
|
scripting::array entry;
|
|
entry["match"] = value.match;
|
|
entry["id"] = value.seq;
|
|
entry["event"] = value.event;
|
|
entry["round"] = value.round;
|
|
entry["body"] = value.body;
|
|
entry["stored"] = true;
|
|
entries.emplace_back(entry);
|
|
}
|
|
return entries;
|
|
}
|
|
catch (...) { storage_error(); return {}; }
|
|
});
|
|
gsc::function::add("telemetrystore", [](const std::string& server, const std::string& endpoint, const std::string& body)
|
|
{
|
|
try { spool(server, endpoint).store(body); return true; }
|
|
catch (...) { storage_error(); return false; }
|
|
});
|
|
gsc::function::add("telemetryremove", [](const std::string& server, const std::string& endpoint, const std::string& match, const int seq)
|
|
{
|
|
try { spool(server, endpoint).erase(match, seq); return true; }
|
|
catch (...) { storage_error(); return false; }
|
|
});
|
|
gsc::function::add("telemetryresponse", [](const std::string& response)
|
|
{
|
|
const auto ack = utils::telemetry::parse_acknowledgement(response);
|
|
scripting::array result;
|
|
result["accepted"] = ack.accepted;
|
|
result["match_id"] = ack.match_id;
|
|
return result;
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
REGISTER_COMPONENT(telemetry::component)
|