Module:Crops by category
Jump to navigation
Jump to search
Used in {{Crops by category}}, for listing crops that meet a certain criteria and in {{Crop profit}} for additional profit information on those crops.
- Subpages
--Thank you to coralisland.wiki for the core parts of this module!
local p = {}
local lib = require('Module:Feature')
local Parse = require('Module:Parser').getTemplateArgs
local Icon = require('Module:Icon')._main
local Round = require('Module:Round')._main
local ter = lib.ternary
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
parentOnly = false, -- read from #invoke args, even for the wrapper
wrapper = {
'Template:Crops by category',
'Template:Crop profit'
}
})
if lib.isNotEmpty(args.pages) then
local data = p.parsePages(args.pages)
if #data>0 then
return p._main(args, data)
else
return args.noresult or 'No crop matches the category.'
end
else
return args.noresult or 'No crop matches the category.'
end
end
function p._main(args, items)
local show_profit = ter(tonumber(args.profit)==1, true, false)
local curr_page = mw.title.getCurrentTitle().rootText
local out = mw.html.create('')
local _table = out:tag('table'):addClass('sortable article-table tdc3 tdc4 ' .. ter(show_profit, 'tdc5 tdc6 tdc7', ''))
function isNum(x)
return x ~= nil and #string.gsub(x, "^%s*(.-)%s*$", "%1") > 0 and tonumber(x) ~= nil
end
-- Header
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Item')
tr:tag('th'):wikitext('Growth time')
tr:tag('th'):wikitext('Yield')
tr:tag('th'):wikitext('Sell price')
-- Extra columns for /Profit
if show_profit then
tr:tag('th'):wikitext('Seed price')
tr:tag('th'):wikitext('Max harvest')
tr:tag('th'):wikitext('Profit per day')
end
for _, data in ipairs(items) do
if next(data) ~= nil then
tr = _table:tag('tr')
tr:tag('td'):node(Icon{data.name or data.PAGENAME, size=40})
local td = tr:tag('td')
if isNum(data.growth) then
td:wikitext(data.growth, ter(tonumber(data.growth)==1, ' day', ' days'))
if isNum(data.regrowth) then
td:tag('br')
td:tag('small')
:wikitext(
data.regrowth,
ter(tonumber(data.regrowth)==1, ' day', ' days'),
' (regrowth)'
)
end
else
td:tag('i'):wikitext('TBD')
end
tr:tag('td'):wikitext(data.cropYield or '--')
local td = tr:tag('td')
if isNum(data.sell) then
td
:attr('data-sort-value', tonumber(data.sell))
--:node(Icon{
-- data.selltype,
-- size=16,
-- notext=1,
-- nolink=1,
-- amount=tonumber(data.sell)
--})
:wikitext(data.sell, '[[File:', data.selltype, '.png|24px|link=]]')
else
td:attr('data-sort-value', 0):tag('i'):wikitext('--')
end
-- Extra columns for /Profit
if show_profit then
local td = tr:tag('td')
if isNum(data.seedPrice) then
td
:attr('data-sort-value', tonumber(data.seedPrice))
--:node(Icon{
-- data.selltype,
-- size=16,
-- notext=1,
-- nolink=1,
-- amount=tonumber(data.seedPrice)
--})
:wikitext(data.seedPrice, '[[File:', data.selltype, '.png|24px|link=]]')
else
td:attr('data-sort-value', 0):tag('i'):wikitext('TBD')
end
if isNum(data.growth) then
local season = #(lib.split(data.season or '', ';'))
mw.logObject(data, 'data')
mw.logObject(season, 'season')
local growth = ter(isNum(data.growth), tonumber(data.growth), 1)
local cropYield = ter(isNum(data.cropYield), tonumber(data.cropYield), 1)
local regrows = isNum(data.regrowth) and tonumber(data.regrowth) > 0
local regrowth = ter(isNum(data.regrowth), tonumber(data.regrowth), 0)
local maxHarvest = ter(regrows,
-- regrowth is a number, calculate regrowing crop
math.floor(((28*season)-(growth+1))/regrowth)+1,
-- no regrowth, maxHarvest is 1
1
)
local growingDays = ((maxHarvest-1)*regrowth)+growth
--local max_yield = cropYield*maxHarvest
tr:tag('td'):wikitext(maxHarvest)
if isNum(data.sell) then
local daily_profit = Round{ cut = 2, num =
(
((tonumber(data.sell)*cropYield)*maxHarvest)-data.seedPrice
)/(growingDays)
}
local td = tr:tag('td')
td:attr('data-sort-value', daily_profit)
td:wikitext(daily_profit, '[[File:', data.selltype, '.png|24px|link=]]')
else
tr:tag('td'):tag('i'):wikitext('TBD')
end
else
tr:tag('td'):tag('i'):wikitext('TBD')
tr:tag('td'):tag('i'):wikitext('TBD')
end
end
end
end
-- if show_profit then
-- out:tag('ul'):tag('li'):wikitext('Max harvest assumes that the crop was planted on the very first day it was available, and replanted once harvested. Count for it starts on day 1 until the end of the season before it withers.')
-- end
return out
end
function p.parsePages(PAGES)
local data = {}
for page in lib.gsplit(PAGES, ';;;', {removeEmpty=true}) do
local Pdata = Parse(page, { only='agriculture infobox' } )
if Pdata then
Pdata.PAGENAME = page
table.insert(data, Pdata)
end
end
return data
end
return p