Editing Module:Products

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
require('Module:Mw.html extension')
local Array = require('Module:Array')
local currency = require('Module:Currency').parse
local yesno = require('Module:Yesno')
local purge = require('Module:Purge')._purge

local p = {}
local p = {}


Line 11: Line 5:
end
end


local function recipe_sort(recipe_a, recipe_b)
function p._main(args)
args = args or {}
--if one is nil but not both, put nil levels after known levels
args.item = args.item or mw.title.getCurrentTitle().text
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then
local showPrices = args.showPrices or false
return recipe_b.level == nil
local showValues = args.showValues or false
end
local products = p.getProducts(args)
if products ~= 0 then
-- if both are nil, sort by name
local recipeNames = p.extractRecipeNames(products)
if recipe_a.level == nil then
local prodprices = p.getShopBuyPrices(products)
return recipe_a.output[1].name < recipe_b.output[1].name
-- Generate and return a table containing the product information
end
local output = p.displayProductTable(prodprices, showPrices, showValues)
return output
-- if neither is nil, sort first by level
else
if recipe_a.level ~= recipe_b.level then
return "There are no known products for item '''args.item'''"
return recipe_a.level < recipe_b.level
end
end
end


function p.extractRecipeNames(products)
-- if neither is nil and levels are the same, sort by name
local recipeNames = {}
return recipe_a.output[1].name < recipe_b.output[1].name
for _, recipeInfo in ipairs(products) do
local recipeLink = recipeInfo["Recipe"]
if recipeLink then
local displayName = recipeLink:match("%[%[.-|(.+)%]%]")
if displayName then
table.insert(recipeNames, displayName)
end
end
end
return recipeNames
end
end


function p._main(args)
function p.getProducts(args)
local item = mw.smw.ask('[[:+]][[Uses item::' .. args.item .. ']]|?Uses item |?Uses item and quantity |?Profession A |?Profession Level A|?Value |mainlabel=Recipe') or 0
args = args or {}
return item
local item = args[1] or mw.title.getCurrentTitle().text
end
local showPrices = yesno(args.showPrices)

local showValues = yesno(args.showValues)
function p.getShopBuyPrices(products)
local limit = tonumber(args.limit or 0) or 0
local mw = require('mw')
if limit <= 0 then

limit = 500
for _, product in ipairs(products) do
end
local usesItems = product["Uses item and quantity"]
if type(usesItems) == "string" then
-- Query for data
-- Single value
local smw_data = mw.smw.ask{
usesItems = { usesItems }
'[[Uses item::' .. item .. ']] OR [[Activity input::' .. item .. ']] OR [[Activity container::' .. item .. ']]',
end
'?Recipe JSON',
for _, item in ipairs(usesItems) do
'?Activity JSON',
-- Extract item name and quantity from the "item,#" format
showValues and '?Value',
local itemName, quantity = item:match("^([^,]+),([0-9.]+)$")
limit = limit
quantity = tonumber(quantity) or 1 -- Default to 1 if quantity is missing
}
if not smw_data then
return ":''No products found. To force an update, click "
..purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span')
..".''[[Category:Empty products lists]]"
end


if itemName then
-- Create a list of all recipes that use this item
-- Query for the shop buy price of the item
local recipes = {}
local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName
for _, product in ipairs(smw_data) do
local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
-- this part will need to be addressed if a page has both a recipe and an activity
local shopPrice = 0
local jsons = product['Recipe JSON'] or product['Activity JSON']

if type(jsons) == 'string' then
if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
jsons = { jsons }
shopPrice = tonumber(shopPriceResult[1]["Shop buy price"]) or 0
end
end
for _, json in ipairs(jsons) do

local json = mw.text.jsonDecode(json)
-- Multiply the price by the quantity
json.Value = product.Value
local totalPrice = shopPrice * quantity
-- Filter out when this item isn't actually used (Will happen on pages with multiple recipes)

if Array.any(json.materials, function(mat)
-- Update the product with the formatted string and total price
return mat.name == item
product[itemName .. "_Shop_buy_price"] = totalPrice > 0 and totalPrice or "N/A"
end) then
product[itemName .. "_Formatted"] = tostring(quantity) .. " &times; " .. itemName
table.insert(recipes, json)
end
end
end
end
end
return products
end

function p.displayProductTable(products, showPrices, showValues)
local currency = require('Module:Currency').parse
local out = {}
table.insert(out, '{| class="wikitable"')
local headerRow = {}
table.insert(headerRow, '!colspan="2" | Recipe')
table.insert(headerRow, '!! Level')

if showValues then
table.insert(headerRow, '!! Value')
end
end


table.insert(headerRow, '!! Ingredients')
-- Sort list of recipes by the level of the recipe (cross-profession)
table.sort(recipes, recipe_sort)


if showPrices then
-- if recipe is passive, want per hour
table.insert(headerRow, '!! Price')
for _, recipe in ipairs(recipes) do
recipe.recipePerHour = recipe.duration and math.floor(3600/recipe.duration)
-- replace quantities with per-hour quantities
-- if recipe.passive then
for _,item in ipairs(recipe.output) do
item.displayQuantity = (item.quantity and recipe.recipePerHour and item.quantity * recipe.recipePerHour) or ''
end
for _,item in ipairs(recipe.materials) do
item.displayQuantity = (item.quantity and recipe.recipePerHour and item.quantity * recipe.recipePerHour) or ''
end
-- end
end
end
-- Calculate shop prices
if showPrices then
local price_cache = {}
for _, recipe in ipairs(recipes) do
for _, item in ipairs(recipe.materials) do
-- Extract item name and quantity from the "item,#" format


table.insert(out, table.concat(headerRow, ' '))
-- Query for the shop buy price of the item
local shopPriceResult = price_cache[item.name] or mw.smw.ask{
'[[Sold item::' .. item.name .. ']]',
'?Shop buy price'
} or {}
price_cache[item.name] = shopPriceResult
local shopPrice


for _, product in ipairs(products) do
if shopPriceResult[1] then
local recipeRawText = product["Recipe"]:match("%[%[.-|(.+)%]%]")
shopPrice = tonumber(shopPriceResult[1]['Shop buy price'] or 0) or 0
local recipeImage = "[[File:" .. recipeRawText .. ".png|30px]]"
end
local recipe = product["Recipe"] or "Unknown"
local professionName = product["Profession A"]:match("%[%[.-|(.+)%]%]") or product["Profession A"]:match("%[%[(.-)%]%]") or product["Profession A"] or "Unknown"
local professionLevel = "[[File:" .. professionName .. " small icon.png|15px]] " .. (product["Profession Level A"] or "Unknown")
local usesItems = product["Uses item"] or {}
local value = currency(product["Value"]) or "Unknown"


-- Concatenate uses items and their shop buy prices in unordered lists
-- Update the product with the total price
local usesItemStr = "<ul style='list-style:none; margin:0; padding-left:0;'>"
if shopPrice ~= nil then
local shopBuyPriceStr = "<ul style='list-style:none; margin:0; padding-left:0; text-align:right;'>"
item.price = shopPrice * item.quantity
end
end
end
end


if type(usesItems) == "table" then
-- Create table
for _, item in ipairs(usesItems) do
local out = mw.html.create('table')
local itemName = item:match("%[%[.-|(.+)%]%]") or item:match("%[%[(.-)%]%]") or item
:addClass('wikitable align-right-1 sortable')
usesItemStr = usesItemStr .. "<li>" .. product[itemName .. "_Formatted"] .. "</li>"
:tag('tr')
local shopBuyPrice = product[itemName .. "_Shop_buy_price"] or 0
:tag('th')
if shopBuyPrice == "N/A" then shopBuyPrice = 0 end
:attr{ colspan = '3' }
shopBuyPriceStr = shopBuyPriceStr .. "<li>" .. currency(shopBuyPrice) .. "</li>"
:wikitext('Product')
end
:done()
elseif type(usesItems) == "string" then
:tag('th'):wikitext('Level'):done()
local itemName = usesItems:match("%[%[.-|(.+)%]%]") or usesItems:match("%[%[(.-)%]%]") or usesItems
:IF(showValues)
usesItemStr = usesItemStr .. "<li>" .. product[itemName .. "_Formatted"] .. "</li>"
:tag('th'):wikitext('Value'):done()
local shopBuyPrice = product[itemName .. "_Shop_buy_price"] or 0
:END()
if shopBuyPrice == "N/A" then shopBuyPrice = 0 end
:tag('th'):wikitext('Inputs'):done()
shopBuyPriceStr = shopBuyPriceStr .. "<li>" .. currency(shopBuyPrice) .. "</li>"
:IF(showPrices)
end
:tag('th'):wikitext('Price'):done()
:END()
:done()


usesItemStr = usesItemStr .. "</ul>"
for _, recipe in ipairs(recipes) do
shopBuyPriceStr = shopBuyPriceStr .. "</ul>"
local row = out:tag('tr')

:tag('td')
table.insert(out, '|-')
:css{ ['border-right'] = '0', ['padding-right'] = '0' }
:attr{ ['data-sort-value'] = recipe.output[1].name }
local valuesRow = {}
:IF(recipe.passive)
:wikitext(recipe.output[1].displayQuantity .. ' (/hr) &times;')
table.insert(valuesRow, '| ' .. recipeImage)
:ELSE()
table.insert(valuesRow, '|| ' .. recipe)
:wikitext(recipe.output[1].quantity .. ' &times;')
table.insert(valuesRow, '|| ' .. professionLevel)
:END()
:done()
:tag('td')
:addClass('plinkt-image no-border')
:css{ ['border-left'] = '0', ['padding-left'] = '0' }
:wikitext('[[File:' .. recipe.output[1].name .. '.png|link=' .. recipe.output[1].name .. '|30px]]')
:done()
:tag('td')
:addClass('plinkt-link no-border')
:wikitext('[[' .. recipe.output[1].name .. ']]')
:done()
:tag('td')
:IF(recipe.profession)
:wikitext(('[[File:%s small icon.png|15px|link=%s]] %s'):format(recipe.profession, recipe.profession, recipe.level or 'Unknown'))
:ELSE()
:wikitext(('[[FileUnknown profession small icon.png|15px|link=Professions]] %s'):format(recipe.level or 'Unknown'))
:END()
:done()


if showValues then
if showValues then
table.insert(valuesRow, '|| ' .. value)
if recipe.Value then
row:tag('td')
:wikitext(currency(recipe.Value))
:done()
else
row:tag('td')
:attr{ ['data-sort-value'] = '' }
:wikitext('Unknown')
:done()
end
end
end


table.insert(valuesRow, '|| ' .. usesItemStr)
local ingredients = row:tag('td')
:attr{ ['data-sort-value'] = table.concat(Array.map(recipe.materials, function(item) return item.name end), '\0') }
:tag('ul')
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' }

for _, item in ipairs(recipe.materials) do
ingredients:tag('li')
:IF(recipe.passive)
:wikitext(('%s (/hr) &times; [[File:%s.png|link=%s|18px]] [[%s]]'):format(item.displayQuantity, item.name, item.name, item.name))
:ELSE()
:wikitext(('%s &times; [[File:%s.png|link=%s|18px]] [[%s]]'):format(item.quantity, item.name, item.name, item.name))
:END()
:done()
end


if showPrices then
if showPrices then
table.insert(valuesRow, '|| ' .. shopBuyPriceStr)
local prices = row:tag('td')
:tag('ul')
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' }

for _, item in ipairs(recipe.materials) do
if item.price then
prices:tag('li'):wikitext(currency(item.price)):done()
else
prices:tag('li'):wikitext('Unknown'):done()
end
end
end
end


table.insert(out, table.concat(valuesRow, ' '))
end
--table.insert(out, '| ' .. recipe .. ' || ' .. professionLevel .. ' || ' .. value .. ' || ' .. usesItemStr .. ' || ' .. shopBuyPriceStr)
end


table.insert(out, '|}')
return out
return table.concat(out, '\n')
end
end


Please note that all contributions to Brighter Shores Wiki are considered to be released under the CC BY-NC-SA 3.0 (see Brighter Shores:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

This page is a member of a hidden category: