Module:Crops by category table: Difference between revisions
Jump to navigation
Jump to search
Salty Nori (talk | contribs) (Created page with "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 table', 'Template:Crops by category table/Profit' } })...") |
Salty Nori (talk | contribs) No edit summary |
||
Line 181: | Line 181: | ||
for page in lib.gsplit(PAGES, ';;;', {removeEmpty=true}) do | for page in lib.gsplit(PAGES, ';;;', {removeEmpty=true}) do | ||
local Pdata = Parse(page, { only='Infobox | local Pdata = Parse(page, { only='Infobox item' } ) | ||
if Pdata then | if Pdata then | ||
Pdata.PAGENAME = page | Pdata.PAGENAME = page |
Latest revision as of 04:48, 29 January 2024
Documentation for this module may be created at Module:Crops by category table/doc
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 table',
'Template:Crops by category table/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 ' .. ter(show_profit, 'tdc7 tdc8', ''))
function isNum(x)
if x ~= '' and x ~= nil and tonumber(x) ~= nil then
return true
else
return false
end
end
-- Header
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Item')
tr:tag('th'):wikitext('Type')
tr:tag('th'):wikitext('Rank')
tr:tag('th'):wikitext('Growth time')
tr:tag('th'):wikitext('Seed price')
tr:tag('th'):wikitext('Sell price')
-- Extra columns for /Profit
if show_profit then
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 lib.isNotEmpty(data['type']) then
if curr_page==data['type'] then td:wikitext('[[', data['type'], ']]')
else td:wikitext(data['type']) end
end
tr:tag('td'):wikitext(data.rank or '<i>TBD</i>')
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
local td = tr:tag('td')
if isNum(data.seed_price) then
td
:attr('data-sort-value', tonumber(data.seed_price))
:node(Icon{
'Coin',
size=16,
notext=1,
nolink=1,
amount=tonumber(data.seed_price)
})
else
td:attr('data-sort-value', 0):tag('i'):wikitext('TBD')
end
local td = tr:tag('td')
if isNum(data.sell) then
td
:attr('data-sort-value', tonumber(data.sell))
:node(Icon{
'Coin',
size=16,
notext=1,
nolink=1,
amount=tonumber(data.sell)
})
else
td:attr('data-sort-value', 0):tag('i'):wikitext('TBD')
end
-- Extra columns for /Profit
if show_profit then
if isNum(data.growth) then
local seasons = #(lib.split(data.seasons or '', ';'))
mw.logObject(data, 'data')
mw.logObject(seasons, 'seasons')
local max_yield =
ter(lib.isNotEmpty(data.seed) and data.seed:find('seedling'), 2, ter(lib.isNotEmpty(data.seed) and data.seed:find('sapling'), 3, 1))* -- fruit plants yield two per harvest, fruit tree yields 3
(isNum(data.regrowth) and
-- fruit trees
(data.seed:find('sapling') and
(Round{((28*seasons)-1)/tonumber(data.regrowth),mode='down'}))
or
-- regrowing crop
isNum(data.regrowth) and
(Round{(28*seasons-tonumber(data.growth)-1)/tonumber(data.regrowth),mode='down'}+1)
or
-- replanting crop
(Round{((28*seasons)-1)/tonumber(data.growth),mode='down'})
)
tr:tag('td'):wikitext(max_yield)
if isNum(data.sell) then
local base_profit = Round{ cut = 2, num =
(
max_yield*tonumber(data.sell)-
tonumber(data.seed_price)*ter(isNum(data.regrowth), 1, max_yield)
)/(28*seasons)
}
local max_profit = Round{ cut = 2, num =
(
max_yield*tonumber(data.sell)*2- --osmium price is double of base
tonumber(data.seed_price)*ter(isNum(data.regrowth), 1, max_yield)
)/(28*seasons)
}
local td = tr:tag('td')
td:attr('data-sort-value', base_profit)
td:tag('b'):wikitext('Base: ')
td:wikitext(base_profit)
td:tag('br')
td:node(Icon{'Osmium', size=15, notext=1, nolink=1})
td:tag('b'):wikitext(': ')
td:wikitext(max_profit)
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 or fruit plant 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. Also assumes that fruit tree was fully mature on the first day of harvesting season.')
end
return out
end
function p.parsePages(PAGES)
local data = {}
for page in lib.gsplit(PAGES, ';;;', {removeEmpty=true}) do
local Pdata = Parse(page, { only='Infobox item' } )
if Pdata then
Pdata.PAGENAME = page
table.insert(data, Pdata)
end
end
return data
end
return p