fixed highscores

This commit is contained in:
2025-12-21 21:33:31 +01:00
parent fb82ac06d0
commit 70946fc720
5 changed files with 104 additions and 57 deletions

View File

@ -3,6 +3,7 @@
#include <nlohmann/json.hpp>
#include <thread>
#include <iostream>
#include <algorithm>
#include <cmath>
using json = nlohmann::json;
@ -35,6 +36,13 @@ static CurlInit g_curl_init;
namespace supabase {
static bool g_verbose = false;
void SetVerbose(bool enabled) {
g_verbose = enabled;
}
void SubmitHighscoreAsync(const ScoreEntry &entry) {
std::thread([entry]() {
try {
@ -68,18 +76,21 @@ void SubmitHighscoreAsync(const ScoreEntry &entry) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
// Debug: print outgoing request
std::cerr << "[Supabase] POST " << url << "\n";
std::cerr << "[Supabase] Body: " << body << "\n";
if (g_verbose) {
std::cerr << "[Supabase] POST " << url << "\n";
std::cerr << "[Supabase] Body: " << body << "\n";
}
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "[Supabase] POST error: " << curl_easy_strerror(res) << "\n";
if (g_verbose) std::cerr << "[Supabase] POST error: " << curl_easy_strerror(res) << "\n";
} else {
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
std::cerr << "[Supabase] POST response code: " << http_code << " body_len=" << resp.size() << "\n";
if (!resp.empty()) std::cerr << "[Supabase] POST response: " << resp << "\n";
if (g_verbose) {
std::cerr << "[Supabase] POST response code: " << http_code << " body_len=" << resp.size() << "\n";
if (!resp.empty()) std::cerr << "[Supabase] POST response: " << resp << "\n";
}
}
curl_slist_free_all(headers);
@ -97,15 +108,17 @@ std::vector<ScoreEntry> FetchHighscores(const std::string &gameType, int limit)
if (!curl) return out;
std::string path = "highscores";
// Clamp limit to max 10 to keep payloads small
int l = std::clamp(limit, 1, 10);
std::string query;
if (!gameType.empty()) {
if (gameType == "challenge") {
query = "?game_type=eq." + gameType + "&order=level.desc,time_sec.asc&limit=" + std::to_string(limit);
query = "?game_type=eq." + gameType + "&order=level.desc,time_sec.asc&limit=" + std::to_string(l);
} else {
query = "?game_type=eq." + gameType + "&order=score.desc&limit=" + std::to_string(limit);
query = "?game_type=eq." + gameType + "&order=score.desc&limit=" + std::to_string(l);
}
} else {
query = "?order=score.desc&limit=" + std::to_string(limit);
query = "?order=score.desc&limit=" + std::to_string(l);
}
std::string url = buildUrl(path) + query;
@ -123,15 +136,16 @@ std::vector<ScoreEntry> FetchHighscores(const std::string &gameType, int limit)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
// Debug: print outgoing GET
std::cerr << "[Supabase] GET " << url << "\n";
if (g_verbose) std::cerr << "[Supabase] GET " << url << "\n";
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK) {
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
std::cerr << "[Supabase] GET response code: " << http_code << " body_len=" << resp.size() << "\n";
if (!resp.empty()) std::cerr << "[Supabase] GET response: " << resp << "\n";
if (g_verbose) {
std::cerr << "[Supabase] GET response code: " << http_code << " body_len=" << resp.size() << "\n";
if (!resp.empty()) std::cerr << "[Supabase] GET response: " << resp << "\n";
}
try {
auto j = json::parse(resp);
if (j.is_array()) {
@ -151,10 +165,10 @@ std::vector<ScoreEntry> FetchHighscores(const std::string &gameType, int limit)
}
}
} catch (...) {
std::cerr << "[Supabase] GET parse error" << std::endl;
if (g_verbose) std::cerr << "[Supabase] GET parse error" << std::endl;
}
} else {
std::cerr << "[Supabase] GET error: " << curl_easy_strerror(res) << "\n";
if (g_verbose) std::cerr << "[Supabase] GET error: " << curl_easy_strerror(res) << "\n";
}
curl_slist_free_all(headers);

View File

@ -11,4 +11,7 @@ void SubmitHighscoreAsync(const ScoreEntry &entry);
// Fetch highscores for a game type. If gameType is empty, fetch all (limited).
std::vector<ScoreEntry> FetchHighscores(const std::string &gameType, int limit);
// Enable or disable verbose logging to stderr. Disabled by default.
void SetVerbose(bool enabled);
} // namespace supabase