ham_radio/helpers.lua

32 lines
979 B
Lua
Raw Normal View History

2019-12-07 20:24:49 +01:00
function ham_radio.validate_frequency(frequency)
2019-12-07 20:54:21 +01:00
local num_freq = tonumber(frequency)
2019-12-07 20:24:49 +01:00
local freq = tostring(num_freq)
return freq == frequency
2019-12-07 20:54:21 +01:00
and num_freq ~= nil
and num_freq == math.floor(num_freq)
2019-12-07 20:24:49 +01:00
and num_freq >= ham_radio.settings.frequency.min
and num_freq <= ham_radio.settings.frequency.max
end
2019-12-07 23:46:20 +01:00
function ham_radio.find_transmitters(frequency)
local transmitter_list = {}
for key, transmitter in pairs(ham_radio.transmitters) do
if transmitter.frequency == frequency then
transmitter_list[key] = transmitter
end
end
return transmitter_list
end
function ham_radio.find_free_frequency()
local frequency = -1
while frequency == -1 do
frequency = tostring(math.floor(math.random(ham_radio.settings.frequency.min, ham_radio.settings.frequency.max)));
local are_there_transmitters = ham_radio.find_transmitters(frequency)
if next(are_there_transmitters) then
frequency = -1
end
end
return frequency
end