delete trades from trade_list even if not in edit mode

This commit is contained in:
Sokomine 2021-05-29 18:02:57 +02:00
parent 08f6e0d309
commit 7fb42d95ab
3 changed files with 42 additions and 9 deletions

View File

@ -120,3 +120,6 @@ The trade list can be accessed from the NPC's inventory.
If there are trades that ought to show up in the general trade list (i.e. not If there are trades that ought to show up in the general trade list (i.e. not
only attached to dialog options), then a button "Let's trade" will be shown only attached to dialog options), then a button "Let's trade" will be shown
as option for the first dialog. as option for the first dialog.
Trades that are attached to the trade list (and not dialog options) can be
added and deleted without entering edit mode ("I am your owner. ...").

View File

@ -112,9 +112,8 @@ yl_speak_up.get_fs_trade_list = function(player)
"label[1,1;Sorry. There are currently no offers available.]") "label[1,1;Sorry. There are currently no offers available.]")
end end
-- button "add trade" for those who can edit the NPC -- button "add trade" for those who can edit the NPC (entering edit mode is not required)
if((yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id) if(yl_speak_up.may_edit_npc(player, n_id)) then
and (yl_speak_up.speak_to[pname].n_id)) then
table.insert(formspec, table.insert(formspec,
"button["..tostring(yl_speak_up.trade_max_cols * 4 - 2.2).. "button["..tostring(yl_speak_up.trade_max_cols * 4 - 2.2)..
",0.2;2.0,0.9;trade_list_add_trade;Add trade]") ",0.2;2.0,0.9;trade_list_add_trade;Add trade]")

View File

@ -9,6 +9,7 @@ yl_speak_up.trade_fail_fs = "size[6,2]"..
-- possible inputs: -- possible inputs:
-- fields.edit_trade_simple go on to showing the add_trade_simple formspec -- fields.edit_trade_simple go on to showing the add_trade_simple formspec
-- fields.abort_trade_simple, ESC, depends on context -- fields.abort_trade_simple, ESC, depends on context
-- fields.delete_trade_simple delete this trade
-- fields.finished_trading -- fields.finished_trading
-- if in edit mode: go back to edit options dialog -- if in edit mode: go back to edit options dialog
-- if traded at least once: go on to the target dialog -- if traded at least once: go on to the target dialog
@ -29,6 +30,32 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields)
return return
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)
-- * 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
if(dialog and dialog.trades and trade.trade_id
and dialog.trades[ trade.trade_id ] and
not(dialog.trades[ trade.trade_id ].d_id)) then
-- 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
-- TODO: save dialog
-- we are done with this trade
yl_speak_up.trade[pname] = nil
end
-- go back showing the trade list (since we deleted this trade)
minetest.show_formspec(pname, "yl_speak_up:trade_list",
yl_speak_up.get_fs_trade_list(player))
return
end
-- can the player edit this trade? -- can the player edit this trade?
if(fields.edit_trade_simple if(fields.edit_trade_simple
and (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id and (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id
@ -216,7 +243,7 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
-- record this as a change -- record this as a change
table.insert(yl_speak_up.npc_was_changed[ n_id ], table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Trade: Deleted offer "..tostring(trade.trade_id)..".") "Trade: Deleted offer "..tostring(trade.trade_id)..".")
-- delete this particular trade and update the IDs in the array -- delete this particular trade
dialog.trades[ trade.trade_id ] = nil dialog.trades[ trade.trade_id ] = nil
elseif(trade.r_id) then elseif(trade.r_id) then
-- record this as a change -- record this as a change
@ -374,7 +401,8 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
trade_done = 0, trade_done = 0,
-- we need to know which option this is -- we need to know which option this is
target_dialog = d_id, target_dialog = d_id,
trade_is_trade_list = true trade_is_trade_list = true,
trade_id = trade_id
} }
if(dialog.trades[ trade_id ]) then if(dialog.trades[ trade_id ]) then
trade.player_gives = dialog.trades[ trade_id ].pay[1] trade.player_gives = dialog.trades[ trade_id ].pay[1]
@ -433,16 +461,18 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(trade.n_id)}) local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(trade.n_id)})
-- show edit button for the owner if in edit_mode -- show edit button for the owner if in edit_mode
if(yl_speak_up.may_edit_npc(player, trade.n_id) if(yl_speak_up.may_edit_npc(player, trade.n_id)) then
and (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id)) then
-- for trades as part of the results/effects: allow edit -- for trades as part of the results/effects: allow edit
if(not(trade.trade_is_trade_list)) then if(not(trade.trade_is_trade_list)
and (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id)) then
formspec = formspec.. formspec = formspec..
"button[0.2,1.6;1.0,0.9;edit_trade_simple;Edit]".. "button[0.2,1.6;1.0,0.9;edit_trade_simple;Edit]"..
"tooltip[edit_trade_simple;Edit this trade. You can do so only ".. "tooltip[edit_trade_simple;Edit this trade. You can do so only "..
"if you can edit the NPC as such (i.e. own it).]" "if you can edit the NPC as such (i.e. own it).]"
-- for trades in trade list: allow delete (new trades can easily be added) -- for trades in trade list: allow delete (new trades can easily be added)
else -- allow delete for trades in trade list even if not in edit mode
-- (entering edit mode for that would be too much work)
elseif(trade.trade_is_trade_list) then
-- TODO: make this button work -- TODO: make this button work
formspec = formspec.. formspec = formspec..
"button[0.2,1.6;1.2,0.9;delete_trade_simple;Delete]".. "button[0.2,1.6;1.2,0.9;delete_trade_simple;Delete]"..
@ -515,6 +545,7 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
"button[0.2,0.0;2.0,0.9;back_to_trade_list;Back to list]".. "button[0.2,0.0;2.0,0.9;back_to_trade_list;Back to list]"..
"tooltip[back_to_trade_list;Click here once you've traded enough with this ".. "tooltip[back_to_trade_list;Click here once you've traded enough with this "..
"NPC and want to get back to the trade list.]" "NPC and want to get back to the trade list.]"
-- TODO: offer button to show the edit options dialog if suitable
end end
return formspec.. return formspec..