Module:Housing

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

local p = {}
local utils = require("Module:Utils")
local constant = mw.loadJsonData("Module:Data/constant.json")
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

function p.createHousingList(frame)
	local str = ""
	
	local whitelist = constant["Housing Whitelist"]
	for k, v in pairs(whitelist) do
		local item = data[v]
		local filter = {
		    ["Decor"] = "decor",
		    ["Artifacts"] = "artifacts",
		    ["Trophies"] = "trophies",
		    ["Pets"] = "pets",
		    ["Furnishings"] = "furnishings",
		    ["Lighting"] = "lighting",
		    ["Decorations"] = "decorations",
		    ["Wall Decor"] = "walldecor",
		    ["Floor"] = "floor",
		    ["Item Surface"] = "itemsurface",
		    ["Wall"] = "wall",
		    ["Ceiling"] = "ceiling"
		}
		
		local class = "filter-item " .. filter[item.type] .. (item.subtype ~= nil and " " .. filter[item.subtype] or "")
		for k2, v2 in pairs(item.placement) do
			class = class .. " " .. filter[v2]
		end
		
		local template = {
			image = string.gsub(v, ":", ""),
			name = v,
			class = class
		}
		
		str = str .. frame:expandTemplate{title = 'Item box', args = template}
	end

	return str
end

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