Module:Convert
Jump to navigation
Jump to search
local p = {}
function p.normalizeUnit(u)
local unit = u
if u == 'tick' or u == 'ticks' or u == 'gt' or u == 'game ticks' then unit = 'game tick'
elseif u == 'rt' or u == 'redstone ticks' then u = 'redstone tick'
elseif u == 's' or u == 'sec' or u == 'secs' or u == 'seconds' then unit = 'second'
elseif u == 'm' or u == 'min' or u == 'mins' or u == 'minutes' then unit = 'minute'
elseif u == 'day' or u == 'days' or u == 'igd' or u == 'in game day' then unit = 'in-game day'
end
return unit
end
function p.convert(value, inputUnit, outputUnit)
inputUnit = p.normalizeUnit(inputUnit or 'game tick') -- using default value of 'game tick'
outputUnit = p.normalizeUnit(outputUnit or 'second') -- using default value of 'second'
local convertedValue = value
local conversions = {
['second -> game tick'] = convertedValue * 20, ['game tick -> second'] = convertedValue / 20,
['second -> redstone tick'] = convertedValue * 10, ['redstone tick -> second'] = convertedValue / 10,
['minute -> second'] = convertedValue * 60, ['second -> minute'] = convertedValue / 60,
['minute -> game tick'] = convertedValue * 20 * 60, ['game tick -> minute'] = convertedValue / 20 / 60,
['minute -> redstone tick'] = convertedValue * 10 * 60, ['redstone tick -> minute'] = convertedValue / 10 / 60,
['game tick -> redstone tick'] = convertedValue / 2, ['redstone tick -> game tick'] = convertedValue * 2,
['in-game day -> minute'] = convertedValue * 20, ['minute -> in-game day'] = convertedValue / 20,
['in-game day -> second'] = convertedValue * 20 * 60, ['second -> in-game day'] = convertedValue / 20 / 60,
['in-game day -> game tick'] = convertedValue * 24000, ['game tick -> in-game day'] = convertedValue / 24000,
}
convertedValue = conversions[inputUnit .. ' -> ' .. outputUnit]
return convertedValue
end
function p.output(f)
local args = f
if f == mw.getCurrentFrame() then
args = require('Module:ProcessArgs').merge(true)
end
local value = args[1]*1 or 1 -- using default value of 1
local sourceUnit = args[2]
if not args[2] then return 1 end
if not args[3] then return value .. ' ' .. sourceUnit end
local separator = args['separator'] or args['sep'] or args['s'] or '('
local startSeparator = ' ' .. separator .. ' '
local endSeparator = ''
if separator == '(' then startSeparator = ' (' endSeparator = ')' end
local fullOutput = ''
for i,unit in ipairs(args) do
if (i > 2 and args[i]) then
unit = p.normalizeUnit(unit)
local outputVal = p.convert(value, sourceUnit, unit)
if unit == 'game tick' or unit == 'redstone tick' then unit = '[[' .. unit .. ']]' end
if outputVal ~= 1 then unit = unit .. 's' end -- plural
local midSeparator = startSeparator
if i > 3 and midSeparator == ' (' then midSeparator = '; ' end
fullOutput = fullOutput .. midSeparator .. outputVal .. ' ' .. unit
end
end
sourceUnit = p.normalizeUnit(sourceUnit)
if value ~= 1 then sourceUnit = sourceUnit .. 's' end
return value .. ' ' .. sourceUnit .. fullOutput .. endSeparator
end
return p