File size: 3,450 Bytes
dd76471
 
 
bb90e77
 
 
 
dd76471
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb90e77
 
 
 
 
 
dd76471
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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}`);
});