mirror of
https://github.com/technix/ham_radio.git
synced 2024-08-19 20:54:07 +02:00
better frequency validation
This commit is contained in:
parent
89581339a3
commit
67fe473658
@ -32,7 +32,7 @@ ham_radio.digiline_effector = function(pos, _, channel, msg)
|
|||||||
|
|
||||||
elseif msg.command == "frequency" then
|
elseif msg.command == "frequency" then
|
||||||
local new_frequency = msg.value
|
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)
|
meta:set_string("frequency", new_frequency)
|
||||||
ham_radio.transmitter_update_infotext(meta)
|
ham_radio.transmitter_update_infotext(meta)
|
||||||
ham_radio.save_transmitter(pos, meta)
|
ham_radio.save_transmitter(pos, meta)
|
||||||
|
26
helpers.lua
26
helpers.lua
@ -1,23 +1,33 @@
|
|||||||
function ham_radio.validate_frequency(frequency, is_receiver)
|
function ham_radio.validate_frequency(frequency, is_receiver)
|
||||||
if frequency == "" then
|
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
|
end
|
||||||
local transmission_is_allowed = true
|
local transmission_is_allowed = true
|
||||||
local num_freq = tonumber(frequency)
|
local num_freq = tonumber(frequency)
|
||||||
local freq = tostring(num_freq)
|
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
|
if num_freq >= ham_radio.settings.locked_frequency.min
|
||||||
and num_freq <= ham_radio.settings.locked_frequency.max then
|
and num_freq <= ham_radio.settings.locked_frequency.max then
|
||||||
-- transmitter is in locked frequency range
|
-- transmitter is in locked frequency range
|
||||||
transmission_is_allowed = false
|
transmission_is_allowed = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return freq == frequency
|
local result = true
|
||||||
and num_freq ~= nil
|
local message = ''
|
||||||
and num_freq == math.floor(num_freq)
|
if freq ~= frequency or num_freq ~= math.floor(num_freq) then
|
||||||
and num_freq >= ham_radio.settings.frequency.min
|
result = false
|
||||||
and num_freq <= ham_radio.settings.frequency.max
|
message = 'Error: invalid frequency value.'
|
||||||
and transmission_is_allowed
|
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
|
end
|
||||||
|
|
||||||
function ham_radio.find_transmitters(frequency)
|
function ham_radio.find_transmitters(frequency)
|
||||||
|
4
init.lua
4
init.lua
@ -40,6 +40,10 @@ function ham_radio.play_tuning_sound(player)
|
|||||||
)
|
)
|
||||||
end
|
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.."/config.lua")
|
||||||
|
|
||||||
dofile(modpath.."/helpers.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
|
if formname ~= "ham_radio:configure_handheld_receiver" or not minetest.is_player(player) then
|
||||||
return false
|
return false
|
||||||
end
|
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
|
return false
|
||||||
end
|
end
|
||||||
local item = player:get_wielded_item()
|
local item = player:get_wielded_item()
|
||||||
|
@ -62,11 +62,16 @@ minetest.register_node("ham_radio:transmitter", {
|
|||||||
if (
|
if (
|
||||||
fields.quit ~= "true"
|
fields.quit ~= "true"
|
||||||
or minetest.is_protected(pos, sender:get_player_name())
|
or minetest.is_protected(pos, sender:get_player_name())
|
||||||
or not ham_radio.validate_frequency(fields.frequency)
|
|
||||||
) then
|
) then
|
||||||
return
|
return
|
||||||
end
|
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)
|
local meta = minetest.get_meta(pos)
|
||||||
meta:set_string("frequency", fields.frequency)
|
meta:set_string("frequency", fields.frequency)
|
||||||
meta:set_string("rds_message", fields.rds_message)
|
meta:set_string("rds_message", fields.rds_message)
|
||||||
|
Loading…
Reference in New Issue
Block a user