import express from 'express'; import cors from 'cors'; import { getLastRounds, getAllCotes } from './db.js'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); const PORT = 7860; app.use(cors()); app.use(express.json()); // Route de test app.get('/api/health', (req, res) => { res.json({ status: 'ok', service: 'aviator-api' }); }); // Route principale : Statistiques dynamiques app.get('/api/stats', (req, res) => { try { const seuil = parseFloat(req.query.seuil) || 2.0; const cotes = getAllCotes(); if (cotes.length === 0) { return res.json({ error: "Aucune donnée encore" }); } // 1. Calculs de base const totalRounds = cotes.length; const sommeCotes = cotes.reduce((a, b) => a + b, 0); const coteMoyenne = sommeCotes / totalRounds; const coteMin = Math.min(...cotes); const coteMax = Math.max(...cotes); // 2. Calculs liés au seuil (Win/Loss) let gains = 0; let pertes = 0; let currentStreak = 0; let maxStreakGains = 0; let maxStreakPertes = 0; cotes.forEach(cote => { const isWin = cote > seuil; if (isWin) { gains++; currentStreak = currentStreak > 0 ? currentStreak + 1 : 1; maxStreakGains = Math.max(maxStreakGains, currentStreak); } else { pertes++; currentStreak = currentStreak < 0 ? currentStreak - 1 : -1; maxStreakPertes = Math.max(maxStreakPertes, Math.abs(currentStreak)); } }); const pourcentageGains = ((gains / totalRounds) * 100).toFixed(2); const pourcentagePertes = ((pertes / totalRounds) * 100).toFixed(2); // 3. Valeur Espérée (EV) // Si on mise 1 et cashout à seuil : on gagne (seuil - 1) si on gagne, on perd 1 si on perd const ev = ((gains / totalRounds) * (seuil - 1)) - ((pertes / totalRounds) * 1); const avantageMaison = ((-ev) * 100).toFixed(2); // En pourcentage // 4. Derniers rounds pour le graphique const lastRounds = getLastRounds(100).reverse(); res.json({ seuil: seuil, totalRounds, coteMoyenne: coteMoyenne.toFixed(2), coteMin, coteMax, distribution: { gains: gains, pertes: pertes, pourcentageGains: parseFloat(pourcentageGains), pourcentagePertes: parseFloat(pourcentagePertes) }, streaks: { maxGains: maxStreakGains, maxPertes: maxStreakPertes }, valeurEsperee: ev.toFixed(4), avantageMaison: parseFloat(avantageMaison), historique: lastRounds }); } catch (error) { console.error("❌ [API] Erreur /stats:", error); res.status(500).json({ error: "Erreur serveur" }); } }); // Servir le frontend React compilé (en production) app.use(express.static(path.join(__dirname, '../../web/dist'))); app.get('*', (req, res) => { res.sendFile(path.join(__dirname, '../../web/dist/index.html')); }); app.listen(PORT, '0.0.0.0', () => { console.log(`🌐 [API] Serveur démarré sur le port ${PORT}`); });