Module:Sandbox/AnataBakka/Hextech

-- --@Documentation: Template:Hextech --@Author: User:Nanaki local getArgs = require('Dev:Arguments').getArgs local skins = { [390] = 3,   [460] = 1,    [520] = 162,    [750] = 128,    [975] = 282,    [1350] = 180,    [1820] = 37,    [2450] = 6,    [2775] = 1,    [3250] = 4, } local reroll_threshold = 2450 local function formula(avg, a, b, c)   a = (a >= reroll_threshold and 0.5 or 0.2) * a    b = (b >= reroll_threshold and 0.5 or 0.2) * b    c = (c >= reroll_threshold and 0.5 or 0.2) * c    return avg - 300 - a - b - c end -- Util local function keys(tab, sorted) local keys = {} for k,v in pairs(tab) do       table.insert(keys, k)    end if sorted then table.sort(keys) end return keys end local function copytable(tab) local new = {} for k,v in pairs(tab) do       new[k] = v    end return new end -- Display functions local function summary_table(heading, values) local t = mw.html.create('table'):addClass('article-table'):css('width', '400px') local total, rp = 0, 0 for k,v in pairs(values) do       total = total + v        rp = rp + k*v end t:tag('tr') :tag('th') :wikitext(heading) :attr('colspan', 2) t:tag('tr') :tag('th') :wikitext('Total:') :attr('scope', 'row') :css('width', '100px') :done :tag('td') :wikitext(total) t:tag('tr') :tag('th') :wikitext('RP value:') :attr('scope', 'row') :done :tag('td') :wikitext(rp) if total == 0 then return t end t:tag('tr') :tag('th') :wikitext('Average:') :attr('scope', 'row') :done :tag('td') :wikitext(string.format('%.2f', rp/total)) local breakdown = t:tag('tr') :tag('td') :attr('colspan', 2) :tag('div') :addClass('mw-collapsible mw-collapsed') :attr('data-expandtext', 'show breakdown') :attr('data-collapsetext', 'hide breakdown') :wikitext('\n') for i,v in ipairs(keys(values, true)) do       breakdown:wikitext(string.format('* \'\'\'%d\'\'\': %d (%.1f%%)\n', v, values[v], values[v]/total*100)) end return t end local function rerolls_table(values) local t = mw.html.create('table'):addClass('article-table sortable') t:tag('caption') :wikitext('Recomended rerolls') t:tag('tr') :tag('th') :wikitext('1') :attr('scope', 'col') :css('width', '50px') :done :tag('th') :wikitext('2') :attr('scope', 'col') :css('width', '50px') :done :tag('th') :wikitext('3') :attr('scope', 'col') :css('width', '50px') :done :tag('th') :wikitext('Value') :attr('scope', 'col') :css('width', '150px') for i,v in ipairs(values) do       if v[4] >= 0 then t:tag('tr') :tag('td') :wikitext(v[1]) :done :tag('td') :wikitext(v[2]) :done :tag('td') :wikitext(v[3]) :done :tag('td') :wikitext(string.format('%.1f', v[4])) end end return t end -- Calculations local function rerolls(average) local allskins = copytable(skins) local prices = keys(allskins, true) local res = {} for i = 1, #prices, 1 do       local v1 = prices[i] if allskins[v1] > 0 then allskins[v1] = allskins[v1] - 1 for j = i, #prices, 1 do               local v2 = prices[j] if allskins[v2] > 0 then allskins[v2] = allskins[v2] - 1 for k = j, #prices, 1 do                       local v3 = prices[k] if allskins[v3] > 0 then table.insert(res, {v1, v2, v3, formula(average, v1, v2, v3)}) end end allskins[v2] = allskins[v2] + 1 end end allskins[v1] = allskins[v1] + 1 end end return res end -- Invokable local p = {} function p.reroll(frame) local out = mw.html.create local args = getArgs(frame, {parentFirst=true}) local prices = keys(skins, true) -- Check/prep input values local owned, owned_total, owned_rp = {}, 0, 0 local errors = {} for i,v in ipairs(prices) do       owned[v] = tonumber(args[v]) or 0 -- assume 0 for non-number owned owned_total = owned_total + owned[v] owned_rp = owned_rp + owned[v]*v if owned[v] < 0 or owned[v] > skins[v] then table.insert(errors, v)       end end -- Error message for bad values if #errors > 0 then local res = ' Values out of bounds: ' for i,v in ipairs(errors) do           res = res .. mw.ustring.format('\n* \'\'\'%d\'\'\' = %d [0 - %d]', v, owned[v], skins[v]) end return res end out:node(summary_table('Owned skins', owned)):wikitext('\n\n') -- Calculate skins you don't own local missing, missing_total, missing_rp = {}, 0, 0 for i,v in ipairs(prices) do       missing[v] = skins[v] - owned[v] missing_total = missing_total + missing[v] missing_rp = missing_rp + missing[v]*v end out:node(summary_table('Missing skins', missing)):wikitext('\n\n') if missing_total == 0 then out:wikitext('You own all skins') else out:node(rerolls_table(rerolls(missing_rp/missing_total))) end return out end function p.usage(frame) local args = getArgs(frame, {frameOnly=true}) local res = '' if args[1] then res = '{{' .. args[1] else local title = mw.title.getCurrentTitle if title.namespace == 10 then res = '{{' .. title.rootText else res = '{{' .. title.nsText .. ':' .. title.text end end for i,v in ipairs(keys(skins, true)) do       res = res .. mw.ustring.format('\n| %-4d = 0 – %d', v, skins[v]) end res = res .. '\n}}' return ' ' .. res .. ' ' end return p

-- --