File size: 10,276 Bytes
f7cecf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f058dc
 
f7cecf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { useState } from "react";
import { getPlayerPrice } from "../utils/fplLogic";

export const useFplSolverApi = (abortControllerRef) => {
  const [isSolving, setIsSolving] = useState(false);
  const [isChipSolving, setIsChipSolving] = useState(false);
  const [isRunningSens, setIsRunningSens] = useState(false);
  const [pendingSolutions, setPendingSolutions] = useState([]);
  const [chipSolveSolutions, setChipSolveSolutions] = useState([]);
  const [sensResults, setSensResults] = useState(null);
  const [sensViewGw, setSensViewGw] = useState(null);

  // Reusable helper to format players for the backend
  const formatMarketPlayers = (globalPlayers, teamData, horizonGWs) => {
    return globalPlayers.map((p) => {
      const squadPlayer = teamData.find((sp) => sp.ID === p.ID);
      const sellPrice = squadPlayer ? squadPlayer.Price : getPlayerPrice(p);
      const evs = {};
      horizonGWs.forEach((gw) => {
        evs[String(gw)] = Number(p[`${gw}_Pts`]) || 0;
      });
      return {
        id: p.ID,
        name: p.Name,
        pos: p.Pos,
        team: p.Team,
        now_cost: getPlayerPrice(p),
        sell_price: sellPrice,
        evs,
      };
    });
  };

  // Extracts the basic frontend settings (bans, locks, chips) into a clean dictionary
  const buildBaseSettings = (quickSettings, chipsByGw, forceIterations = null) => {
    return {
      decay_base: Number(quickSettings?.decay || 0.85),
      ft_value_base: Number(quickSettings?.ft_value || 1.5),
      iterations: forceIterations !== null ? forceIterations : Number(quickSettings?.iterations || 1),
      banned_ids: quickSettings?.banned?.map((p) => p.ID) || [],
      locked_ids: quickSettings?.locked?.map((p) => p.ID) || [],
      ban_this_gw: quickSettings?.ban_this_gw?.map((p) => p.ID) || [],
      lock_this_gw: quickSettings?.lock_this_gw?.map((p) => p.ID) || [],
      use_wc: Object.entries(chipsByGw || {}).filter(([, c]) => c === "wc").map(([g]) => Number(g)),
      use_fh: Object.entries(chipsByGw || {}).filter(([, c]) => c === "fh").map(([g]) => Number(g)),
      use_bb: Object.entries(chipsByGw || {}).filter(([, c]) => c === "bb").map(([g]) => Number(g)),
      use_tc: Object.entries(chipsByGw || {}).filter(([, c]) => c === "tc").map(([g]) => Number(g)),
    };
  };

  const handleSolve = async ({
    teamId, solveGWs, horizonGWs, teamData, globalPlayers, itb, availableFts,
    quickSettings, chipsByGw, comprehensiveSettings,
    lockedBaselineEv, pastBaselineEv // <-- ADDED HERE
  }) => {
    setIsSolving(true);
    abortControllerRef.current = new AbortController();
    try {
      const payload = {
        team_id: parseInt(teamId, 10) || 0,
        horizon_gws: solveGWs,
        current_squad_ids: teamData.filter((p) => !p.isBlank && typeof p.ID === "number").map((p) => p.ID),
        market_players: formatMarketPlayers(globalPlayers, teamData, horizonGWs),
        in_the_bank: itb,
        free_transfers: availableFts,
        settings: buildBaseSettings(quickSettings, chipsByGw),
        comprehensive_settings: comprehensiveSettings, 
      };

      const res = await fetch("https://anayshukla-fpl-solver.hf.space/api/solve", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(payload),
        signal: abortControllerRef.current.signal,
      });

      const data = await res.json();
      if (!res.ok) throw new Error(data.detail || data.message || "Solver failed");

      if (data.status === "success" && Array.isArray(data.solutions)) {
        // THE FIX: Inject the specific baselines and calculate the padded visual total
        const enhancedSolutions = data.solutions.map(sol => ({
          ...sol,
          lockedBaselineEv,
          paddedTotalEv: (sol.ev || 0) + (pastBaselineEv || 0)
        }));

        const sorted = [...enhancedSolutions].sort((a, b) => {
          const oa = a.objective_score != null ? a.objective_score : a.ev;
          const ob = b.objective_score != null ? b.objective_score : b.ev;
          if (ob !== oa) return ob - oa;
          return (b.ev || 0) - (a.ev || 0);
        });
        setPendingSolutions(sorted);
      } else {
        alert("Solver failed: " + (data.message || data.detail || "Unknown"));
      }
    } catch (err) {
      if (err.name === 'AbortError') return;
      alert(err.message || "Failed to run the solver.");
    } finally {
      setIsSolving(false);
    }
  };

  const handleChipSolve = async ({
    teamId, horizonGWs, teamData, globalPlayers, itb, availableFts,
    quickSettings, comprehensiveSettings, chipSolveOptions,
    lockedBaselineEv, pastBaselineEv // <-- ADDED HERE
  }) => {
    const hasOptions = Object.values(chipSolveOptions).some((v) => v.length > 0);
    if (!hasOptions) {
      alert("Select at least one GW for a chip before running the chip solve.");
      return;
    }
    
    setIsChipSolving(true);
    setChipSolveSolutions([]);
    abortControllerRef.current = new AbortController();

    try {
      const payload = {
        team_id: parseInt(teamId, 10) || 0,
        horizon_gws: horizonGWs,
        current_squad_ids: teamData.filter((p) => !p.isBlank && typeof p.ID === "number").map((p) => p.ID),
        market_players: formatMarketPlayers(globalPlayers, teamData, horizonGWs),
        in_the_bank: itb,
        free_transfers: availableFts,
        settings: buildBaseSettings(quickSettings, {}), 
        comprehensive_settings: comprehensiveSettings,
        chip_gw_options: {
          wc: chipSolveOptions.wc,
          fh: chipSolveOptions.fh,
          bb: chipSolveOptions.bb,
          tc: chipSolveOptions.tc,
        },
      };

      const res = await fetch("https://anayshukla-fpl-solver.hf.space/api/chip-solve", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(payload),
        signal: abortControllerRef.current.signal,
      });

      const data = await res.json();
      if (!res.ok) {
        const d = data.detail;
        const msg = typeof d === "string" ? d : Array.isArray(d) ? d.map((x) => x.msg || x).join(", ") : JSON.stringify(d);
        throw new Error(msg || "Chip solve failed");
      }

      if (data.status === "success" && Array.isArray(data.solutions)) {
        // THE FIX: Inject the specific baselines
        const enhancedSolutions = data.solutions.map(sol => ({
          ...sol,
          lockedBaselineEv,
          paddedTotalEv: (sol.ev || 0) + (pastBaselineEv || 0)
        }));
        setChipSolveSolutions(enhancedSolutions);
      } else {
        alert("Chip solve failed: " + (data.message || "Unknown error"));
      }
    } catch (err) {
      if (err.name === 'AbortError') return;
      alert(err.message || "Failed to run chip solve.");
    } finally {
      setIsChipSolving(false);
    }
  };

  const handleSensAnalysis = async ({
    teamId, solveGWs, horizonGWs, teamData, globalPlayers, itb, availableFts,
    quickSettings, chipsByGw, comprehensiveSettings, numSims, lockedBaselineEv, pastBaselineEv
  }) => {
    setIsRunningSens(true);
    setSensResults(null);
    abortControllerRef.current = new AbortController();

    try {
      const payload = {
        team_id: parseInt(teamId, 10) || 0,
        horizon_gws: solveGWs,
        current_squad_ids: teamData.filter((p) => !p.isBlank && typeof p.ID === "number").map((p) => p.ID),
        market_players: formatMarketPlayers(globalPlayers, teamData, horizonGWs),
        in_the_bank: itb,
        free_transfers: availableFts,
        settings: buildBaseSettings(quickSettings, chipsByGw, 1), // Force 1 iteration for sens analysis
        comprehensive_settings: comprehensiveSettings,
        num_sims: numSims,
        analysis_gw: solveGWs[0] || null,
      };

      const res = await fetch("https://anayshukla-fpl-solver.hf.space/api/sensitivity", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(payload),
        signal: abortControllerRef.current.signal,
      });

      const data = await res.json();
      if (!res.ok) {
        const d = data.detail;
        const msg = typeof d === "string" ? d : Array.isArray(d) ? d.map((x) => x.msg || x).join(", ") : JSON.stringify(d);
        throw new Error(msg || "Sensitivity analysis failed");
      }

      if (data.status === "success") {
        setSensResults(data);
        setSensViewGw(data.horizon_gws?.[0] ?? null);
      } else {
        alert("Sensitivity failed: " + (data.message || "Unknown error"));
      }
    } catch (err) {
      if (err.name === 'AbortError') return;
      alert(err.message || "Failed to run sensitivity analysis.");
    } finally {
      setIsRunningSens(false);
    }
  };

  const loadSettingsFromCloud = async (teamId) => {
    try {
      const res = await fetch(`https://anayshukla-fpl-solver.hf.space/api/settings/${teamId}`);
      const data = await res.json();
      if (data.status === "success") {
        console.log("📥 LOADED FROM CLOUD:", data);
        return {
          quick: data.quick_settings,
          advanced: data.advanced_settings 
        };
      }
    } catch (err) {
      console.warn("Failed to load cloud settings:", err);
    }
    return null;
  };

  const saveSettingsToCloud = async (teamId, quickSettings, compSettings) => {
    console.log("📤 SAVING TO CLOUD. Advanced Settings payload:", compSettings);
    try {
      await fetch("https://anayshukla-fpl-solver.hf.space/api/settings/save", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          team_id: parseInt(teamId, 10),
          quick_settings: quickSettings,
          advanced_settings: compSettings // <-- Safely maps React state to Python expectation
        })
      });
    } catch (err) {
      console.warn("Failed to save settings to cloud:", err);
    }
  };

  return {
    isSolving,
    isChipSolving,
    isRunningSens,
    pendingSolutions,
    setPendingSolutions,
    chipSolveSolutions,
    setChipSolveSolutions,
    sensResults,
    setSensResults,
    sensViewGw,
    setSensViewGw,
    handleSolve,
    handleChipSolve,
    handleSensAnalysis,
    loadSettingsFromCloud,
    saveSettingsToCloud,
  };
};