Module:Housing

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

local p = {}
local utils = require("Module:Utils")
local data = mw.loadJsonData("Module:Data/housing.json")
local resource = mw.loadJsonData("Module:Data/resource.json")

function p.createInfobox(frame)
	local name = frame.args.name
	if name == nil or name == "" then name = mw.title.getCurrentTitle().subpageText end
	local image = frame.args.image
	local item = data[name]
	
	local str = { 
		image = image,
		type = item.type,
		subtype = item.subtype,
		placement = utils.concat(item.placement),
		size = item.size * 2
	}
	
	return frame:expandTemplate{title = 'Infobox housing', args = str}
end

function p.getDescription(frame)
	local name = frame.args.name
	if name == nil or name == "" then name = mw.title.getCurrentTitle().subpageText end
	
	return data[name].description
end

function p.createCraftingTable(frame)
	local name = frame.args.name
	if name == nil or name == "" then name = mw.title.getCurrentTitle().subpageText end
	local item = data[name]
	local total = {}

	local str = {
		basic = "",
		expanded = "",
		total = ""
	}

	local cat = {
		unique = {},
		rare = {},
		common = {},
		currency = {}
	}

	for k, v in ipairs(item.craftingRecipes) do
		if v.name == "Gold" then
			str.basic = str.basic .. frame:expandTemplate{title = 'Gold', args = {v.amount}} .. ", "
		else
			str.basic = str.basic .. frame:expandTemplate{title = 'Icon', args = {v.name, v.amount}} .. ", "
		end
	end
	str.basic = str.basic:sub(1, -3)
	
	for k, v in ipairs(item.craftingRecipes) do
		if v.name == "Gold" then
			str.expanded = str.expanded .. frame:expandTemplate{title = 'Gold', args = {v.amount}} .. ", "
		else
			str.expanded = str.expanded .. frame:expandTemplate{title = 'Icon', args = {v.name, v.amount}} .. ", "
		end
		
		if resource[v.name] ~= nil and resource[v.name].craftingRecipes ~= nil then
			for k2, v2 in ipairs(resource[v.name].craftingRecipes) do
				if total[v2.name] == nil then
					total[v2.name] = v2.amount
				else
					total[v2.name] = total[v2.name] + v2.amount
				end
				if v2.name == "Gold" then
					str.expanded = str.expanded .. 'class="subcraft" | ' .. frame:expandTemplate{title = 'Gold', args = {v2.amount}} .. ", "
				else
					str.expanded = str.expanded .. 'class="subcraft" | ' .. frame:expandTemplate{title = 'Icon', args = {v2.name, v2.amount}} .. ", "
				end
			end
		else
			if total[v.name] == nil then
				total[v.name] = v.amount
			else
				total[v.name] = total[v.name] + v.amount
			end
		end
	end
	str.expanded = str.expanded:sub(1, -3)

	for k, v in pairs(total) do
		local object = resource[k] or data[k]
		if object.type == "Currency" then
			cat.currency[k] = v
		elseif string.find(k, "Spectra") then
			cat.common[k] = v
		elseif string.find(k, "Vestige") or string.find(k, "Essence") or string.find(k, "Recipe") then
			cat.unique[k] = v
		else
			cat.rare[k] = v
		end
	end
	
	local first = true
	for k, v in pairs(cat.unique) do
		if first then
			str.total = str.total .. "|-\n"
			str.total = str.total .. '! colspan=3 | <div style="color:#5ba3d7;font-size:200%;font-weight:700;font-style:italic;margin:1rem">Rare resources</div>\n'
		end
		first = false
		
		local object = resource[k] or data[k]
		
		str.total = str.total .. "|-\n"
		str.total = str.total .. "| " .. frame:expandTemplate{title = 'Icon', args = {k}} .. "\n"
		str.total = str.total .. "| " .. v .. "\n"
		str.total = str.total .. "| " .. object.description .. "@@"
	end
	
	for k, v in pairs(cat.rare) do
		if first then
			str.total = str.total .. "|-\n"
			str.total = str.total .. '! colspan=3 | <div style="color:#5ba3d7;font-size:200%;font-weight:700;font-style:italic;margin:1rem">Rare resources</div>\n'
		end
		first = false
		
		str.total = str.total .. "|-\n"
		str.total = str.total .. "| " .. frame:expandTemplate{title = 'Icon', args = {k}} .. "\n"
		str.total = str.total .. "| " .. v .. "\n"
		str.total = str.total .. "| " .. resource[k].description .. "@@"
	end
	
	first = true
	for k, v in pairs(cat.common) do
		if first then
			str.total = str.total .. "|-\n"
			str.total = str.total .. '! colspan=3 | <div style="color:#5ba3d7;font-size:200%;font-weight:700;font-style:italic;margin:1rem">Common resources</div>\n'
		end
		first = false
		
		str.total = str.total .. "|-\n"
		str.total = str.total .. "| " .. frame:expandTemplate{title = 'Icon', args = {k}} .. "\n"
		str.total = str.total .. "| " .. v .. "\n"
		str.total = str.total .. "| " .. resource[k].description .. "@@"
	end
	
	first = true
	for k, v in pairs(cat.currency) do
		if first then
			str.total = str.total .. "|-\n"
			str.total = str.total .. '! colspan=3 | <div style="color:#5ba3d7;font-size:200%;font-weight:700;font-style:italic;margin:1rem">Currency</div>\n'
		end
		first = false
		
		str.total = str.total .. "|-\n"
		str.total = str.total .. "| " .. frame:expandTemplate{title = 'Icon', args = {k}} .. "\n"
		str.total = str.total .. "| " .. v .. "\n"
		str.total = str.total .. "| " .. resource[k].description .. "@@"
	end
	str.total = str.total:sub(1, -3)
	
	return frame:expandTemplate{title = 'Crafting table', args = str}
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.