Skip to content

Commit

Permalink
fix sheet settings not updating until restart, fix config not working…
Browse files Browse the repository at this point in the history
… without main role range, add first time setup
  • Loading branch information
pv42 committed Jan 24, 2022
1 parent 351f3de commit 4a412da
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 148 deletions.
10 changes: 8 additions & 2 deletions TheTruth/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using nlohmann::json;
Settings::Settings() {
std::filesystem::path p = std::filesystem::current_path() / "addons" / "thetruth-settings.json";
if (std::filesystem::exists(p)) {
showFirstTimeSetup = false;
try {
json j;
std::ifstream file(p);
Expand All @@ -23,17 +24,22 @@ Settings::Settings() {
}
} else {
Logger::i("no settings file found, using default settings");
showFirstTimeSetup = true;
// windows
ownWindowShowTitle = true;
lockOwnRoleWindow = false;
showInCharSelectAndLoading = true;
showHeaderInOwnRoles = false;
flipRowsAndCols = false;
weekday = 6;
for (int wing = 1; wing <= 7; wing++) {
showWings[wing - 1] = wing >= 5;
wingRolesRanges.push_back("");
wingHeaderRanges.push_back("");
}
showAllRolesMode = ALL_AERODROME;
showCurrentWing = true;
showOwnRolesMode = OWN_ALWAYS;
showOwnRolesMode = OWN_TIMED;
windowToggleKey.keyCode = 82; // r
windowToggleKey.requireArcMod1 = true;
windowToggleKey.requireArcMod2 = true;
Expand All @@ -43,7 +49,7 @@ Settings::Settings() {
showBgColorInOwnRoles = true;
useSheetsConditionalColors = true;
// sheet setup
ownName = "Pv";
ownName = "my name";
}
}

Expand Down
1 change: 1 addition & 0 deletions TheTruth/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Settings {
ShowAllRolesMode showAllRolesMode;
bool showInCharSelectAndLoading;
vector<pair<string, ImColor>> customColors;
bool showFirstTimeSetup;
void save() const;
private:
void loadFromJson(json j);
Expand Down
32 changes: 18 additions & 14 deletions TheTruth/SheetsAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ SheetsAPI::SheetsAPI() : api_key("") , settings(), hasMetaData(false) {
Logger::d("default constr");
}

SheetsAPI::SheetsAPI(const string api_key_, Settings& setttings_) : api_key(api_key_), settings(setttings_) {
SheetsAPI::SheetsAPI(const string api_key_, shared_ptr<Settings> settings_) : api_key(api_key_), settings(settings_) {
downloading = false;
hasMetaData = false;
}

unique_ptr<json> getSheetValuesJson(const string& sheetId, const string& range, const string api_key) {
if (sheetId.length() == 0 || range.length() == 0)
return NULL;
cpr::Url url = cpr::Url("https://sheets.googleapis.com/v4/spreadsheets/" + sheetId + "/values/" + range);
Logger::d(string_format("getting url %s", url.c_str()));
cpr::Response r = cpr::Get(url, cpr::Parameters{ { "key", api_key } });
Expand Down Expand Up @@ -148,9 +150,9 @@ string escape_table_range(string& unescaped_range) {
void SheetsAPI::downloadNames() {
try {
Logger::d("downloading names");
string range = settings.namesRange;
string range = settings->namesRange;
range = escape_table_range(range);
names_cache = std::make_shared<vector<string>>(getValuesAs1DArray(settings.sheetId, range, api_key));
names_cache = std::make_shared<vector<string>>(getValuesAs1DArray(settings->sheetId, range, api_key));
Logger::d(string_format("got %d names", names_cache->size()));
Logger::d("downloaded names");
downloading = false;
Expand All @@ -161,15 +163,16 @@ void SheetsAPI::downloadNames() {
}

void SheetsAPI::downloadMainRoles() {
if (settings.mainRolesRange.length() == 0) {
if (settings->mainRolesRange.length() == 0) {
Logger::d("no main roles range");
main_roles_cache = std::make_shared<vector<string>>();
downloading = false;
} else {
try {
Logger::d("downloading main roles");
string range = settings.mainRolesRange;
string range = settings->mainRolesRange;
range = escape_table_range(range);
main_roles_cache = std::make_shared<vector<string>>(getValuesAs1DArray(settings.sheetId, range, api_key));
main_roles_cache = std::make_shared<vector<string>>(getValuesAs1DArray(settings->sheetId, range, api_key));
Logger::d(string_format("got %d main roles", main_roles_cache->size()));
Logger::d("downloaded main roles");
downloading = false;
Expand All @@ -183,18 +186,18 @@ void SheetsAPI::downloadMainRoles() {
void SheetsAPI::downloadWing(int wing) {
try {
Logger::d(string_format("downloading wing %d", wing));
string range = settings.getWingRolesRange(wing);
string range = settings->getWingRolesRange(wing);
if (range.empty()) {
downloading = false;
return;
}
roles_cache[wing] = getValuesAs2DArray(settings.sheetId, escape_table_range(range), api_key, settings.flipRowsAndCols);
string header_range = settings.getWingHeaderRange(wing);
roles_cache[wing] = getValuesAs2DArray(settings->sheetId, escape_table_range(range), api_key, settings->flipRowsAndCols);
string header_range = settings->getWingHeaderRange(wing);
if (header_range.empty()) {
downloading = false;
return;
}
header_cache[wing] = getValuesAs1DArray(settings.sheetId, escape_table_range(header_range), api_key);
header_cache[wing] = getValuesAs1DArray(settings->sheetId, escape_table_range(header_range), api_key);
Logger::d(string_format("downloaded wing %d", wing));
} catch (std::exception e) {
Logger::e("failed to download wing");
Expand Down Expand Up @@ -236,14 +239,14 @@ void SheetsAPI::downloadSheetMetaData() {
// find a valid range
int wing = 1;
while (wing <= 7 && range.size() == 0) {
range = settings.getWingHeaderRange(wing);
range = settings->getWingHeaderRange(wing);
wing++;
}
if (range.size() == 0) {
Logger::w("could not find valid range to retrive conditional formating");
return;
}
shared_ptr<json> whole_json = getSheetJson(settings.sheetId, api_key);
shared_ptr<json> whole_json = getSheetJson(settings->sheetId, api_key);
json sheet_json = getSheetFromRangeAndJson(whole_json, range);
if (sheet_json.contains("conditionalFormats") && sheet_json["conditionalFormats"].is_array()) {
for (json format_json : sheet_json["conditionalFormats"]) {
Expand Down Expand Up @@ -288,7 +291,7 @@ void SheetsAPI::downloadSheetMetaData() {
}
} catch (std::exception e) {
Logger::e("failed to download meta data");
Logger::e(e.what());
Logger::e(string(e.what()));
}
hasMetaData = true; // dont try again if it failed
downloading = false;
Expand All @@ -307,7 +310,7 @@ void SheetsAPI::requestNames() {
void SheetsAPI::requestMainRoles() {
if (downloading) return;
if (main_roles_cache == NULL) {
Logger::d("getting names");
Logger::d("getting main roles");
downloading = true;
if (downloadThread.joinable()) downloadThread.join();
downloadThread = thread(&SheetsAPI::downloadMainRoles, this);
Expand Down Expand Up @@ -337,6 +340,7 @@ void SheetsAPI::requestWing(int wing) {
}

void SheetsAPI::clearCache() {
names_cache = NULL;
main_roles_cache = NULL;
roles_cache.clear();
colors_cache.clear();
Expand Down
4 changes: 2 additions & 2 deletions TheTruth/SheetsAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SheetsAPI {
public:
SheetsAPI();
~SheetsAPI();
SheetsAPI(const string api_key, Settings& setttings);
SheetsAPI(const string api_key, const shared_ptr<Settings> setttings);
void downloadNames();
void downloadMainRoles();
void downloadWing(int wing);
Expand All @@ -36,7 +36,7 @@ class SheetsAPI {
void clearCache();
bool isDownloading();
private:
Settings settings;
const shared_ptr<Settings> settings;
atomic_bool downloading;
bool hasMetaData;
string api_key;
Expand Down
Loading

0 comments on commit 4a412da

Please sign in to comment.