selling and buying is working now

master
ademant 2019-02-21 14:36:30 +01:00
parent 1b3c4e1ba2
commit 65b11dab22
6 changed files with 237 additions and 43 deletions

38
api.lua
View File

@ -1,7 +1,11 @@
kiosk.inventar={}
function kiosk.demangle_for_formspec(str)
-- copied from unified_inventory
return string.gsub(str, "_([0-9]+)_", function (v) return string.char(v) end)
end
kiosk.add_inventar=function(name,buy_value,sell_value,stock)
kiosk.add_inventar=function(name,buy_value,sell_value,stock,fixed)
if name == nil then
return
end
@ -22,15 +26,20 @@ kiosk.add_inventar=function(name,buy_value,sell_value,stock)
end
if kiosk.inventar[name] == nil then
kiosk.inventar[name] = {name = name,
local kinv = {name = name,
sell = sell_value,
buy = sell_value,
stock=stock,
}
if fixed ~= nil then kinv.fixed=fixed end
kiosk.inventar[name] = kinv
else
local kiosk_inv=kiosk.inventar[name]
kiosk_inv.sell=sell_value
kiosk_inv.buy=buy_value
if fixed ~= nil then
kiosk_inv.fixed = fixed
end
end
end
@ -58,3 +67,26 @@ end
kiosk.get_stock=function(name)
return kiosk.get_value(name,"stock")
end
kiosk.player_sell=function(player)
if player == nil then return end
local playername=player:get_player_name()
if playername == nil then return end
if playername == "" then return end
local player_inv=minetest.get_inventory({type="detached",name=playername.."_sell"})
if player_inv == nil then return end
local sell_stack=player_inv:get_stack("main",1)
if sell_stack == nil then return end
local sell_item=sell_stack:get_name()
if sell_item == nil then return end
if sell_item == "" then return end
local sell_count = sell_stack:get_count()
if sell_count < 1 then return end
local price_single = kiosk.get_sell(sell_item)
local price=price_single * sell_count
xpfw.player_add_attribute(player,kiosk.account,price)
local kinv=kiosk.inventar[sell_item]
kinv.stock=kinv.stock+sell_count
player_inv:set_stack("main",1,nil)
end

View File

@ -1 +1,15 @@
kiosk.sell_factor=tonumber(minetest.settings:get("kiosk.sell_factor")) or 0.95
kiosk.prefix=minetest.settings:get("kiosk.prefix") or "kiosk"
kiosk.account=minetest.settings:get("kiosk.account") or "kiosk_balance"
kiosk.player={}
kiosk.inventar={}
for i,mg in ipairs({"currency:minegeld","currency:minegeld_5","currency:minegeld_10","currency:minegeld_50"}) do
local value=tonumber(mg:split("_")[2])
if value == nil then value = 1 end
if minetest.registered_items[mg] ~= nil then
kiosk.add_inventar(mg,value,value,0,1)
end
end

View File

@ -18,8 +18,8 @@ if not minetest.global_exists("unified_inventory") then
return
end
dofile(kiosk.path .. "/config.lua")
dofile(kiosk.path .. "/api.lua")
dofile(kiosk.path .. "/config.lua")
if rawget(_G,"unified_inventory") then
dofile(kiosk.path .. "/register.lua")

22
locale/de.po Normal file
View File

@ -0,0 +1,22 @@
# German Translation for farming mod.
# Copyright (C) 2017
# This file is distributed under the same license as the farming package.
# Xanthin.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: 1.27\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-02-21 HO:MI+ZONE\n"
"PO-Revision-Date: 2019-02-21 HO:MI+ZONE\n"
"Last-Translator: ademant\n"
"Language-Team: \n"
"Language: German \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "Balance"
msgstr "Kontostand"

View File

@ -2,66 +2,190 @@ local S = kiosk.intllib
local F = minetest.formspec_escape
minetest.register_on_player_receive_fields(function(player, formname, fields)
-- if formname == "default:team_choose" then -- This is your form name
print("Player "..player:get_player_name().." submitted fields "..dump(fields))
-- end
-- check received fields for items handled by kiosk function
print(dump2(fields))
local ui_current_page=unified_inventory.current_page[player:get_player_name()]
if ui_current_page ~= nil then
if ui_current_page == kiosk.prefix then
for i,v in pairs(fields) do
if i == "kiosk_button_sell" then
-- Sell button is pressed and items in sell field are converted into account
kiosk.player_sell(player)
unified_inventory.set_inventory_formspec(player, "kiosk")
end
if string.sub(i, 1, 21) == "item_button_nochange_" then
-- Item in preview is pressed. One item is placed into buy field and price is shown
local buy_item=kiosk.demangle_for_formspec(i:sub(22))
print(dump2(buy_item))
local buy_price=kiosk.get_buy(buy_item)
print(buy_item.." @ "..buy_price)
local playername=player:get_player_name()
-- place one item
local player_inv=minetest.get_inventory({type="detached",name=playername.."_buy"})
player_inv:set_stack("main",1,{name=buy_item,count=1})
-- update formspec
unified_inventory.set_inventory_formspec(player, "kiosk")
return player,formname,{}
end
end
end
end
end)
xpfw.register_attribute("kiosk_account",{min=0,max=math.huge,default=0,
minetest.register_on_joinplayer(function(player)
-- create detached inventories for selling and bying
local playername=player:get_player_name()
if playername == nil then return end
if playername == "" then return end
-- sell inventory with on_put function. If item is placed here the price is printed into formspec
local sell = minetest.create_detached_inventory(playername.."_sell",{
on_put = function(inv, listname, index, stack, player)
local item_stack=inv:get_stack(listname,index)
if item_stack == nil then return end
local item_name=item_stack:get_name()
if item_name == nil then return end
local item_count=item_stack:get_count()
if item_count <1 then return end
local price = kiosk.get_sell(item_name)
print("Would buy "..item_name.." @ "..price..". For "..item_count.." you get "..(item_count*price)..".")
local player_name = player:get_player_name()
unified_inventory.set_inventory_formspec(player, "kiosk")
end,
})
sell:set_size("main", 1)
-- buy inventory
local buy = minetest.create_detached_inventory(playername.."_buy", {
on_put = function(inv, listname, index, stack, player)
local player_name = player:get_player_name()
end,
allow_put = function(inv, listname, index, stack, player)
-- not allowing putting items in this field. Doesn't make sense
return 0
end,
allow_take = function(inv, listname, index, stack, player)
-- check, if account has enough money for taking itemstack
local item_stack=inv:get_stack(listname,index)
if item_stack == nil then return 0 end
local item_name=item_stack:get_name()
if item_name == nil then return 0 end
local item_count=item_stack:get_count()
if item_count <1 then return 0 end
local price = kiosk.get_buy(item_name)
local player_balance=xpfw.player_get_attribute(player,kiosk.account)
local max_allowed= (-1)
if player_balance <= price then
max_allowed= 0
end
return max_allowed
end,
on_take = function(inv, listname, index, stack, player)
-- take items from detached inventory
local item_stack=inv:get_stack(listname,index)
if item_stack == nil then return end
local item_name=item_stack:get_name()
if item_name == nil then return end
local item_count=item_stack:get_count()
if item_count <1 then return end
local price = kiosk.get_buy(item_name) * item_count
-- calc price and substract from account
xpfw.player_sub_attribute(player,kiosk.account,price)
-- reset detached inventory
local player_inv=minetest.get_inventory({type="detached",name=player:get_player_name().."_buy"})
player_inv:set_stack("main",1,{name=item_name,count=1})
local kinv=kiosk.inventar[item_name]
kinv.stock=kinv.stock-item_count
unified_inventory.set_inventory_formspec(player, "kiosk")
end,
})
buy:set_size("main", 1)
local pdef={name=playername,
sell=sell,
buy=buy,}
kiosk.player[playername]=pdef
end)
-- register xpfw attribute
xpfw.register_attribute(kiosk.account,{min=0,max=math.huge,default=0,
hud=1,})
-- ui button for formspec
unified_inventory.register_button("kiosk", {
type = "image",
image = "kiosk_button_128.png",
tooltip = S("Kiosk")
})
local sell = minetest.create_detached_inventory("sell", {
on_put = function(inv, listname, index, stack, player)
-- inv:set_stack(listname, index, nil)
local item_stack=inv:get_stack(listname,index)
if item_stack == nil then return end
local item_name=item_stack:get_name()
if item_name == nil then return end
local item_count=item_stack:get_count()
if item_count <1 then return end
local price = kiosk.get_sell(item_name)
print("Would buy "..item_name.." @ "..price..". For "..item_count.." you get "..(item_count*price)..".")
local player_name = player:get_player_name()
-- minetest.sound_play("trash", {to_player=player_name, gain = 1.0})
end,
})
sell:set_size("main", 1)
local buy = minetest.create_detached_inventory("buy", {
on_put = function(inv, listname, index, stack, player)
-- inv:set_stack(listname, index, nil)
local player_name = player:get_player_name()
-- minetest.sound_play("trash", {to_player=player_name, gain = 1.0})
end,
})
buy:set_size("main", 1)
unified_inventory.register_page("kiosk", {
-- ui formspec definition
unified_inventory.register_page(kiosk.prefix, {
get_formspec = function(player, perplayer_formspec)
local fy = perplayer_formspec.formspec_y
local name = player:get_player_name()
local player_inv=minetest.get_inventory({type="detached",name=name.."_sell"})
local sellprice=""
-- check sell field
if player_inv ~= nil then
local sell_stack=player_inv:get_stack("main",1)
if sell_stack ~= nil then
local sell_count = sell_stack:get_count()
if sell_count >= 1 then
sellprice=sellprice.." "..sell_count.."x"
end
local sell_item=sell_stack:get_name()
if sell_item ~= nil then
if sell_item ~= "" then
sellprice=sellprice.." "..sell_item
end
end
local price_single = kiosk.get_sell(sell_item)
local price=price_single * sell_count
sellprice=sellprice.." @ "..price
end
end
-- check buy field
local player_buy=minetest.get_inventory({type="detached",name=name.."_buy"})
local buyprice=""
if player_buy ~= nil then
local buy_stack=player_buy:get_stack("main",1)
if buy_stack ~= nil then
local buy_count = 1
if buy_count >= 1 then
buyprice=buyprice.." "..buy_count.."x"
end
local buy_item=buy_stack:get_name()
if buy_item ~= nil then
if buy_item ~= "" then
buyprice=buyprice.." "..buy_item
end
end
local price_single = kiosk.get_buy(buy_item)
local price=price_single * buy_count
buyprice=buyprice.." @ "..price
end
end
-- check account balance
local kiosk_balance=math.ceil(xpfw.player_get_attribute(player,kiosk.account)*100)/100
-- generate formspec
local formspec = "label[0,0;"..F(S("Kiosk")).."]"..
"list[detached:sell;main;1,1;1,1;]"..
"list[detached:buy;main;1,2;1,1;]"..
"button[3,1;2,1;kiosk_button_sell;"..S("Sell").."]"..
"button[3,2;2,1;kiosk_button_buy;"..S("Buy").."]"..
"label[5.0,"..(fy + 0.0)..";"..F(S("Buy at")).."]"..
"label[5.0,"..(fy + 0.5)..";"..F(S("Sell at")).."]"..
"list[detached:"..name.."_sell;main;1,1;1,1;]"..
"list[detached:"..name.."_buy;main;1,2;1,1;]"..
"button[2,1;2,1;kiosk_button_sell;"..S("Sell").."]"..
"button[2,2;2,1;kiosk_button_buy;"..S("Buy").."]"..
"label[4.0,1;"..F(sellprice).."]"..
"label[4.0,2;"..F(buyprice).."]"..
"label[2.0,3;"..F(S("Balance")..": "..kiosk_balance).."]"..
"listring[current_player;main]"..
"listring[detached:"..name.."_kiosk;kiosk]"
return {formspec=formspec}
end,
})
--[[
minetest.after(0.01,function()
print("size")
print(dump2(VoxelArea:getExtent()))
for i,def in pairs(minetest.registered_nodes) do
local nodeinfo=minetest.find_nodes_in_area({x=(-10),y=(-10),z=(-10)},{x=(10),y=(10),z=(10)},i)
if #nodeinfo>0 then
@ -69,4 +193,4 @@ minetest.after(0.01,function()
end
end
end)
]]

View File

@ -1 +1,3 @@
kiosk.sell_factor (Difference between Sell and buy value) float 0.95
kiosk.prefix (Prefix for kiosk items) string "kiosk"
kiosk.account (Internal name of account) string "kiosk_balance"