Skip to content

Commit

Permalink
Merge pull request #16 from leigholiver/fix/score-tracker-rewind
Browse files Browse the repository at this point in the history
Fixes score tracker ignoring rewind
  • Loading branch information
leigholiver authored Oct 9, 2020
2 parents 3edf82a + 14f870a commit ce3ce69
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 76 deletions.
5 changes: 3 additions & 2 deletions APIState.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ struct player {
};

struct APIState {
std::vector<const char*> activeScreens;
std::vector<const char *> activeScreens;
bool isReplay;
bool isRewind;
double displayTime;
std::vector<player*> players;
};
};
4 changes: 2 additions & 2 deletions Config.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ 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.96;
float latestVer = std::stof(urlText);
float currentVer = 0.98;
if(latestVer > currentVer) {
json_t* url2 = json_object_get(root, "html_url");
const char *urlText2 = json_string_value(url2);
Expand Down
22 changes: 10 additions & 12 deletions Config.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "obs-util.h"
#include <obs-frontend-api.h>

using namespace std;

class Config {
public:
Config();
Expand All @@ -20,8 +18,8 @@ class Config {

// sc2data
std::string ipAddr;
vector<std::string> usernames;
vector<std::string> recentUsernames;
std::vector<std::string> usernames;
std::vector<std::string> recentUsernames;

// scene switcher
OBSWeakSource inGameScene;
Expand All @@ -48,7 +46,7 @@ class Config {
bool scoresEnabled;
bool popupsEnabled;

vector<std::string> webhookURLList;
std::vector<std::string> webhookURLList;


private:
Expand All @@ -73,7 +71,7 @@ static void LoadSaveHandler(obs_data_t *save_data, bool saving, void *) {
obs_data_set_bool(obj, "switch_on_load", cfg->switchOnLoad);

obs_data_array_t *array = obs_data_array_create();
for (string &s : cfg->webhookURLList) {
for (std::string &s : cfg->webhookURLList) {
obs_data_t *array_obj = obs_data_create();
obs_data_set_string(array_obj, "URL", s.c_str());
obs_data_array_push_back(array, array_obj);
Expand All @@ -83,7 +81,7 @@ static void LoadSaveHandler(obs_data_t *save_data, bool saving, void *) {
obs_data_array_release(array);

obs_data_array_t *array2 = obs_data_array_create();
for (string &s : cfg->usernames) {
for (std::string &s : cfg->usernames) {
obs_data_t *array_obj = obs_data_create();
obs_data_set_string(array_obj, "username", s.c_str());
obs_data_array_push_back(array2, array_obj);
Expand Down Expand Up @@ -125,11 +123,11 @@ static void LoadSaveHandler(obs_data_t *save_data, bool saving, void *) {

cfg->ipAddr = obs_data_get_string(obj, "ip_addr");

vector<std::string> emptyusernames;
std::vector<std::string> emptyusernames;
cfg->usernames = emptyusernames;
obs_data_array_t *array = obs_data_get_array(obj, "usernames");
size_t count = obs_data_array_count(array);
vector<std::string> seen;
std::vector<std::string> seen;
for (size_t i = 0; i < count; i++) {
obs_data_t *array_obj = obs_data_array_item(array, i);
std::string un = obs_data_get_string(array_obj, "username");
Expand All @@ -140,11 +138,11 @@ static void LoadSaveHandler(obs_data_t *save_data, bool saving, void *) {
}
obs_data_array_release(array);

vector<std::string> emptywebhookURLList;
std::vector<std::string> emptywebhookURLList;
cfg->webhookURLList = emptywebhookURLList;
obs_data_array_t *array2 = obs_data_get_array(obj, "webhookURLs");
size_t count2 = obs_data_array_count(array2);
vector<std::string> seen2;
std::vector<std::string> seen2;
for (size_t i = 0; i < count2; i++) {
obs_data_t *array_obj = obs_data_array_item(array2, i);
std::string url = obs_data_get_string(array_obj, "URL");
Expand All @@ -158,7 +156,7 @@ static void LoadSaveHandler(obs_data_t *save_data, bool saving, void *) {
cfg->textSourceName = obs_data_get_string(obj, "textSourceName");


string scoreString = obs_data_get_string(obj, "scoreString");
std::string scoreString = obs_data_get_string(obj, "scoreString");
if (scoreString != "") {
cfg->scoreString = scoreString;
}
Expand Down
18 changes: 13 additions & 5 deletions SC2Data.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,23 @@ void SC2Data::update() {
state = newState;
}

if(state->appState != newState->appState ||
state->gameState != newState->gameState ||
state->menuState != newState->menuState ) {
// figure out if we're rewinding
bool stillInGame = state->appState == APP_INGAME && newState->appState == APP_INGAME;
bool nowAReplay = !state->fullState.isReplay && newState->fullState.isReplay;
bool rewinding = stillInGame && nowAReplay;
newState->fullState.isRewind = rewinding;

bool menuChanged = state->menuState != newState->menuState;
bool gameChanged = state->gameState != newState->gameState;
bool appChanged = state->appState != newState->appState;

if (menuChanged || gameChanged || appChanged || rewinding) {
s2log("state has changed, notifying");
for (size_t i = 0; i < watchers.size(); i++) {
s2log("notifying " + watchers[i]->getName());
watchers[i]->notify(state, newState);
watchers[i]->notify(state, newState);
}
state = newState;
}
}
}
}
Empty file modified SC2State.cpp
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions SceneSwitcher.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void SceneSwitcher::notify(SC2State*& previous, SC2State*& current) {
s2log("Entered Menus. Finding Menu Scene");
OBSWeakSource tmpScene;

s2log("current->menustate is set: " + to_string(current->menuState));
s2log("current->menustate is set: " + std::to_string(current->menuState));
if (current->menuState == MENU_NONE) {
s2log("Menu state is MENU_NONE. Switching to Out of Game Scene");
tmpScene = cfg->outGameScene;
Expand Down Expand Up @@ -134,4 +134,4 @@ void SceneSwitcher::notify(SC2State*& previous, SC2State*& current) {
obs_source_release(source);
}
}
}
}
73 changes: 37 additions & 36 deletions ScoreTracker.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,41 @@ std::string ScoreTracker::getName() { return "Score Tracker"; }

void ScoreTracker::notify(SC2State*& previous, SC2State*& current) {
Config* cfg = Config::Current();
if(cfg->scoresEnabled) {
if(previous->appState != current->appState &&
current->appState == APP_MENU && previous->appState != APP_MENU) {
if(current->fullState.players.size() == 2 && !current->fullState.isReplay) {
bool iAmPlayerA = false;
bool iAmPlayerB = false;
for (size_t i = 0; i < current->fullState.players.size(); i++) {
for (size_t j= 0; j < cfg->usernames.size(); j++) {
bool found = current->fullState.players[i]->name == cfg->usernames[j];
if(found && i==0) {
iAmPlayerA = true;
}
if(found && i==1) {
iAmPlayerB = true;
}
if(cfg->scoresEnabled && current->fullState.players.size() == 2) {
bool leftGame = previous->appState != APP_MENU &&
current->appState == APP_MENU &&
!current->fullState.isReplay;

if (leftGame || current->fullState.isRewind) {
bool iAmPlayerA = false;
bool iAmPlayerB = false;
for (size_t i = 0; i < current->fullState.players.size(); i++) {
for (size_t j= 0; j < cfg->usernames.size(); j++) {
bool found = current->fullState.players[i]->name == cfg->usernames[j];
if(found && i==0) {
iAmPlayerA = true;
}
if(found && i==1) {
iAmPlayerB = true;
}
}
if (iAmPlayerA && iAmPlayerB) {
// sc2 only tells us the name and barcodes are a thing.common names could also cause stats to
// be recorded incorrectly.only way to get accurate info is asking the user to confirm
addConfirmMessage(current->fullState.players[0], current->fullState.players[1]);
}
else if (!iAmPlayerA && !iAmPlayerB) {
// we didnt know which player the user was so we have to ask
addConfirmMessage(current->fullState.players[0], current->fullState.players[1]);
}
if (iAmPlayerA && iAmPlayerB) {
// sc2 only tells us the name and barcodes are a thing.common names could also cause stats to
// be recorded incorrectly.only way to get accurate info is asking the user to confirm
addConfirmMessage(current->fullState.players[0], current->fullState.players[1]);
}
else if (!iAmPlayerA && !iAmPlayerB) {
// we didnt know which player the user was so we have to ask
addConfirmMessage(current->fullState.players[0], current->fullState.players[1]);
}
else {
// normal result
if (iAmPlayerB) {
recordScore(current->fullState.players[0]->race, current->fullState.players[1]->result);
}
else {
// normal result
if (iAmPlayerB) {
recordScore(current->fullState.players[0]->race, current->fullState.players[1]->result);
}
else {
recordScore(current->fullState.players[1]->race, current->fullState.players[0]->result);
}
recordScore(current->fullState.players[1]->race, current->fullState.players[0]->result);
}
}
}
Expand All @@ -83,19 +84,19 @@ std::string ScoreTracker::getScoreString() {
std::map <size_t, std::map <std::string, std::string>> scoreMap;

scoreMap[0]["search"] = "${tw}";
scoreMap[0]["replace"] = to_string(scores["Terr"]["Victory"]);
scoreMap[0]["replace"] = std::to_string(scores["Terr"]["Victory"]);
scoreMap[1]["search"] = "${tl}";
scoreMap[1]["replace"] = to_string(scores["Terr"]["Defeat"]);
scoreMap[1]["replace"] = std::to_string(scores["Terr"]["Defeat"]);

scoreMap[2]["search"] = "${zw}";
scoreMap[2]["replace"] = to_string(scores["Zerg"]["Victory"]);
scoreMap[2]["replace"] = std::to_string(scores["Zerg"]["Victory"]);
scoreMap[3]["search"] = "${zl}";
scoreMap[3]["replace"] = to_string(scores["Zerg"]["Defeat"]);
scoreMap[3]["replace"] = std::to_string(scores["Zerg"]["Defeat"]);

scoreMap[4]["search"] = "${pw}";
scoreMap[4]["replace"] = to_string(scores["Prot"]["Victory"]);
scoreMap[4]["replace"] = std::to_string(scores["Prot"]["Victory"]);
scoreMap[5]["search"] = "${pl}";
scoreMap[5]["replace"] = to_string(scores["Prot"]["Defeat"]);
scoreMap[5]["replace"] = std::to_string(scores["Prot"]["Defeat"]);

for (size_t i = 0; i < scoreMap.size(); i++) {
std::string search = scoreMap[i]["search"];
Expand Down
10 changes: 5 additions & 5 deletions Webhook.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void Webhook::sendRequest(SC2State*& game, std::string event) {
// vector of handles
vector<CURL*> handles;

for (string &url : cfg->webhookURLList) {
for (std::string &url : cfg->webhookURLList) {
s2log("Adding url: " + url);
CURL *handle;
handle = curl_easy_init();
Expand Down Expand Up @@ -155,11 +155,11 @@ std::string Webhook::getJSONStringFromSC2State(SC2State*& game, std::string even

ScoreTracker* st = ScoreTracker::Current();
resp = resp + "\"scores\": { ";
resp = resp + "\"Terr\": {\"Victory\": " + to_string(st->scores["Terr"]["Victory"]) + ", \"Defeat\": " + to_string(st->scores["Terr"]["Defeat"]) + " },";
resp = resp + "\"Prot\": {\"Victory\": " + to_string(st->scores["Prot"]["Victory"]) + ", \"Defeat\": " + to_string(st->scores["Prot"]["Defeat"]) + " },";
resp = resp + "\"Zerg\": {\"Victory\": " + to_string(st->scores["Zerg"]["Victory"]) + ", \"Defeat\": " + to_string(st->scores["Zerg"]["Defeat"]) + " }";
resp = resp + "\"Terr\": {\"Victory\": " + std::to_string(st->scores["Terr"]["Victory"]) + ", \"Defeat\": " + std::to_string(st->scores["Terr"]["Defeat"]) + " },";
resp = resp + "\"Prot\": {\"Victory\": " + std::to_string(st->scores["Prot"]["Victory"]) + ", \"Defeat\": " + std::to_string(st->scores["Prot"]["Defeat"]) + " },";
resp = resp + "\"Zerg\": {\"Victory\": " + std::to_string(st->scores["Zerg"]["Victory"]) + ", \"Defeat\": " + std::to_string(st->scores["Zerg"]["Defeat"]) + " }";
resp = resp + "}";

resp = resp + "}";
return resp;
}
}
22 changes: 10 additions & 12 deletions forms/SettingsDialog.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "Constants.h"
#include "obs-util.h"

using namespace std;

Config* config = Config::Current();

SettingsDialog::SettingsDialog(QWidget* parent) :
Expand Down Expand Up @@ -206,7 +204,7 @@ void SettingsDialog::on_replayScene_currentTextChanged(const QString& text) {

void SettingsDialog::on_ipAddr_textChanged(const QString& text) {
if(!isLoading) {
config->ipAddr = (string)text.toUtf8().constData();
config->ipAddr = (std::string) text.toUtf8().constData();
}
}

Expand Down Expand Up @@ -359,7 +357,7 @@ void SettingsDialog::on_versusScene_currentTextChanged(const QString &text) {
void SettingsDialog::on_addUsernameButton_clicked() {
if(!isLoading) {
// add the username to our usernames
string username = ui->usernameLine->text().toUtf8().constData();
std::string username = ui->usernameLine->text().toUtf8().constData();

if (std::find(config->usernames.begin(), config->usernames.end(), username) == config->usernames.end() && username != "") {
config->usernames.push_back(username); // add it to the ui list
Expand All @@ -379,7 +377,7 @@ void SettingsDialog::on_removeUsernameButton_clicked() {
// remove the list item from the ui list
// have to iterate backwards because the size of the list changes as items are removed
// also store the name first as the value of usernameLine changes as items are removed and new items selected
string searchName = ui->usernameLine->text().toUtf8().constData();
std::string searchName = ui->usernameLine->text().toUtf8().constData();
for (int i = ui->userNames->count()-1; i >= 0; i--) {
QListWidgetItem *item = ui->userNames->item(i);
if (item->text() == QString::fromStdString(searchName)) {
Expand Down Expand Up @@ -414,16 +412,16 @@ void SettingsDialog::on_recentUsernames_itemSelectionChanged() {

void SettingsDialog::on_textSourceName_textChanged(const QString& text) {
if(!isLoading) {
if (config->textSourceName != (string)text.toUtf8().constData()) {
config->textSourceName = (string)text.toUtf8().constData();
if (config->textSourceName != (std::string)text.toUtf8().constData()) {
config->textSourceName = (std::string)text.toUtf8().constData();
}
}
}

void SettingsDialog::on_textTemplate_textChanged() {
if(!isLoading) {
if (config->scoreString != (string)ui->textTemplate->toPlainText().toUtf8().constData()) {
config->scoreString = (string)ui->textTemplate->toPlainText().toUtf8().constData();
if (config->scoreString != (std::string)ui->textTemplate->toPlainText().toUtf8().constData()) {
config->scoreString = (std::string)ui->textTemplate->toPlainText().toUtf8().constData();
}
}
}
Expand Down Expand Up @@ -630,7 +628,7 @@ void SettingsDialog::on_pLossMinus_clicked() {

void SettingsDialog::on_addURLButton_clicked() {
if(!isLoading) {
string username = ui->webhookEnterGame->text().toUtf8().constData();
std::string username = ui->webhookEnterGame->text().toUtf8().constData();
config->webhookURLList.push_back(username);
ui->webhookURLList->addItem(ui->webhookEnterGame->text());
ui->webhookEnterGame->setText("");
Expand All @@ -641,7 +639,7 @@ void SettingsDialog::on_removeURLButton_clicked() {
if(!isLoading) {
config->webhookURLList.erase(std::remove(config->webhookURLList.begin(), config->webhookURLList.end(), ui->webhookEnterGame->text().toUtf8().constData()), config->webhookURLList.end());

string searchName = ui->webhookEnterGame->text().toUtf8().constData();
std::string searchName = ui->webhookEnterGame->text().toUtf8().constData();
for (int i = ui->webhookURLList->count()-1; i >= 0; i--) {
QListWidgetItem *item = ui->webhookURLList->item(i);
if (item->text() == QString::fromStdString(searchName)) {
Expand All @@ -662,4 +660,4 @@ void SettingsDialog::on_webhookURLList_itemSelectionChanged() {
ui->webhookEnterGame->setText(ui->webhookURLList->currentItem()->text());
}
}
}
}

0 comments on commit ce3ce69

Please sign in to comment.