made delete button work for buy via button

This commit is contained in:
Sokomine 2022-09-09 20:23:54 +02:00
parent 60ae2585ae
commit 4067fd8f59
2 changed files with 79 additions and 54 deletions

View File

@ -110,7 +110,12 @@ yl_speak_up.input_trade_via_buy_button = function(player, formname, fields)
-- this is another trade; count from 0 again
yl_speak_up.speak_to[pname].trade_done = nil
end
-- TODO: delete button if(fields.delete_trade_via_buy_button) then
if(fields.delete_trade_via_buy_button) then
local trade_id = yl_speak_up.speak_to[pname].trade_id
yl_speak_up.delete_trade_simple(player, trade_id)
return
end
-- the owner wants to go back to the trade list from a dialog trade (action) view
if(fields.back_to_trade_list_dialog_trade) then
@ -340,7 +345,10 @@ yl_speak_up.get_fs_trade_via_buy_button = function(player, trade_id)
"NPC and want to get back to the trade list.]")
-- go back to dialog
table.insert(formspec, "button[4.2,0.0;3.8,1.0;back_to_dialog;Back to dialog ")
table.insert(formspec, minetest.formspec_escape(dialog.trades[ trade_id ].d_id))
table.insert(formspec, minetest.formspec_escape(dialog.trades[ trade_id ].d_id)..
" (option "..
minetest.formspec_escape(dialog.trades[ trade_id ].o_id)..
")")
table.insert(formspec, "]")
table.insert(formspec, "tooltip[back_to_dialog;"..
"Click here once you've traded enough with this "..

View File

@ -6,6 +6,73 @@ yl_speak_up.trade_fail_fs = "size[6,2]"..
"button_exit[2,1.5;1,0.9;exit;Exit]"
-- helper function for
-- yl_speak_up.input_do_trade_simple (here) and
-- yl_speak_up.input_trade_via_buy_button (in fs_trade_via_buy_button.lua)
--
-- delete a trade; this can be done here only if..
-- * it is a trade from the trade list (not an effect of a dialog option)
-- * it is a trade associated with a dialog option and the player is in
-- edit mode
-- * the player has the necessary privs
-- This option is available without having to enter edit mode first.
yl_speak_up.delete_trade_simple = function(player, trade_id)
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
-- not a really helpful message - but then, this should never happen (player probably cheated)
return yl_speak_up.trade_fail_msg
end
-- get the necessary dialog data
local dialog = yl_speak_up.speak_to[pname].dialog
-- store d_id and o_id in order to be able to return to the right
-- edit options dialog
local back_to_d_id = nil
local back_to_o_id = nil
if(dialog and dialog.trades and trade_id
and dialog.trades[ trade_id ] and n_id) then
if( dialog.trades[ trade_id ].d_id
and yl_speak_up.edit_mode[pname] ~= n_id) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:do_trade_simple",
formspec = "size[6,2]"..
"label[0.2,-0.2;"..
"Trades that are attached to dialog options\n"..
"can only be deleted in edit mode. Please tell\n"..
"your NPC that you are its owner and have\n"..
"new commands!]"..
"button[2,1.5;1,0.9;back_from_error_msg;Back]"})
return
end
if( dialog.trades[ trade_id ].d_id ) then
back_to_d_id = dialog.trades[ trade_id ].d_id
back_to_o_id = dialog.trades[ trade_id ].o_id
end
-- log the change
yl_speak_up.log_change(pname, n_id,
"Trade: Deleted offer "..tostring(trade_id)..".")
-- delete this particular trade
dialog.trades[ trade_id ] = nil
-- actually save the dialog to disk
yl_speak_up.save_dialog(n_id, dialog)
-- we are done with this trade
yl_speak_up.trade[pname] = nil
yl_speak_up.speak_to[pname].trade_id = nil
yl_speak_up.speak_to[pname].trade_done = nil
end
-- always return to edit options dialog if deleting a trade that belonged to one
if(back_to_d_id and back_to_o_id) then
yl_speak_up.show_fs(player, "edit_option_dialog",
{n_id = n_id, d_id = back_to_d_id, o_id = back_to_o_id})
return
end
-- go back showing the trade list (since we deleted this trade)
yl_speak_up.show_fs(player, "trade_list")
return
end
-- possible inputs:
-- fields.edit_trade_simple go on to showing the add_trade_simple formspec
-- fields.abort_trade_simple, ESC, depends on context
@ -73,58 +140,8 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields)
end
end
-- delete a trade; this can be done here only if..
-- * it is a trade from the trade list (not an effect of a dialog option)
-- * it is a trade associated with a dialog option and the player is in
-- edit mode
-- * the player has the necessary privs
-- This option is available without having to enter edit mode first.
if(fields.delete_trade_simple
and yl_speak_up.may_edit_npc(player, trade.n_id)) then
-- get the necessary dialog data
local dialog = yl_speak_up.speak_to[pname].dialog
-- store d_id and o_id in order to be able to return to the right
-- edit options dialog
local back_to_d_id = nil
local back_to_o_id = nil
if(dialog and dialog.trades and trade.trade_id
and dialog.trades[ trade.trade_id ] and trade.n_id) then
if( dialog.trades[ trade.trade_id ].d_id
and yl_speak_up.edit_mode[pname] ~= trade.n_id) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:do_trade_simple",
formspec = "size[6,2]"..
"label[0.2,-0.2;"..
"Trades that are attached to dialog options\n"..
"can only be deleted in edit mode. Please tell\n"..
"your NPC that you are its owner and have\n"..
"new commands!]"..
"button[2,1.5;1,0.9;back_from_error_msg;Back]"})
return
end
if( dialog.trades[ trade.trade_id ].d_id ) then
back_to_d_id = dialog.trades[ trade.trade_id ].d_id
back_to_o_id = dialog.trades[ trade.trade_id ].o_id
end
-- log the change
yl_speak_up.log_change(pname, n_id,
"Trade: Deleted offer "..tostring(trade.trade_id)..".")
-- delete this particular trade
dialog.trades[ trade.trade_id ] = nil
-- actually save the dialog to disk
yl_speak_up.save_dialog(n_id, dialog)
-- we are done with this trade
yl_speak_up.trade[pname] = nil
end
-- always return to edit options dialog if deleting a trade that belonged to one
if(back_to_d_id and back_to_o_id) then
yl_speak_up.show_fs(player, "edit_option_dialog",
{n_id = n_id, d_id = back_to_d_id, o_id = back_to_o_id})
return
end
-- go back showing the trade list (since we deleted this trade)
yl_speak_up.show_fs(player, "trade_list")
if(fields.delete_trade_simple) then
yl_speak_up.delete_trade_simple(player, trade.trade_id)
return
end