Nie podano opisu zmian |
Nie podano opisu zmian |
||
Linia 187: | Linia 187: | ||
end |
end |
||
− | function p. |
+ | function p.removeSpecialCharacters(str) |
⚫ | |||
+ | str = string.gsub(str, "[®]", "") |
||
+ | str = string.gsub(str, "[™]", "") |
||
+ | str = string.gsub(str, "[:]", "") |
||
+ | return str |
||
+ | end |
||
+ | |||
+ | function p.resolveFileName(str, keepSpaces) |
||
+ | keepSpaces = keepSpaces or false |
||
local result = "" |
local result = "" |
||
− | result = string. |
+ | result = string.lower(str) |
+ | result = p.removeSpecialCharacters(result) |
||
result = p.CapitalizeName(result) |
result = p.CapitalizeName(result) |
||
+ | if not keepSpaces then |
||
⚫ | |||
+ | result = string.gsub(result, "[ ]", "") |
||
+ | end |
||
--In future if there will be needed replace charactere such as "é" just add another substitution |
--In future if there will be needed replace charactere such as "é" just add another substitution |
||
+ | |||
− | |||
return result |
return result |
||
+ | end |
||
+ | |||
+ | function p.resolveImageName(name) |
||
+ | if mw.title.new("File:" .. name .. ".png").exists then return name .. ".png" end |
||
+ | if mw.title.new("File:" .. name .. ".jpg").exists then return name .. ".jpg" end |
||
+ | return name .. ".png" |
||
end |
end |
||
Linia 218: | Linia 236: | ||
return "black" |
return "black" |
||
end |
end |
||
+ | end |
||
+ | |||
+ | function p.getPageTitle() |
||
+ | return mw.title.getCurrentTitle().text |
||
end |
end |
||
--------------------------------------------------------------------------------- |
--------------------------------------------------------------------------------- |
||
Linia 273: | Linia 295: | ||
end |
end |
||
+ | --------------------------------------------------------------------------------- |
||
+ | |||
+ | function p.getCountOfSoundtracks() |
||
+ | return utils.getCount("ost") |
||
+ | end |
||
+ | |||
+ | function p.resolveDateTime(datetime) --with weekday |
||
+ | local datetime = p.getDatetime(datetime) |
||
+ | return os.date("%d %B %Y (%A)", datetime) |
||
+ | end |
||
+ | |||
+ | function p.getDatetime(datestamp) |
||
+ | local day, month, year = datestamp:match("^(%d%d)%.(%d%d)%.(%d%d%d%d)$") |
||
+ | return os.time{year = year, month = month, day = day} |
||
+ | end |
||
return p |
return p |
Wersja z 12:16, 21 maj 2020
local p = {}
local mathOps = require("Module:MathOps")
local frame = mw.getCurrentFrame()
function p.getCount(subject)
local list
--if you have another list just add it into a list then call appropriate function
if subject == "map" then list = maps
elseif subject == "realm" then list = realms
elseif subject == "killer" then list = killers
elseif subject == "survivor" then list = survivors
elseif subject == "dlc" then list = dlcs
elseif subject == "chapter" then return getCountDlcType(1)
elseif subject == "paragraph" then return getCountDlcType(2)
elseif subject == "clothing" then return getCountDlcType(3)
elseif subject == "ost" then return getCountDlcType(4)
elseif subject == "ccy" then return getCountCCY(true)
elseif subject == "ccy-gc" then return getCountCCY(true) --redundant option to keep convention
elseif subject == "ccy-rc" then return getCountCCY(false)
else return 0
end
local i = 0
while list[i + 1] do i = i + 1 end
return i
end
function getCountDlcType(type)
local count = 0
for i, dlc in ipairs(dlcs) do
if dlc.category == type then count = count + 1 end
end
return count
end
function getCountCCY(gc)
local list = ccy
local i = 0
while list[i + 1] and list[i + 1].gc == gc do i = i + 1 end
return i
end
-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{yesno}}.
function p.bool(val, default)
-- If your wiki uses non-ascii characters for any of "yes", "no", etc., you
-- should replace "val:lower()" with "mw.ustring.lower(val)" in the
-- following line.
val = type(val) == 'string' and val:lower() or val
if val == nil then
return nil
elseif val == true
or val == 'yes'
or val == 'y'
or val == 'true'
or val == 't'
or val == 'on'
or tonumber(val) == 1
then
return true
elseif val == false
or val == 'no'
or val == 'n'
or val == 'false'
or val == 'f'
or val == 'off'
or tonumber(val) == 0
then
return false
else
return default
end
end
--Example usage:
--amount = 1333444.1
--print(format_num(amount,2))
--print(format_num(amount,-2,"US$"))
--amount = -22333444.5634
--print(format_num(amount,2,"$"))
--print(format_num(amount,2,"$","()"))
--print(format_num(amount,3,"$","NEG "))
--Output:
--1,333,444.10
--US$1,333,400
---$22,333,444.56
--($22,333,444.56)
--NEG $22,333,444.563
function p.format_num(amount, decimal, prefix, neg_prefix)
local str_amount, formatted, famount, remain
decimal = decimal or 2 -- default 2 decimal places
neg_prefix = neg_prefix or "-" -- default negative sign
famount = math.abs(mathOps.round(amount,decimal))
famount = math.floor(famount)
remain = mathOps.round(math.abs(amount) - famount, decimal)
-- comma to separate the thousands
formatted = p.commaFormat(famount)
-- attach the decimal portion
if (decimal > 0) then
remain = string.sub(tostring(remain),3)
formatted = formatted .. "." .. remain ..
string.rep("0", decimal - string.len(remain))
end
-- attach prefix string e.g '$'
formatted = (prefix or "") .. formatted
-- if value is negative then format accordingly
if (amount<0) then
if (neg_prefix=="()") then
formatted = "("..formatted ..")"
else
formatted = neg_prefix .. formatted
end
end
return formatted
end
function p.commaFormat(amount)
local formatted = amount
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return formatted
end
--Converting Roman numbers to Arabic
function ToNumeral(roman)
local Num = { ["M"] = 1000, ["D"] = 500, ["C"] = 100, ["L"] = 50, ["X"] = 10, ["V"] = 5, ["I"] = 1 }
local numeral = 0
local i = 1
local strlen = string.len(roman)
while i < strlen do
local z1, z2 = Num[ string.sub(roman,i,i) ], Num[ string.sub(roman,i+1,i+1) ]
if z1 < z2 then
numeral = numeral + ( z2 - z1 )
i = i + 2
else
numeral = numeral + z1
i = i + 1
end
end
if i <= strlen then numeral = numeral + Num[ string.sub(roman,i,i) ] end
return numeral
end
function resolveNameWithRomanNumbers(str)
local index = string.find(str, " [^ ]*$") + 1
local romanNumber = (string.sub(str, index))
local result
if string.match(romanNumber, "[MDCLXVI]+$") then --finding ONLY Roman letters in last "word"
--Cowshed
mw.log(romanNumber)
result = str
--result = string.sub(str, 1, index - 1) .. ToNumeral(romanNumber)
else
mw.log("Non-Roman form")
result = str
end
return result
end
function p.CapitalizeName(str)
return string.gsub(" "..str, "%W%l", string.upper):sub(2)
end
function p.removeSpecialCharacters(str)
str = string.gsub(str, "[']", "")
str = string.gsub(str, "[®]", "")
str = string.gsub(str, "[™]", "")
str = string.gsub(str, "[:]", "")
return str
end
function p.resolveFileName(str, keepSpaces)
keepSpaces = keepSpaces or false
local result = ""
result = string.lower(str)
result = p.removeSpecialCharacters(result)
result = p.CapitalizeName(result)
if not keepSpaces then
result = string.gsub(result, "[ ]", "")
end
--In future if there will be needed replace charactere such as "é" just add another substitution
return result
end
function p.resolveImageName(name)
if mw.title.new("File:" .. name .. ".png").exists then return name .. ".png" end
if mw.title.new("File:" .. name .. ".jpg").exists then return name .. ".jpg" end
return name .. ".png"
end
function p.clr(text, color)
return frame:expandTemplate{title = "clr", args = {color, text}}
end
function p.resolveTextColorByBackground(hexBgColor)
if(type(hexBgColor) == "table") then
hexBgColor = hexBgColor.args[1]
end
local red = tonumber(string.sub(hexBgColor, 1, 2), 16)
local green = tonumber(string.sub(hexBgColor, 3, 4), 16)
local blue = tonumber(string.sub(hexBgColor, 3, 4), 16)
local darkness = (0.299 * red + 0.587 * green + 0.114 * blue) / 255
mw.log("Red: " .. red .." | Blue: " .. blue .. " | Green: " .. green)
mw.log(darkness)
if(darkness < 0.32) then
return "white"
else
return "black"
end
end
function p.getPageTitle()
return mw.title.getCurrentTitle().text
end
---------------------------------------------------------------------------------
function p.getSumOfASTiles(row)
local result = 0
local i = 1
while row[i] do
result = result + row[i][1] -- hardcoded first variable in table
i = i + 1
end
return result
end
function compASTiles(row1, row2)
local sum1 = row1.ASTiles
local sum2 = row2.ASTiles
if(type(sum1) == "table") then
sum1 = p.getSumOfASTiles(sum1) --converting back from table to number
end
if(type(sum2) == "table") then
sum2 = p.getSumOfASTiles(sum2)
end
if sum1 > sum2 then return true
elseif sum1 < sum2 then return false
else
if row1.realm < row2.realm then
return true
--return resolveNameWithRomanNumbers(row1.name) < resolveNameWithRomanNumbers(row2.name)
elseif row1.realm > row2.realm then
return false
--return resolveNameWithRomanNumbers(row1.name) > resolveNameWithRomanNumbers(row2.name)
else
return resolveNameWithRomanNumbers(row1.name) < resolveNameWithRomanNumbers(row2.name)
end
end
--return sum1 > sum2
end
function compMapRateSize(row1, row2)
return row1.size > row2.size --comparison specificly made for mapRates table
end
function p.sortMapsByASTiles()
local m = require("Module:Datatable")
--mw.log(mw.dumpObject(maps))
table.sort(maps,compASTiles)
--mw.log(mw.dumpObject(maps))
end
function p.sortMapRates(rankTable)
--mw.log(mw.dumpObject(rankTable))
table.sort(rankTable, compMapRateSize)
--mw.log(mw.dumpObject(rankTable))
end
---------------------------------------------------------------------------------
function p.getCountOfSoundtracks()
return utils.getCount("ost")
end
function p.resolveDateTime(datetime) --with weekday
local datetime = p.getDatetime(datetime)
return os.date("%d %B %Y (%A)", datetime)
end
function p.getDatetime(datestamp)
local day, month, year = datestamp:match("^(%d%d)%.(%d%d)%.(%d%d%d%d)$")
return os.time{year = year, month = month, day = day}
end
return p