Skip to content

Commit

Permalink
fixed webhook not triggering on 'quit and rewind'
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 17, 2018
1 parent 484abc5 commit 6c6495b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void Config::checkForUpdates() {
json_t* url = json_object_get(root, "tag_name");
const char *urlText = json_string_value(url);
float latestVer = stof(urlText);
float currentVer = 0.92;
float currentVer = 0.93;
if(latestVer > currentVer) {
json_t* url2 = json_object_get(root, "html_url");
const char *urlText2 = json_string_value(url2);
Expand Down
97 changes: 52 additions & 45 deletions Webhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ void Webhook::notify(SC2State*& previous, SC2State*& current) {
Config* cfg = Config::Current();
if(cfg->webhookEnabled) {
std::string event = "";
// quit and rewind
if(previous->gameState == GAME_INGAME &&
previous->appState == APP_INGAME &&
current->gameState == GAME_REPLAY) {
if(current->fullState.players.size() == 2) {
event = "exit";
}
}

if(previous->appState != current->appState &&
current->appState != APP_INGAME && previous->appState == APP_INGAME) {
// just quit
if(current->appState != APP_INGAME &&
previous->appState == APP_INGAME) {
if(current->fullState.players.size() == 2 && !current->fullState.isReplay) {
event = "exit";
}
}

if(previous->appState != current->appState &&
current->appState == APP_INGAME && previous->appState != APP_INGAME) {
if(current->appState == APP_INGAME &&
previous->appState != APP_INGAME) {
event = "enter";
}

Expand All @@ -36,53 +45,51 @@ void Webhook::notify(SC2State*& previous, SC2State*& current) {
}

void Webhook::sendRequest(SC2State*& game, std::string event) {
if(!game->fullState.isReplay) {
Config* cfg = Config::Current();

int still_running = 0;
CURLM *multi_handle;
multi_handle = curl_multi_init();

// vector of handles
vector<CURL*> handles;

for (string &url : cfg->webhookURLList) {
CURL *handle;
handle = curl_easy_init();
if (handle) {
curl_multi_add_handle(multi_handle, handle);
handles.push_back(handle);
std::string qdelim = "?";
std::size_t found = url.find(qdelim);
if (found!=std::string::npos) {
qdelim = "&";
}

std::string resp = getJSONStringFromSC2State(game, event);
std::string c_url = url + qdelim + "json=" + curl_easy_escape(handle, resp.c_str(), 0);
curl_easy_setopt(handle, CURLOPT_URL, c_url.c_str());
curl_easy_setopt(handle, CURLOPT_TIMEOUT_MS, 500);
Config* cfg = Config::Current();

int still_running = 0;
CURLM *multi_handle;
multi_handle = curl_multi_init();

// vector of handles
vector<CURL*> handles;

for (string &url : cfg->webhookURLList) {
CURL *handle;
handle = curl_easy_init();
if (handle) {
curl_multi_add_handle(multi_handle, handle);
handles.push_back(handle);
std::string qdelim = "?";
std::size_t found = url.find(qdelim);
if (found!=std::string::npos) {
qdelim = "&";
}

std::string resp = getJSONStringFromSC2State(game, event);
std::string c_url = url + qdelim + "json=" + curl_easy_escape(handle, resp.c_str(), 0);
curl_easy_setopt(handle, CURLOPT_URL, c_url.c_str());
curl_easy_setopt(handle, CURLOPT_TIMEOUT_MS, 500);
}
}

curl_multi_perform(multi_handle, &still_running);
while(still_running) {
CURLMcode mc; /* curl_multi_wait() return code */
int numfds;
mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
curl_multi_perform(multi_handle, &still_running);
while(still_running) {
CURLMcode mc; /* curl_multi_wait() return code */
int numfds;
mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);

if(mc != CURLM_OK) {
break;
}
curl_multi_perform(multi_handle, &still_running);
}
// clean up each handle
for (CURL* &handle : handles) {
curl_multi_remove_handle(multi_handle, handle);
curl_easy_cleanup(handle);
if(mc != CURLM_OK) {
break;
}
curl_multi_cleanup(multi_handle);
curl_multi_perform(multi_handle, &still_running);
}
// clean up each handle
for (CURL* &handle : handles) {
curl_multi_remove_handle(multi_handle, handle);
curl_easy_cleanup(handle);
}
curl_multi_cleanup(multi_handle);
}

std::string Webhook::getQueryStringFromSC2State(SC2State*& game, std::string event, CURL* curl) {
Expand Down

0 comments on commit 6c6495b

Please sign in to comment.