Module:Infobox Item

From Eco - English Wiki
Revision as of 09:34, 14 August 2020 by ZeelNightwolf (talk | contribs) (Argh.)

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

If the template is passed an Infobox will be made using details from the following Modules:

Credit

Original Infobox (now known as Infobox_Item) created by Pradoxzon was then edited by Nesphit and TreeNuts0. Fyre (FishAus) and Scotty (ZeelNightwolf) further edited the Infobox_Item. They then duplicated and used as a base for Infobox_Skill, Infobox_Plant, and Infobox_Animal.


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 an Item Infobox
function itemBox( args, itemData )
    -- check that all necessary arguments are passed correctly
    if args.name == nil or args.name == '' then
        return '\'name\' must be specified.'
    end
    
	local item = args.name
    
	local itemimagename = string.gsub(item, ' ', '')
        
	local itemTable = itemData.items[item]
	if itemTable == nil then
		return item .. ' could not be found in Module:ItemData.'
	end
    
    -- string used to build the infobox
    local infobox = '{| class=\"infobox\"\n'
    
	-- 'Name and Image' section
		-- name of the item
		infobox = infobox .. '|- style=\"color: white; background-color: darkred; text-align: center;\"\n| colspan=\"2\" | \'\'\'<big>' .. item .. '</big>\'\'\'\n'
    
		-- the item's type (ItemData - group)
		infobox = infobox .. '|- style=\"text-align: center; color: white; background-color: '
	
		if itemTable.group == 'Food' then
			infobox = infobox .. '#4b9130;\"\n| colspan=\"2\" | \'\'\'Food\'\'\'\n'
		elseif itemTable.group == 'Skill Books' or itemTable.group == 'Skill Scrolls' then
			infobox = infobox .. '#af8d33;\"\n| colspan=\"2\" | \'\'\'' .. itemTable.group .. '\'\'\'\n'
		else
			infobox = infobox .. '#517ab2;\"\n| colspan=\"2\" | \'\'\'' .. itemTable.group .. '\'\'\'\n'
		end
    
		-- Items 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]]'
		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
    
		infobox = infobox .. '|-\n| colspan=\"2\" style=\"padding: 10px;\" | [[File:' .. image .. '|center|frameless|95px]]\n'
    
		  -- 'Description' section header
		infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'Description\'\'\'\n'	
		
		if itemTable.description == nil then
			infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | \'\'\'None\'\'\'\n'
		elseif itemTable.description == '' then
			infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | \'\'\'None\'\'\'\n'
		else
			infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | ' .. itemTable.description .. '\n'
		end
	
    -- 'General' section header
		infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'General\'\'\'\n'
    
		-- created at
		local craftingRecipes = require( "Module:CraftingRecipes" )
		local recipes = craftingRecipes.recipes
	
		local function addToSet(set, key)
			set[key] = true
		end
	
		local function setNotContains(set, key)
			return set[key] == nil
		end
	
		if craftingRecipes.items[item] ~= nil and #craftingRecipes.items[item] >= 1 then
			local count = 0
			local stations = {}
			local sortStations = {}
			local stationString = ''
			local found = false
			for k, v in ipairs( craftingRecipes.items[item] ) do
				for num = 1, #recipes[v].products do	
					if (recipes[v].products[num][1] ~= nil and recipes[v].products[num][1] == item) then
						if setNotContains(stations, recipes[v].craftStn[1]) then
							addToSet(stations, recipes[v].craftStn[1])
							count = count + 1
						end	
						if found == false then
							found = true
							infobox = infobox .. '|-\n| Created at:\n| style=\"text-align: right;\" |'
						end
					end
				end
			end
		
			local track = 0
			if found == true then
				for a, b in pairs(stations) do
					table.insert(sortStations, a)
				end
				table.sort(sortStations)
				for i, n in ipairs(sortStations) do				
					stationString = stationString .. ' [[' .. n .. ']]'
					track = track + 1
					if track ~= count then -- Changed these line to produce comma if not last table.
						stationString = stationString .. ','
					end
				end
			infobox = infobox .. stationString .. '\n'		
			else
				infobox = infobox .. '|-\n| Created at:\n| style=\"text-align: right;\" | N/A\n'
			end
		elseif args.created ~= nil and args.created ~= '' then
			infobox = infobox .. '|-\n| Created at:\n| style=\"text-align: right;\" | ' .. args.created .. '\n'
		else
			infobox = infobox .. '|-\n| Created at:\n| style=\"text-align: right;\" | N/A\n'
		end

		-- calories and nutrients (if itemTable.group == 'Food')
		if itemTable.group == 'Food' then
			infobox = infobox .. '|-\n| Calories:\n| style=\"text-align: right;\" | ' .. itemTable.calories .. '\n|- valign=\"center\"\n| rowspan=\"4\" | Nutrients:\n'
			infobox = infobox .. '| style=\"color: red; text-align: right;\" | Carbs: ' .. itemTable.carbs .. '\n'
			infobox = infobox .. '|- valign=\"center\"\n| style=\"color: orange; text-align: right;\" | Protein: ' .. itemTable.protein .. '\n'
			infobox = infobox .. '|- valign=\"center\"\n| style=\"color: yellow; text-align: right;\" | Fat: ' .. itemTable.fat .. '\n'
			infobox = infobox .. '|- valign=\"center\"\n| style=\"color: limegreen; text-align: right;\" | Vitamins: ' .. itemTable.vitamins .. '\n'
			infobox = infobox .. '|-\n| Nutrition Density:\n| style=\"text-align: right;\" | ' .. itemTable.density .. ' per 100 cals\n'
		end
	
		-- carried
		if args.carried ~= nil and args.carried ~= '' then
			infobox = infobox .. '|-\n| Carried in:\n| style=\"text-align: right;\" | ' .. args.carried .. '\n'
		else
			infobox = infobox .. '|-\n| Carried in:\n| style=\"text-align: right;\" | ' .. itemTable.carried .. '\n'
		end
    
		-- weight
		if itemTable.weight ~= nil then
			infobox = infobox .. '|-\n| Weight:\n| style="text-align: right;" | ' .. itemTable.weight .. ' kg\n'
		else
			infobox = infobox .. '|-\n| Weight:\n| style="text-align: right;" | 0.00kg\n'
		end
    
		-- stack limit
		if itemTable.maxStack ~= nil then
			infobox = infobox .. '|-\n| Stack limit:\n| style=\"text-align: right;\" | ' .. itemTable.maxStack .. '\n'
		end
	
		-- yield 
		if itemTable.yield ~= nil then
			infobox = infobox .. '|-\n| Improve Gathering: \n| style=\"text-align: right;\" | ' .. itemTable.yield .. '\n'
		end 	
	
		-- fuel
		if itemTable.fuel ~= nil then
			infobox = infobox .. '|-\n| Fuel energy:\n| style=\"text-align: right;\" | ' .. itemTable.fuel .. ' J\n'
		end
    
		-- currency
		if itemTable.currency ~= nil then
			infobox = infobox .. '|-\n| Used as Currency:\n| style=\"text-align: right;\" | ' .. itemTable.currency .. '\n'
		end
	
    -- 'IDs' section header
		infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'IDs\'\'\'\n'
    
		-- item id (type)
		infobox = infobox .. '|- valign=\"center\"\n| Item ID:\n| style=\"text-align: right;\" | ' .. itemTable.type .. '\n'
    
		-- id number (type id)
		infobox = infobox .. '|- valign=\"center\"\n| ID Number:\n| style=\"text-align: right;\" | ' .. itemTable.typeID .. '\n'

    -- 'Item' World Object header (if itemTable.group = Placeable or Blocks)
		if itemTable.group == 'Block Items' or itemTable.group == 'World Object Items' then
			infobox = infobox .. '|- style=\"text-align: center; background-color: #517ab2;\"\n| colspan=\"2\" | \'\'\'World Object\'\'\'\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
			infobox = infobox .. '|-\n| colspan=\"2\" style=\"padding: 10px;\" | [[File:' .. image .. '|center|border|240px]]\n'
			  
		-- 'Placement' section
			--Placement Header
			infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'Placement\'\'\'\n'

			--Vechile
			if itemTable.mobile ~= nil then
				infobox = infobox .. '|-\n| Vehicle/Mobile Object:\n| style=\"text-align: right;\" | ' .. itemTable.mobile .. '\n'
			end			
				
			--Dimensions 
			if itemTable.footprint ~= nil then
				infobox = infobox .. '|-\n| Dimensions (X,Y,Z):\n| style=\"text-align: right;\" | ' .. itemTable.footprint .. '\n'
			end
			
			--Material Tier
			if itemTable.materialTier ~= nil or itemTable.materialTier == 0 then
				infobox = infobox .. '|-\n| Room Material:\n| style=\"text-align: right;\" | Tier ' .. itemTable.materialTier .. '\n'
			end
			
			--Room Req.
			if itemTable.roomContainReq ~= nil then
				infobox = infobox .. '|-\n| Room Required:\n| style=\"text-align: right;\" | ' .. itemTable.roomContainReq .. '\n'
			end	
			
			--Room Size. Req.
			if itemTable.roomSizeReq ~= nil then
				infobox = infobox .. '|-\n| Room Size:\n| style=\"text-align: right;\" | ' .. itemTable.roomSizeReq .. ' m³\n'
			end
			
			--Room Mat. Req.
			if itemTable.roomMatReq ~= nil then
				infobox = infobox .. '|-\n| Room Materials:\n| style=\"text-align: right;\" | ' .. itemTable.roomMatReq .. '\n'
			end						
			
			-- Object Form Image
			if itemTable.group == 'Block Items' then
				local image = 'NoImage.png|link=https://wiki.play.eco/index.php?title=Special:Upload&wpDestFile=' .. itemimagename .. '_Form.png|[[Category:Pages_with_missing_form]]'
				if mw.title.makeTitle('File', itemimagename .. '_Form.png').file.exists then
						image = itemimagename .. '_Form.png'
				elseif mw.title.makeTitle('File', itemimagename .. '_Form.jpg').file.exists then
						image = itemimagename .. '_Form.jpg'
				end
			infobox = infobox .. '|-\n| colspan=\"2\" style=\"padding: 10px;\" | [[File:' .. image .. '|center|border|240px]]\n'
			end	

		-- 'Housing' section (if there is a Room Category)
			if itemTable.roomCategory ~= nil then  
				-- Housing Header
				infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'Housing\'\'\'\n'
    
				--roomCategory
				infobox = infobox .. '|-\n| Room Category:\n| style=\"text-align: right;\" | ' .. itemTable.roomCategory .. '\n'

				if itemTable.roomCategory ~= 'Industrial' then
					if itemTable.furnitureType ~= nil then 
					--furnitureType
						infobox = infobox .. '|-\n| Furniture Type:\n| style=\"text-align: right;\" | ' .. itemTable.furnitureType .. '\n'
            
					--Value of the object
						infobox = infobox .. '|-\n| Value:\n| style=\"text-align: right;\" | ' .. itemTable.skillValue .. '\n'
            
					--Dim. Return % of Object  
						infobox = infobox .. '|-\n| Dim. Return %:\n| style=\"text-align: right;\" | ' .. itemTable.repeatsDepreciation .. '\n'    
					end
				end
		
				if itemTable.roomCategory == 'Industrial' then
					infobox = infobox .. '|- style=\"background-color: red; text-align: center;\"\n| colspan=\"2\" | \'\'\'ALL ROOM VALUE LOST\'\'\'\n'
				end
			end
		-- 'Storage' Section (if inventorySlots is not nil)
			if itemTable.inventorySlots ~= nil then
				-- Storage Header
				infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'Storage\'\'\'\n'

				--Inventory Slots
				infobox = infobox .. '|-\n| Inventory Slots:\n| style=\"text-align: right;\" | ' .. itemTable.inventorySlots .. '\n'
		
				--inventoryMaxWeight
				if itemTable.inventoryMaxWeight ~= nil then
					maxWeightKg = itemTable.inventoryMaxWeight/1000
					infobox = infobox .. '|-\n| Inventory Max Weight:\n| style=\"text-align: right;\" | ' .. maxWeightKg .. ' Kg\n'
				else
					infobox = infobox .. '|-\n| Inventory Max Weight:\n| style=\"text-align: right;\" | Unlimited \n'
				end
			end	

		-- 'Power' section (if EngeryType is ``not nil)
			if itemTable.energyType ~= nil then  
				-- Power Header
				infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'Power\'\'\'\n'

				--EngergyType 
				infobox = infobox .. '|-\n| Energy Type:\n| style=\"text-align: right;\" | ' .. itemTable.energyType .. '\n'
		
				--Grid Radius
				infobox = infobox .. '|-\n| Grid Radius:\n| style=\"text-align: right;\" | ' .. itemTable.gridRadius .. ' m\n'
		
				--Energy Produced
				infobox = infobox .. '|-\n| Energy Produced:\n| style=\"text-align: right;\" | ' .. itemTable.energyProduced .. ' J\n'
		
				--Energy Used
				infobox = infobox .. '|-\n| Energy Used:\n| style=\"text-align: right;\" | ' .. itemTable.energyUsed .. ' J\n'	
			end	

			-- 'Fuel' Section (if fuelsUsed by Object)
			if itemTable.fuelsUsed ~= nil then
				-- Fuel Header
				infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'Fuel\'\'\'\n'
	
				--Fuels Used by Object
				infobox = infobox .. '|-\n| Fuels Used:\n| style=\"text-align: right;\" | ' .. itemTable.fuelsUsed .. '\n'
			end
	
 -- 'Fluid' section (if fludisUsed is not nil)    
            if itemTable.fluidsUsed ~= nil then 
            -- Liquid/Gas Header
                infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'Liquid/Gas\'\'\'\n'
            
                --Input (fludisUsed) 
                infobox = infobox .. '|-\n| Input: \n| style=\"text-align: right;\" | ' 
                    for a,b in ipairs(itemTable.fluidsUsed) do
                        local acceptedType =  b[1]
                        local cRateString = string.gsub(b[2], "%s+", "")
                        local consumingRate = tonumber(cRateString)
                        infobox = infobox .. acceptedType .. ' at ' .. consumingRate .. ' L'
                        if (a ~= #itemTable.fluidsUsed) then
                            infobox = infobox .. ', '
                        end
                    end
                infobox = infobox .. '\n'
		
				--Output (liquidProduced)
				infobox = infobox .. '|-\n| Output: \n| style=\"text-align: right;\" | ' 
					for a,b in ipairs(itemTable.fluidsProduced) do
						local producedType =  b[1]
                        local pRateString = string.gsub(b[2], "%s+", "")
                        local producingRate = tonumber(pRateString)
						if (producingRate == 0) then
								producingRate = 'Rate of Input'
						end
						infobox = infobox .. producedType .. ' at ' .. producingRate .. ' L'
						if (a ~= #itemTable.fluidsProduced) then
							infobox = infobox .. ', '
						end
					end
				infobox = infobox .. '\n'
			end
		end
	
	-- Road Object header (if group == Road Items)
		if itemTable.group == 'Road Items' then
		infobox = infobox .. '|- style=\"text-align: center; background-color: #517ab2;\"\n| colspan=\"2\" | \'\'\'Road Object\'\'\'\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
			infobox = infobox .. '|-\n| colspan=\"2\" style=\"padding: 10px;\" | [[File:' .. image .. '|center|border|240px]]\n'
		end
	
	infobox = infobox .. '|}'
	return infobox
	end
	

-- Main entry point for the Module
function p.ItemMain()
    -- get args from the Template
    local args = norm()
    
    -- get item data
    local itemData = require( "Module:ItemData" )
    return itemBox( args, itemData )
end

return p