Module:Crop calendar

From Sun Haven Wiki
Jump to navigation Jump to search

Used in the {{Crop calendar}} template.

Subpages

local p = {}
local lib = require('Module:Feature')
local Exists = require('Module:Exists').checkExists

function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame, {
        parentFirst = true,
        wrapper = {'Template:Crop calendar'}
    })
    return p._main(args)
end

function p._main(args)
	local crop = args.crop or mw.title.getCurrentTitle().rootText
	local category = mw.html.create()
	assert(args[1], 'Missing stage information')
	local stages = args[1]:find(';') and lib.split(args[1], ';') or args
	local growth = {}
	local regrowth = {0, ''}
	local images, padding, noRep, last = {}, {}, {}
	for i, stage in ipairs(stages) do
		if stage == 'R0' then
			regrowth[2] = last
			regrowth[1] = -1
		elseif stage == 'R' then
			regrowth[1] = regrowth[1] + 1
			if last ~= 'R' then regrowth[2] = last end
			last = stage
			if not images[stage] then 
				local filename = 'File:' .. crop .. ' stages ' .. stage .. '.png'
				if Exists(filename) then
					images[stage] = '[[' .. filename .. '|30px]]'
					padding[stage] = {padding = 0}
				else
					images[stage] = 'Regrow'
					category:wikitext('[[Category:Missing crop regrowth image]]')
					padding[stage] = {}
				end
			end
		elseif last == 'R' then
			regrowth[2] = stage
			break
		elseif not last or last == stage or not noRep[stage] then
			table.insert(growth, stage)
			last = stage
			noRep[stage] = true
			if not images[stage] then 
				local filename = 'File:' .. crop .. ' stages ' .. stage .. '.png'
				if Exists(filename) then
					images[stage] = '[[' .. filename .. '|30px]]'
					padding[stage] = {padding = 0}
				else
					images[stage] = 'Stage ' .. stage .. lib.ternary((stages[i+1] ~= nil and stages[i+1] == 'R'), '<br />Harvestable', '') 
					category:wikitext('[[Category:Missing crop growth stage ', stage,' image]]')
					padding[stage] = {}
				end
			end
		end
	end
	local stage, cycle = 1, #growth
	local calendar = mw.html.create('table'):addClass('article-table tablesmall'):css{['text-align'] = 'center'}
	calendar:tag('caption'):wikitext('')
	local tr = calendar:tag('tr')
	tr
		:tag('th'):wikitext('Sun')
		:tag('th'):wikitext('Mon')
		:tag('th'):wikitext('Tue')
		:tag('th'):wikitext('Wed')
		:tag('th'):wikitext('Thu')
		:tag('th'):wikitext('Fri')
		:tag('th'):wikitext('Sat')
	for i = 1, 28 do
		if i == 1 or i == 8 or i == 15 or i == 22 then
			tr = calendar:tag('tr')
		end
		if stage<=cycle then
			tr:tag('td'):css(padding[growth[stage]]):wikitext(images[growth[stage]])
			stage = stage + 1
			if stage>cycle and regrowth[1]==0 then stage = 2 end
		elseif regrowth[1]>0 then
			local regstage = 1
			for y = i, 28 do
				if y ~= i and (y == 1 or y == 8 or y == 15 or y == 22) then
					tr = calendar:tag('tr')
				end
				if regstage<=regrowth[1] then
					tr:tag('td'):css(padding['R']):wikitext(images['R'])
					regstage = regstage + 1
				elseif regstage>regrowth[1] then
					tr:tag('td'):css(padding[regrowth[2]]):wikitext(images[regrowth[2]])
					regstage = 1
				end
			end
			break
		elseif regrowth[1] == -1 then
			-- crops that regrow the very next day, having virtually no regrowth period
			for y = i, 28 do
				if y ~= i and (y == 1 or y == 8 or y == 15 or y == 22) then
					tr = calendar:tag('tr')
				end
				tr:tag('td'):css(padding[regrowth[2]]):wikitext(images[regrowth[2]])
			end
			break
		end
	end
	
	calendar:node(require('Module:Namespace detect').main{main=category})
	
	return calendar
end

return p