108 lines
2.9 KiB
Lua
108 lines
2.9 KiB
Lua
--- Server Shops API
|
|
--
|
|
-- @topic api.lua
|
|
|
|
|
|
local ss = server_shop
|
|
local S = core.get_translator(ss.modname)
|
|
|
|
local registered_currencies = {}
|
|
ss.currency_suffix = nil
|
|
|
|
ss.currency_is_registered = function()
|
|
for k, v in pairs(registered_currencies) do return true end
|
|
return false
|
|
end
|
|
|
|
ss.get_currencies = function()
|
|
return table.copy(registered_currencies)
|
|
end
|
|
|
|
ss.register_currency = function(item, value)
|
|
if not core.registered_items[item] then
|
|
ss.log("warning", "Registriere unbekanntes Item als Währung: " .. item)
|
|
end
|
|
value = tonumber(value)
|
|
if not value or value <= 0 then
|
|
ss.log("error", "Währungswert für " .. item .. " muss eine positive Zahl sein.")
|
|
return
|
|
end
|
|
registered_currencies[item] = value
|
|
ss.log("action", item .. " als Währung mit Wert " .. value .. " registriert.")
|
|
end
|
|
|
|
if ss.use_currency_defaults then
|
|
if not core.get_modpath("currency") then
|
|
ss.log("warning", "Mod 'currency' nicht gefunden, Standardwährung wird nicht geladen.")
|
|
else
|
|
local all_currency = {
|
|
{"currency:minegeld", 100}, {"currency:minegeld_5", 500},
|
|
{"currency:minegeld_10", 1000}, {"currency:minegeld_50", 5000},
|
|
{"currency:minegeld_100", 10000}, {"currency:minegeld_cent_5", 5},
|
|
{"currency:minegeld_cent_10", 10}, {"currency:minegeld_cent_25", 25},
|
|
}
|
|
for _, c in ipairs(all_currency) do
|
|
ss.register_currency(c[1], c[2])
|
|
end
|
|
ss.currency_suffix = "MG"
|
|
end
|
|
end
|
|
|
|
ss.get_shop = function(pos)
|
|
if not pos then return nil end
|
|
local meta = core.get_meta(pos)
|
|
if meta:get_string("owner") == "" then return nil end
|
|
|
|
return {
|
|
name = meta:get_string("name"),
|
|
owner = meta:get_string("owner"),
|
|
prices = core.deserialize(meta:get_string("prices")) or {},
|
|
inv = meta:get_inventory(),
|
|
}
|
|
end
|
|
|
|
ss.update_infotext = function(pos)
|
|
local shop = ss.get_shop(pos)
|
|
if not shop then return end
|
|
|
|
local text = shop.name
|
|
local inv_list = shop.inv:get_list("main")
|
|
local inv_size = shop.inv:get_size("main")
|
|
|
|
for i=1, inv_size do
|
|
local item = inv_list[i]
|
|
local price_int = shop.prices[i]
|
|
if not item:is_empty() and price_int and price_int > 0 then
|
|
local count = item:get_count()
|
|
local description = item:get_description()
|
|
local price_str = string.format("%.2f", price_int / 100)
|
|
|
|
text = text .. "\n" .. count .. "x " .. description .. ": " .. price_str .. " " .. (ss.currency_suffix or "")
|
|
end
|
|
end
|
|
|
|
core.get_meta(pos):set_string("infotext", text)
|
|
|
|
local pos_top = {x=pos.x, y=pos.y + 1, z=pos.z}
|
|
if core.get_node(pos_top).name == ss.modname..":shop_large_top" then
|
|
core.get_meta(pos_top):set_string("infotext", text)
|
|
end
|
|
end
|
|
|
|
|
|
ss.is_shop_admin = function(player)
|
|
if not player then return false end
|
|
return core.check_player_privs(player, "server")
|
|
end
|
|
|
|
ss.is_shop_owner = function(pos, player)
|
|
if not player or not pos then
|
|
return false
|
|
end
|
|
|
|
local player_name = player:get_player_name()
|
|
local meta = core.get_meta(pos)
|
|
local owner_name = meta:get_string("owner")
|
|
local is_owner = (player_name == owner_name)
|
|
return is_owner
|
|
end
|