Jump to content

Module:Craft Usage: Difference between revisions

From MISERY wiki
Created page with "local p = {} local recipeParams = { station = 'station', time = 'time', output = 'output', yield = 'yield', } local labels = { craftTitle = 'Crafting', craftInputText = 'The item can be used in the following craft recipes', craftOutputText = 'The item can be created in the following craft recipes', incineratorTitle = 'Incinerator', incineratorInputText = 'The item can be used in the following incinerator recipes', incineratorOutputText = 'The item can be c..."
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local recipeParams = {
local recipeParams = {
   station = 'station',
   station = true,
   time = 'time',
   time = true,
   output = 'output',
   output = true,
   yield = 'yield',
   yield = true,
}
}


local labels = {
local labels = {
craftTitle = 'Crafting',
  craftTitle = 'Crafting',
craftInputText = 'The item can be used in the following craft recipes',
  craftInputText = 'The item can be used in the following craft recipes',
craftOutputText = 'The item can be created in the following craft recipes',
  craftOutputText = 'The item can be created in the following craft recipes',
incineratorTitle = 'Incinerator',
  incineratorTitle = 'Incinerator',
incineratorInputText = 'The item can be used in the following incinerator recipes',
  cookingTitle = 'Cooking',
incineratorOutputText = 'The item can be created in the following incinerator recipes',
  farmingTitle = 'Farming',
cookingTitle = 'Cooking',
  waterTitle = 'Water Source',
cookingInputText = 'The item can be used in the following cooking recipes',
cookingOutputText = 'The item can be created in the following cooking recipes',
farmingTitle = 'Farming',
farmingInputText = 'The item can be used to farm the following items',
farmingOutputText = 'The item can be grown from the following items',
waterTitle = 'Water Source',
waterInputText = 'The item can be used at a water source to gain the following items',
waterOutputText = 'The item can be gained at a water source by using the following items the following items',
}
}


function split(str, delimiter)
local function split(str, delimiter)
     local returnTable = {}
     local result = {}
     for k, v in string.gmatch(str, "([^" .. delimiter .. "]+)")  
    if not str then return result end
    do
 
         returnTable[#returnTable+1] = k
     for part in string.gmatch(str, "([^" .. delimiter .. "]+)") do
         table.insert(result, part)
     end
     end
     return returnTable
 
     return result
end
end


function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
    local args = require('Module:Arguments').getArgs(frame)
parentFirst = true,
wrapper = { 'Template:Craft_Usage' }
})


if not args.item then
    local item = args.item or mw.title.getCurrentTitle().text
args.item = mw.title.getCurrentTitle().text
    local mode = args.mode or 'input'
end


local title = mw.title.new('Recipes')
    local title = mw.title.new('Recipes')
local content = title:getContent()
    local content = title and title:getContent() or ''


local recipeStringResult = ''
    local wrapper = mw.html.create('div')
for recipe in content:gmatch('{{Recipe|[^}]*}}') do
recipeStringResult = recipeStringResult..recipe
end


local recipes = {
    local function processRecipes(typeName, sectionTitle)
craftRecipes = {},
        local block = wrapper:tag('div')
incineratorRecipes = {},
cookingRecipes = {},
farmingRecipes = {},
waterRecipes = {}
}


for recipeKey, recipe in pairs(split(recipeStringResult, '}}{{')) do
        block:tag('h3'):wikitext(sectionTitle)
local typeKey = 'craftRecipes'
local recipeTable = {}
local inputText = ''
local inputs = {}
local outputs = {}
for propertyStringKey, propertyString in pairs(split(recipe, '|')) do
if not (propertyString  == 'Recipe') then
local propertySplitTable = split(propertyString, '=')


if not recipeParams[propertySplitTable[1]] then
        local tableHtml = block:tag('table')
inputText = inputText..'[['..propertySplitTable[1]..']]'..' x '..propertySplitTable[2]..'</br>'
inputs[propertySplitTable[1]] = true
else
recipeTable[propertySplitTable[1]] = propertySplitTable[2]
if propertySplitTable[1] == 'output' then
outputs[propertySplitTable[2]] = true
elseif propertySplitTable[1] == 'station' then
inputs[propertySplitTable[2]] = true
end
end
end
end


recipeTable['inputText'] = inputText
        tableHtml:tag('tr')
            :tag('th'):wikitext('Ingredients'):done()
if recipeTable.station == 'Incinerator' then
            :tag('th'):wikitext('Output')
typeKey = 'incineratorRecipes'
elseif recipeTable.station == 'Cooking' then
typeKey = 'cookingRecipes'
elseif recipeTable.station == 'Farming' then
typeKey = 'farmingRecipes'
elseif recipeTable.station == 'Water Source' then
typeKey = 'waterRecipes'
end


if (args.mode == 'input' and inputs[args.item]) or (args.mode == 'output' and outputs[args.item]) then
        for recipe in content:gmatch('{{Recipe|[^}]*}}') do
table.insert(recipes[typeKey], recipeTable)
            if recipe:find(item) then
end
                local ingredients = recipe:match('input=(.-)|') or ' - '
end
                local output = recipe:match('output=(.-)|') or ' - '
local wrapper = mw.html.create('div')
if recipes.craftRecipes[1] then
local craftWrapper = wrapper:tag('div')


local craftTitleWrapper = craftWrapper:tag('h3')
                local tr = tableHtml:tag('tr')
local craftTitle = craftTitleWrapper:tag('span'):addClass('mw-headline'):wikitext(labels.craftTitle)
                tr:tag('td'):wikitext(ingredients)
                tr:tag('td'):wikitext(output)
local craftTextLabel = ''
            end
if args.mode == 'input' then
        end
craftTextLabel = labels.craftInputText
    end
elseif args.mode == 'output' then
craftTextLabel = labels.craftOutputText
end
local craftText = craftWrapper:tag('p'):wikitext(craftTextLabel)
local _table = craftWrapper:tag('table'):addClass('fandom-table article-table sortable alternating-colors-table')
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Ingredients')
tr:tag('th'):wikitext('Station')
tr:tag('th'):wikitext('Time')
tr:tag('th'):wikitext('Output')
for recipeKey, recipe in pairs(recipes.craftRecipes) do
if recipe.output then
local tr = _table:tag('tr')
tr:tag('td'):wikitext(recipe.inputText or " - ")
tr:tag('td'):wikitext(recipe.station and '[['..recipe.station..']]' or " - ")
tr:tag('td'):wikitext(recipe.time or " - ")
tr:tag('td'):wikitext('[['..recipe.output..']]'.." x "..(recipe.yield or 1))
end
end
end
 
if recipes.incineratorRecipes[1] then
local incineratorWrapper = wrapper:tag('div')
 
local incineratorTitleWrapper = incineratorWrapper:tag('h3')
local incineratorTitle = incineratorTitleWrapper:tag('span'):addClass('mw-headline'):wikitext(labels.incineratorTitle)
local incineratorTextLabel = ''
if args.mode == 'input' then
incineratorTextLabel = labels.incineratorInputText
elseif args.mode == 'output' then
incineratorTextLabel = labels.incineratorOutputText
end
local incineratorText = incineratorWrapper:tag('p'):wikitext(incineratorTextLabel)
local _table = incineratorWrapper:tag('table'):addClass('fandom-table article-table sortable alternating-colors-table')
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Ingredients')
tr:tag('th'):wikitext('Output')
for recipeKey, recipe in pairs(recipes.incineratorRecipes) do
if recipe.output then
local tr = _table:tag('tr')
tr:tag('td'):wikitext(recipe.inputText or " - ")
tr:tag('td'):wikitext('[['..recipe.output..']]'.." x "..(recipe.yield or 1))
end
end
end
 
if recipes.cookingRecipes[1] then
local cookingWrapper = wrapper:tag('div')
 
local cookingTitleWrapper = cookingWrapper:tag('h3')
local cookingTitle = cookingTitleWrapper:tag('span'):addClass('mw-headline'):wikitext(labels.cookingTitle)
local cookingTextLabel = ''
if args.mode == 'input' then
cookingTextLabel = labels.cookingInputText
elseif args.mode == 'output' then
cookingTextLabel = labels.cookingOutputText
end
local cookingText = cookingWrapper:tag('p'):wikitext(cookingTextLabel)
local _table = cookingWrapper:tag('table'):addClass('fandom-table article-table sortable alternating-colors-table')
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Ingredients')
tr:tag('th'):wikitext('Output')
for recipeKey, recipe in pairs(recipes.cookingRecipes) do
if recipe.output then
local tr = _table:tag('tr')
tr:tag('td'):wikitext(recipe.inputText or " - ")
tr:tag('td'):wikitext('[['..recipe.output..']]'.." x "..(recipe.yield or 1))
end
end
end
 
if recipes.farmingRecipes[1] then
local farmingWrapper = wrapper:tag('div')
 
local farmingTitleWrapper = farmingWrapper:tag('h3')
local farmingTitle = farmingTitleWrapper:tag('span'):addClass('mw-headline'):wikitext(labels.farmingTitle)
local farmingTextLabel = ''
if args.mode == 'input' then
farmingTextLabel = labels.farmingInputText
elseif args.mode == 'output' then
farmingTextLabel = labels.farmingOutputText
end
local farmingText = farmingWrapper:tag('p'):wikitext(farmingTextLabel)
local _table = farmingWrapper:tag('table'):addClass('fandom-table article-table sortable alternating-colors-table')
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Ingredients')
tr:tag('th'):wikitext('Output')
for recipeKey, recipe in pairs(recipes.farmingRecipes) do
if recipe.output then
local tr = _table:tag('tr')
tr:tag('td'):wikitext(recipe.inputText or " - ")
tr:tag('td'):wikitext('[['..recipe.output..']]'.." x "..(recipe.yield or 1))
end
end
end
 
if recipes.waterRecipes[1] then
local waterWrapper = wrapper:tag('div')
 
local waterTitleWrapper = waterWrapper:tag('h3')
local waterTitle = waterTitleWrapper:tag('span'):addClass('mw-headline'):wikitext(labels.waterTitle)
local waterTextLabel = ''
if args.mode == 'input' then
waterTextLabel = labels.waterInputText
elseif args.mode == 'output' then
waterTextLabel = labels.waterOutputText
end
local waterText = waterWrapper:tag('p'):wikitext(waterTextLabel)
local _table = waterWrapper:tag('table'):addClass('fandom-table article-table sortable alternating-colors-table')
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Ingredients')
tr:tag('th'):wikitext('Output')
for recipeKey, recipe in pairs(recipes.waterRecipes) do
if recipe.output then
local tr = _table:tag('tr')
tr:tag('td'):wikitext(recipe.inputText or " - ")
tr:tag('td'):wikitext('[['..recipe.output..']]'.." x "..(recipe.yield or 1))
end
end
end


    processRecipes('craft', labels.craftTitle)


return wrapper
    return wrapper
end
end


return p
return p

Revision as of 23:29, 28 June 2026

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

local p = {}

local recipeParams = {
  station = true,
  time = true,
  output = true,
  yield = true,
}

local labels = {
  craftTitle = 'Crafting',
  craftInputText = 'The item can be used in the following craft recipes',
  craftOutputText = 'The item can be created in the following craft recipes',
  incineratorTitle = 'Incinerator',
  cookingTitle = 'Cooking',
  farmingTitle = 'Farming',
  waterTitle = 'Water Source',
}

local function split(str, delimiter)
    local result = {}
    if not str then return result end

    for part in string.gmatch(str, "([^" .. delimiter .. "]+)") do
        table.insert(result, part)
    end

    return result
end

function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame)

    local item = args.item or mw.title.getCurrentTitle().text
    local mode = args.mode or 'input'

    local title = mw.title.new('Recipes')
    local content = title and title:getContent() or ''

    local wrapper = mw.html.create('div')

    local function processRecipes(typeName, sectionTitle)
        local block = wrapper:tag('div')

        block:tag('h3'):wikitext(sectionTitle)

        local tableHtml = block:tag('table')

        tableHtml:tag('tr')
            :tag('th'):wikitext('Ingredients'):done()
            :tag('th'):wikitext('Output')

        for recipe in content:gmatch('{{Recipe|[^}]*}}') do
            if recipe:find(item) then
                local ingredients = recipe:match('input=(.-)|') or ' - '
                local output = recipe:match('output=(.-)|') or ' - '

                local tr = tableHtml:tag('tr')
                tr:tag('td'):wikitext(ingredients)
                tr:tag('td'):wikitext(output)
            end
        end
    end

    processRecipes('craft', labels.craftTitle)

    return wrapper
end

return p