add trades again directly to the trade list

This commit is contained in:
Sokomine 2021-05-29 19:06:35 +02:00
parent 7fb42d95ab
commit 4ab240df52

View File

@ -130,11 +130,6 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
return 0
end
local pname = player:get_player_name()
-- the trade can only be changed in edit mode
if(yl_speak_up.edit_mode[pname] ~= yl_speak_up.speak_to[pname].n_id
or not(yl_speak_up.speak_to[pname].n_id)) then
return 0
end
-- we return from showing an error message (the player may not have noticed
-- a chat message while viewing a formspec; thus, we showed a formspec message)
@ -147,6 +142,30 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
-- which trade are we talking about?
local trade = yl_speak_up.trade[pname]
-- clicking on abort here when adding a new trade via the trade list
-- goes back to the trade list (does not require special privs)
if(fields.abort_trade_simple and trade and trade.trade_id and trade.trade_id == "new") then
-- we are no longer trading
yl_speak_up.trade[pname] = nil
-- ..else go back to the edit options formspec
minetest.show_formspec(pname, "yl_speak_up:trade_list",
yl_speak_up.get_fs_trade_list(player))
return
end
-- the trade can only be changed in edit mode
if(yl_speak_up.edit_mode[pname] ~= yl_speak_up.speak_to[pname].n_id
or not(yl_speak_up.speak_to[pname].n_id)) then
local dialog = yl_speak_up.speak_to[pname].dialog
-- exception: when adding a new trade via the trade list
-- (that is allowed without having to be in edit mode)
if(not(yl_speak_up.may_edit_npc(player, trade.n_id))
or not(dialog) or not(dialog.trades)
or not(trade.trade_id) or trade.trade_id ~= "new") then
return 0
end
end
local d_id = yl_speak_up.speak_to[pname].d_id
local n_id = yl_speak_up.speak_to[pname].n_id
local o_id = trade.o_id
@ -183,13 +202,16 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
local bs = buy:get_name().." "..tostring(buy:get_count())
local r_id = "?"
-- is this a trade attached to the trade list?
if(trade.trade_is_trade_list) then
if(trade.trade_is_trade_list or trade.trade_id == "new") then
-- if the player adds the same trade again, the ID is reused; other
-- than that, the ID is uniq
local trade_id = "trade "..ps.." for "..bs
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Trade: Added offer "..tostring(trade_id)..".")
dialog.trades[ trade_id ] = {pay={ps},buy={bs}}
trade.trade_id = "trade "..ps.." for "..bs
-- log the change
yl_speak_up.log_change(pname, n_id,
"Trade: Added offer "..tostring(trade.trade_id)..".")
-- add this new trade
dialog.trades[ trade.trade_id ] = {pay={ps},buy={bs}}
-- TODO: save dialog
-- is this a trade stored as a result of an option?
else
-- does this option have a trade result already?
@ -432,6 +454,13 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
"label[2.0,1.8;Ups! Something went wrong.]"..
"button[6.2,1.6;2.0,0.9;abort_trade_simple;Back]"
end
local delete_button =
"button[0.2,2.6;1.0,0.9;delete_trade_simple;Delete]"..
"tooltip[delete_trade_simple;Delete this trade.]"
-- no point in deleting a new trade - it doesn't exist yet
if(trade_id and trade_id == "new") then
delete_button = ""
end
return formspec..
-- show the second slot of the setup inventory in the detached player's inv
"list[detached:yl_speak_up_player_"..pname..";setup;2,1.5;1,1;]"..
@ -442,12 +471,11 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
"label[1.5,2.8;Put items in the two slots and click on \"Store trade\".]"..
"label[1.5,3.2;You will get your items back when storing the trade.]"..
"button[0.2,1.6;1.0,0.9;abort_trade_simple;Abort]"..
"button[0.2,2.6;1.0,0.9;delete_trade_simple;Delete]"..
delete_button..
"button[6.2,1.6;2.0,0.9;store_trade_simple;Store trade]"..
"tooltip[store_trade_simple;Click here to store this as a new trade. Your "..
"items will be returned to you and the trade will be shown the way "..
"the customer can see it.]"..
"tooltip[delete_trade_simple;Delete this trade.]"..
"tooltip[abort_trade_simple;Abort setting up this new trade.]"
end