Lokal Leaderboard
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <algorithm>
|
||||
#include <preferencemanager.h>
|
||||
#include <vector>
|
||||
|
||||
@@ -54,6 +55,9 @@ struct UserData {
|
||||
bool exists;
|
||||
};
|
||||
|
||||
// Forward declarations für Leaderboard-Funktionen
|
||||
void addLocalTime(String uid, String name, unsigned long timeMs);
|
||||
|
||||
// Prüft, ob ein Benutzer mit der angegebenen UID in der Datenbank existiert und
|
||||
// gibt dessen Daten zurück.
|
||||
UserData checkUser(const String &uid) {
|
||||
@@ -349,5 +353,135 @@ void setupBackendRoutes(AsyncWebServer &server) {
|
||||
// Andere Logik wie in getBestLocs
|
||||
});
|
||||
|
||||
// Lokales Leaderboard API
|
||||
server.on("/api/leaderboard", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
// Sortiere nach Zeit (beste zuerst)
|
||||
std::sort(localTimes.begin(), localTimes.end(),
|
||||
[](const LocalTime &a, const LocalTime &b) {
|
||||
return a.timeMs < b.timeMs;
|
||||
});
|
||||
|
||||
DynamicJsonDocument doc(2048);
|
||||
JsonArray leaderboard = doc.createNestedArray("leaderboard");
|
||||
|
||||
// Nimm die besten 5
|
||||
int count = 0;
|
||||
for (const auto &time : localTimes) {
|
||||
if (count >= 5)
|
||||
break;
|
||||
|
||||
JsonObject entry = leaderboard.createNestedObject();
|
||||
entry["rank"] = count + 1;
|
||||
entry["name"] = time.name;
|
||||
entry["uid"] = time.uid;
|
||||
entry["time"] = time.timeMs / 1000.0;
|
||||
|
||||
// Format time inline
|
||||
float seconds = time.timeMs / 1000.0;
|
||||
int totalSeconds = (int)seconds;
|
||||
int minutes = totalSeconds / 60;
|
||||
int remainingSeconds = totalSeconds % 60;
|
||||
int milliseconds = (int)((seconds - totalSeconds) * 100);
|
||||
|
||||
String timeFormatted;
|
||||
if (minutes > 0) {
|
||||
timeFormatted = String(minutes) + ":" +
|
||||
(remainingSeconds < 10 ? "0" : "") +
|
||||
String(remainingSeconds) + "." +
|
||||
(milliseconds < 10 ? "0" : "") + String(milliseconds);
|
||||
} else {
|
||||
timeFormatted = String(remainingSeconds) + "." +
|
||||
(milliseconds < 10 ? "0" : "") + String(milliseconds);
|
||||
}
|
||||
entry["timeFormatted"] = timeFormatted;
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
String result;
|
||||
serializeJson(doc, result);
|
||||
request->send(200, "application/json", result);
|
||||
});
|
||||
|
||||
// Add more routes as needed
|
||||
}
|
||||
|
||||
// Hilfsfunktionen um UID und Status abzufragen (aus communication.h)
|
||||
String getStart1UID();
|
||||
String getStart2UID();
|
||||
bool wasStart1FoundLocally();
|
||||
bool wasStart2FoundLocally();
|
||||
|
||||
// Funktion um Zeit an Online-API zu senden
|
||||
void sendTimeToOnlineAPI(int lane, String uid, float timeInSeconds) {
|
||||
// Nur senden wenn User online gefunden wurde
|
||||
bool wasOnlineFound =
|
||||
(lane == 1) ? !wasStart1FoundLocally() : !wasStart2FoundLocally();
|
||||
|
||||
if (!wasOnlineFound) {
|
||||
Serial.println("Zeit nicht gesendet - User wurde lokal gefunden");
|
||||
return;
|
||||
}
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("Keine Internetverbindung - Zeit nicht gesendet");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Sende Zeit an Online-API für Lane " + String(lane));
|
||||
|
||||
HTTPClient http;
|
||||
http.begin(String(BACKEND_SERVER) + "/api/v1/private/create-time");
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
http.addHeader("Authorization", String("Bearer ") + licence);
|
||||
|
||||
// Zeit in M:SS.mmm Format konvertieren (ohne führende Null bei Minuten)
|
||||
int minutes = (int)(timeInSeconds / 60);
|
||||
int seconds = (int)timeInSeconds % 60;
|
||||
int milliseconds = (int)((timeInSeconds - (int)timeInSeconds) * 1000);
|
||||
|
||||
String formattedTime =
|
||||
String(minutes) + ":" + (seconds < 10 ? "0" : "") + String(seconds) +
|
||||
"." + (milliseconds < 10 ? "00" : (milliseconds < 100 ? "0" : "")) +
|
||||
String(milliseconds);
|
||||
|
||||
StaticJsonDocument<200> requestDoc;
|
||||
requestDoc["rfiduid"] = uid;
|
||||
requestDoc["location_name"] =
|
||||
getLocationIdFromPrefs(); // Aus den Einstellungen
|
||||
requestDoc["recorded_time"] = formattedTime;
|
||||
|
||||
String requestBody;
|
||||
serializeJson(requestDoc, requestBody);
|
||||
|
||||
Serial.println("API Request Body: " + requestBody);
|
||||
|
||||
int httpCode = http.POST(requestBody);
|
||||
|
||||
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_CREATED) {
|
||||
String response = http.getString();
|
||||
Serial.println("Zeit erfolgreich gesendet: " + response);
|
||||
} else {
|
||||
Serial.printf("Fehler beim Senden der Zeit: HTTP %d\n", httpCode);
|
||||
if (httpCode > 0) {
|
||||
String response = http.getString();
|
||||
Serial.println("Response: " + response);
|
||||
}
|
||||
}
|
||||
|
||||
http.end();
|
||||
}
|
||||
|
||||
// Funktionen für lokales Leaderboard
|
||||
void addLocalTime(String uid, String name, unsigned long timeMs) {
|
||||
LocalTime newTime;
|
||||
newTime.uid = uid;
|
||||
newTime.name = name;
|
||||
newTime.timeMs = timeMs;
|
||||
newTime.timestamp = millis();
|
||||
|
||||
localTimes.push_back(newTime);
|
||||
|
||||
Serial.printf("Lokale Zeit hinzugefügt: %s (%s) - %.2fs\n", name.c_str(),
|
||||
uid.c_str(), timeMs / 1000.0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user