x_marketplace/api.lua

71 lines
1.8 KiB
Lua

-- Search items in marketplace object
-- @param string the string
-- @return string with found items, if no items found returns boolean false
function x_marketplace.store_find(string)
local found = ""
for k, v in pairs(x_marketplace.store_list) do
if string.find(k, string) then
found = found..k.." buy: "..string.format("%.2f", v.buy).." sell: "..string.format("%.2f", v.sell).."\n"
end
end
if found == "" then
found = false
end
return found
end
-- Get random items from the store
-- @param number the integer
-- @return string with found items
function x_marketplace.store_get_random(number)
local keys, i, suggest = {}, 1, ""
local how_many = number or 5
for k, v in pairs(x_marketplace.store_list) do
keys[i] = k
i = i + 1
end
for i = 1, how_many do
suggest = suggest.."\n"..keys[math.random(1, #keys)]
end
return suggest
end
-- Get players balance of BitGold
-- @param name the string of player name
-- @return string the current balance
function x_marketplace.get_player_balance(name)
if not name then
return ""
end
local player = minetest.get_player_by_name(name)
local balance = player:get_attribute("balance") or 0
return balance
end
-- Set players balance of BitGold
-- @param name the string of player name
-- @param amount the number of what should be added/deducted (if negative) from players balance
-- @return string the balance info message, returns false if not enough funds
function x_marketplace.set_player_balance(name, amount)
if not name or not amount then
return ""
end
local player = minetest.get_player_by_name(name)
local balance = player:get_attribute("balance") or 0
local new_balance = balance + amount
if new_balance < 0 then
return false
end
player:set_attribute("balance", new_balance)
return new_balance
end