Module:WWVillagerItemOutput

From Nookipedia, the Animal Crossing wiki

Module documentation (view)


Usage

This Lua module outputs the villager data info for item pages from Animal Crossing: Wild World in regards to their furniture, clothing, umbrella, and interior items. These are put through {{WWFurnitureVillagers}} and {{WWClothingVillagers}}. Please refer to the documentation of each of these templates for more information.


local p = {}
local cargo = mw.ext.cargo
local formatList = require('Module:List').listFormat

function tableEmpty(s)
	return next(s) == nil
end

function isEmpty(s)
	return s == nil or s == ''
end

function p.furnitureOutput( frame )
	local print = ''
	local data = {}

	-- Cargo query for ww_house
    local tables = 'ww_house'
    local fields = "ww_house.villager=villager,ww_house._pageTitle=pageName"
    local args = {
        where = "ww_house.items LIKE '%\"" .. frame.args['1']:gsub("\'","\\'") .. "\"%'",
        orderBy = 'ww_house.villager',
        limit = 300,
        default = ''
    }
    local results = cargo.query( tables, fields, args )
    
    if not tableEmpty(results) then
    	if not isEmpty(frame.args['disable-clothing']) then
    		print = print .. "However, this item "
    	else
    		print = print .. "This item "
    	end
    	if not isEmpty(frame.args['clothing']) and isEmpty(frame.args['disable-clothing']) then
    		print = print .. "also "
    	else
    		print = print .. " "
    	end
    	print = print .. "appears as a furniture item in the homes of "
	    for r = 1, #results do
	    	data[r] =  "[[File:" .. results[r].villager .. "'s Pic WW Texture.png|x25px|link=|alt=|" .. results[r].villager .. "]] [[" .. results[r].pageName .. "|" .. results[r].villager .. "]]"
	    end
	    print = print .. formatList(data) .. "."
	else
		print = print .. "No villagers have this item in their home."
    end
    
    return print
end

function p.clothingOutput( frame )
	local print = ''
	local data = {}

	-- Cargo query for ww_villager
    local tables = 'ww_villager'
    local fields = "ww_villager.pic=icon,ww_villager.name=villager,ww_villager._pageTitle=pageName"
    local args = {
        where = "ww_villager.clothing = '" .. frame.args['1']:gsub("\'","\\'").. "'",
        orderBy = 'ww_villager.name_sort',
        limit = 300,
        default = ''
    }
    local results = cargo.query( tables, fields, args )
    if not tableEmpty(results) then
    	print = print .. "This item is worn by "
	    for r = 1, #results do
	    	data[r] =  "[[File:" .. results[r].villager .. "'s Pic WW Texture.png|x25px|link=|alt=|" .. results[r].villager .. "]] [[" .. results[r].pageName .. "|" .. results[r].villager .. "]]"
	    end
	    print = print .. formatList(data) .. " as their default outfit."
	else
		print = print .. "No villagers wear this item as their default outfit."
    end

    return print
end

function p.umbrellaOutput( frame )
	local print = ''
	local data = {}

	-- Cargo query for ww_villager
    local tables = 'ww_villager'
    local fields = "ww_villager.pic=icon,ww_villager.name=villager,ww_villager._pageTitle=pageName"
    local args = {
        where = "ww_villager.umbrella = '" .. frame.args['1']:gsub("\'","\\'") .. "'",
        orderBy = 'ww_villager.name_sort',
        limit = 300,
        default = ''
    }
    local results = cargo.query( tables, fields, args )

    if not tableEmpty(results) then
    	print = print .. "This item is used by "
	    for r = 1, #results do
	    	data[r] =  "[[File:" .. results[r].villager .. "'s Pic WW Texture.png|x25px|link=|alt=|" .. results[r].villager .. "]] [[" .. results[r].pageName .. "|" .. results[r].villager .. "]]"
	    end
	    print = print .. formatList(data) .. " as their default umbrella during periods of [[weather|rain]]."
    else
		print = print .. "No villagers use this item as their default umbrella during periods of [[weather|rain]]."
    end

    return print
end

function p.interiorOutput( frame )
	local print = ''
	local data = {}

	-- Cargo query for ww_house
	local tables
	local fields
	local args
	local results
	if not isEmpty(frame.args['type']) and frame.args['type']:lower() == "wallpaper" then
	    tables = 'ww_house'
	    fields = "ww_house.villager=villager,ww_house._pageTitle=pageName"
	    args = {
	        where = "ww_house.wallpaper = '" .. frame.args['1']:gsub("\'","\\'") .. "'",
	        orderBy = 'ww_house.villager',
	        limit = 300,
	        default = ''
	    }
	    results = cargo.query( tables, fields, args )
	elseif not isEmpty(frame.args['type']) and frame.args['type']:lower() == "carpet" then
	    tables = 'ww_house'
	    fields = "ww_house.villager=villager,ww_house._pageTitle=pageName"
	    args = {
	        where = "ww_house.flooring = '" .. frame.args['1']:gsub("\'","\\'") .. "'",
	        orderBy = 'ww_house.villager',
	        limit = 300,
	        default = ''
	    }
	    results = cargo.query( tables, fields, args )
	else
		results = {}
	end
    if not tableEmpty(results) then
    	print = print .. "This item appears in the homes of "
	    for r = 1, #results do
	    	data[r] =  "[[File:" .. results[r].villager .. "'s Pic WW Texture.png|x25px|link=|alt=|" .. results[r].villager .. "]] [[" .. results[r].pageName .. "|" .. results[r].villager .. "]]"
	    end
	    print = print .. formatList(data)
	    if not isEmpty(frame.args['type']) and frame.args['type']:lower() == "wallpaper" then
	    	print = print .. ' as the default wallpaper.'
	    elseif not isEmpty(frame.args['type']) and frame.args['type']:lower() == "carpet" then
	    	print = print .. ' as the default carpet.'
	    end
	else
		print = print .. "No villagers have this item in their home."
    end

    return print
end

return p