anti cheat: don't trust the client, move trip completions to server
All checks were successful
pedestrian-simulator / build (push) Successful in 1m11s

This commit is contained in:
2026-01-14 17:17:58 -07:00
parent f0172afb1e
commit 16c6c9c074
5 changed files with 121 additions and 83 deletions

View File

@@ -969,9 +969,21 @@ async function calculateAndStartRoute(startAddress, endAddress, isRestoring = fa
};
// Initialize step reference
// Initialize/Reset Server Trip
// Only start a new trip on server if this is a fresh start, not a restore
if (!isRestoring) {
fetch('/api/trip', { method: 'POST' }).catch(console.error);
const tripMetadata = {
trip_type: 'address',
route_name: `${leg.start_address} to ${leg.end_address}`,
start_address: leg.start_address,
end_address: leg.end_address,
total_distance: routeTotalDistance / 1000 // In km
};
fetch('/api/trip', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(tripMetadata)
}).catch(console.error);
}
// Reset display steps for visual consistency
@@ -2126,7 +2138,17 @@ function startFromCustomRoute(pathData, routeName, isRestoring = false, markers
// Initialize/Reset Server Trip
if (!isRestoring) {
fetch('/api/trip', { method: 'POST' }).catch(console.error);
const tripMetadata = {
trip_type: 'kml',
route_name: routeName,
kml_id: currentKmlFile ? currentKmlFile.id : null,
total_distance: routeTotalDistance / 1000 // In km
};
fetch('/api/trip', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(tripMetadata)
}).catch(console.error);
}
// Reset display steps
@@ -2274,26 +2296,13 @@ async function triggerCelebration(finalDistance) {
overlay.classList.add('active');
createConfetti();
// Report to backend
if (currentTripDetails) {
try {
await fetch('/api/trip/complete', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
type: currentTripDetails.type,
route_name: currentTripDetails.route_name,
start_address: currentTripDetails.start_address || '',
end_address: currentTripDetails.end_address || '',
kml_filename: currentTripDetails.kml_filename || '',
kml_owner_id: currentTripDetails.kml_owner_id || '',
distance: currentTripDetails.distance
})
});
} catch (err) {
console.error("Failed to record trip completion:", err);
}
}
// Clean up local state
localStorage.removeItem(LOCATION_STORAGE);
currentTripDetails = null;
masterPath = [];
routeTotalDistance = 0;
// We keep window.hasCelebrated = true to prevent immediate re-trigger
// but starting a new trip will reset it.
}
function createConfetti() {