Compare commits
5 Commits
ff20bb6194
...
7e2ea5b1d2
Author | SHA1 | Date |
---|---|---|
Juraj Vajda | 7e2ea5b1d2 | |
Juraj Vajda | 63595c26a9 | |
Juraj Vajda | faca6894e3 | |
Juraj Vajda | 20df0c106c | |
Juraj Vajda | fa7ec33c4e |
74
api.lua
74
api.lua
|
@ -20,39 +20,53 @@ function x_marketplace.isnan(value)
|
|||
return value ~= value
|
||||
end
|
||||
|
||||
local function esc(x)
|
||||
return (x:gsub('%%', '%%%%')
|
||||
:gsub('^%^', '%%^')
|
||||
:gsub('%$$', '%%$')
|
||||
:gsub('%(', '%%(')
|
||||
:gsub('%)', '%%)')
|
||||
:gsub('%.', '%%.')
|
||||
:gsub('%,', '%%,')
|
||||
:gsub('%;', '%%;')
|
||||
:gsub('%\\', '%%\\')
|
||||
:gsub('%[', '%%[')
|
||||
:gsub('%]', '%%]')
|
||||
:gsub('%*', '%%*')
|
||||
:gsub('%+', '%%+')
|
||||
:gsub('%-', '%%-')
|
||||
:gsub('%?', '%%?'))
|
||||
end
|
||||
|
||||
-- rounds a number to the nearest decimal places
|
||||
--- rounds a number to the nearest decimal places
|
||||
-- @local
|
||||
local function round(val, decimal)
|
||||
if (decimal) then
|
||||
return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
|
||||
else
|
||||
return math.floor(val+0.5)
|
||||
end
|
||||
if (decimal) then
|
||||
return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
|
||||
else
|
||||
return math.floor(val+0.5)
|
||||
end
|
||||
end
|
||||
|
||||
-- Search items in marketplace object
|
||||
local function string_endswith(full, part)
|
||||
return full:find(part, 1, true) == #full - #part + 1
|
||||
end
|
||||
|
||||
--- Normalize nodename string without mod prefix and return nodename with mod prefix. This function was borrowed from WorldEdit mod.
|
||||
-- @param nodename name of the node as a string, can be without the mod prefix
|
||||
-- @return nil if node doesn't exists or nodename with mode prefix
|
||||
-- @see https://github.com/Uberi/Minetest-WorldEdit
|
||||
function x_marketplace.normalize_nodename(nodename)
|
||||
if not nodename then
|
||||
return false
|
||||
end
|
||||
|
||||
nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces
|
||||
if nodename == "" then return nil end
|
||||
|
||||
local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases
|
||||
if minetest.registered_nodes[fullname] or fullname == "air" then -- full name
|
||||
return fullname
|
||||
end
|
||||
for key, value in pairs(minetest.registered_nodes) do
|
||||
if string_endswith(key, ":" .. nodename) then -- matches name (w/o mod part)
|
||||
return key
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- 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 = ""
|
||||
local str = esc(string)
|
||||
local str = x_marketplace.normalize_nodename(string)
|
||||
|
||||
if not str then
|
||||
return false
|
||||
end
|
||||
|
||||
for k, v in pairs(x_marketplace.store_list) do
|
||||
if string.find(k, str) then
|
||||
|
@ -67,7 +81,7 @@ function x_marketplace.store_find(string)
|
|||
return found
|
||||
end
|
||||
|
||||
-- Get random items from the store
|
||||
--- Get random items from the store
|
||||
-- @param number the integer
|
||||
-- @return string with found items
|
||||
function x_marketplace.store_get_random(number)
|
||||
|
@ -86,7 +100,7 @@ function x_marketplace.store_get_random(number)
|
|||
return suggest
|
||||
end
|
||||
|
||||
-- Get players balance of BitGold
|
||||
--- Get players balance of BitGold
|
||||
-- @param name the string of player name
|
||||
-- @return string the current balance
|
||||
function x_marketplace.get_player_balance(name)
|
||||
|
@ -99,7 +113,7 @@ function x_marketplace.get_player_balance(name)
|
|||
return balance
|
||||
end
|
||||
|
||||
-- Set players balance of BitGold
|
||||
--- 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
|
||||
|
@ -191,7 +205,7 @@ minetest.register_craftitem("x_marketplace:head", {
|
|||
end
|
||||
|
||||
else
|
||||
minetest.chat_send_player(user:get_player_name(), minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: If you want to sell the head, you have to punch steel sign with text '/mp sellhead' on it."))
|
||||
minetest.chat_send_player(user:get_player_name(), minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: If you want to sell the head, you have to punch Bones Sign with text '/mp sellhead' on it."))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -206,6 +206,8 @@ minetest.register_chatcommand("mp", {
|
|||
-- sell inventory
|
||||
--
|
||||
elseif params[1] == "sellinv" then
|
||||
params[2] = x_marketplace.normalize_nodename(params[2])
|
||||
|
||||
-- item name is missing from param[2]
|
||||
if not params[2] then
|
||||
return false, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: You need to write the item name you want to sell. example: /mp sellinv default:stone. See some suggestion from the store: ")..x_marketplace.store_get_random()
|
||||
|
@ -252,7 +254,7 @@ minetest.register_chatcommand("mp", {
|
|||
local new_balance = x_marketplace.set_player_balance(caller, sell_price)
|
||||
|
||||
if amount == 0 then
|
||||
return false, minetest.chat_send_player(caller, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: We couldn't buy all your items without going above your maximum balance."))
|
||||
return false, minetest.chat_send_player(caller, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: You have nothing to sell in your inventory."))
|
||||
end
|
||||
|
||||
if over_max_balance then
|
||||
|
@ -275,6 +277,8 @@ minetest.register_chatcommand("mp", {
|
|||
-- buy inventory
|
||||
--
|
||||
elseif params[1] == "buyinv" then
|
||||
params[2] = x_marketplace.normalize_nodename(params[2])
|
||||
|
||||
-- item name is missing from param[2]
|
||||
if not params[2] then
|
||||
return false, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: You need to write the item name you want to buy. example: /mp buyinv default:stone, or check out other items in the store: ")..x_marketplace.store_get_random()
|
||||
|
@ -337,13 +341,14 @@ minetest.register_chatcommand("mp", {
|
|||
--
|
||||
elseif params[1] == "buy" then
|
||||
local amount = tonumber(params[3])
|
||||
params[2] = x_marketplace.normalize_nodename(params[2])
|
||||
|
||||
-- check for param[2] - item name
|
||||
|
||||
if not params[2] then
|
||||
return false, minetest.colorize(x_marketplace.colors.red, "MARKET PLACE: You need to write the item name you want to buy. example: /mp buy default:stone 10, or check out other items in the store: ")..x_marketplace.store_get_random()
|
||||
end
|
||||
|
||||
-- find sign
|
||||
local player = minetest.get_player_by_name(caller)
|
||||
local find_signs = x_marketplace.find_signs(player:get_pos(), "/mp buy", "x_marketplace:sign_wall_diamond")
|
||||
|
||||
|
@ -353,16 +358,7 @@ minetest.register_chatcommand("mp", {
|
|||
|
||||
-- item not in store
|
||||
if not x_marketplace.store_list[params[2]] then
|
||||
local suggestions = x_marketplace.store_find(params[2])
|
||||
|
||||
-- try suggest item from store, else show random items
|
||||
if suggestions then
|
||||
return false, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: This item is not in store, check out other items from the store: \n")..suggestions
|
||||
|
||||
else
|
||||
-- still not found in the store - pick random
|
||||
return false, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: This item is not in store, check out other items from the store: ")..x_marketplace.store_get_random()
|
||||
end
|
||||
return false, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: This item is not in store, check out other items from the store: ")..x_marketplace.store_get_random()
|
||||
end
|
||||
|
||||
-- check for param[3] - amount
|
||||
|
@ -456,12 +452,12 @@ minetest.register_chatcommand("mp", {
|
|||
local msg =
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp find").." <item name>, find item in store\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp balance")..", show your current balance in BitGold\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp sellhand")..", sell item(s) currently holding in hand, you must be near steel sign with text '/mp sell' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp buyhand").." [<amount>], buy <amount> of item(s) currently holding in hand, when <amount> is not provided then amount is 1, you must be near steel sign with text '/mp buy' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp sellhand")..", sell item(s) currently holding in hand, you must be near Mese Sign with text '/mp sell' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp buyhand").." [<amount>], buy <amount> of item(s) currently holding in hand, when <amount> is not provided then amount is 1, you must be near Diamond Sign with text '/mp buy' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp infohand")..", show more information about the item(s) you are currently holding in hand from the store\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp buy").." <item name> [<amount>], buy <amount> of <item name> from store, if <amount> is not provided then amount is 1, you must be near steel sign with text '/mp buy' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp sellinv").." <item name>, sell all items <item name> from the 'main' inventory list, you must be near steel sign with text '/mp sell' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp buyinv").." <item name>, buy full inventory of items <item name>, empty slots in the 'main' inventory are required, you must be near steel sign with text '/mp buy' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp buy").." <item name> [<amount>], buy <amount> of <item name> from store, if <amount> is not provided then amount is 1, you must be near Diamond Sign with text '/mp buy' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp sellinv").." <item name>, sell all items <item name> from the 'main' inventory list, you must be near Mese Sign with text '/mp sell' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp buyinv").." <item name>, buy full inventory of items <item name>, empty slots in the 'main' inventory are required, you must be near Diamond Sign with text '/mp buy' on it\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp top")..", show top 5 richest players currently online\n"..
|
||||
minetest.colorize(x_marketplace.colors.cyan, "/mp help")..", print out this help\n"
|
||||
|
||||
|
|
BIN
screenshot.png
BIN
screenshot.png
Binary file not shown.
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 35 KiB |
Loading…
Reference in New Issue