Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gw2bot integration #30

Merged
merged 3 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 101 additions & 6 deletions arcdps_uploader/Uploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ using json = nlohmann::json;

const char* INI_SECTION_SETTINGS = "Settings";
const char* INI_WVW_DETAILED_SETTING = "WvW_Detailed";
const char* INI_GW2BOT_ENABLED = "GW2Bot_Enabled";
const char* INI_GW2BOT_KEY = "GW2Bot_Key";
const char* INI_GW2BOT_SUCCESS_ONLY = "GW2Bot_Success_Only";

inline auto initStorage(const std::string& path)
{
Expand Down Expand Up @@ -56,7 +59,7 @@ static std::unique_ptr<Storage> storage;
Uploader::Uploader(fs::path data_path, std::optional<fs::path> custom_log_path)
: is_open(false)
, in_combat(false)
, wvw_detailed_enabled(false)
, settings{false, false, "", false}
, ini_enabled(true)
{
//INI
Expand All @@ -65,7 +68,11 @@ Uploader::Uploader(fs::path data_path, std::optional<fs::path> custom_log_path)
SI_Error error = ini.LoadFile(ini_path.string().c_str());
if (error == SI_OK) {
LOG_F(INFO, "Loaded INI file");
wvw_detailed_enabled = ini.GetBoolValue(INI_SECTION_SETTINGS, INI_WVW_DETAILED_SETTING, false);
settings.wvw_detailed_enabled = ini.GetBoolValue(INI_SECTION_SETTINGS, INI_WVW_DETAILED_SETTING, false);
settings.gw2bot_enabled = ini.GetBoolValue(INI_SECTION_SETTINGS, INI_GW2BOT_ENABLED, false);
const char* pv = ini.GetValue(INI_SECTION_SETTINGS, INI_GW2BOT_KEY, "");
strcpy(settings.gw2bot_key, pv);
settings.gw2bot_success_only = ini.GetBoolValue(INI_SECTION_SETTINGS, INI_GW2BOT_SUCCESS_ONLY, false);
}

//Sqlite Database
Expand Down Expand Up @@ -146,7 +153,10 @@ Uploader::~Uploader()
LOG_F(INFO, "Uploader destructor begin...");
// Save our settings if we previously loaded/created an ini file
if (ini_enabled) {
ini.SetBoolValue(INI_SECTION_SETTINGS, INI_WVW_DETAILED_SETTING, wvw_detailed_enabled);
ini.SetBoolValue(INI_SECTION_SETTINGS, INI_WVW_DETAILED_SETTING, settings.wvw_detailed_enabled);
ini.SetBoolValue(INI_SECTION_SETTINGS, INI_GW2BOT_ENABLED, settings.gw2bot_enabled);
ini.SetValue(INI_SECTION_SETTINGS, INI_GW2BOT_KEY, settings.gw2bot_key);
ini.SetBoolValue(INI_SECTION_SETTINGS, INI_GW2BOT_SUCCESS_ONLY, settings.gw2bot_success_only);
ini.SaveFile(ini_path.string().c_str());
}

Expand Down Expand Up @@ -564,9 +574,54 @@ uintptr_t Uploader::imgui_tick()
ImGui::TreePop();
}

if (ImGui::TreeNode("GW2Bot"))
{
ImGui::Checkbox("GW2Bot Integration Enabled", &settings.gw2bot_enabled);
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("Post logs to GW2Bot for use with the EVTC Automation commands.");
ImGui::EndTooltip();
}

ImGui::SameLine();
ImGui::TextDisabled("(?)");
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted("Use the /evtc_automation autopost add_destination command to have GW2Bot post logs to a Discord channel.\nSee gw2bot.info/commands for details.");
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}

if (settings.gw2bot_enabled)
{
ImGui::PushItemWidth(ImGui::GetContentRegionAvailWidth() - ImGui::CalcTextSize("EVTC Api Key").x - 5);
ImGui::InputText("EVTC Api Key", settings.gw2bot_key, sizeof(settings.gw2bot_key));
ImGui::PopItemWidth();
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("Use GW2Bot's /evtc_automation api_key command to generate an API key.");
ImGui::EndTooltip();
}

ImGui::Checkbox("Clears only", &settings.gw2bot_success_only);
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("Only post clears/successful logs to GW2Bot.");
ImGui::EndTooltip();
}
}

ImGui::TreePop();
}

if (ImGui::TreeNode("Other"))
{
ImGui::Checkbox("Enable detailed WvW reports", &wvw_detailed_enabled);
ImGui::Checkbox("Enable detailed WvW reports", &settings.wvw_detailed_enabled);
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
Expand Down Expand Up @@ -791,6 +846,43 @@ void Uploader::check_webhooks(int log_id)
}
}

void Uploader::check_gw2bot(int log_id)
{
if (!settings.gw2bot_enabled) return;

auto log = storage->get_pointer<Log>(log_id);
if (log)
{
bool process = true;
if (!log->success && settings.gw2bot_success_only) process = false;

if (process) {
LOG_F(INFO, "Posting to GW2Bot: %s", log->permalink.c_str());
auto gw2bot_future = std::async(std::launch::async, [this](const char* key, Log log) {
cpr::Response response;
response = cpr::Post(
cpr::Url{"https://api.gw2bot.info/v1/evtc/notification"},
cpr::Header{
{"accept", "application/json"},
{"Authorization", "Bearer " + std::string(key)},
{"Content-Type", "application/json"},
},
cpr::Body{"{\"dpsreport_url\": \"" + log.permalink + "\"}"}
);
if (response.status_code != 201) {
StatusMessage status;
status.msg = "GW2Bot Error: " + response.text;
{
std::lock_guard<std::mutex> lk(ts_msg_mutex);
thread_status_messages.push_back(status);
}
}
LOG_F(INFO, "GW2Bot response: %s", response.text.c_str());
}, settings.gw2bot_key, *log);
}
}
}

void Uploader::start_async_refresh_log_list() {
LOG_F(INFO, "Starting Async Log Refresh");
using namespace sqlite_orm;
Expand Down Expand Up @@ -955,7 +1047,7 @@ void Uploader::upload_thread_loop() {
params.AddParameter({ "userToken", userToken.value });
}

if (wvw_detailed_enabled) {
if (settings.wvw_detailed_enabled) {
params.AddParameter({ "detailedwvw", "true" });
}

Expand Down Expand Up @@ -1012,7 +1104,10 @@ void Uploader::upload_thread_loop() {
try
{
storage->update(*log);
if (log->uploaded && !log->error) check_webhooks(log->id);
if (log->uploaded && !log->error) {
check_webhooks(log->id);
check_gw2bot(log->id);
};
}
catch (std::system_error e)
{
Expand Down
12 changes: 11 additions & 1 deletion arcdps_uploader/Uploader.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,29 @@ struct Webhook
char filter_buf[128];
};

struct Settings
{
bool wvw_detailed_enabled;
bool gw2bot_enabled;
char gw2bot_key[256];
bool gw2bot_success_only;
};

class Uploader
{
bool ini_enabled;
fs::path ini_path;
CSimpleIniA ini;

Settings settings;

fs::path log_path;
std::vector<Log> logs;
std::future<decltype(logs)> ft_file_list;
std::chrono::system_clock::time_point refresh_time;

std::deque<int> upload_queue;
std::vector<std::future<cpr::Response>> ft_uploads;
bool wvw_detailed_enabled;
std::vector<UserToken> userTokens;
UserToken userToken;
std::vector<Webhook> webhooks;
Expand Down Expand Up @@ -88,6 +97,7 @@ class Uploader
void create_log_table(Log& l);

void check_webhooks(int log_id);
void check_gw2bot(int log_id);

void start_async_refresh_log_list();

Expand Down
2 changes: 1 addition & 1 deletion revtc
Submodule revtc updated from b8d37e to 823dad