implemented trading via buy button in yl_speak_up.do_trade_direct (plus showing how many times traded)
This commit is contained in:
parent
b313f6fd98
commit
9c95aa2074
@ -42,6 +42,16 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields)
|
||||
end
|
||||
|
||||
if(fields.buy_directly) then
|
||||
local error_msg = yl_speak_up.do_trade_direct(player)
|
||||
|
||||
if(error_msg ~= "") 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;"..error_msg.."]"..
|
||||
"button[2,1.5;1,0.9;back_from_error_msg;Back]"})
|
||||
return
|
||||
end
|
||||
yl_speak_up.show_fs(player, "trade_simple", yl_speak_up.speak_to[pname].trade_id)
|
||||
return
|
||||
end
|
||||
@ -516,6 +526,65 @@ yl_speak_up.do_trade_simple = function(player, count)
|
||||
end
|
||||
|
||||
|
||||
-- try to do the trade directly - without moving items in the buy/sell inventory slot
|
||||
-- returns error_msg or "" when successful
|
||||
yl_speak_up.do_trade_direct = function(player)
|
||||
if(not(player)) then
|
||||
return "Player, where are you?"
|
||||
end
|
||||
local pname = player:get_player_name()
|
||||
-- which trade are we talking about?
|
||||
local trade = yl_speak_up.trade[pname]
|
||||
-- do we have all the necessary data?
|
||||
if(not(trade) or trade.trade_type ~= "trade_simple") then
|
||||
return "No trade found!"
|
||||
end
|
||||
-- the players' inventory
|
||||
local player_inv = player:get_inventory()
|
||||
-- the NPCs' inventory
|
||||
local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(trade.n_id)})
|
||||
-- has the NPC the item he wants to sell?
|
||||
if( not(npc_inv:contains_item("npc_main", trade.npc_gives))) then
|
||||
return "Sorry. This item is sold out!"
|
||||
-- has the NPC room for the payment?
|
||||
elseif(not(npc_inv:room_for_item("npc_main", trade.player_gives))) then
|
||||
return "Sorry. No room to store your payment!\n"..
|
||||
"Please try again later."
|
||||
-- can the player pay the price?
|
||||
elseif(not(player_inv:contains_item("main", trade.player_gives))) then
|
||||
return "You can't pay the price!"
|
||||
-- has the player room for the sold item?
|
||||
elseif(not(player_inv:room_for_item("main", trade.npc_gives))) then
|
||||
return "You don't have enough free inventory space.\n"..
|
||||
"Trade aborted."
|
||||
end
|
||||
local payment = player_inv:remove_item("main", trade.player_gives)
|
||||
local sold = npc_inv:remove_item("npc_main", trade.npc_gives)
|
||||
-- used items cannot be sold as there is no fair way to indicate how
|
||||
-- much they are used
|
||||
if(payment:get_wear() > 0 or sold:get_wear() > 0) then
|
||||
-- revert the trade
|
||||
player_inv:add_item("main", payment)
|
||||
npc_inv:add_item("npc_main", sold)
|
||||
return "At least one of the items that shall be traded\n"..
|
||||
"is dammaged. Trade aborted."
|
||||
end
|
||||
player_inv:add_item("main", sold)
|
||||
npc_inv:add_item("npc_main", payment)
|
||||
-- save the inventory of the npc so that the payment does not get lost
|
||||
yl_speak_up.save_npc_inventory( trade.n_id )
|
||||
-- store for statistics how many times the player has executed this trade
|
||||
-- (this is also necessary to switch to the right target dialog when
|
||||
-- dealing with dialog options trades)
|
||||
yl_speak_up.trade[pname].trade_done = yl_speak_up.trade[pname].trade_done + 1
|
||||
-- log the trade
|
||||
yl_speak_up.log_change(pname, trade.n_id,
|
||||
"bought "..tostring(trade.npc_gives)..
|
||||
" for "..tostring(trade.player_gives))
|
||||
return ""
|
||||
end
|
||||
|
||||
|
||||
-- simple trade: one item(stack) for another
|
||||
-- handles configuration of new trades and showing the formspec for trades;
|
||||
-- checks if payment and buying is possible
|
||||
@ -688,6 +757,11 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
|
||||
"free inventory space to store your purchase."
|
||||
end
|
||||
|
||||
local trades_done = "Not yet traded."
|
||||
if(yl_speak_up.trade[pname].trade_done > 0) then
|
||||
trades_done = "Traded: "..tostring(yl_speak_up.trade[pname].trade_done).." time(s)"
|
||||
end
|
||||
|
||||
return formspec..
|
||||
"label[2.5,0.0;Trading with "..minetest.formspec_escape(trade.npc_name).."]"..
|
||||
"label[1.5,0.7;You pay:]"..
|
||||
@ -701,7 +775,8 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
|
||||
"list[detached:yl_speak_up_player_"..pname..";pay;2,2.0;1,1;]" ..
|
||||
-- show the buy slot from the same inventory
|
||||
"list[detached:yl_speak_up_player_"..pname..";buy;5,2.0;1,1;]" ..
|
||||
"label[1.5,3.0;"..trade_possible_msg.."]"
|
||||
"label[1.5,3.0;"..trade_possible_msg.."]"..
|
||||
"label[6.0,1.5;"..trades_done.."]"
|
||||
end
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user