Module:Food/table/data

Jump to navigation Jump to search
Documentation[create] [refresh]
This module has no documentation. If you know how to use this module, please create it.
-- Lua implementation of Food/table/data.
local p = {}
local getArgs = require('Module:Arguments').getArgs
local slot = require('Module:Inventory slot').slot
local expr = mw.ext.ParserFunctions.expr

local function fst(t)
	if type(elem) == 'table' then
		return t[1]	
	else
		return t
	end
end

local function map(t, f)
    local r = {}
    for k,v in pairs(t) do
        r[k] = f(v, k)
    end
    return r
end

-- Main entry point
function p.ftd(f)
	local args = {}
	if f.args ~= nil then
		args = getArgs(f)
	else
		args = f
		f = mw.getCurrentFrame()
	end

	-- Normalize some names.
	args.name = args.name or args[1]
	args.hun = args.hun or args[2]
	args.sat = args.sat or args[3]
	args.src = args.src or args[4]
	args.link = args.link or args.name
	
	-- Preprocess.
	local only_je
	local only_be
	local diff = false
	if args.hun ~= nil then
		args.hun = tonumber(args.hun)	
	end
	if args.sat ~= nil then
		if strings.find(args.sat, '\\') ~= nil then
			args.sat = mw.text.split(args.sat, '\\', true)
			args.sat = map(args.sat, tonumber)
			diff = true
			only_je = f:expandTemplate{
				title = 'Template:Only',
				args = {'java', short=1}
			}
			only_be = f:expandTemplate{
				title = 'Template:Only',
				args = {'be', short=1}
			}
		else
			args.sat = {tonumber(args.sat)}
		end
	end
	
	if args.poison ~= nul then
		args.poison = tonumber(expr(args.sat))
	end
	
	-- Start emitting.
	local out_table = {}
	-- begin row
	out_table.insert('|-')
	-- link
	out_table.insert('!scope="row" id="args.name"|' ..
		('[[' .. args.link .. '|' .. args.name ']]') ..
		(args.note and ' ' .. args.note or '')
	)
	-- pic
	out_table.insert('|' ..
		slot({args.name, ['link']=args.link})
	)
	-- hun
	out_table.insert('|' ..
		f:expandTemplate{
			title = 'Template:Hunger',
			args = {args.hun}
		}
	)
	-- sat
	local function insert_value(val)
		if type(val) ~= 'table' then
			return val
		elseif #val == 1 then
			return val[1]
		elseif #val == 2 then
			return val[1] .. only_je .. '<br/>' .. val[2] .. only_be
		elseif #val == 3 then
			return val[1] .. ' (' .. val[3] .. ')'
		elseif #val == 4 then
			return val[1] .. ' (' .. val[3] .. ')' .. only_je .. '<br/>' ..
				   val[2] .. ' (' .. val[4] .. ')' .. only_be
		end
	end
	if args.sat ~= nil then
		if args.poison ~= nil then
			args.sat[3] = args.sat[1] - args.poison
			if args.sat[2] ~= nil then
				args.sat[4]	= args.sat[2] - args.poison
			end
		end
		out_table.insert('|' ..
			(args.poison and 'data-sort-value=' .. tostring(fst(args.sat) - 0.01) .. '|' or '') ..
			insert_value(args.sat)
		)
	else
		out_table.insert('| ?')
	end
	if args.sat ~= nil and args.hun ~= nil then
		-- total
		local total = map(args.sat, function(e)
			return e + args.hun
		end)
		out_table.insert('|' ..
			(args.poison and 'data-sort-value=' .. tostring(fst(total) - 0.01) .. '|' or '') ..
			insert_value(total)
		)
		-- ratio
		local ratio = map(args.sat, function(e)
			return e / args.hun
		end)
		out_table.insert('|' ..
			(args.poison and 'data-sort-value=' .. tostring(fst(ratio) - 0.01) .. '|' or '') ..
			insert_value(ratio)
		)
	else
		out_table.insert('| ?')
		out_table.insert('| ?')
	end
	return out_table:join('\n')
end

return p