From b7c2354cd96061b37e6864602bf0eec0330914c5 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Mon, 17 Oct 2022 19:05:58 +0200 Subject: [PATCH] put yl_speak_up.get_list_of_effects_and_target_dialog_and_effect into extra function so that it can be used in fs_edit_trade_limit.lua as well --- fs_edit_options_dialog.lua | 76 ++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/fs_edit_options_dialog.lua b/fs_edit_options_dialog.lua index 917a03d..97cb5ae 100644 --- a/fs_edit_options_dialog.lua +++ b/fs_edit_options_dialog.lua @@ -1,4 +1,43 @@ +-- helper function; used by +-- * yl_speak_up.get_fs_edit_option_dialog and +-- * yl_speak_up.get_fs_edit_trade_limit +yl_speak_up.get_list_of_effects_and_target_dialog_and_effect = function(dialog, results, pname, target_dialog, target_effect) + local list_of_effects = "" + local count_effects = 0 + if(results) then + local sorted_key_list = yl_speak_up.sort_keys(results) + for i, k in ipairs(sorted_key_list) do + local v = results[ k ] + if v.r_type == "dialog" and (dialog.n_dialogs[v.r_value] ~= nil or v.r_value == "d_end" or v.r_value == "d_got_item") then + list_of_effects = list_of_effects.. + minetest.formspec_escape(v.r_id)..",#999999,".. + minetest.formspec_escape(v.r_type)..",".. + minetest.formspec_escape( + yl_speak_up.show_effect(v, pname)).."," + -- there may be more than one in the data structure + target_dialog = v.r_value + target_effect = v + elseif v.r_type ~= "dialog" then + list_of_effects = list_of_effects.. + minetest.formspec_escape(v.r_id)..",#FFFF00,".. + minetest.formspec_escape(v.r_type)..",".. + minetest.formspec_escape( + yl_speak_up.show_effect(v, pname)).."," + end + count_effects = count_effects + 1 + end + end + if(count_effects < yl_speak_up.max_result_effects) then + list_of_effects = list_of_effects..",#00FF00,add,Add a new (Ef)fect" + else + list_of_effects = list_of_effects..",#AAAAAA,-,".. + "Maximum amount of allowed (Ef)fects per option reached!" + end + return {list = list_of_effects, target_dialog = target_dialog, target_effect = target_effect} +end + + -- process input from formspec created in get_fs_edit_option_dialog(..) yl_speak_up.input_edit_option_dialog = function(player, formname, fields) if formname ~= "yl_speak_up:edit_option_dialog" then @@ -332,9 +371,7 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle -- set this to a fallback for yl_speak_up.show_colored_dialog_text local target_effect = {r_id = "-?-", r_type = "dialog"} -- and build the list of effects - local list_of_effects = "" local results = d_option.o_results - local count_effects = 0 -- create a new dialog type option if needed if(not(results) or not(next(results))) then target_dialog = yl_speak_up.prepare_new_dialog_for_option( @@ -344,35 +381,12 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle -- make sure we are up to date (a new option was inserted) results = d_option.o_results end - if(results) then - local sorted_key_list = yl_speak_up.sort_keys(results) - for i, k in ipairs(sorted_key_list) do - local v = results[ k ] - if v.r_type == "dialog" and (dialog.n_dialogs[v.r_value] ~= nil or v.r_value == "d_end" or v.r_value == "d_got_item") then - list_of_effects = list_of_effects.. - minetest.formspec_escape(v.r_id)..",#999999,".. - minetest.formspec_escape(v.r_type)..",".. - minetest.formspec_escape( - yl_speak_up.show_effect(v, pname)).."," - -- there may be more than one in the data structure - target_dialog = v.r_value - target_effect = v - elseif v.r_type ~= "dialog" then - list_of_effects = list_of_effects.. - minetest.formspec_escape(v.r_id)..",#FFFF00,".. - minetest.formspec_escape(v.r_type)..",".. - minetest.formspec_escape( - yl_speak_up.show_effect(v, pname)).."," - end - count_effects = count_effects + 1 - end - end - if(count_effects < yl_speak_up.max_result_effects) then - list_of_effects = list_of_effects..",#00FF00,add,Add a new (Ef)fect" - else - list_of_effects = list_of_effects..",#AAAAAA,-,".. - "Maximum amount of allowed (Ef)fects per option reached!" - end + -- constructs the list_of_effects; may also update target_dialog and target_effect + local res = yl_speak_up.get_list_of_effects_and_target_dialog_and_effect(dialog, results, pname, + target_dialog, target_effect) + list_of_effects = res.list + target_dialog = res.target_dialog + target_effect = res.target_effect -- if no target dialog has been selected: default is to go to the dialog with d_sort 0 if(not(target_dialog) or target_dialog == "" or