-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "BetterInfoStatsV2.h" | ||
#include "../utils.hpp" | ||
|
||
#include <fstream> | ||
#include <Geode/utils/web.hpp> | ||
|
||
bool BetterInfoStatsV2::init(){ | ||
if(!BaseJsonManager::init("stats.json")) return false; | ||
return true; | ||
} | ||
|
||
void BetterInfoStatsV2::validateLoadedData() { | ||
validateIsObject("levels"); | ||
validateIsObject("levels-daily"); | ||
validateIsObject("levels-gauntlet"); | ||
} | ||
|
||
BetterInfoStatsV2::BetterInfoStatsV2(){} | ||
|
||
auto& BetterInfoStatsV2::levelObject(GJGameLevel* gjLevel) { | ||
waitForLoad(); | ||
|
||
const char* key = "levels"; | ||
if(gjLevel->m_dailyID > 0) key = "levels-daily"; | ||
if(gjLevel->m_gauntletLevel) key = "levels-gauntlet"; | ||
|
||
auto idStr = std::to_string(gjLevel->m_levelID.value()); | ||
auto& levels = m_json[key]; | ||
if(!levels[idStr].is_object()) levels[idStr] = json::Object(); | ||
auto& level = levels[idStr]; | ||
if(!level["attempts"].is_array()) level["attempts"] = json::Array(); | ||
return level; | ||
} | ||
|
||
void BetterInfoStatsV2::logDeath(GJGameLevel* level, bool practice, LevelDeath death) { | ||
if(practice) return; | ||
if(level->m_levelType == GJLevelType::Editor) return; | ||
|
||
levelObject(level)["attempts"].as_array().push_back(death); | ||
doSave(); | ||
} | ||
|
||
std::pair<int, int> BetterInfoStatsV2::getCommonFail(GJGameLevel* gjLevel) { | ||
std::unordered_map<int, int> fails; | ||
|
||
auto attempts = levelObject(gjLevel)["attempts"].as_array(); | ||
if(attempts.size() == 0) return {0,0}; | ||
|
||
for(auto& attempt : attempts) { | ||
if(!attempt.is_object()) continue; | ||
if(!attempt["percentage"].is_number()) continue; | ||
|
||
fails[attempt["percentage"].as_int()] += 1; | ||
} | ||
|
||
auto max = std::max_element( | ||
fails.begin(), fails.end(), | ||
[] (const std::pair<int, int>& a, const std::pair<int, int>& b) { | ||
return a.second < b.second; | ||
} | ||
); | ||
|
||
return *max; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include "BaseJsonManager.h" | ||
#include "../objects/LevelDeath.h" | ||
|
||
class BetterInfoStatsV2 : public BaseJsonManager { | ||
inline static BetterInfoStatsV2* m_instance = nullptr; | ||
BetterInfoStatsV2(); | ||
|
||
public: | ||
bool init(); | ||
|
||
void validateLoadedData(); | ||
|
||
auto& levelObject(GJGameLevel* level); | ||
void logDeath(GJGameLevel* level, bool practice, LevelDeath death); | ||
std::pair<int, int> getCommonFail(GJGameLevel* gjLevel); | ||
|
||
static BetterInfoStatsV2* sharedState(){ | ||
if(m_instance == nullptr){ | ||
m_instance = new BetterInfoStatsV2; | ||
m_instance->init(); | ||
m_instance->retain(); | ||
} | ||
return m_instance; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <time.h> | ||
|
||
struct LevelDeath { | ||
public: | ||
int percentage; | ||
float x, y, rotation; | ||
time_t time = std::time(nullptr); | ||
}; | ||
|
||
template <> | ||
struct json::Serialize<LevelDeath> { | ||
static LevelDeath from_json(const json::Value& value) { | ||
return LevelDeath { | ||
.percentage = value["percentage"].as_int(), | ||
.x = (float) value["x"].as_double(), | ||
.y = (float) value["y"].as_double(), | ||
.rotation = (float) value["rotation"].as_double(), | ||
.time = value["time"].as_int() | ||
}; | ||
} | ||
static json::Value to_json(const LevelDeath& death) { | ||
return json::Object { | ||
{ "percentage", death.percentage }, | ||
{ "x", death.x }, | ||
{ "y", death.y }, | ||
{ "rotation", death.rotation }, | ||
{ "time", death.time } | ||
}; | ||
} | ||
}; |