Module:Products: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
No edit summary
(x -> × simplify single value / table of values implementation; allow quantity to have a decimal point (like when it's "0.05"))
Line 46: Line 46:
for _, product in ipairs(products) do
for _, product in ipairs(products) do
local usesItems = product["Uses item and quantity"]
local usesItems = product["Uses item and quantity"]
if type(usesItems) == "table" then
if type(usesItems) == "string" then
for _, item in ipairs(usesItems) do
-- Single value
usesItems = { usesItems }
-- Extract item name and quantity from the "item,#" format
end
local itemName, quantity = item:match("([^,]+),(%d+)")
for _, item in ipairs(usesItems) do
quantity = tonumber(quantity) or 1 -- Default to 1 if quantity is missing
-- Extract item name and quantity from the "item,#" format

if itemName then
local itemName, quantity = item:match("^([^,]+),([0-9.]+)$")
-- Query for the shop buy price of the item
local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName
local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
local shopPrice = 0

if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
shopPrice = tonumber(shopPriceResult[1]["Shop buy price"]) or 0
end

-- Multiply the price by the quantity
local totalPrice = shopPrice * quantity

-- Update the product with the formatted string and total price
product[itemName .. "_Shop_buy_price"] = totalPrice > 0 and totalPrice or "N/A"
product[itemName .. "_Formatted"] = quantity .. " x " .. itemName
end
end
elseif type(usesItems) == "string" then
-- Handle single uses item_and_quantity (not a table)
local itemName, quantity = usesItems:match("([^,]+),(%d+)")
quantity = tonumber(quantity) or 1 -- Default to 1 if quantity is missing
quantity = tonumber(quantity) or 1 -- Default to 1 if quantity is missing


Line 90: Line 70:
-- Update the product with the formatted string and total price
-- Update the product with the formatted string and total price
product[itemName .. "_Shop_buy_price"] = totalPrice > 0 and totalPrice or "N/A"
product[itemName .. "_Shop_buy_price"] = totalPrice > 0 and totalPrice or "N/A"
product[itemName .. "_Formatted"] = quantity .. " x " .. itemName
product[itemName .. "_Formatted"] = tostring(quantity) .. " × " .. itemName
end
end
end
end