Module:Recipe

From RuneScape Classic Wiki
Jump to: navigation, search

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

--<nowiki>

local p = {}

local yesno = require('Module:Yesno')
local params = require('Module:Paramtest')
local commas = require('Module:Addcommas')
local editbutton = require('Module:Edit button')
local onmain = require('Module:Mainonly').on_main

-- Tools that need special handling
local toolsList = {
}

local facilitiesIcons = {
}

function p.main(frame)
	local args = frame:getParent().args

	local function mat_list()
		local ret_list = {}
		for i=1,10,1 do
			local mat = args['mat'..i]
			if mat and params.has_content(mat) then
				local name = mat
				local qty = params.default_to(args['mat'..i..'quantity'],'1')
				local img = params.default_to(args['mat'..i..'pic'], name..'.png')

				table.insert(ret_list, {
					name = name,
					quantity = qty,
					image = string.format('[[File:%s|link=%s]]', img, mat),
				} )
			end
		end
		return ret_list
	end

	local skills = {}
	if args.skill ~= nil then
		table.insert(skills, {name = args.skill, xp = args.skillexp or 0, lvl = args.skilllvl or 1})
		if args.skill2 ~= nil then
			table.insert(skills, {name = args.skill2, xp = args.skill2exp or 0, lvl = args.skill2lvl or 1})
		end
		if args.skill3 ~= nil then
			table.insert(skills, {name = args.skill3, xp = args.skill3exp or 0, lvl = args.skill3lvl or 1})
		end
	end

	local name = args.outputname or mw.title.getCurrentTitle().text
	local image_name = args.outputpic or (name .. '.png')
	local output = {
		name = name,
		image = string.format('[[File:%s|link=%s]]', image_name, name),
		quantity = args.outputquantity or 1,
	}

	local materials = mat_list()

	local members = ''
	if params.has_content(args.members) then
		members = yesno(args.members, true)
		if members then
			members = 'Yes'
		else
			members = 'No'
		end
	end

	return p._main(frame, args, args.tools, skills, members, args.quest, materials, output, args.facilities, args.ticks, args.ticksnote)
end

--
-- Main
--
function p._main(frame, args, tools, skills, members, quest, materials, output, facilities, ticks, ticksnote)
	local hasreftag = false
	local function make_row(item_data)
		return mw.html.create('tr')
			:tag('td'):wikitext(item_data.image):done()
			:tag('td'):wikitext('[[' .. item_data.name .. ']]'):done()
			:tag('td'):wikitext(commas._add(item_data.quantity)):done()
	end
	
	local function toolImages(t)
		local images = {}
				
		if t == nil then
			return 'None'
		end
		
		local spl = mw.text.split(t, ",")
		for _, image_i in ipairs(spl) do
			image_i = mw.text.trim(image_i)
			if toolsList[image_i] then
				table.insert(images, toolsList[trimmed])
			else
				table.insert(images, string.format("[[File:%s.png|link=%s]]", image_i, image_i))
			end
		end
		return table.concat(images)
	end
	
	local function facilityLinks(f)
		local links = {}
		
		if f == nil then
			return 'None'
		end
		
		local spl = mw.text.split(f, ",")
		for _, link_i in ipairs(spl) do
			if facilitiesIcons[link_i] ~= nil then
				table.insert(links, string.format("%s [[%s]]", facilitiesIcons[link_i], link_i))
			else
				table.insert(links, string.format("[[%s]]", link_i))
			end
		end
		return table.concat(links, "<br />")
	end
	local parent = mw.html.create('div')
			:css({width = 'max-content' })

	local requirements = mw.html.create('table')
			:addClass('wikitable align-center-2 align-right-3')
			:css({ width = '100%',
				['margin-bottom'] = '0' })
	
	requirements:tag('caption'):wikitext("Requirements"):done()
	local tr = requirements:tag('tr')
		if #skills ~= 0 then
			tr:tag('th'):attr('colspan', 2):wikitext('Skill'):done()
			tr:tag('th'):wikitext('Level'):done()
			tr:tag('th'):wikitext('XP'):done()
		end	
	
	local membersTemplate = editbutton('?')
	if members == 'Yes' then
		membersTemplate = "[[File:Member icon.png|center|link=Members]]"	
	elseif members == 'No' then
		membersTemplate = "[[File:Free-to-play icon.png|center|link=Free-to-play]]"	
	end

	if #skills ~= 0 then
		for i, v in ipairs(skills) do
			requirements:tag('tr')
				:tag('td'):attr('colspan', 2):wikitext('[[' .. v.name .. ']]'):done()
				:tag('td'):wikitext(v.lvl):done()
				:tag('td'):wikitext(v.xp):done()
		end
	end
	
	if quest ~= nil then
		requirements:tag('tr')
			:tag('td'):attr('colspan', 4):wikitext('Quest: ' .. quest):done()
	end
	
	local tr = requirements:tag('tr')
	tr:tag('th'):wikitext('Members'):done()
	tr:tag('td'):wikitext(membersTemplate):done()
	tr:tag('th'):attr('title', 'Ticks per action'):wikitext('[[RuneScape clock#Length of a tick|Ticks]]'):done()
	if (ticks or '') == '' then
		tr:tag('td'):wikitext('?'):done()
	elseif string.lower(ticks) == 'varies' then
		local note = ''
		if ticksnote ~= nil then
			note = frame:extensionTag{ name='ref', content = ticksnote, args = { group='r' } }
			hasreftag = true
		end
		tr:tag('td'):wikitext('Varies' .. note):done()
	else
		local secs = tonumber(ticks, 10) * 0.6
		local note = ''
		if ticksnote ~= nil then
			note = frame:extensionTag{ name='ref', content = ticksnote, args = { group='r' } }
			hasreftag = true
		end
		tr:tag('td'):attr('title', ticks .. ' ticks (' .. secs .. 's) per action'):wikitext(ticks .. ' (' .. secs .. 's) ' .. note):done()
	end
	
	if tools ~= nil or facilities ~= nil then
		local toolImgs = toolImages(tools)
		local facilityLnks = facilityLinks(facilities)
		requirements:tag('tr')
			:tag('th'):wikitext('Tools'):done()
			:tag('td'):css({ ['text-align'] = 'center' }):wikitext(toolImgs):done()
			:tag('th'):wikitext('Facilities'):done()
			:tag('td'):css({ ['text-align'] = 'center' }):wikitext(facilityLnks):done()
	end
	
	local materialsTable = mw.html.create('table')
			:addClass('wikitable align-center-1 align-right-3 align-right-4')
			:css({ width = '100%',
				['margin-top'] = '-1px' })	

	materialsTable:tag('caption'):wikitext("Materials"):done()
	materialsTable:tag('tr')
		:tag('th'):attr('colspan', 2):wikitext('Item'):done()
		:tag('th'):wikitext('Quantity'):done()

	for i, v in ipairs(materials) do
		materialsTable:node(make_row(v))
	end
	
	materialsTable:tag('tr'):tag('th'):attr('colspan', 3)
	materialsTable:node(make_row(output))
	
	parent:node(requirements)
	parent:node(materialsTable)
	
	local jsonObject = {skills = skills, materials = {}, output = output}
	local materialNames = {}
	for _, v in ipairs(materials) do
		table.insert(jsonObject.materials, {name = v.name, quantity = v.quantity})
		table.insert(materialNames, v.name)
	end
	
	mw.smw.set({
		["Uses material"] = materialNames,
		['Production JSON'] = mw.text.jsonEncode(jsonObject)
	})

	local outro = ''
	if hasreftag then
		outro = '\n' .. frame:extensionTag{ name='references', args = { group='r' } }
	end

	return tostring(parent) .. categories(args) .. outro
end

function categories(args)
	local cats = {}
	
	if (args.ticks or '') == '' then
		table.insert(cats, 'Recipes missing ticks')
	end
	
	if args.tools ~= nil then
		table.insert(cats, 'Recipes that require a tool')
	end
	
	if args.facilities ~= nil then
		table.insert(cats, 'Recipes that use a facility')
	end
	
	for i,v in ipairs(cats) do
		cats[i] = string.format('[[Category:%s]]',v)
	end
	return table.concat(cats,'')
end

return p