Модуль:NumToName
Для документации этого модуля может быть создана страница Модуль:NumToName/doc
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local ranks = {
novice = "Новичок",
experienced = "Опытный",
veteran = "Ветеран",
master = "Мастер",
super_master = "Супер мастер" -- ???
}
local reputations = {
terrible = "Ужасно",
very_bad = "Очень плохо",
bad = "Плохо",
neutral = "Нейтрал",
good = "Хорошо",
very_good = "Очень хорошо",
excellent = "Отлично"
}
local rank_tables = {
build_1935 = {
thresholds = {10, 50, 100},
values = {ranks.novice, ranks.veteran, ranks.master, ranks.super_master},
inclusive = true
},
build_2205 = {
thresholds = {30, 60, 90},
values = {ranks.novice, ranks.experienced, ranks.veteran, ranks.master}
},
soc = {
thresholds = {300, 600, 900},
values = {ranks.novice, ranks.experienced, ranks.veteran, ranks.master}
},
cs = {
thresholds = {300, 600, 900},
values = {ranks.novice, ranks.experienced, ranks.veteran, ranks.master}
},
cop = {
thresholds = {30, 60, 90},
values = {ranks.novice, ranks.experienced, ranks.veteran, ranks.master}
}
}
local reputation_tables = {
build_1935 = {
thresholds = {-20, 20, 40},
values = {reputations.bad, reputations.neutral, reputations.good, reputations.very_good},
inclusive = true
},
soc = {
thresholds = {-1000, -150, -50, 50, 150, 1000},
values = {reputations.terrible, reputations.very_bad, reputations.bad, reputations.neutral, reputations.good, reputations.very_good, reputations.excellent}
}
}
local function getName(num, tbl)
for i = 1, #tbl.values do
if not tbl.thresholds[i] or num < tbl.thresholds[i] or (num == tbl.thresholds[i] and tbl.inclusive) then
return tbl.values[i]
end
end
end
local function getNum(name, tbl, rank)
for i = 1, #tbl.values do
if name == tbl.values[i] then
local maxValue = tbl.thresholds[i] or nil
local minValue = tbl.thresholds[i - 1] or nil
minValue = minValue or (rank and 0)
minValue = (minValue and tbl.inclusive) and minValue + 1 or minValue
maxValue = (maxValue and not tbl.inclusive) and maxValue - 1 or maxValue
return (maxValue and minValue) and tostring(minValue) .. "−" .. tostring(maxValue) or
(minValue) and tostring(minValue) .. "+" or tostring(maxValue) .. '-'
end
end
assert(false, 'Промежуток для нужного значения не найден у указанной игры')
end
local function convertToText(frame, rank)
local args = getArgs(frame)
local names = rank and ranks or reputations
local tables = rank and rank_tables or reputation_tables
local pagename = rank and 'Ранг' or 'Репутация (элемент игрового процесса)'
local game = args[2] or 'soc'
local hint = yesno(args.hint) or (tonumber(args[1]) and true or false)
assert(args[1], 'Не указано значение в первом параметре')
assert(tables[game], 'Неверно указана игра во втором параметре')
local value, image, selectedName
if tonumber(args[1]) then
value = tonumber(args[1])
assert(value >= 0 or not rank, 'Ранг не может быть отрицательным')
selectedName = getName(value, tables[game])
else
selectedName = string.lower(names[args[1]])
assert(selectedName, 'Неверное указано значение в первом параметре')
value = getNum(selectedName, tables[game], rank)
end
image = (args.icon and rank) and string.format('[[Файл:Ранг - %s.png]]', selectedName) .. '<br>' or ''
local link = (pagename == mw.title.getCurrentTitle().fullText) and selectedName or string.format('[[' .. pagename .. '#%s|%s]]', selectedName, selectedName)
value = tostring(value):gsub("-", "−")
if rank and game == 'soc' and hint then
value = frame:expandTemplate{title = 'Comment', args = { value, 'Значение на момент появления, в начале игры может сразу же увеличиться на 1—30 единиц. В некоторых случаях это приводит к повышению ранга.' }}
end
local fullText = image .. string.format('%s (%s)', link, value)
return mw.html.create('span'):node(fullText):allDone()
end
function p.rank(frame)
return convertToText(frame, true)
end
function p.reputation(frame)
return convertToText(frame, false)
end
return p