League of Legends Wiki
League of Legends Wiki
Nie podano opisu zmian
Znacznik: sourceedit
Nie podano opisu zmian
Znacznik: sourceedit
Linia 152: Linia 152:
 
for i,v in ipairs(list) do set[v] = true end
 
for i,v in ipairs(list) do set[v] = true end
 
 
local ul = Rotation.formats['ul'](list)
+
local ul = Rotation.formats['ul'](list, { size = 48 })
 
div:node(ul)
 
div:node(ul)
 
table.insert(uls, ul)
 
table.insert(uls, ul)

Wersja z 06:11, 6 wrz 2017

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Rotacja/opis

local Rotation = {}

local S = require('Module:Sezon').m
local getArgs = require('Dev:Arguments').getArgs
local counters = require('Module:Lista')._counters
local cleanup = require('Module:Lista')._cleanup

local seasons = mw.loadData('Module:Rotacja/sezony')

function strtotime(str)
    local year, month, day = mw.ustring.match(str, '(%d+)-(%d+)-(%d+)')
    return os.time{ year = year, month = month, day = day }
end

function getWeek(time)
    local season
    for i,v in ipairs(seasons) do
        if v['end'] == nil or v['end'] >= time then
            if v['start'] > time then
                error(mw.ustring.format('Given date is before first %sseason (%s)',
                    v.preseason and 'pre' or '',
                    os.date('%Y-%m-%d', v.start)
                ), 2)
            end
            season = i
            break
        end
    end
    
    local week = math.floor((time - seasons[season]['start']) / 604800) + 1
    
    local start = os.date('*t', seasons[season]['start'])
    local diff = (os.date('*t', time).wday - start.wday) % 7
    
    local week_start = os.date('*t', time - diff * 86400)
    week_start.hour = 0
    
    return {
        no = week,
        start = os.time(week_start),
        season = seasons[season]
    }
end
Rotation._getWeek = getWeek


function getChamps(args)
    local champs, keys = {}, {}
    for k,v in pairs(args) do
        local n = mw.ustring.match(k, 'Bohater ([0-9]+)')
        if n then
            n = tonumber(n)
            champs[n] = v
            table.insert(keys, n)
        end
    end
    table.sort(keys)
    local list = {}
    for i,v in ipairs(keys) do
        table.insert(list, champs[v])
    end
    return list
end

Rotation.formats = require('Module:Lista/formaty')
function Rotation.format(frame)
    local args = getArgs(frame, {frameOnly = true})
    local pargs = getArgs(frame, {parentOnly = true})
    
    local f = args[1] or 'table'
    local champs = getChamps(args)
    
    if Rotation.formats[f] == nil then
        error('Format doesn\'t exist', 3)
    end
    
    if pargs['counters'] then counters(champs) end
    
    return Rotation.formats[f](champs, pargs, frame)
end

local generator_format = 'Project:Rotacja bohaterów - %s, tydzień %d'
function Rotation.generator(frame)
    local args = getArgs(frame, {frameOnly = true})
    if args['date'] then
        local res = getWeek(strtotime(args['date']))
        return mw.ustring.format(generator_format, res.season.name, res.no)
    end
end

function Rotation.history(frame)
    local args = getArgs(frame, {frameOnly = true})
    local season, s, e
    
    if args['season'] then
        args['season'] = mw.ustring.lower(S{args['season']} or args['season'])
        for i,v in ipairs(seasons) do
            if args['season'] == mw.ustring.lower(v['name']) then
                season = v
            end
        end
        if not season then
            error('No such season', 2)
        end
    end
    if not season and args['from'] or args['to'] then
        season = getWeek(strtotime(args['from'] or args['to'])).season
    end
    s = season['start']
    e = season['end']
    
    if args['from'] then
        local t = strtotime(args['from'])
        if e and t > e then
            error(mw.ustring.format('\'from\' is after the end of season (%s)', season.name), 2)
        end
        s = math.max(s, t)
    end
    if args['to'] then
        local t = strtotime(args['to'])
        if t < s then
            error(mw.ustring.format('\'to\' is before the start of season (%s)', season.name), 2)
        end
        e = math.min(e, t)
    end
    
    local start_week = getWeek(s)
    local end_week = getWeek(e or os.time())
    
    local lang = mw.getContentLanguage()
    local res = mw.html.create('')
    local columns = tonumber(args['columns']) or 0
    local uls, set = {}, {}
    
    for week = start_week.no, end_week.no, 1 do
        local i = week - start_week.no
        local time = start_week.start + 60*60*24*7*i
        local page = mw.ustring.format(generator_format, season.name, week)
        
        local div = mw.html.create('div'):addClass('rotation-row')
        div:wikitext(mw.ustring.format('Tydzień %d. – %s', week, lang:formatDate('j xg Y',  os.date('%Y-%m-%d', time)))):newline()
        
        local status, list = pcall(function()
            mw.log(page)
            return frame:expandTemplate{ title = page, args = { 'plain' }}
        end)
        if not status then break end
        
        list = cleanup(mw.text.split(list, '%s*[,;\n]+%s*'))
        columns = math.max(columns, #list)
        counters(list, 'rotation-history')
        for i,v in ipairs(list) do set[v] = true end
        
        local ul = Rotation.formats['ul'](list, { size = 48 })
        div:node(ul)
        table.insert(uls, ul)
        
        res:node(div):newline()
    end
    for i,v in ipairs(uls) do
        for i = 1, math.max(0, columns - #v.nodes), 1 do v:tag('div') end
    end
    if frame and frame.preprocess then
        frame:preprocess('{{#if:{{#arraysize:rotation-history-list}}||{{#arraydefine:rotation-history-list}}}}')
        local list = {}
        for k,v in pairs(set) do
            table.insert(list, k)
        end
        frame:preprocess(mw.ustring.format('{{#arraydefine:rotation-history-temp|%s|;}}{{#arrayunion:rotation-history-list|rotation-history-list|rotation-history-temp}}{{#arraysort:rotation-history-list|asc}}{{#arrayreset:rotation-history-temp}}', table.concat(list, ';')) .. math.random())
    end
    return res
end

function Rotation.nav()
    local div = mw.html.create('div'):addClass('rotation-nav')
    
    local last, elem
    for i,v in ipairs(seasons) do
        local no = v.season or v.preseason
        if no ~= last then
            last = no
            elem = div:tag('div')
            elem:wikitext(mw.ustring.format('\n\'\'\'%s\'\'\'', S{no, short=true}))
        end
        local page = v.page
        if page == nil then
            page = mw.ustring.format('Historia rotacji darmowych bohaterów (%s)', v.name)
        end
        elem:wikitext(mw.ustring.format('\n* [[%s|%s]]',
            page,
            v.season and 'Sezon' or 'Przed-sezon'
        ))
    end
    return div
end

return Rotation