Module:Recipe

local p = {}

local recipes = mw.loadData('Module:Recipe/data')

function tableLength(t) local count = 0; for _ in pairs(t) do count = count + 1 end return count end

function atIndex(frame) return recipes[frame.args[1]] end

function byName(name) for k, v in pairs(recipes) do		if v.name == name then return v		end end return nil end

function p.station(frame) local name = frame.args[1] local recipe = byName(name) if recipe == nil then return nil end return recipe.station end

function p.stationItem(frame) local name = frame.args[1] local recipe = byName(name) if recipe == nil then return nil end if recipe.stationItem == '' then return nil end return recipe.stationItem end

function p.inputLength(frame) local name = frame.args[1] local recipe = byName(name) if recipe == nil then return 0 end local inputs = recipe.input return tableLength(inputs) end

function p.inputItem(frame) local name = frame.args[1] local index = tonumber(frame.args[2]) local recipe = byName(name) if recipe == nil then return nil end local inputs = recipe.input local inputItem = inputs[index] if inputItem == nil then return nil end return inputItem.item end

function _inputCount(name, index) local recipe = byName(name) if recipe == nil then return nil end local inputs = recipe.input local inputItem = inputs[index] if inputItem == nil then return nil end return inputItem.count end

function p.inputCount(frame) local name = frame.args[1] local index = tonumber(frame.args[2]) return _inputCount(name, index) end

function p.inputCountString(frame) local name = frame.args[1] local index = tonumber(frame.args[2]) local count = _inputCount(name, index) if count == nil or count == 1 then return "" else return tostring(count) .. "x" end end

function p.outputItem(frame) local name = frame.args[1] local index = tonumber(frame.args[2]) local recipe = byName(name) if recipe == nil then return nil end local outputs = recipe.output local outputItem = outputs[index] if outputItem == nil then return nil end return outputItem.item end

function _outputCount(name, index) local recipe = byName(name) if recipe == nil then return nil end local outputs = recipe.output local outputItem = outputs[index] if outputItem == nil then return nil end return outputItem.count end

function p.outputCount(frame) local name = frame.args[1] local index = tonumber(frame.args[2]) return _outputCount(name, index) end

function p.outputCountString(frame) local name = frame.args[1] local index = tonumber(frame.args[2]) local count = _outputCount(name, index) if count == nil or count == 1 then return "" else return tostring(count) .. "x" end end

function allAtStation(station, stationItem) local out = {} for k, v in pairs(recipes) do		if v.station == station and v.stationItem == stationItem then table.insert(out, v)		end end return out end

return p