Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Recipe

From The Petit Planet Wiki
Revision as of 23:41, 13 November 2025 by Eleiyas (talk | contribs) (Replacing with my custom version from ZZZ, because it's better here.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}
local Card = require('Module:Card')
local category = mw.html.create()

--Arguments for template
local param = {
  sCharArg = 'character',
  sTypeArg = 'type',
  sSortArg = 'sort',
  sSortDelim = ';', --Needs to be pattern
  sSortDelim2 = '¤',
  sTimeArg = 'time',
  sYieldArg = 'yield',
  sPage = 'page'
}
p.param = param

function padLeft(str, amt)
	return string.rep('0', amt - str:len()) .. str
end

function addRecipe(sAmt, sName)
  local cardNode = Card._main({
    name = sName,
    text = sAmt,
    show_caption = '1',
    mobile = 'bulleted'
  })
  category:wikitext('[[Category:Uses ', sName, '|', padLeft(sAmt:gsub('%D', ''), 10), ']]')
  return cardNode
end

function pluralize(sOut,plural)
  if plural then
    sOut = sOut .. 's'
  end
  return sOut
end

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

function p._main(args,frame)
  local output = mw.html.create('div')
    :addClass('recipe_container')
  if type(args[param.sTypeArg]) ~= 'string' then
    output:tag('span'):addClass('error'):wikitext('Recipe type error. Must include recipe type.')
  else
    local tArgsProcessed = {}
    local header = output:tag('div'):addClass('recipe_header')

    --Type Parse
    local sType = args[param.sTypeArg]
    header:tag('span'):addClass('recipe_header_main')
      -- Mobile Icon
      :tag('span'):addClass('mobileHide'):wikitext('<big>' .. sType .. '</big>'):done()
      -- Desktop Icon
      :tag('span'):addClass('mobileHide'):wikitext('<big>' .. sType .. '</big>'):done()
      -- Text
      :tag('span'):addClass('recipe_header_text'):wikitext('[[' ..  sType .. ']]'):done()
    category:wikitext('[[Category:' .. sType .. ']]')

    --Time Parse
    if type(args[param.sTimeArg]) == 'string' then
      local tTime = {}
      local key = false
      local plural = false
      for n1, d, n2, h, n3, m, n4, s in mw.ustring.gmatch(args[param.sTimeArg], '(%d+)(d?)(%d*)(h?)(%d*)(m?)(%d*)(s?)') do
          table.insert(tTime,n1) --insert first value definitely
          if n1 ~= '1' then
              plural = true --generate plural
          end
          if d == 'd' then
              table.insert(tTime, pluralize('day', plural))
              plural = false --consume plural
              key = true
          end
          if n2 ~= '' then
              table.insert(tTime, n2)
              if n2 ~= '1' then
                  plural = true --generate plural
              end
          end
          if h == 'h' then
              table.insert(tTime,pluralize('hour', plural))
              plural = false --consume plural
              key = true
          end
          if n3 ~= '' then
              table.insert(tTime, n3)
              if n3 ~= '1' then
                  plural = true --generate plural
              end
          end
          if m == 'm' then
              table.insert(tTime, pluralize('minute', plural))
              plural = false --consume plural
              key = true
          end
          if n4 ~= '' then
              table.insert(tTime, n4)
              if n4 ~= '1' then
                  plural = true --generate plural
              end
          end
          if s == 's' then
              table.insert(tTime, pluralize('second', plural))
              key = true
          end
      end
      if not(key) then
          table.insert(tTime,pluralize('minute', plural))
      end
      local sTime = ''
      for _, each in ipairs(tTime) do
          sTime = sTime .. each .. ' '
      end
      header:tag('span'):addClass('recipe_header_sub')
        :tag('span'):addClass('mobileHide'):wikitext(' for '):done()
        :tag('span'):addClass('hidden'):wikitext(' for '):done()
        :tag('span'):addClass('recipe_header_text'):wikitext(sTime):done()
    end
    --Character Parse
    if type(args[param.sCharArg]) == 'string' then
      local sCharName = args[param.sCharArg]
      header:tag('span'):addClass('recipe_header_sub')
        :tag('span'):addClass('mobileHide'):wikitext(' with '):done()
        :tag('span'):addClass('hidden'):wikitext('[[File:' .. sCharName .. ' Icon.png|24x24px|link=' .. sCharName .. ']]'):done()
        :tag('span'):addClass('recipe_header_text'):wikitext('[[' .. sCharName .. ']]'):done()
    end

	local body = output:tag('div'):addClass('recipe_body')

	--Check if sSortDelim2 exists in sSortArg
	if ((args[param.sSortArg] or ''):find(param.sSortDelim2) ~= nil) then
		param.sSortDelim = param.sSortDelim2
	end
	
    --Process Sort Parameter (Split by Delimiter)
    for sKey in string.gmatch(args[param.sSortArg] or '', '([^' .. param.sSortDelim .. ']+)') do
      if type(args[sKey]) ~= 'nil' then
        body:node(addRecipe(args[sKey], sKey))
        tArgsProcessed[sKey] = true
      end
    end

    --Process Rest of Unrestricted Parameters
    for sKey,sVal in pairs(args) do
      --Check if restricted parameter
      local restricted = false
      for _, keyParam in pairs(param) do
        if keyParam == sKey then
          restricted = true
        end
      end
      --If not processed, and not a restricted parameter
      if tArgsProcessed[sKey] ~= true and not(restricted) then
        body:node(addRecipe(sVal, sKey))
      end
    end

    --Yield Parse
    if args[param.sYieldArg] ~= '0' then
	    local sYield = args[param.sYieldArg] or '1'
	    local pageTitle = args.page or mw.title.getCurrentTitle().text
	    local yieldCard = Card._main{
	      name = pageTitle,
	      text = sYield,
    	  show_caption = '1',
	    }
	    body
	      :tag('div'):addClass('recipe_body_icon')
	        :wikitext('➜'):done()
	      :tag('div'):addClass('recipe_body_yield')
	        :node(yieldCard)
	        
	    local itemText = require('Module:Item').main{
	    	[1] = pageTitle,
	    	[2] = '15',
	    	x = sYield
	    }
	    
    end
    body:done()
  end
  output:node(require('Module:Namespace detect').main{["main"]=category})
  return output
end

return p