League of Legends Wiki

Module:Sale

2,740pages on
this wiki
Add New Page
Talk0
local Sale = {}
local lib = require('Module:Feature')
local FN = require('Module:Filename')
 
Sale.args = {}
 
function Sale.verkauf(frame)
    local groups
    Sale.args, groups = lib.groupedArguments(lib.arguments(lib.mergeFrames(frame, frame:getParent())))
 
    Sale.width = (tonumber(Sale.args['Breite']) or 103) - 2
    Sale.height = math.floor(Sale.width*1.818+0.5)
 
    Sale.cols = math.max(1, tonumber(Sale.args['Spalte']) or 6)
    Sale.discount = (100 - math.min(100, math.max(0, tonumber(Sale.args['Rabatt']) or 50))) / 100
 
    local keys = lib.getSortedKeys(groups)
 
    local salediv = mw.html.create('div'):addClass('sale-box'):css('text-align', 'center')
    local when
 
    if Sale.args['Start'] or Sale.args['Ende'] then
        local from, to
        when, from, to = getTimespan(Sale.args['Start'], Sale.args['Ende'])
        if when then
            from.hour, from.min, from.sec, to.hour, to.min, to.sec = 0, 0, 0, 23, 59, 59
 
            local now = os.time()
            from = os.time(from)
            to = os.time(to)
 
            if os.difftime(now, to) > 0 then -- after sale ended - show nothing
                return ''
            elseif os.difftime(now, from) < 0 then -- before sale starts - add class and notice
                salediv:addClass('upcoming-sale'):tag('div'):addClass('sale-notice'):wikitext('Anstehendes Angebot:')
            else
                salediv:tag('div'):addClass('sale-notice'):wikitext('Aktuelles Angebot:')
            end
        end
    end
 
    if Sale.args['Titel'] then salediv:tag('div'):addClass('sale-title'):wikitext(Sale.args['Titel']) end
    if when then salediv:tag('div'):addClass('sale-time'):wikitext(when) end
 
    local row
    local counter = 1
    for i,k in ipairs(keys) do
        local box = singleBox(groups[k])
        if box then
            if i%Sale.cols == 1 then
                row = mw.html.create('table'):cssText('width:100%;table-layout:fixed;'):tag('tr')
                salediv:node(row:allDone())
            end
            row:tag('td'):css('vertical-align', 'top'):node(box)
            counter = counter + 1
        end
    end
 
    return salediv
end
function getTimespan(from, to)
    if from == nil and to == nil then
        return nil, nil, nil
    elseif from ~= nil and to ~= nil then
        local fromformat, toformat = 'd.m.Y', 'd.m.Y'
        local start = os.date('*t', tonumber(mw.getCurrentFrame():preprocess('{{#time:U|' .. from .. '}}')))
        local finish = os.date('*t', tonumber(mw.getCurrentFrame():preprocess('{{#time:U|' .. to .. '}}')))
 
        if start.year == finish.year then
            fromformat = 'd.m.Y'
            if start.month == finish.month then
                fromformat = 'd.m.Y'
                if start.day == finish.day then
                    return mw.getCurrentFrame():preprocess('{{#iferror:{{#time:j xg Y|' .. from .. '}}|' .. from .. '}}'), start, finish
                end
            end
        end
 
        return mw.getCurrentFrame():preprocess('{{#iferror:{{#time:' .. fromformat .. '|' .. from .. '}}|' .. from .. '}} – {{#iferror:{{#time:' .. toformat .. '|' .. to .. '}}|' .. to .. '}}'), start, finish
    else
        from = from or to
        local start = os.date('*t', tonumber(mw.getCurrentFrame():preprocess('{{#time:U|' .. from .. '}}')))
        return mw.getCurrentFrame():preprocess('{{#iferror:{{#time:j xg Y|' .. from .. '}}|' .. from .. '}}'), start, start
    end
end
function singleBox(args)
    if args['Champion'] == nil then
        return
    end
    local box = mw.html.create('div'):addClass('sale-skin character-icon'):css('width', (Sale.width+2)..'px'):attr('data-character', args['Champion'])
 
    local image = box:tag('div'):addClass('sale-image'):css('height', (Sale.height)..'px')
    local label = box:tag('div'):addClass('sale-label')
 
    local skin = mw.ustring.gsub(args['Skin'] or '', '%s+', '')
    local link, text
 
    if skin ~= '' then
        link = args['link'] or (args['Champion'] .. '/SkinsTrivia#' .. mw.uri.anchorEncode(skin))
        text = args['text'] or args['Skin'] .. ' ' .. args['Champion']  or ''
    else
        skin = nil
        link = args['link'] or args['Champion'] or ''
        text = args['text'] or args['Champion'] or ''
    end
    if link == '*none*' then link = '' end
 
    if link ~= '' then
        label:wikitext('[[' .. link .. '|' .. text .. ']]')
    else
        label:wikitext(text)
    end
 
    local img = image:tag('div')
    local file = FN.loading{
        champion = args['Champion'],
        skin = skin
    }
    img:wikitext(mw.ustring.format('[[File'..':%s|%dpx|%s|link=%s]]', file, Sale.width, skin or args['Champion'], link))
 
    if skin == nil then
        local price = tonumber(args['RP'] or mw.getCurrentFrame():preprocess('{{Data ' .. args['Champion'] .. '|pst2|ip}}'))
        if price ~= nil then
            local ip = image:tag('div'):addClass('sale-ip-price'):wikitext(math.floor(price))
        end
    else
        box:attr('data-skin', skin)
    end
 
    local full = tonumber(args['RP vorher'])
    local disc = tonumber(args['RP nachher'])
 
    if skin == nil and full == nil and desc == nil then
        full = tonumber(args['RP'] or mw.getCurrentFrame():preprocess('{{Data ' .. args['Champion'] .. '|pst2|rp}}'))
    end
 
    if full ~= nil and disc == nil then
        full = math.floor(full)
        disc = math.floor(full*Sale.discount)
    elseif full == nil and disc ~= nil then
        disc = math.floor(disc)
        full = math.floor(disc/Sale.discount)
    end
 
    if full ~= nil and disc ~= nil then
        local rpn = image:tag('div'):addClass('sale-rp-price-new'):wikitext(disc)
        local rpo = image:tag('div'):addClass('sale-rp-price-old'):wikitext(full)
        local change = priceChange(full, disc)
        if change < 0 then local discount = image:tag('div'):addClass('sale-discount'):wikitext(change .. '%') end
    end
 
    return box
end
function priceChange(original, new) return math.floor((new/original)*100+0.5) - 100 end
return Sale

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.