1
0
Fork 0
mirror of https://github.com/Detanup01/gbe_fork.git synced 2025-06-07 09:45:55 +02:00

Merge pull request #205 from universal963/patch-globalstat

Initial implementation of `GetGlobalStat()`
This commit is contained in:
Detanup01 2025-04-18 11:36:29 +02:00 committed by GitHub
commit 2a7b47c2b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 30 deletions

View file

@ -92,6 +92,10 @@ struct Stat_config {
float default_value_float; float default_value_float;
int32 default_value_int; int32 default_value_int;
}; };
union {
double global_value_double;
int64 global_value_int64;
};
}; };
struct Image_Data { struct Image_Data {

View file

@ -828,38 +828,27 @@ static void parse_leaderboards(class Settings *settings_client, class Settings *
} }
// stats.txt // stats.json
static void parse_stats(class Settings *settings_client, class Settings *settings_server) static void parse_stats(class Settings *settings_client, class Settings *settings_server, class Local_Storage *local_storage)
{ {
std::string stats_config_path = Local_Storage::get_game_settings_path() + "stats.txt"; nlohmann::json stats_items;
std::ifstream input( std::filesystem::u8path(stats_config_path) ); std::string stats_json_path = Local_Storage::get_game_settings_path() + "stats.json";
if (input.is_open()) { if (local_storage->load_json(stats_json_path, stats_items)) {
common_helpers::consume_bom(input); for (const auto &stats : stats_items) {
for( std::string line; getline( input, line ); ) {
if (!line.empty() && line[line.length()-1] == '\n') {
line.pop_back();
}
if (!line.empty() && line[line.length()-1] == '\r') {
line.pop_back();
}
std::string stat_name; std::string stat_name;
std::string stat_type; std::string stat_type;
std::string stat_default_value; std::string stat_default_value = "0";
std::string stat_global_value = "0";
std::size_t deliminator = line.find("="); try {
if (deliminator != 0 && deliminator != std::string::npos && deliminator != line.size()) { stat_name = stats.value("name", std::string(""));
stat_name = line.substr(0, deliminator); stat_type = stats.value("type", std::string(""));
std::size_t deliminator2 = line.find("=", deliminator + 1); stat_default_value = stats.value("default", std::string("0"));
stat_global_value = stats.value("global", std::string("0"));
if (deliminator2 != std::string::npos && deliminator2 != line.size()) { }
stat_type = line.substr(deliminator + 1, deliminator2 - (deliminator + 1)); catch (const std::exception &e) {
stat_default_value = line.substr(deliminator2 + 1); PRINT_DEBUG("Error reading current stat item in stats.json, reason: %s", e.what());
} else { continue;
stat_type = line.substr(deliminator + 1);
stat_default_value = "0";
}
} }
std::transform(stat_type.begin(), stat_type.end(), stat_type.begin(),[](unsigned char c){ return std::tolower(c); }); std::transform(stat_type.begin(), stat_type.end(), stat_type.begin(),[](unsigned char c){ return std::tolower(c); });
@ -869,18 +858,21 @@ static void parse_stats(class Settings *settings_client, class Settings *setting
if (stat_type == "float") { if (stat_type == "float") {
config.type = GameServerStats_Messages::StatInfo::STAT_TYPE_FLOAT; config.type = GameServerStats_Messages::StatInfo::STAT_TYPE_FLOAT;
config.default_value_float = std::stof(stat_default_value); config.default_value_float = std::stof(stat_default_value);
config.global_value_double = std::stod(stat_global_value);
} else if (stat_type == "int") { } else if (stat_type == "int") {
config.type = GameServerStats_Messages::StatInfo::STAT_TYPE_INT; config.type = GameServerStats_Messages::StatInfo::STAT_TYPE_INT;
config.default_value_int = std::stol(stat_default_value); config.default_value_int = std::stol(stat_default_value);
config.global_value_int64 = std::stoll(stat_global_value);
} else if (stat_type == "avgrate") { } else if (stat_type == "avgrate") {
config.type = GameServerStats_Messages::StatInfo::STAT_TYPE_AVGRATE; config.type = GameServerStats_Messages::StatInfo::STAT_TYPE_AVGRATE;
config.default_value_float = std::stof(stat_default_value); config.default_value_float = std::stof(stat_default_value);
config.global_value_double = std::stod(stat_global_value);
} else { } else {
PRINT_DEBUG("Error adding stat %s, type %s isn't valid", stat_name.c_str(), stat_type.c_str()); PRINT_DEBUG("Error adding stat %s, type %s isn't valid", stat_name.c_str(), stat_type.c_str());
continue; continue;
} }
} catch (...) { } catch (...) {
PRINT_DEBUG("Error adding stat %s, default value %s isn't valid", stat_name.c_str(), stat_default_value.c_str()); PRINT_DEBUG("Error adding stat %s, default value %s or global value %s isn't valid", stat_name.c_str(), stat_default_value.c_str(), stat_global_value.c_str());
continue; continue;
} }
@ -1802,7 +1794,7 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
parse_app_paths(settings_client, settings_server, program_path); parse_app_paths(settings_client, settings_server, program_path);
parse_leaderboards(settings_client, settings_server); parse_leaderboards(settings_client, settings_server);
parse_stats(settings_client, settings_server); parse_stats(settings_client, settings_server, local_storage);
parse_depots(settings_client, settings_server); parse_depots(settings_client, settings_server);
parse_subscribed_groups(settings_client, settings_server); parse_subscribed_groups(settings_client, settings_server);
load_subscribed_groups_clans(local_storage->get_global_settings_path(), settings_client, settings_server); load_subscribed_groups_clans(local_storage->get_global_settings_path(), settings_client, settings_server);

View file

@ -625,6 +625,18 @@ bool Steam_User_Stats::GetGlobalStat( const char *pchStatName, int64 *pData )
{ {
PRINT_DEBUG_TODO(); PRINT_DEBUG_TODO();
std::lock_guard<std::recursive_mutex> lock(global_mutex); std::lock_guard<std::recursive_mutex> lock(global_mutex);
if (!pchStatName) return false;
std::string stat_name = common_helpers::to_lower(pchStatName);
const auto &stats_config = settings->getStats();
auto stats_data = stats_config.find(stat_name);
if (stats_data != stats_config.end()) {
if (stats_data->second.type != GameServerStats_Messages::StatInfo::STAT_TYPE_INT)
return false;
if (pData)
*pData = stats_data->second.global_value_int64;
return true;
}
return false; return false;
} }
@ -632,6 +644,18 @@ bool Steam_User_Stats::GetGlobalStat( const char *pchStatName, double *pData )
{ {
PRINT_DEBUG_TODO(); PRINT_DEBUG_TODO();
std::lock_guard<std::recursive_mutex> lock(global_mutex); std::lock_guard<std::recursive_mutex> lock(global_mutex);
if (!pchStatName) return false;
std::string stat_name = common_helpers::to_lower(pchStatName);
const auto &stats_config = settings->getStats();
auto stats_data = stats_config.find(stat_name);
if (stats_data != stats_config.end()) {
if (stats_data->second.type == GameServerStats_Messages::StatInfo::STAT_TYPE_INT)
return false;
if (pData)
*pData = stats_data->second.global_value_double;
return true;
}
return false; return false;
} }