Module:Egg moves

From Official Temtem Wiki
Jump to navigation Jump to search

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

local p = {}
local cargo = mw.ext.cargo

function p.main(frame)
	
	local parentName = frame.args[1]
	
	-- For our first query here, we query children and technique based on the parent
	-- We use this to get a list of all children given a parent and a technique
	local tables = 'Learnsets, LearnsetsParents, Temtem'
	local fields = 'LearnsetsParents.parent, Learnsets.technique, LearnsetsParents.child, Temtem.no'
	local args = {
		join = 'Learnsets.technique = LearnsetsParents.technique, LearnsetsParents.child = Temtem.name',
		where = 'LearnsetsParents.parent = "' .. parentName .. '"',
        groupBy = 'parent, child, technique',
        orderBy = 'Learnsets.technique, Temtem.no'
    }
    local results = cargo.query( tables, fields, args )
    
    -- start the header of the wikitable we'll output for display
    local output = '<table class="wikitable learnsets-children"><tr><th>Technique</th><th>Can be passed to</th><th>Source</th></tr>'
    
    -- If we have no results, return here with a message instead of data
    if next(results) == nil then
    	return output .. '<tr><td colspan="3">' .. parentName .. ' does not learn any moves it can pass on.' .. '</td></tr></table>'
    end

    -- Build the first part of our table here. This will be in the form of
	-- {
	--   ["Hologram"] = {
	--     ["children"] = {
	--       "Chubee",
	--       "Waspeen",
	--     },
	--   repeat for each technique
	-- }
    local techniques = {}
    for i, row in pairs(results) do
    	if techniques[row['Learnsets.technique']] == nil then
    		techniques[row['Learnsets.technique']] = { children = {}, source = '' }
    	end
    	table.insert(techniques[row['Learnsets.technique']]['children'], row['LearnsetsParents.child'])
    end
    
    -- This second query gets the method by which the parent learns the technique
    -- I couldn't find a way to reasonably get this inside the first query so we do a second one
    results = cargo.query( 'Learnsets', 'Learnsets.technique, Learnsets.method', {where = 'Learnsets.temtem = "' .. parentName .. '"'} )    
    
    -- Add this method under each technique table, so our final table looks like
	-- {
	--   ["Hologram"] = {
	--     ["children"] = {
	--       "Chubee",
	--       "Waspeen",
	--     },
	--     ["source"] = "Level up/Breeding/Technique Course",
	--   },
	--   repeat for each technique
	-- }
    for i, row in pairs(results) do
    	if techniques[row['Learnsets.technique']] then
    		techniques[row['Learnsets.technique']]['source'] = row['Learnsets.method']
    	end
    end
    
    -- Create a sorted list of the techniques to iterate through so we can have these in order because Lua sucks
    local orderedTechniques = {}
	for tech, _ in pairs(techniques) do
		table.insert(orderedTechniques, tech)
	end
	table.sort(orderedTechniques)
    
    -- Build the rest of our table output table
    for _,tech in pairs(orderedTechniques) do
    	output = output .. '<tr><td>[[' .. tech .. ']]</td><td>'
    	for i, tem in pairs(techniques[tech]['children']) do
    		output = output .. frame:expandTemplate{ title = 'Temtem portrait', args = { tem, '35px', background = '1'}}
    	end
    	output = output .. '</td><td>' .. techniques[tech]['source'] .. '</td></tr>'
    end
    
    output = output .. '</table>'
    return output
	
end

return p