|
| 1 | +const PLANET_DISTANCE = { |
| 2 | + Mercury: 3, |
| 3 | + Venus: 2, |
| 4 | + Earth: 0.5, |
| 5 | + Mars: 4, |
| 6 | + Jupiter: 25, |
| 7 | + Saturn: 50, |
| 8 | + Neptune: 200, |
| 9 | +}; |
| 10 | + |
| 11 | +const MODE_SPEED = { |
| 12 | + 1: 10, // NORMAL |
| 13 | + 2: 25, // TURBO |
| 14 | + 3: 100, // HYPERJUMP |
| 15 | +}; |
| 16 | + |
| 17 | +// Populate planet dropdown |
| 18 | +window.onload = () => { |
| 19 | + const select = document.getElementById("planet"); |
| 20 | + Object.keys(PLANET_DISTANCE).forEach((planet) => { |
| 21 | + const option = document.createElement("option"); |
| 22 | + option.value = planet; |
| 23 | + option.textContent = planet; |
| 24 | + select.appendChild(option); |
| 25 | + }); |
| 26 | +}; |
| 27 | + |
| 28 | +function estimateDeliveryTime(planet, mode, surgeLoad, weatherDelay) { |
| 29 | + if (!(planet in PLANET_DISTANCE)) { |
| 30 | + throw new Error("Unknown destination"); |
| 31 | + } |
| 32 | + if (!(mode in MODE_SPEED)) { |
| 33 | + throw new Error("Invalid delivery mode"); |
| 34 | + } |
| 35 | + if (surgeLoad < 1) { |
| 36 | + throw new Error("surgeLoad must be >= 1"); |
| 37 | + } |
| 38 | + if (weatherDelay < 0) { |
| 39 | + throw new Error("weatherDelay must be >= 0"); |
| 40 | + } |
| 41 | + |
| 42 | + const distance = PLANET_DISTANCE[planet]; |
| 43 | + const speed = MODE_SPEED[mode]; |
| 44 | + |
| 45 | + let travelTime = distance / speed; |
| 46 | + |
| 47 | + // Fatigue penalty for extreme distances |
| 48 | + if (distance > 100) { |
| 49 | + travelTime *= 1.2; |
| 50 | + } |
| 51 | + |
| 52 | + const total = travelTime * surgeLoad + weatherDelay; |
| 53 | + return Math.round(total * 100) / 100; |
| 54 | +} |
| 55 | + |
| 56 | +// Wire UI to estimator |
| 57 | +document.getElementById("estimate").onclick = () => { |
| 58 | + const planet = document.getElementById("planet").value; |
| 59 | + const mode = Number(document.getElementById("mode").value); |
| 60 | + const surge = Number(document.getElementById("surge").value); |
| 61 | + const weather = Number(document.getElementById("weather").value); |
| 62 | + |
| 63 | + try { |
| 64 | + const time = estimateDeliveryTime(planet, mode, surge, weather); |
| 65 | + document.getElementById("result").textContent = |
| 66 | + `Estimated delivery time: ${time} hours`; |
| 67 | + } catch (err) { |
| 68 | + document.getElementById("result").textContent = "Error: " + err.message; |
| 69 | + } |
| 70 | +}; |
0 commit comments