better frequency validation

This commit is contained in:
techniX 2019-12-08 21:32:14 +02:00
parent 89581339a3
commit 67fe473658
5 changed files with 32 additions and 11 deletions

@ -32,7 +32,7 @@ ham_radio.digiline_effector = function(pos, _, channel, msg)
elseif msg.command == "frequency" then
local new_frequency = msg.value
if ham_radio.validate_frequency(new_frequency) then
if ham_radio.validate_frequency(new_frequency).result then
meta:set_string("frequency", new_frequency)
ham_radio.transmitter_update_infotext(meta)
ham_radio.save_transmitter(pos, meta)

@ -1,23 +1,33 @@
function ham_radio.validate_frequency(frequency, is_receiver)
if frequency == "" then
return true -- empty frequency is allowed to disable transmitter/receiver
return { result = true, message = '' } -- empty frequency is allowed to disable transmitter/receiver
end
local transmission_is_allowed = true
local num_freq = tonumber(frequency)
local freq = tostring(num_freq)
if is_receiver ~= nil and next(ham_radio.find_transmitters(frequency)) then
if is_receiver == nil and next(ham_radio.find_transmitters(frequency)) then
if num_freq >= ham_radio.settings.locked_frequency.min
and num_freq <= ham_radio.settings.locked_frequency.max then
-- transmitter is in locked frequency range
transmission_is_allowed = false
end
end
return freq == frequency
and num_freq ~= nil
and num_freq == math.floor(num_freq)
and num_freq >= ham_radio.settings.frequency.min
and num_freq <= ham_radio.settings.frequency.max
and transmission_is_allowed
local result = true
local message = ''
if freq ~= frequency or num_freq ~= math.floor(num_freq) then
result = false
message = 'Error: invalid frequency value.'
elseif num_freq == nil then
result = false
message = 'Error: frequency should be numeric.'
elseif num_freq < ham_radio.settings.frequency.min or num_freq > ham_radio.settings.frequency.max then
result = false
message = 'Error: frequency is out of range.'
elseif transmission_is_allowed == false then
result = false
message = 'Error: frequency is occupied by other transmitter.'
end
return { result = result, message = message }
end
function ham_radio.find_transmitters(frequency)

@ -40,6 +40,10 @@ function ham_radio.play_tuning_sound(player)
)
end
function ham_radio.errormsg(player, message)
minetest.chat_send_player(player:get_player_name(), minetest.colorize("#FCAD00", message))
end
dofile(modpath.."/config.lua")
dofile(modpath.."/helpers.lua")

@ -37,7 +37,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "ham_radio:configure_handheld_receiver" or not minetest.is_player(player) then
return false
end
if not ham_radio.validate_frequency(fields.frequency, true) then
local is_frequency_valid = ham_radio.validate_frequency(fields.frequency, true)
if is_frequency_valid.result == false then
ham_radio.errormsg(player, is_frequency_valid.message)
return false
end
local item = player:get_wielded_item()

@ -62,11 +62,16 @@ minetest.register_node("ham_radio:transmitter", {
if (
fields.quit ~= "true"
or minetest.is_protected(pos, sender:get_player_name())
or not ham_radio.validate_frequency(fields.frequency)
) then
return
end
local is_frequency_valid = ham_radio.validate_frequency(fields.frequency)
if is_frequency_valid.result == false then
ham_radio.errormsg(sender, is_frequency_valid.message)
return
end
local meta = minetest.get_meta(pos)
meta:set_string("frequency", fields.frequency)
meta:set_string("rds_message", fields.rds_message)