summaryrefslogtreecommitdiff
path: root/frontend/src/Options.js
blob: b490f673f71eec46f78e19c3d9d213ec35fa0c96 (plain)
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
import React, { useState, useEffect } from 'react';
import './Options.css';
import useSSE from './useSSE';

const Options = () => {
  const [message, setMessage] = useState('');
  const [messageType, setMessageType] = useState('info');
  const [isSettingWaypoints, setIsSettingWaypoints] = useState(false);
  const [settings, setSettings] = useState({
    cargo: '',
    max_warp_speed: '',
    max_subwarp_speed: '',
    risk_cost: '',
    ship_cost: '',
    tax_rate: '',
    profit_threshold: '',
    max_capital: '',
    snipe_profit_threshold: ''
  });
  const { data: sseData } = useSSE('http://localhost:8000/data_stream');
  const route = Array.isArray(sseData?.route) ? sseData.route : [];
  const hasRouteWaypoints = route.length > 1;

  useEffect(() => {
    fetch('http://localhost:8000/settings')
      .then(res => res.json())
      .then(data => {
        setSettings({
          cargo: data.cargo,
          max_warp_speed: data.max_warp_speed,
          max_subwarp_speed: data.max_subwarp_speed,
          risk_cost: data.risk_cost,
          ship_cost: data.ship_cost,
          tax_rate: data.tax_rate,
          profit_threshold: data.profit_threshold,
          max_capital: data.max_capital,
          snipe_profit_threshold: data.snipe_profit_threshold
        });
      })
      .catch(err => console.error("Error fetching settings", err));
  }, []);

  const handleSettingsChange = (e) => {
    const { name, value } = e.target;
    setSettings(prev => ({ ...prev, [name]: value }));
  };

  const handleSaveSettings = async () => {
    try {
      const response = await fetch('http://localhost:8000/settings', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          cargo: Number(settings.cargo),
          max_warp_speed: Number(settings.max_warp_speed),
          max_subwarp_speed: Number(settings.max_subwarp_speed),
          risk_cost: Number(settings.risk_cost),
          ship_cost: Number(settings.ship_cost),
          tax_rate: Number(settings.tax_rate),
          profit_threshold: Number(settings.profit_threshold),
          max_capital: Number(settings.max_capital),
          snipe_profit_threshold: Number(settings.snipe_profit_threshold)
        })
      });
      if (response.ok) {
        setMessageType('success');
        setMessage('Settings saved');
        setTimeout(() => setMessage(''), 3000);
      }
    } catch (error) {
      setMessageType('error');
      setMessage(`Error: ${error.message}`);
    }
  };

  const handleCreateRoute = async () => {
    try {
      await fetch('http://localhost:8000/create_route', { method: 'POST' });
    } catch (error) {
      setMessage(`Error: ${error}`);
    }
  };

  const handleSetWaypoints = async () => {
    if (!hasRouteWaypoints) {
      setMessageType('error');
      setMessage('No route exists');
      return;
    }

    setIsSettingWaypoints(true);
    setMessageType('info');
    setMessage('Sending to EVE...');
    try {
      const response = await fetch('http://localhost:8000/set_waypoints', { method: 'POST' });
      const resData = await response.json();
      const nextMessage = resData.message || resData.status || 'Waypoint request completed';
      const nextType = resData.success ? 'success' : resData.status === 'partial' ? 'warning' : 'error';
      setMessageType(nextType);
      setMessage(nextMessage);
      if (resData.success) {
        setTimeout(() => setMessage(''), 4000);
      }
    } catch (error) {
      setMessageType('error');
      setMessage(`Error: ${error}`);
    } finally {
      setIsSettingWaypoints(false);
    }
  };

  const messageClassName = messageType === 'error'
    ? 'mb-1 text-red-400'
    : messageType === 'warning'
      ? 'mb-1 text-yellow-300'
      : 'mb-1 text-green-400';

  const waypointButtonClassName = hasRouteWaypoints && !isSettingWaypoints
    ? 'options-button flex-1 px-2 py-1 text-sm bg-green-700 hover:bg-green-600 text-white rounded'
    : 'options-button flex-1 px-2 py-1 text-sm bg-green-900 text-white rounded opacity-50 cursor-not-allowed';

  return (
    <div className="p-2 options-container flex flex-col content-start h-full text-white text-xs">
      <div className="flex gap-2 mb-2">
        <button className="options-button flex-1 px-2 py-1 text-sm bg-blue-600 hover:bg-blue-500 text-white rounded" onClick={handleCreateRoute}>
          Create Route
        </button>
        <button
          className={waypointButtonClassName}
          onClick={handleSetWaypoints}
          disabled={!hasRouteWaypoints || isSettingWaypoints}
        >
          Set in Game
        </button>
      </div>
      
      {message && <p className={messageClassName}>{message}</p>}

      <div className="settings-form grid grid-cols-4 gap-x-2 gap-y-1 items-center">
        <label>Cargo (m³)</label>
        <input name="cargo" type="number" value={settings.cargo} onChange={handleSettingsChange} className="text-black p-1 w-full" />
        
        <label>Risk (ISK)</label>
        <input name="risk_cost" type="number" value={settings.risk_cost} onChange={handleSettingsChange} className="text-black p-1 w-full" />
        
        <label>Warp (AU/s)</label>
        <input name="max_warp_speed" type="number" step="0.1" value={settings.max_warp_speed} onChange={handleSettingsChange} className="text-black p-1 w-full" />
        
        <label>Ship (ISK)</label>
        <input name="ship_cost" type="number" value={settings.ship_cost} onChange={handleSettingsChange} className="text-black p-1 w-full" />
        
        <label>Sub (m/s)</label>
        <input name="max_subwarp_speed" type="number" step="0.1" value={settings.max_subwarp_speed} onChange={handleSettingsChange} className="text-black p-1 w-full" />
        
        <label>Tax (%)</label>
        <input name="tax_rate" type="number" step="0.1" value={settings.tax_rate} onChange={handleSettingsChange} className="text-black p-1 w-full" />

        <label>Prof. Thr.</label>
        <input name="profit_threshold" type="number" value={settings.profit_threshold} onChange={handleSettingsChange} className="text-black p-1 w-full" />
        
        <label>Max Cap.</label>
        <input name="max_capital" type="number" value={settings.max_capital} onChange={handleSettingsChange} className="text-black p-1 w-full" />

        <label>Snipe Thr.</label>
        <input name="snipe_profit_threshold" type="number" value={settings.snipe_profit_threshold} onChange={handleSettingsChange} className="text-black p-1 w-full" />
      </div>
      
      <button className="mt-2 bg-blue-600 hover:bg-blue-500 py-1 px-2 text-white font-bold rounded" onClick={handleSaveSettings}>
        Save
      </button>
    </div>
  );
}

export default Options;