mirror of
https://github.com/MinetestForFun/minercantile.git
synced 2025-02-03 19:30:21 +01:00
add define price depending on wear
This commit is contained in:
parent
09d48dc614
commit
29b7a0440b
131
shop.lua
131
shop.lua
@ -134,24 +134,51 @@ function minercantile.shop.get_nb(item)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- sell fonction
|
-- sell fonction
|
||||||
function minercantile.shop.get_price(item, object)
|
function minercantile.shop.get_buy_price(item)
|
||||||
if item == "maptools:copper_coin" or item == "maptools:silver_coin" or item == "maptools:gold_coin" then -- dont's buy/sell coins
|
if item == "maptools:copper_coin" or item == "maptools:silver_coin" or item == "maptools:gold_coin" then -- dont's buy/sell coins
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
local price = nil
|
local price = nil
|
||||||
local money = minercantile.shop.get_money()
|
local money = minercantile.shop.get_money()
|
||||||
if not minercantile.stock.items[item] then
|
if not minercantile.stock.items[item] then
|
||||||
minercantile.stock.items[item] = {nb=math.random(5000, 10000)}
|
minercantile.stock.items[item] = {nb=math.random(500, 1000)}
|
||||||
end
|
end
|
||||||
|
|
||||||
local nb = minercantile.stock.items[item].nb
|
local nb = minercantile.stock.items[item].nb
|
||||||
if minercantile.stock.items[item].price ~= nil then -- if defined price
|
if minercantile.stock.items[item].price ~= nil then -- if defined price
|
||||||
price = math.ceil(minercantile.stock.items[item].price)
|
price = math.ceil(minercantile.stock.items[item].price)
|
||||||
elseif object == "sell" then
|
else
|
||||||
price = math.ceil((money/10)/(math.log(nb+2000+99)*10)*1000000/(math.pow((nb+2000+99),(2.01))))
|
|
||||||
elseif object == "buy" then
|
|
||||||
price = math.ceil((money/10)/(math.log(nb+2000-99)*10)*1000000/(math.pow((nb+2000-99),(2.01))))
|
price = math.ceil((money/10)/(math.log(nb+2000-99)*10)*1000000/(math.pow((nb+2000-99),(2.01))))
|
||||||
end
|
end
|
||||||
|
if price and price < 1 then price = 1 end
|
||||||
|
return price
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- sell fonction
|
||||||
|
function minercantile.shop.get_sell_price(item, wear)
|
||||||
|
if item == "maptools:copper_coin" or item == "maptools:silver_coin" or item == "maptools:gold_coin" then -- dont's buy/sell coins
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
local price = nil
|
||||||
|
local money = minercantile.shop.get_money()
|
||||||
|
if not minercantile.stock.items[item] then
|
||||||
|
minercantile.stock.items[item] = {nb=math.random(500, 1000)}
|
||||||
|
end
|
||||||
|
|
||||||
|
local nb = minercantile.stock.items[item].nb
|
||||||
|
|
||||||
|
if minercantile.stock.items[item].price ~= nil then -- if defined price
|
||||||
|
price = math.ceil(minercantile.stock.items[item].price)
|
||||||
|
else
|
||||||
|
price = math.ceil((money/10)/(math.log(nb+2000+99)*10)*1000000/(math.pow((nb+2000+99),(2.01))))
|
||||||
|
end
|
||||||
|
|
||||||
|
if wear and wear > 0 then --calcul price with % wear, (0-65535)
|
||||||
|
local pct = math.ceil(((65535-wear)*100)/65535)
|
||||||
|
price = math.floor((price * pct)/100)
|
||||||
|
end
|
||||||
|
|
||||||
if price < 1 then price = 1 end
|
if price < 1 then price = 1 end
|
||||||
return price
|
return price
|
||||||
end
|
end
|
||||||
@ -192,7 +219,7 @@ local function get_shop_inventory_by_page(name)
|
|||||||
if not item then break end
|
if not item then break end
|
||||||
local nb = minercantile.shop.get_nb(item)
|
local nb = minercantile.shop.get_nb(item)
|
||||||
if nb > 0 then
|
if nb > 0 then
|
||||||
local price = minercantile.shop.get_price(item, "buy")
|
local price = minercantile.shop.get_buy_price(item)
|
||||||
if price and price > 0 then
|
if price and price > 0 then
|
||||||
table.insert(inv_list, {name=item, nb=nb, price=price})
|
table.insert(inv_list, {name=item, nb=nb, price=price})
|
||||||
end
|
end
|
||||||
@ -208,7 +235,7 @@ local function get_shop_inventory_by_page(name)
|
|||||||
if item then
|
if item then
|
||||||
local nb = minercantile.shop.get_nb(item)
|
local nb = minercantile.shop.get_nb(item)
|
||||||
if nb > 0 then
|
if nb > 0 then
|
||||||
local price = minercantile.shop.get_price(item, "buy")
|
local price = minercantile.shop.get_buy_price(item)
|
||||||
if price and price > 0 then
|
if price and price > 0 then
|
||||||
table.insert(inv_list, {name=item,nb=nb,price=price})
|
table.insert(inv_list, {name=item,nb=nb,price=price})
|
||||||
end
|
end
|
||||||
@ -286,7 +313,7 @@ local function show_formspec_to_buy(name)
|
|||||||
local x = 0.2
|
local x = 0.2
|
||||||
local y = 2
|
local y = 2
|
||||||
local j = 1
|
local j = 1
|
||||||
--print(dump(inv_items))
|
|
||||||
for i=1, 32 do
|
for i=1, 32 do
|
||||||
local item = inv_items[i]
|
local item = inv_items[i]
|
||||||
if item then
|
if item then
|
||||||
@ -339,11 +366,8 @@ local function get_formspec_buy_items(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- sell
|
-- sell
|
||||||
function minercantile.shop.player_sell(name, item, nb, price)
|
function minercantile.shop.player_sell(name)
|
||||||
local player = minetest.get_player_by_name(name)
|
local player = minetest.get_player_by_name(name)
|
||||||
if not player then return false end
|
if not player then return false end
|
||||||
local player_inv = player:get_inventory()
|
local player_inv = player:get_inventory()
|
||||||
@ -353,16 +377,16 @@ function minercantile.shop.player_sell(name, item, nb, price)
|
|||||||
minetest.show_formspec(name, "minercantile:confirmed", "size[6,3]bgcolor[#2A2A2A;]label[2.6,0;Shop]label[1,1;Sorry, shop have not enough money]button[1.3,2.1;1.5,1;return_sell;Return]button_exit[3.3,2.1;1.5,1;close;Close]")
|
minetest.show_formspec(name, "minercantile:confirmed", "size[6,3]bgcolor[#2A2A2A;]label[2.6,0;Shop]label[1,1;Sorry, shop have not enough money]button[1.3,2.1;1.5,1;return_sell;Return]button_exit[3.3,2.1;1.5,1;close;Close]")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
local item = shop[name].item
|
||||||
|
local index = item.index
|
||||||
|
local nb = shop[name].nb
|
||||||
|
local price = shop[name].price
|
||||||
|
local stack = player_inv:get_stack("main", index)
|
||||||
|
local itname = stack:get_name()
|
||||||
|
local items_nb = stack:get_count()
|
||||||
|
|
||||||
local items_nb = 0
|
if itname ~= item.name or items_nb == 0 then
|
||||||
for i=1, player_inv:get_size("main") do
|
minetest.show_formspec(name, "minercantile:confirmed", "size[6,3]bgcolor[#2A2A2A;]label[2.6,0;Shop]label[1.7,1;Sorry, You have 0 item ..".. itname.."]button[1.3,2.1;1.5,1;return_sell;Return]button_exit[3.3,2.1;1.5,1;close;Close]")
|
||||||
if player_inv:get_stack("main", i):get_name() == item then
|
|
||||||
items_nb = items_nb + player_inv:get_stack("main", i):get_count()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if items_nb == 0 then
|
|
||||||
minetest.show_formspec(name, "minercantile:confirmed", "size[6,3]bgcolor[#2A2A2A;]label[2.6,0;Shop]label[1.7,1;Sorry, You have 0 item ..".. item.."]button[1.3,2.1;1.5,1;return_sell;Return]button_exit[3.3,2.1;1.5,1;close;Close]")
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -373,7 +397,7 @@ function minercantile.shop.player_sell(name, item, nb, price)
|
|||||||
|
|
||||||
local stock = minercantile.shop.get_nb(item)
|
local stock = minercantile.shop.get_nb(item)
|
||||||
if stock >= minercantile.shop.max_stock then
|
if stock >= minercantile.shop.max_stock then
|
||||||
minetest.show_formspec(name, "minercantile:confirmed", "size[6,3]bgcolor[#2A2A2A;]label[2.6,0;Shop]label[1.7,1;Sorry, the shop has too much stock of ..".. item.."]button[1.3,2.1;1.5,1;return_sell;Return]button_exit[3.3,2.1;1.5,1;close;Close]")
|
minetest.show_formspec(name, "minercantile:confirmed", "size[6,3]bgcolor[#2A2A2A;]label[2.6,0;Shop]label[1.7,1;Sorry, the shop has too much stock of ..".. itname.."]button[1.3,2.1;1.5,1;return_sell;Return]button_exit[3.3,2.1;1.5,1;close;Close]")
|
||||||
return false
|
return false
|
||||||
elseif (stock + item_can_sell) > minercantile.shop.max_stock then
|
elseif (stock + item_can_sell) > minercantile.shop.max_stock then
|
||||||
item_can_sell = (item_can_sell -((stock + item_can_sell) - minercantile.shop.max_stock))
|
item_can_sell = (item_can_sell -((stock + item_can_sell) - minercantile.shop.max_stock))
|
||||||
@ -390,20 +414,22 @@ function minercantile.shop.player_sell(name, item, nb, price)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local sell_price = math.floor(shop_can_buy * price)
|
local taken = stack:take_item(shop_can_buy)
|
||||||
|
|
||||||
local stack = ItemStack(item.." "..shop_can_buy)
|
local sell_price = math.floor((taken:get_count()) * price)
|
||||||
player_inv:remove_item("main", stack)
|
player_inv:set_stack("main", index, stack)
|
||||||
|
minercantile.stock.items[itname].nb = minercantile.stock.items[itname].nb + shop_can_buy
|
||||||
minercantile.stock.items[item].nb = minercantile.stock.items[item].nb + shop_can_buy
|
|
||||||
minercantile.shop.take_money(sell_price, true)
|
minercantile.shop.take_money(sell_price, true)
|
||||||
|
|
||||||
minercantile.wallet.give_money(name, sell_price, " Sell "..shop_can_buy .." "..item..", price "..sell_price)
|
minercantile.wallet.give_money(name, sell_price, " Sell "..shop_can_buy .." "..itname..", price "..sell_price)
|
||||||
minetest.show_formspec(name, "minercantile:confirmed", "size[6,3]bgcolor[#2A2A2A;]label[2.6,0;Shop]label[1,1;You sell "..shop_can_buy .." "..item..", price "..sell_price.."$]button[1.3,2.1;1.5,1;return_sell;Return]button_exit[3.3,2.1;1.5,1;close;Close]")
|
minetest.show_formspec(name, "minercantile:confirmed", "size[6,3]bgcolor[#2A2A2A;]label[2.6,0;Shop]label[1,1;You sell "..shop_can_buy .." "..itname..", price "..sell_price.."$]button[1.3,2.1;1.5,1;return_sell;Return]button_exit[3.3,2.1;1.5,1;close;Close]")
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- show sell formspec
|
-- show sell formspec
|
||||||
local function show_formspec_to_sell(name)
|
local function show_formspec_to_sell(name)
|
||||||
local player = minetest.get_player_by_name(name)
|
local player = minetest.get_player_by_name(name)
|
||||||
@ -412,34 +438,27 @@ local function show_formspec_to_sell(name)
|
|||||||
table.insert(formspec, "label[0,0;shop money:"..minercantile.shop.get_money().."$]")
|
table.insert(formspec, "label[0,0;shop money:"..minercantile.shop.get_money().."$]")
|
||||||
table.insert(formspec, "label[0,0.5;Your money:"..minercantile.wallet.get_money(name) .."$]")
|
table.insert(formspec, "label[0,0.5;Your money:"..minercantile.wallet.get_money(name) .."$]")
|
||||||
local player_inv = player:get_inventory()
|
local player_inv = player:get_inventory()
|
||||||
local inv_items = {}
|
shop[name] = {}
|
||||||
|
shop[name].items = {}
|
||||||
for i=1, player_inv:get_size("main") do
|
for i=1, player_inv:get_size("main") do
|
||||||
local stack = player_inv:get_stack("main", i)
|
local stack = player_inv:get_stack("main", i)
|
||||||
if not stack:is_empty() and minercantile.shop.is_available(stack:get_name()) then
|
if not stack:is_empty() and minercantile.shop.is_available(stack:get_name()) then
|
||||||
local item = stack:get_name()
|
local itname = stack:get_name()
|
||||||
local price = minercantile.shop.get_price(item, "sell")
|
local nb = stack:get_count()
|
||||||
|
local wear = stack:get_wear()
|
||||||
|
local price = minercantile.shop.get_sell_price(itname, wear)
|
||||||
if price and price > 0 then
|
if price and price > 0 then
|
||||||
if not inv_items[item] then
|
table.insert(shop[name].items, {name=itname, nb=nb, price=price, index=i, wear=wear})
|
||||||
inv_items[item] = {nb=0}
|
|
||||||
end
|
|
||||||
inv_items[item].nb = inv_items[item].nb + stack:get_count()
|
|
||||||
inv_items[item].price = price
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
shop[name] = {}
|
|
||||||
shop[name].items = table.copy(inv_items)
|
|
||||||
local inv_list = {}
|
|
||||||
for n, def in pairs(inv_items) do
|
|
||||||
table.insert(inv_list, {name=n,nb=def.nb,price=def.price})
|
|
||||||
end
|
|
||||||
|
|
||||||
local x = 0.2
|
local x = 0.2
|
||||||
local y = 1
|
local y = 1
|
||||||
for i=1, 32 do
|
for i=1, 32 do
|
||||||
local item = inv_list[i]
|
local item = shop[name].items[i]
|
||||||
if item then
|
if item then
|
||||||
table.insert(formspec, "item_image_button["..x..","..y..";1,1;"..tostring(item.name)..";buttonchoice_"..tostring(item.name)..";"..item.nb.."]")
|
table.insert(formspec, "item_image_button["..x..","..y..";1,1;"..tostring(item.name)..";buttonchoice_"..tostring(i)..";"..item.nb.."]")
|
||||||
table.insert(formspec, "label["..(x)..","..(y+0.8)..";"..item.price.."$]")
|
table.insert(formspec, "label["..(x)..","..(y+0.8)..";"..item.price.."$]")
|
||||||
else
|
else
|
||||||
table.insert(formspec, "image["..x..","..y..";1,1;minercantile_img_inv.png]")
|
table.insert(formspec, "image["..x..","..y..";1,1;minercantile_img_inv.png]")
|
||||||
@ -457,16 +476,19 @@ end
|
|||||||
|
|
||||||
local function get_formspec_sell_items(name)
|
local function get_formspec_sell_items(name)
|
||||||
local item = shop[name].item
|
local item = shop[name].item
|
||||||
|
|
||||||
|
local index = shop[name].index
|
||||||
local max = shop[name].max
|
local max = shop[name].max
|
||||||
local nb = shop[name].nb
|
local nb = shop[name].nb
|
||||||
local price = shop[name].price
|
local price = minercantile.shop.get_sell_price(item.name, item.wear)
|
||||||
|
shop[name].price = price
|
||||||
local formspec = {"size[8,6]bgcolor[#2A2A2A;]label[3.5,0;Sell Items]"}
|
local formspec = {"size[8,6]bgcolor[#2A2A2A;]label[3.5,0;Sell Items]"}
|
||||||
|
|
||||||
table.insert(formspec, "button[0.6,2;1,1;amount;-1]")
|
table.insert(formspec, "button[0.6,2;1,1;amount;-1]")
|
||||||
table.insert(formspec, "button[1.6,2;1,1;amount;-10]")
|
table.insert(formspec, "button[1.6,2;1,1;amount;-10]")
|
||||||
table.insert(formspec, "button[2.6,2;1,1;amount;-20]")
|
table.insert(formspec, "button[2.6,2;1,1;amount;-20]")
|
||||||
--table.insert(formspec, "label[3.7,5.2;"..tostring(nb).."]")
|
--table.insert(formspec, "label[3.7,5.2;"..tostring(nb).."]")
|
||||||
table.insert(formspec, "item_image_button[3.6,2;1,1;"..item..";buttonchoice_"..item..";"..nb.."]")
|
table.insert(formspec, "item_image_button[3.6,2;1,1;"..item.name..";buttonchoice_"..index..";"..nb.."]")
|
||||||
table.insert(formspec, "button[4.6,2;1,1;amount;+20]")
|
table.insert(formspec, "button[4.6,2;1,1;amount;+20]")
|
||||||
table.insert(formspec, "button[5.6,2;1,1;amount;+10]")
|
table.insert(formspec, "button[5.6,2;1,1;amount;+10]")
|
||||||
table.insert(formspec, "button[6.6,2;1,1;amount;+1]")
|
table.insert(formspec, "button[6.6,2;1,1;amount;+1]")
|
||||||
@ -541,7 +563,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
shop_buy[name].max = tonumber(n)
|
shop_buy[name].max = tonumber(n)
|
||||||
shop_buy[name].nb = 1
|
shop_buy[name].nb = 1
|
||||||
--shop_buy[name].price = shop_buy[name].items[shop_buy[name].item].price
|
--shop_buy[name].price = shop_buy[name].items[shop_buy[name].item].price
|
||||||
shop_buy[name].price = minercantile.shop.get_price(item, "buy")
|
shop_buy[name].price = minercantile.shop.get_buy_price(item)
|
||||||
minetest.show_formspec(name, "minercantile:shop_buy_items", get_formspec_buy_items(name))
|
minetest.show_formspec(name, "minercantile:shop_buy_items", get_formspec_buy_items(name))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -595,10 +617,15 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
if not shop[name] then
|
if not shop[name] then
|
||||||
shop[name] = {}
|
shop[name] = {}
|
||||||
end
|
end
|
||||||
shop[name].item = string.sub(b, 14)
|
local index = tonumber(string.sub(b, 14))
|
||||||
shop[name].max = tonumber(n)
|
shop[name].index = index
|
||||||
|
local item = shop[name].items[index]
|
||||||
|
shop[name].item = item
|
||||||
|
shop[name].itname = item.name
|
||||||
|
shop[name].max = item.nb
|
||||||
|
shop[name].wear = item.wear
|
||||||
shop[name].nb = 1
|
shop[name].nb = 1
|
||||||
shop[name].price = shop[name].items[shop[name].item].price
|
shop[name].price = minercantile.shop.get_sell_price(item.name, item.wear)
|
||||||
minetest.show_formspec(name, "minercantile:shop_sell_items", get_formspec_sell_items(name))
|
minetest.show_formspec(name, "minercantile:shop_sell_items", get_formspec_sell_items(name))
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -623,7 +650,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
show_formspec_to_sell(name)
|
show_formspec_to_sell(name)
|
||||||
return
|
return
|
||||||
elseif fields["confirm"] then
|
elseif fields["confirm"] then
|
||||||
minercantile.shop.player_sell(name, shop[name].item, shop[name].nb, shop[name].price)
|
minercantile.shop.player_sell(name)
|
||||||
return
|
return
|
||||||
elseif fields["quit"] then
|
elseif fields["quit"] then
|
||||||
shop[name] = nil
|
shop[name] = nil
|
||||||
|
Loading…
Reference in New Issue
Block a user