Module:Craft Usage: Difference between revisions

No edit summary
mNo edit summary
 
Line 1: Line 1:
local p = {}
local p = {}
local recipeParams = {
local recipeParams = {
   station = true,
   station = 'station',
   time = true,
   time = 'time',
   output = true,
   output = 'output',
   yield = true,
   yield = 'yield',
}
}


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',
  cookingTitle = 'Cooking',
incineratorInputText = 'The item can be used in the following incinerator recipes',
  farmingTitle = 'Farming',
incineratorOutputText = 'The item can be created in the following incinerator recipes',
  waterTitle = 'Water Source',
cookingTitle = 'Cooking',
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',
}
}


local function split(str, delimiter)
function split(str, delimiter)
     local result = {}
     local returnTable = {}
    if not str then return result end
     for k, v in string.gmatch(str, "([^" .. delimiter .. "]+)") 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
args.item = mw.title.getCurrentTitle().text
end
 
local title = mw.title.new('Recipes')
local content = title:getContent()
 
if not content then
return '<div class="error">Strona Recipes nie istnieje.</div>'
end
 
local recipeStringResult = ''
for recipe in content:gmatch('{{Recipe|[^}]*}}') do
recipeStringResult = recipeStringResult..recipe
end
 
local recipes = {
craftRecipes = {},
incineratorRecipes = {},
cookingRecipes = {},
farmingRecipes = {},
waterRecipes = {}
}
 
for recipeKey, recipe in pairs(split(recipeStringResult, '}}{{')) do
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
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
 
if recipeTable.station == 'Incinerator' then
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
table.insert(recipes[typeKey], recipeTable)
end
end
 
local wrapper = mw.html.create('div')


    local item = args.item or mw.title.getCurrentTitle().text
local function buildTable(wrapper, titleText, labelText, recipeList, showStation, showTime)
    local mode = args.mode or 'input'
if not recipeList[1] then return end


    local title = mw.title.new('Recipes')
local section = wrapper:tag('div')
    local content = title and title:getContent() or ''
section:tag('h3'):tag('span'):addClass('mw-headline'):wikitext(titleText)
section:tag('p'):wikitext(labelText)


    local wrapper = mw.html.create('div')
-- używamy wikitable zamiast fandom-table
local _table = section:tag('table'):addClass('wikitable sortable')
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Ingredients')
if showStation then tr:tag('th'):wikitext('Station') end
if showTime then tr:tag('th'):wikitext('Time') end
tr:tag('th'):wikitext('Output')


    local function processRecipes(typeName, sectionTitle)
for _, recipe in pairs(recipeList) do
        local block = wrapper:tag('div')
if recipe.output then
local row = _table:tag('tr')
row:tag('td'):wikitext(recipe.inputText or '-')
if showStation then row:tag('td'):wikitext(recipe.station and '[['..recipe.station..']]' or '-') end
if showTime then row:tag('td'):wikitext(recipe.time or '-') end
row:tag('td'):wikitext('[['..recipe.output..']]'..' x '..(recipe.yield or 1))
end
end
end


        block:tag('h3'):wikitext(sectionTitle)
local mode = args.mode or 'output'


        local tableHtml = block:tag('table')
buildTable(wrapper, labels.craftTitle,
mode == 'input' and labels.craftInputText or labels.craftOutputText,
recipes.craftRecipes, true, true)


        tableHtml:tag('tr')
buildTable(wrapper, labels.incineratorTitle,
            :tag('th'):wikitext('Ingredients'):done()
mode == 'input' and labels.incineratorInputText or labels.incineratorOutputText,
            :tag('th'):wikitext('Output')
recipes.incineratorRecipes, false, false)


        for recipe in content:gmatch('{{Recipe|[^}]*}}') do
buildTable(wrapper, labels.cookingTitle,
            if recipe:find(item) then
mode == 'input' and labels.cookingInputText or labels.cookingOutputText,
                local ingredients = recipe:match('input=(.-)|') or ' - '
recipes.cookingRecipes, false, false)
                local output = recipe:match('output=(.-)|') or ' - '


                local tr = tableHtml:tag('tr')
buildTable(wrapper, labels.farmingTitle,
                tr:tag('td'):wikitext(ingredients)
mode == 'input' and labels.farmingInputText or labels.farmingOutputText,
                tr:tag('td'):wikitext(output)
recipes.farmingRecipes, false, false)
            end
        end
    end


    processRecipes('craft', labels.craftTitle)
buildTable(wrapper, labels.waterTitle,
mode == 'input' and labels.waterInputText or labels.waterOutputText,
recipes.waterRecipes, false, false)


    return wrapper
return wrapper
end
end


return p
return p