J.A.R.V.I.S-rust/resources/commands/unit_convert/length.lua

34 lines
1.5 KiB
Lua
Raw Permalink Normal View History

2026-05-15 18:41:44 +03:00
-- Длина: метры ↔ футы, км ↔ мили
local phrase = (jarvis.context.phrase or ""):lower()
local n_str = phrase:match("([%d%.,]+)")
if not n_str then return jarvis.cmd.error("Не понял число.") end
local n = tonumber((n_str:gsub(",", ".")))
if not n then return jarvis.cmd.error("Не понял число.") end
-- detect "in" target — "в футы" / "в метры" / "в км" / "в мили"
-- and source from before "в" or from common units in phrase
local result, unit_out
if phrase:find("в фут") or phrase:find("to feet") then
-- meters → feet
result = n * 3.28084
unit_out = result < 2 and "фут" or (result < 5 and "фута" or "футов")
elseif phrase:find("в метр") or phrase:find("to meters") then
-- feet → meters
result = n / 3.28084
unit_out = "метров"
elseif phrase:find("в милях") or phrase:find("в миль") or phrase:find("to miles") then
-- km → miles
result = n * 0.621371
unit_out = result < 2 and "миля" or (result < 5 and "мили" or "миль")
elseif phrase:find("в километр") or phrase:find("в км") or phrase:find("to km") then
-- miles → km
result = n * 1.60934
unit_out = "километров"
else
return jarvis.cmd.error("Скажите в чём перевести: в метры/футы/км/мили.")
end
local rounded = math.floor(result * 100 + 0.5) / 100
return jarvis.cmd.ok(string.format("%g %s.", rounded, unit_out))