Module:MonthCollectablesNH

From Nookipedia, the Animal Crossing wiki

Module documentation (view)


{{#invoke:MonthCollectablesNH|main|Month (as number or name, defaults to current)|starting section number (defaults to 2)}}

-- Stolen from the animal crossing wikia

local lang = mw.getContentLanguage()
local cargo = mw.ext.cargo

local AC_MONTHS = { -- Bidirectional table for converting months and numbers to each other
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December",
	["January"]=1,
	["February"]=2,
	["March"]=3,
	["April"]=4,
	["May"]=5,
	["June"]=6,
	["July"]=7,
	["August"]=8,
	["September"]=9,
	["October"]=10,
	["November"]=11,
	["December"]=12
}

local CARGO_TABLE_NH_PREFIX = "nh_"
local CREATURE_TYPE_SUFFIXES = {"bug","fish","sea_creature"}
local HEMISPHERE_AVAILABILITY_CHECK_PREFIXES = {"n_","s_"}

local function creature_suffixes_to_name(s)
	if s == "sea_creature" then
		return "Sea creature"
	else
		return lang:ucfirst(s)
	end
end
local function hemisphere_prefixes_to_name(s)
	if s == "n_" then
		return "Northern hemisphere"
	elseif s == "s_" then
		return "Southern hemisphere"
	else
		return "s"
	end
end

return {
	main = function(frame)
		local target_month = tonumber(frame.args[1]) or AC_MONTHS[frame.args[1]] or AC_MONTHS[mw.title.getCurrentTitle().baseText] or tonumber(os.date("%m"))

		local next_month = (target_month % #AC_MONTHS) + 1
		local prev_month = ((target_month+(#AC_MONTHS-2)) % #AC_MONTHS)+1

		local w = mw.html.create()
		
		local starting_section_header_depth = tonumber(frame.args[2]) or 2
		local extra_levels_string = ""
		for i=3,starting_section_header_depth do
			extra_levels_string = extra_levels_string .. "="
		end
		
		for i=1,#HEMISPHERE_AVAILABILITY_CHECK_PREFIXES do
			local cargo_hemisphere_prefix = HEMISPHERE_AVAILABILITY_CHECK_PREFIXES[i]
			w:wikitext(extra_levels_string,"==",hemisphere_prefixes_to_name(cargo_hemisphere_prefix),extra_levels_string,"==\n")
			for j=1,#CREATURE_TYPE_SUFFIXES do
				local cargo_creature_suffix = CREATURE_TYPE_SUFFIXES[j]
				
				w:wikitext(extra_levels_string,"===",creature_suffixes_to_name(cargo_creature_suffix),extra_levels_string,"===\n")
				
				local collectablesTable = w:tag("table")
					:addClass("wikitable")
					:addClass("sortable")
					:css("text-align","center")
					:css("width","100%")

				local tr = collectablesTable:tag("tr")
				tr:tag("th"):wikitext("Name")
				tr:tag("th"):css("width","3.5em"):wikitext("Sale price")
				tr:tag("th"):wikitext("Time of day")

				for _,v in ipairs(cargo.query(CARGO_TABLE_NH_PREFIX..cargo_creature_suffix,"_pageName=page,name,sell_nook,"..cargo_hemisphere_prefix.."m"..target_month.."_time=timeofday,"..cargo_hemisphere_prefix.."m"..(prev_month).."=AvailPrevMonth,"..cargo_hemisphere_prefix.."m"..(next_month).."=AvailNextMonth", { limit = 5000, where = ""..cargo_hemisphere_prefix.."m"..(target_month) })) do
					local tr = collectablesTable:tag("tr")
					local nameCell = tr:tag("td"):wikitext("[[",v.page,"|",v.name,"]]")
					if v.AvailPrevMonth == "0" then
						nameCell:css("font-weight","bold")
					end
					if v.AvailNextMonth == "0" then
						nameCell:css("background-color","#FFC0C0")
					end
					tr:tag("td"):wikitext(lang:formatNum(tonumber(v.sell_nook)))
					tr:tag("td"):wikitext(v.timeofday)
				end
				w:wikitext("\n")
			end
		end
		return w
	end
}