×
Create a new article
Write your page title here:
We currently have 276 articles on Waste Of Space Wiki. Type your article name above or create one of the articles listed here!



    Waste Of Space Wiki

    Module:GetPartInfo: Difference between revisions

    Content added Content deleted
    No edit summary
    Tag: Reverted
    m (Fixed an annoying bug that caused poor formatting on the configuration section of the part template)
     
    (108 intermediate revisions by 4 users not shown)
    Line 1: Line 1:
    local module = {}
    local module = {}


    --mw.loadData has performance benefits over require as it loads the module onnce per page, not of per invocation.
    local Parts = require("Module:PartJSON")
    local Parts = mw.loadData("Module:PartJSON")


    local function getRawMaterials(item)
    local function getRawMaterials(item)
    Line 63: Line 64:
    end
    end


    recipeString = recipeString .. "[[" .. itemName .. "|" .. itemName .. " x" .. amount .. "]]" .. endString
    recipeString = recipeString .. "[[" .. itemName .. "|" .. itemName .. " " .. amount .. "x]]" .. endString
    end
    end


    Line 77: Line 78:
    local field = args[1]
    local field = args[1]


    if field == "Description" then
    if Parts[itemName] then
    local part = Parts[itemName]
    if field == "Description" then
    if not part then
    local part = Parts[itemName]
    return "No description found"
    local description = part.Description
    end
    local description = part.Description
    if description then
    local descriptionSplit = mw.text.split(description, "\n")
    local descriptionFormatted = "''\""
    for i, str in pairs(descriptionSplit) do
    if i > 1 and #str > 3 then descriptionFormatted = descriptionFormatted .. "''\n\n''" end
    if #str > 3 then
    descriptionFormatted = descriptionFormatted .. string.gsub(str, "%*%*", "'''") end
    end
    return descriptionFormatted .. "\" ''"
    else
    return "No description found"
    end
    elseif field == "Malleability" then
    local malleability = Parts[itemName]["Malleability"]
    if malleability == 0 or not malleability then
    return "Fixed size"
    else
    return malleability
    end
    elseif field == "Raw recipe" then
    local item = Parts[itemName]
    local rawRecipe = getRawMaterials(item)
    module.rr = rawRecipe
    module.r = item.Recipe
    local recipeFormatted = getRawMaterials(rawRecipe)
    if formatRecipe(rawRecipe) == formatRecipe(formatNormalRecipe(item.Recipe)) then
    return nil
    end
    return formatRecipe(rawRecipe)
    elseif field == "Recipe" then
    local item = Parts[itemName]
    local recipe = item.Recipe
    module.r = recipe
    return formatRecipe(formatNormalRecipe(recipe))
    elseif field == "Durability" then
    return Parts[itemName]["Durability"] or 3
    elseif field == "Configuration" then
    local configData = Parts[itemName]["ConfigData"]
    if not configData then return end
    local formatted = ""
    for _, entry in pairs(configData) do
    local entryName = entry[1]
    local entryType = entry[2]
    local default = entry[3]
    local range = entry[4]
    local description = entry[5]


    if type(default) == "table" then
    if description then
    default = {entry[3][1], entry[3][2]}
    local descriptionSplit = mw.text.split(description, "\n")
    local descriptionFormatted = "''\""


    for i, str in pairs(descriptionSplit) do
    if default[1] == nil then
    if i > 1 and #str > 3 then descriptionFormatted = descriptionFormatted .. "''\n\n''" end
    default[1] = "inf"
    if #str > 3 then
    elseif default[2] == nil then
    descriptionFormatted = descriptionFormatted .. string.gsub(str, "%*%*", "'''") end
    default[2] = "inf"
    end
    end
    if entryType == "number" then
    formatted = formatted .. " \n* "..entryName.."<sup>(number)[" .. range[1] .. " - " .. range[2] .. "]</sup>"
    elseif entryType == "boolean" then
    formatted = formatted .. " \n* "..entryName.."<sup>(boolean)</sup> "
    elseif entryType == "string" then
    formatted = formatted .. " \n* "..entryName.."<sup>(string)</sup> "
    elseif entryType == "Selection" then
    formatted = formatted .. " \n* "..entryName.."<sup>(selection)</sup> "
    elseif entryType == "Coordinate" then
    formatted = formatted .. " \n* "..entryName.."<sup>(coordinate)</sup> "
    elseif entryType == "NumberRange" then
    formatted = formatted .. " \n* "..entryName.."<sup>(numberRange)["..default[1].." - "..default[2] .. "]</sup>"
    end
    end
    end
    return formatted
    elseif field == "ConfigurationLarge" then
    local configData = Parts[itemName]["ConfigData"]
    if not configData then return "* Cannot be configured." end
    local formatted = ""
    for _, entry in pairs(configData) do
    local entryName = entry[1]
    local entryType = entry[2]
    local default = entry[3]
    local range = entry[4]
    local description = entry[5]
    if type(default) == "table" then
    default = {entry[3][1], entry[3][2]}


    return descriptionFormatted .. "\" ''"
    if default[1] == nil then
    else
    default[1] = "inf"
    return "No description found"
    elseif default[2] == nil then
    end
    default[2] = "inf"
    end
    elseif field == "Malleability" then
    end
    local malleability = Parts[itemName]["Malleability"]
    if entryType == "number" then
    formatted = formatted .. " \n* "..entryName.."<sup>(number) ["..range[1].." - "..range[2].."]</sup> - "..description
    elseif entryType == "boolean" then
    formatted = formatted .. " \n* "..entryName.."<sup>(boolean)</sup> - "..description
    elseif entryType == "string" then
    formatted = formatted .. " \n* "..entryName.."<sup>(string)</sup> - "..description
    elseif entryType == "Selection" then
    formatted = formatted .. " \n* "..entryName.."<sup>(selection)</sup> - "..description
    elseif entryType == "Coordinate" then
    formatted = formatted .. " \n* "..entryName.."<sup>(coordinate)</sup> - "..description
    elseif entryType == "NumberRange" then
    formatted = formatted .. " \n* "..entryName.."<sup>(numberRange) ["..default[1].." - "..default[2].."]</sup> - "..description
    end
    end
    return string.sub(formatted, 2)
    elseif field == "Type" then
    local categories = Parts[itemName]["Categories"]
    local formatted = ""
    local invisEnd = " "
    for i, type in pairs(categories) do
    if i > 1 then
    formatted = formatted .. "\n\n"
    end
    invisEnd = invisEnd .. " [[Category:" .. type .. "]]"
    formatted = formatted .. "[[:Category:" .. type .. "|" .. type .. "]]"
    end
    return formatted .. invisEnd
    elseif field == "Flammable" then
    if Parts[itemName]["Flammable"] == "true" then
    return "Yes"
    else
    return "No"
    end
    elseif field == "DefaultColor" or field == "DefaultSize" then
    local conn = ""
    local ccat = ""
    local rgbtable = {}
    if field == "DefaultColor" then conn = ", " else conn = " x " end


    for i, v in pairs(Parts[itemName][field]) do
    if malleability == 0 or not malleability then
    return "Fixed size"
    ccat = ccat .. v
    if i ~= 3 then ccat = ccat .. conn end
    else
    return malleability
    rgbtable[i] = v
    end
    elseif field == "Raw recipe" then
    local item = Parts[itemName]

    if not item then
    return nil
    end

    local rawRecipe = getRawMaterials(item)

    module.rr = rawRecipe
    module.r = item.Recipe

    local recipeFormatted = getRawMaterials(rawRecipe)

    if formatRecipe(rawRecipe) == formatRecipe(formatNormalRecipe(item.Recipe)) then
    return nil
    end

    return formatRecipe(rawRecipe)
    elseif field == "Recipe" then
    local item = Parts[itemName]

    if not item then
    return "No recipe found"
    end

    local recipe = item.Recipe

    module.r = recipe

    return formatRecipe(formatNormalRecipe(recipe))
    elseif field == "Durability" then
    return Parts[itemName]["Durability"] or 3
    elseif field == "Configuration" then
    local configData = Parts[itemName]["ConfigData"]

    local formatted = ""

    for _, entry in pairs(configData) do
    local entryName = entry[1]
    local entryType = entry[2]
    local default = entry[3]
    local range = entry[4]
    local description = entry[5]



    if entryType == "number" then
    formatted = formatted .. "\n * "..entryName.."<sup>(number)[" .. range[1] .. "-" .. range[2] .. "] [default '"..default.."']</sup>"
    elseif entryType == "boolean" then
    formatted = formatted .. "\n * "..entryName.."<sup>(boolean)</sup>"
    elseif entryType == "string" then
    formatted = formatted .. "\n * "..entryName.."<sup>(string)</sup>"
    elseif entryType == "selectable" then
    formatted = formatted .. "\n * "..entryName.."<sup>(selectable)</sup>"
    end
    end
    end


    if field == "DefaultColor" then
    return formatted
    local rgb = (rgbtable[1] * 0x10000) + (rgbtable[2] * 0x100) + rgbtable[3]
    elseif field == "ConfigurationLarge" then
    local configData = Parts[itemName]["ConfigData"]
    local webcolor = string.format("%x", rgb)


    while string.len(webcolor) < 6 do
    local formatted = ""
    webcolor = "0" .. webcolor
    end


    webcolor = "#" .. webcolor
    for _, entry in pairs(configData) do
    local entryName = entry[1]
    local entryType = entry[2]
    local default = entry[3]
    local range = entry[4]
    local description = entry[5]


    if rgbtable[1] > 76.5 or rgbtable[2] > 76.5 or rgbtable[3] > 76.5 then

    ccat = ccat .. '<big><span style="color:' .. webcolor .. '">   ⬢</span></big>'

    if entryType == "number" then
    else
    ccat = ccat .. '<big><span style="color:' .. webcolor .. '; text-shadow: 0px 0px 2px DimGray">   ⬢</span></big>'
    formatted = formatted .. "\n* "..entryName.."<sup>(number) (range "..range[1].."-"..range[2]..")</sup> - "..description
    elseif entryType == "boolean" then
    end
    formatted = formatted .. "\n* "..entryName.."<sup>(boolean)</sup> - "..description
    elseif entryType == "string" then
    formatted = formatted .. "\n* "..entryName.."<sup>(string)</sup> - "..description
    elseif entryType == "selectable" then
    formatted = formatted .. "\n* "..entryName.."<sup>(selectable)</sup> - "..description
    end
    end
    end


    return ccat
    return string.sub(formatted, 2)
    elseif field == "Type" then
    local categories = Parts[itemName]["Categories"]
    local formatted = ""


    elseif Parts[itemName][field] then
    for i, type in pairs(categories) do
    if i > 1 then
    return (tostring(Parts[itemName][field]))
    else
    formatted = formatted .. "\n"
    error("unknown field given: " .. tostring(args[1]))
    end
    formatted = formatted .. type--"[[Category:" .. type .. "|" .. type .. "]]"
    end
    end
    return formatted
    elseif Parts[itemName][field] then
    return tostring(Parts[itemName][field])
    else
    else
    mw.addWarning("Sorry, part not found. Please fill in the fields manually.")
    error("unknown field given: " .. tostring(args[1]))
    return "N/A"
    end
    end
    end
    end

    Latest revision as of 11:04, 12 May 2024

    Documentation

    GetPartInfo can be invoked using

    {{#invoke:GetPartInfo|getField|FIELD}}

    Replace "FIELD" with the wanted field, like:

    {{#invoke:GetPartInfo|getField|Recipe}}


    Additionally, you may add a second parameter to specify which part's information you want to retrieve, if this is not provided it will guess that the wanted part has the same name as the page's title. Usually, this will only need to be used if the page requests information about a part that the page isn't named after. An example of this is:

    {{#invoke:GetPartInfo|getField|Recipe|WirelessButton}}

    Fields

    A list of all fields retrievable in this module can be found here:

    • Recipe
    • Raw recipe
    • Malleability
    • Description

    Use-case Examples:

    These are some examples of the module being used in which all of them specify information of the "HyperDrive" part to be retrieved:

    In-game Description

    "A device used to travel at faster than light speeds to other regions of the universe. Simply input the coordinates to your destination and have a sufficient amount of fuel, and you'll be on your way to your destination. To initiate warping, power the hyperdrive and trigger it. When warping, the hyperdrive will be anchored to prevent any movement. The power required for warping will be dependent on the distance travelled, as well as the size of the object being transported. The amount of power required will be displayed in the hyper drive, shown in the red text. All of the parts connected to the hyperdrive and every player sitting will be teleported, however any free floating parts or standing up players will be sucked into the vaccum of space, so be careful. The red text at the bottom of the hyperdrive indicates how much power is needed to perform the jump. Make sure the numbers on the hyperdrive is facing up to ensure correct stud alignment. "

    Recipe

    Wire 50x

    IonRocket 3x

    Iron 100x


    Raw recipe

    Sulfur 15x

    Copper 110x

    Iron 310x


    Malleability

    Fixed size


    local module = {}
    
    --mw.loadData has performance benefits over require as it loads the module onnce per page, not of per invocation.
    local Parts = mw.loadData("Module:PartJSON")
    
    local function getRawMaterials(item)
        local Materials = {}
    
        local function get(item, amount)
            for _, itemEntry in pairs(item.Recipe) do
                local itemName = itemEntry[1]
                local itemAmount = itemEntry[2]
    
                if not Parts[itemName] or not Parts[itemName]["Recipe"] then
                    if not Materials[itemName] then
                        Materials[itemName] = 0
                    end
                    Materials[itemName] = Materials[itemName] + (amount * itemAmount)
                else
                    get(Parts[itemName], amount * itemAmount)
                end
            end
        end
    
        if item and item.Recipe then
            get(item, 1)
        else
            return false
        end
    
        return Materials
    end
    
    local function formatNormalRecipe(recipe)
        local formattedRecipe = {}
    
        if recipe then
            for _, itemEntry in pairs(recipe) do
                local itemName = itemEntry[1]
                local itemAmount = itemEntry[2]
                
                formattedRecipe[itemName] = itemAmount
            end
    
            return formattedRecipe
        end
    end
    
    local function formatRecipe(recipe)
        if recipe then
            local recipeString = ""
    
            local i = 0
    
            for itemName, amount in pairs(recipe) do
                local endString = ""
    
                i = i + 1
    
                if not i == #recipe then
                    endString = ""
                else
                    endString = "\n\n"
                end
    
                recipeString = recipeString .. "[[" .. itemName .. "|" .. itemName .. " " .. amount .. "x]]" .. endString
            end
    
            return recipeString
        else
            return "No crafting recipe"
        end
    end
    
    function module.getField(page)
        local args = page.args
        local itemName = args[2] or page:getParent():getTitle()
        local field = args[1]
    
        if Parts[itemName] then
            if field == "Description" then
                local part = Parts[itemName]
                local description = part.Description
        
                if description then
                    local descriptionSplit = mw.text.split(description, "\n")
                    local descriptionFormatted = "''\""
        
                    for i, str in pairs(descriptionSplit) do
                        if i > 1 and #str > 3 then descriptionFormatted = descriptionFormatted .. "''\n\n''" end
                        if #str > 3 then 
                            descriptionFormatted = descriptionFormatted .. string.gsub(str, "%*%*", "'''") end
                    end
        
                    return descriptionFormatted .. "\" ''"
                else
                    return "No description found"
                end
            elseif field == "Malleability" then
                local malleability = Parts[itemName]["Malleability"]
        
                if malleability == 0 or not malleability then
                    return "Fixed size"
                else
                    return malleability
                end
            elseif field == "Raw recipe" then
                local item = Parts[itemName]
                local rawRecipe = getRawMaterials(item)
        
                module.rr = rawRecipe
                module.r = item.Recipe
        
                local recipeFormatted = getRawMaterials(rawRecipe)
        
                if formatRecipe(rawRecipe) == formatRecipe(formatNormalRecipe(item.Recipe)) then
                    return nil
                end
        
                return formatRecipe(rawRecipe)
            elseif field == "Recipe" then
                local item = Parts[itemName]
                local recipe = item.Recipe
        
                module.r = recipe
        
                return formatRecipe(formatNormalRecipe(recipe))
            elseif field == "Durability" then
                return Parts[itemName]["Durability"] or 3
            elseif field == "Configuration" then
                local configData = Parts[itemName]["ConfigData"]
        
                if not configData then return end
        
                local formatted = ""
        
                for _, entry in pairs(configData) do
                    local entryName = entry[1]
                    local entryType = entry[2]
                    local default = entry[3]
                    local range = entry[4]
                    local description = entry[5]
    
    		if type(default) == "table" then
    			default = {entry[3][1], entry[3][2]}
    
    	                if default[1] == nil then
    	                    default[1] = "inf"
    	                elseif default[2] == nil then
    	                    default[2] = "inf"
    	                end
    		end
        
                    if entryType == "number" then                
                        formatted = formatted .. " \n* "..entryName.."<sup>(number)[" .. range[1] .. " - " .. range[2] .. "]</sup>"
                    elseif entryType == "boolean" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(boolean)</sup> "
                    elseif entryType == "string" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(string)</sup> "
                    elseif entryType == "Selection" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(selection)</sup> "
                    elseif entryType == "Coordinate" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(coordinate)</sup> "
                    elseif entryType == "NumberRange" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(numberRange)["..default[1].." - "..default[2] .. "]</sup>"
                    end
                end
        
                return formatted
           elseif field == "ConfigurationLarge" then
                local configData = Parts[itemName]["ConfigData"]
        
                if not configData then return "* Cannot be configured." end
        
                local formatted = ""
        
                for _, entry in pairs(configData) do
                    local entryName = entry[1]
                    local entryType = entry[2]
                    local default = entry[3]
                    local range = entry[4]
                    local description = entry[5]
        
    		if type(default) == "table" then
    			default = {entry[3][1], entry[3][2]}
    
    	                if default[1] == nil then
    	                    default[1] = "inf"
    	                elseif default[2] == nil then
    	                    default[2] = "inf"
    	                end
    		end
        
                    if entryType == "number" then                
                        formatted = formatted .. " \n* "..entryName.."<sup>(number) ["..range[1].." - "..range[2].."]</sup> - "..description
                    elseif entryType == "boolean" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(boolean)</sup> - "..description
                    elseif entryType == "string" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(string)</sup> - "..description
                    elseif entryType == "Selection" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(selection)</sup> - "..description
                    elseif entryType == "Coordinate" then
                        formatted = formatted .. " \n* "..entryName.."<sup>(coordinate)</sup> - "..description
    		elseif entryType == "NumberRange" then
    		    formatted = formatted .. " \n* "..entryName.."<sup>(numberRange) ["..default[1].." - "..default[2].."]</sup> - "..description
                    end
                end
        
                return string.sub(formatted, 2)
            elseif field == "Type" then
                local categories = Parts[itemName]["Categories"]
                local formatted = ""
                local invisEnd = " "
        
                for i, type in pairs(categories) do
                    if i > 1 then
                        formatted = formatted .. "\n\n"
                    end
        
                    invisEnd = invisEnd .. " [[Category:" .. type .. "]]" 
                    formatted = formatted .. "[[:Category:" .. type .. "|" .. type .. "]]"
                end
                
                return formatted .. invisEnd
            elseif field == "Flammable" then
                if Parts[itemName]["Flammable"] == "true" then
                    return "Yes"
                else
                    return "No"
                end
            elseif field == "DefaultColor" or field == "DefaultSize" then
                local conn = ""
                local ccat = ""
                local rgbtable = {}
                if field == "DefaultColor" then conn = ", " else conn = " x " end
    
                for i, v in pairs(Parts[itemName][field]) do
                    ccat = ccat .. v
                    if i ~= 3 then ccat = ccat .. conn end
                    rgbtable[i] = v
                end
    
                if field == "DefaultColor" then
                    local rgb = (rgbtable[1] * 0x10000) + (rgbtable[2] * 0x100) + rgbtable[3]
                    local webcolor = string.format("%x", rgb)
    
                    while string.len(webcolor) < 6 do
                        webcolor = "0" .. webcolor
                    end
    
                    webcolor = "#" .. webcolor
    
                    if rgbtable[1] > 76.5 or rgbtable[2] > 76.5 or rgbtable[3] > 76.5 then
                        ccat = ccat .. '<big><span style="color:' .. webcolor .. '">   ⬢</span></big>'
                    else
                        ccat = ccat .. '<big><span style="color:' .. webcolor .. '; text-shadow: 0px 0px 2px DimGray">   ⬢</span></big>'                    
                    end
                end
    
                return ccat
    
            elseif Parts[itemName][field] then
                return (tostring(Parts[itemName][field]))
            else
                error("unknown field given: " .. tostring(args[1]))
            end
        else
            mw.addWarning("Sorry, part not found. Please fill in the fields manually.")
            return "N/A"
        end
    end
    
    return module
    
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.

    Recent changes

  • 116.111.185.163 • 2 days ago
  • 116.111.185.163 • 2 days ago
  • 116.111.185.163 • 2 days ago
  • 116.111.185.163 • 2 days ago
  • Cookies help us deliver our services. By using our services, you agree to our use of cookies.