Module:Coromon

From Coromon Wiki
Jump to navigation Jump to search

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

local p = { }

local util = require("Module:Util")
local coromonList = mw.loadData("Module:Coromon/List");

local errorMsg = "N/A"

function p.isValid (frame)
	
	local name_or_id = frame.args[1] or frame.args["coromon"]
	local mon = p.getCoromon(name_or_id)
	
	return mon==nil and "false" or "true"
end

function p.getID (name_id)
	
	local checkForDarkMagic = util.startsWith(name_id, "Crimsonite") or util.startsWith(name_id, "DARKMAGIC")
	if util.startsWith(name_id, "Crimsonite") then
		name_id = string.gsub(name_id, "Crimsonite ", "")
	end
	
	for id, mon in pairs(coromonList) do
		local isDarkMagic = mon["type"] == "Crimsonite"
		
		if isDarkMagic == checkForDarkMagic and (string.gsub(mon["name"], "ø", "o") == name_id or mon.name == name_id) or id == name_id then
			mw.logObject(mon)
			return id
		end
	end
	
	return nil
end

function p.getCoromon (name_id)
	
	local checkForDarkMagic = util.startsWith(name_id, "Crimsonite") or util.startsWith(name_id, "DARKMAGIC")
	if util.startsWith(name_id, "Crimsonite") then
		name_id = string.gsub(name_id, "Crimsonite ", "")
	end
	
	for id, mon in pairs(coromonList) do
		local isDarkMagic = mon["type"] == "Crimsonite"
		
		if isDarkMagic == checkForDarkMagic and (string.gsub(mon["name"], "ø", "o") == name_id or mon.name == name_id) or id == name_id then
			--mw.logObject(mon)
			return mon
		end
	end
	
	return nil
end

function p.getIcon (frame)
	
	local mon = p.getCoromon(frame.args[1] or frame.args['coromon'])
	local id = p.getID(frame.args[1] or frame.args["coromon"])
	local category = frame.args[2] or frame.args['category'] or 'A'
	
	if  id=='FUSEBOX' then
		return '00_Fusebox_' .. category
	elseif util.startsWith(id, 'TITAN') then
		return '00_titan' .. mon.type .. '_' .. mon.name
	end
	
	return mon.dex_number .. '_' .. mon.name .. '_' .. category
end

function p.dexToName (frame)
	
	local dex_no = frame.args[1] or frame.args["dex_number"]
	
	for k, v in pairs(coromonList) do
		if v["dex_number"] ~= nil and v["dex_number"] == tonumber(dex_no) then
			if v["type"] == "Crimsonite" then
				return 'Crimsonite ' .. v['name']
			else
				return v["name"]
			end
		end
	end
	
	return nil
end

function p.getCoromonID (frame)
	
	local id = p.getID(frame.args[1] or frame.args["coromon"])
	
	if id == nil then
		return errorMsg
	end
	
	return id
end

function p.getFamilyID (frame)
	
	local id = p.getID(frame.args[1] or frame.args["coromon"])
	
	if id == nil then
		return ""
	end
	
	local _, underscore_count = string.gsub(id, "%_", "")
	
	if underscore_count < 2 then
		return id
	end
	
	return string.sub(id,1,string.len(id)-2)
end

function p.getLink (frame)
	
	local mon = p.getCoromon(frame.args[1] or frame.args["coromon"])
	
	if mon == nil then
		return ""
	end
	
	if mon.type == "Crimsonite" then
		return "Crimsonite " .. mon.name
	end
	
	return mon.name
end

function p.getParamOrDefaultFor (coromon, param, valueIfNil)
	
	if coromon == nil then
		return errorMsg
	end

	return coromon[param] or valueIfNil
end

function p.getListOfCoromon (f)
	
	local args = f.args["args"] or f.args[1] or ""
	if args ~= "" then
		args = "|" .. args
	end
	
	local wikitext = '{{CoromonList|Start'..  args ..'}}\n'
			
	local orderedMon = util.spairs(coromonList, 
		function(t,a,b) 
			local da = t[a].dex_number and t[a].dex_number or 999
			local db = t[b].dex_number and t[b].dex_number or 999
			return db > da
	    end)
		
	for k, v in orderedMon do
		if v.dex_number ~= nil then
			if v["type"] == "Crimsonite" then
				wikitext = wikitext .. '{{CoromonList|Crimsonite ' .. v.name .. args ..'}}\n'
			else
				wikitext = wikitext .. '{{CoromonList|' .. v.name .. args ..'}}\n'
			end
		end
	end
	
	wikitext = wikitext .. '{{CoromonList|End}}'
	--return wikitext
	return f:preprocess(wikitext)
end

function p.getAllCoromonOfType (f)
	
	local t = f.args[1] or f.args["type"]
	
	local wikitext = [[{| class="article-table" style="width:40%; text-align:center;"
! scope="col" style="text-align:center;"| #
! scope="col" style="text-align:center;"| Name
]]
			
	local orderedMon = util.spairs(coromonList, 
		function(t,a,b) 
			local da = t[a].dex_number and t[a].dex_number or 999
			local db = t[b].dex_number and t[b].dex_number or 999
			return db > da
	    end)
		
	for k, v in orderedMon do
		if v["type"] == t then
			wikitext = wikitext .. '|- \n' ..
				'| ' .. (v.dex_number and '#' .. v.dex_number or '-')  .. '\n' ..
				'| style="text-align:left;"| {{A|' .. v.name .. '}}\n'
		end
	end
	
	wikitext = wikitext .. '|}'
	--return wikitext
	return f:preprocess(wikitext)
end

function p.statTotal (frame)
	
	local mon = p.getCoromon(frame.args[1] or frame.args["coromon"])
	
	return mon["hp"] + mon["attack"] + mon["defense"] + mon["specialAttack"] + mon["specialDefense"] + mon["speed"] + 34
end

function p.getParam (frame)
	
	local mon = p.getCoromon(frame.args[1] or frame.args["coromon"])
	local param = frame.args[2] or frame.args["param"]
	
	return mon[param]
end

function p.getType (frame)
	return p.getParamOrDefaultFor(p.getCoromon(frame.args[1] or frame.args["coromon"]), "type", "N/A")
end

function p.getRarity (frame)
	return p.getParamOrDefaultFor(p.getCoromon(frame.args[1] or frame.args["coromon"]), "rarity", "")
end

function p.getName (frame)
	return p.getParamOrDefaultFor(p.getCoromon(frame.args[1] or frame.args["coromon"]), "name", "")
end

function p.getEvoID (frame)
	return p.getParamOrDefaultFor(p.getCoromon(frame.args[1] or frame.args["coromon"]), "evo_id", "")
end

function p.getDexNo (frame)
	return p.getParamOrDefaultFor(p.getCoromon(frame.args[1] or frame.args["coromon"]), "dex_number", "-")
end

function p.getProperties (frame)
	
	local mon = p.getCoromon(frame.args['coromon'] or frame.args[1])
	
	if mon == nil then
		return ""
	end
	
	if mon.properties ~= nil then
		
		local output = ''
		for i = 1, util.lengthOf(mon.properties), 1 do
			output = output .. util.firstToUpper(mon.properties[i]) .. '\n'
		end
		return output
	end

	return ''
end

return p