Module:NHMaterialCount

From Nookipedia, the Animal Crossing wiki

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

local p = {}
local cargo = mw.ext.cargo
local getArgs = require('Module:Arguments').getArgs

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

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

function p.main(frame)
    local args      = getArgs(frame)
    local args1     = args[1] or ''
    return p.materialCount(args1)
end

function p.materialCount(args1)
	local print = 0

    local tables = 'nh_recipe'
    local fields = "ROUND(SUM(nh_recipe.material1_num),1)=num"
    local args = {
        where = "nh_recipe.material1 = '" .. args1 .. "'",
        default = 0
    }
    local results1 = cargo.query( tables, fields, args )
    
    local tables2 = 'nh_recipe'
    local fields2 = "ROUND(SUM(nh_recipe.material2_num),1)=num"
    local args2 = {
        where = "nh_recipe.material2 = '" .. args1 .. "'",
        default = 0
    }
    local results2 = cargo.query( tables2, fields2, args2 )
    
    local tables3 = 'nh_recipe'
    local fields3 = "ROUND(SUM(nh_recipe.material3_num),1)=num"
    local args3 = {
        where = "nh_recipe.material3 = '" .. args1 .. "'",
        default = 0
    }
    local results3 = cargo.query( tables3, fields3, args3 )
    
    local tables4 = 'nh_recipe'
    local fields4 = "ROUND(SUM(nh_recipe.material4_num),1)=num"
    local args4 = {
        where = "nh_recipe.material4 = '" .. args1 .. "'",
        default = 0
    }
    local results4 = cargo.query( tables4, fields4, args4 )
    
    local tables5 = 'nh_recipe'
    local fields5 = "ROUND(SUM(nh_recipe.material5_num),1)=num"
    local args5 = {
        where = "nh_recipe.material5 = '" .. args1 .. "'",
        default = 0
    }
    local results5 = cargo.query( tables5, fields5, args5 )
    
    local tables6 = 'nh_recipe'
    local fields6 = "ROUND(SUM(nh_recipe.material6_num),1)=num"
    local args6 = {
        where = "nh_recipe.material6 = '" .. args1 .. "'",
        default = 0
    }
    local results6 = cargo.query( tables6, fields6, args6 )
    
    if not tableEmpty(results1) and not isEmpty(results1[1].num) then
    	print = print + tonumber(results1[1].num)
    end
    if not tableEmpty(results2) and not isEmpty(results2[1].num) then
    	print = print + tonumber(results2[1].num)
    end
    if not tableEmpty(results3) and not isEmpty(results3[1].num) then
    	print = print + tonumber(results3[1].num)
    end
    if not tableEmpty(results4) and not isEmpty(results4[1].num) then
    	print = print + tonumber(results4[1].num)
    end
    if not tableEmpty(results5) and not isEmpty(results5[1].num) then
    	print = print + tonumber(results5[1].num)
    end
    if not tableEmpty(results6) and not isEmpty(results6[1].num) then
    	print = print + tonumber(results6[1].num)
    end
    return print
end

return p