Module:Items

From Sun Haven Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Items/doc

local p = {} --p stands for package
local frame = mw.getCurrentFrame()
local trim = mw.text.trim
local utils = require('Module:Utilities')
local strings = require('Module:Strings')

local data = require( 'Module:Datatables' )
local merchants = require('Module:Merchants')



-- Returns a table showing the growth of a seed with images
-- name = Seed Name
function p.GetSeedGrowthStages(name)
	
	local f = name
	name = utils.resolveParameter(f)
    
    local item = p.GetItemByName(name)
    
    if not item.growthstages then
    	return "Growth Stages not found in Database"
    end
    
    local header = cstr.empty
    local rows = "|-"..nl
    for _, stage in ipairs(item.growthstages) do
		header = header.."! "..stage.title.." !"
		rows = rows.."|[[File:"..stage.image.."|center|link=]]\n"
    end
    
	if header then
		header = header:sub(1, -2)
	end
    
    local result = '{| class="article-table" style="text-align:center;"'..nl..header..nl..rows..nl.."|}"
    
	return frame:preprocess(result)
end


-- Returns a table showing the growth of a seed with images
-- filePrefix = Common part of the growth stage file name
-- footerText = list of days in which the sprite changes
function p.GetSeedGrowthStagesFromInfobox(filePrefix, footerText)
	
	local f = filePrefix
	local filePrefix = utils.resolveParameter(f)
	local footerText = footerText or utils.resolveParameter(f, 2, false)
	local days = {}
	
	footerText = mw.text.split(footerText, ',')
	for i=1, #footerText, 1 do
		footerText[i] = trim(footerText[i])
		if not (footerText[i] == "" or not footerText[i]) then
			days[#days+1] = footerText[i]
		end
	end
	

    local header = cstr.empty
    local rows = "|-"..nl
    local footer = "|-"..nl

    for i=1, #days+2, 1 do
    	if i == #days+2 then
    		header = header.."! Final Stage"
    	else
			header = header.."! Stage "..i.." !"
    	end
    	
    	if i == 1 then
    		footer = footer.."| Seedling "
    	elseif i == 2 then
    		footer = footer.."|| Sprinkle "
    	else
			footer = footer.."|| "..days[i-2].." Day(s) "
    	end
  	
		rows = rows.."|[[File:"..filePrefix..(i-1)..".png|center|link=]]\n"
	end
    
    local result = '{| class="article-table" style="text-align:center;"'..nl..header..nl..rows..nl..footer..nl.."|}"
    
	return frame:preprocess(result)
end


-- [Module] Returns a table of key value pairs representing the requested item
-- name = The name of the item
function p.GetItemByName(name)
	for _, item in ipairs(data.items) do
		if item.name == name then
			return item
		end
	end
	return false
end

-- [Module] Returns a table of key value pairs representing the requested seed
-- name = The name of the seed
function formatAsImages(data)
	if type(data) == "table" then
		result = {}
		for _, d in ipairs(data) do
			result[#result+1] = "{{i|"..d.."}}"
		end
	else
		result = "{{i|"..data.."}}"
	end
	
	return result or false
end

function toStringWithNewlines(data)
	result = cstr.empty
	
	if type(data) == "table" then
		for _, d in ipairs(data) do
			result = result..d..nl
		end
	else
		result = data..nl
	end
	
	return result or false
end

-- Returns the value of Seed/Tag, false if it cannot be found
-- tag = Seed Tag
-- name = Name of the Seed
function p.Get(tag, name)
	local err = cstr.empty
	
	local f = tag
	tag = utils.resolveParameter(f)
	name = name or utils.resolveParameter(f, 2, false)
	
    local item = p.GetItemByName(name)
    return (item and item[tag]) or "Unknown"
end

function p.GetInfobox(itemType, name)
	local f = itemType
	itemType = utils.resolveParameter(f)
	name = name or utils.resolveParameter(f, 2, false)
	
	if itemType == 'Seed' then
		return p.GetSeedInfobox(name)
	else
		return p.GetItemInfobox(name)
	end
	
end

-- Returns a pre-populated Seed/Infobox
-- name = Name of the Seed
function p.GetSeedInfobox(name)
	local f = name
	name = utils.resolveParameter(f)
    
    local seed = p.GetItemByName(name)
	if seed then 
		seed.crop = formatAsImages(seed.crop)
	    m = merchants.GetMerchantsByItem(seed.name)
	    
	    mNames = {}
	    for _, merchant in ipairs(m) do
			mNames[#mNames+1] = merchant.name
	    end
		seed.source = toStringWithNewlines(formatAsImages(mNames))
		
		-- NIL any values not being used by the template
		seed.growthstages = nil
	
	    return frame:expandTemplate{title = "SeedInfobox", args = seed}
	end
	
    return name
end

-- Returns a pre-populated Seed/Infobox
-- name = Name of the Seed
function p.GetItemInfobox(name)
	local f = name
	name = utils.resolveParameter(f)
    
    local item = p.GetItemByName(name)
	if item then 
	    m = merchants.GetMerchantsByItem(item.name)
	    
	    mNames = {}
	    for _, merchant in ipairs(m) do
			mNames[#mNames+1] = merchant.name
	    end
		item.source = toStringWithNewlines(formatAsImages(mNames))
	
	    return frame:expandTemplate{title = "Infobox", args = item}
	end
	
    return name
end

return p