Module:VariantDetails: Difference between revisions

From Eco - English Wiki
[unchecked revision][unchecked revision]
No edit summary
No edit summary
Line 45: Line 45:
      
      
     -- string used to build the card
     -- string used to build the card
    local card = '<div class="col mb-4"><div class="card mb-3" style="background: #d1ecf1; border: 5px solid #7BC2CE;">'
local card = '{| class=\"wikibox\"\n'
   
card = card .. '| <div class="col mb-4"><div class="card mb-3" style="background: #d1ecf1; border: 5px solid #7BC2CE;"> \n'
   
-- Name of card
-- Name of card
card = card .. '<div class="card-header" style="text-align: center;">' .. variantName .. '</div>'
card = card .. '|- style=\"color: white; background-color: #1165AF; text-align: center;\"\n| colspan=\"2\" | \'\'\'<big>' .. variantName .. '</big>\'\'\'\n'


-- Variant: Icon Image
-- Variant: Icon Image
Line 61: Line 62:
-- create image cell
-- create image cell
card = card .. '<div class="iconContainer"><div class="iconStack">[[File:' .. image .. '|frameless|class=iconBlue]]</div><div class=iconBorder style=\"position:absolute;\"></div></div>'
card = card .. '| <div class="iconContainer"><div class="iconStack">[[File:' .. image .. '|frameless|class=iconBlue]]</div><div class=iconBorder style=\"position:absolute;\"></div></div> /n'


-- Object Placed Image
-- Object Placed Image
Line 70: Line 71:
image = itemimagename .. '_Placed.jpg'
image = itemimagename .. '_Placed.jpg'
end
end
card = card .. ' <p class="card-img-bottom" style="text-align-last: center;">  [[File:' .. image .. '|100px]] </p> </div> </div>'
card = card .. '| <p class="card-img-bottom" style="text-align-last: center;">  [[File:' .. image .. '|100px]] </p> </div> </div> /n'
card = card .. '|}\n'
return card
return card

Revision as of 14:17, 4 February 2021

Documentation

This module provides the back end functionality of the Template:VariantDetails.

If the template is passed, this module creates a table using details from the following Modules:


-- Credit:

local p = {}

-- Grabs args from the parent frame
-- Trims and parses the args into a table, then returns the table
function norm()
    local origArgs = mw.getCurrentFrame():getParent().args
    local args = {}
    
    for k, v in pairs( origArgs ) do
        v = mw.text.trim( tostring( v ) )
        if v ~= '' then
            args[k] = v
        end
    end
    
    return args
end

-- build a variant card
function buildVariant( variantName )
	-- variant cards require itemData
	local itemData = require( "Module:ItemData" )
	
	-- check to see if module loaded
	if (itemData == nil) then
		return "Item Data not loaded"
	end -- if itemData does not exist
	
	-- get the item data tables
	local itemList = itemData.items
	
	-- get the variant from the list
	local variant = itemList[variantName]
	
	-- variant does not exist as an item then just return
	if variant == nil then
		return "No variant here"
	end
	
    local itemType = variant.type
	local itemEN = string.sub (itemType, 1, -5)
	local itemimagename = string.gsub(itemEN, ' ', '')
    
    -- string used to build the card
	local card = '{| class=\"wikibox\"\n'
	card = card .. '| <div class="col mb-4"><div class="card mb-3" style="background: #d1ecf1; border: 5px solid #7BC2CE;"> \n'
		    
	-- Name of card
	card = card .. '|- style=\"color: white; background-color: #1165AF; text-align: center;\"\n| colspan=\"2\" | \'\'\'<big>' .. variantName .. '</big>\'\'\'\n'

	-- Variant: Icon Image
	local image = 'NoImage.png|link=https://wiki.play.eco/index.php?title=Special:Upload&wpDestFile=' .. itemimagename .. '_Icon.png|[[Category:Pages_with_missing_icon]]'
	
-- find the image file if it exists	
	if mw.title.makeTitle('File', itemimagename .. '_Icon.png').file.exists then
		image = itemimagename .. '_Icon.png'
	elseif mw.title.makeTitle('File', itemimagename .. '_Icon.jpg').file.exists then
		image = itemimagename .. '_Icon.jpg'
	end -- if image file found
	
	-- create image cell	
	card = card .. '| <div class="iconContainer"><div class="iconStack">[[File:' .. image .. '|frameless|class=iconBlue]]</div><div class=iconBorder style=\"position:absolute;\"></div></div> /n'

	-- Object Placed Image
	local image = 'NoImage.png|link=https://wiki.play.eco/index.php?title=Special:Upload&wpDestFile=' .. itemimagename .. '_Placed.png|[[Category:Pages_with_missing_placed]]'
	if mw.title.makeTitle('File', itemimagename .. '_Placed.png').file.exists then
		image = itemimagename .. '_Placed.png'
	elseif mw.title.makeTitle('File', itemimagename .. '_Placed.jpg').file.exists then
		image = itemimagename .. '_Placed.jpg'
	end
	card = card .. '| <p class="card-img-bottom" style="text-align-last: center;">  [[File:' .. image .. '|100px]] </p> </div> </div> /n'
	card = card .. '|}\n'
		
	return card	
end

-- build all variant cards
function p.main( args )  
	local args = norm()
	
	-- check that all necessary arguments are passed correctly
    if args.name == nil or args.name == '' then
        return '\'name\' must be specified.'
    end
    
	-- name passed in will be the default recipe variant
	local defaultName = args.name
	
	-- need both crafting recipes to find variants
	local recipeData =  require( "Module:CraftingRecipes" )
	
	-- get the tables from the modules
	local recipeList = recipeData.recipes
	
	-- get the main recipe
	local defaultRecipe = recipeList[defaultName]
	
	-- break if defaultName not found
	if (defaultRecipe == nil) then
		return defaultName .. " could not be found in the recipe data"
	end -- if default is nil
	
	-- get the variants for the recipe
	local variants = defaultRecipe.variants
	local numVariants = tonumber(defaultRecipe.numberOfVariants)
	-- break if no variants. return blank to not break page flow.
	if (variants == nil or numVariants <= 1) then
		return "No Variants to find"
	end -- if variants don't exist
	
	-- create variable to store returned text
	local text = ""
	text = text .. '<div class="card-deck">'
	
	for k,v in pairs(variants) do		
		--skip the default variant
		if (k ~= defaultName) then
			-- build the text for each variant
			text = text .. buildVariant(k) .. ''
		end -- if variant is not default
	end -- for each variant
	
	text = text ..' </div>'
	
	return text
end	
	
return p