Module:Charm log

-- -- Implements Template:Charm data --

local p = {} local commas = require('Module:Addcommas')._add

local images = { gold = '', green = '', crimson = '', blue = '' }

function p.main(frame) local args = frame:getParent.args local view = string.lower(args.view or '') local monster = args.monster local log = args.log or monster..'/Charm log' local charm_data = { charms = tonumber(args.charms) or 1, kills = tonumber(args.kills) or 0, gold = tonumber(args.gold) or 0, green = tonumber(args.green) or 0, crimson = tonumber(args.crimson) or 0, blue = tonumber(args.blue) or 0, rare = type(args.rare) == 'string', }	max_charms = charm_data.kills * charm_data.charms

charm_data.any = charm_data.gold + charm_data.green + charm_data.crimson + charm_data.blue

missed_charms = max_charms - charm_data.any charm_data.none = missed_charms / charm_data.charms

local function percent(...) local ret = {} for _, v in ipairs(...) do			if kills == 0 then table.insert(ret,nil) else table.insert(ret,math.floor(100 * (v / max_charms ) ) ) end end return unpack(ret) end charm_data.goldpercent, charm_data.greenpercent, charm_data.crimsonpercent, charm_data.bluepercent, charm_data.anypercent, charm_data.nonepercent = percent{ charm_data.gold, charm_data.green, charm_data.crimson, charm_data.blue, charm_data.any, missed_charms }

return p._main(view,monster,log,charm_data) end

function p._main(view,monster,log,data) local simple_returns = { kills = true, charms = true, gold = true, green = true, crimson = true, blue = true, any = true, none = true, goldpercent = true, greenpercent = true, crimsonpercent = true, bluepercent = true, anypercent = true, nonepercent = true }	if simple_returns[view] then return data[view] end

local min if rare then min = 15 else min = 50 end

local builder

local url = tostring(mw.uri.fullUrl(log,'action=edit')) local pls_add = '['..url..' Please add to the log.] (Only if '..min..' or more creatures are killed)'

if view == 'table' then builder = mw.html.create('table') :addClass('wikitable') :css('text-align','center')

builder	:tag('caption') :wikitext('Charm drop percentages') :done :tag('tr') :tag('th') :wikitext('None') :done :tag('th') :wikitext(images.gold) :done :tag('th') :wikitext(images.green) :done :tag('th') :wikitext(images.crimson) :done :tag('th') :wikitext(images.blue) :done :done :tag('tr') :tag('td') :wikitext(data.nonepercent..'%') :done :tag('td') :wikitext(data.goldpercent..'%') :done :tag('td') :wikitext(data.greenpercent..'%') :done :tag('td') :wikitext(data.crimsonpercent..'%') :done :tag('td') :wikitext(data.bluepercent..'%') :done :done :tag('tr') :tag('td') :attr('colspan','5') :css({ ['font-size'] = 'smaller',					['text-align'] = 'left',					['line-height'] = '15px' }) :wikitext('Based on a sample of '..commas(data.kills)..' kills. '..					data.charms..' '..plural(data.charms,'charm')..' '..					plural(data.charms,'is','are')..' dropped at a time. '..					pls_add) :done :done return builder end

if view == 'raw' then builder = mw.html.create('table') :addClass('wikitable') :addClass('charmtable') :css('text-align','center')

:wikitext('Charm drop numbers') :done :tag('tr') :tag('th') :wikitext('Total kills') :done :tag('th') :wikitext('No charm') :done :tag('th') :wikitext(images.gold) :done :tag('th') :wikitext(images.green) :done :tag('th') :wikitext(images.crimson) :done :tag('th') :wikitext(images.blue) :done :done :tag('tr') :tag('td') :wikitext( commas(data.kills) ) :done :tag('td') :wikitext( commas(data.none) ) :done :tag('td') :wikitext( commas(data.gold) ) :done :tag('td') :wikitext( commas(data.green) ) :done :tag('td') :wikitext( commas(data.crimson) ) :done :tag('td') :wikitext( commas(data.blue) ) :done :done :tag('tr') :tag('td') :attr('colspan','6') :css({ ['font-size'] = 'smaller',					['text-align'] = 'left',					['line-height'] = '15px' }) :wikitext(data.charms..' '..plural(data.charms,'charm')..' '..					plural(data.charms,'is','are')..' dropped at a time') :done :done return builder end --[=[ Make this mw.html eventually? ]=]	if view == 'row' then local ret_row = '\n|-'.. '\n| '..monster..''.. '\n| '..commas(data.kills).. '\n| '..data.charms.. '\n| '..data.goldpercent.. '\n| '..data.greenpercent.. '\n| '..data.crimsonpercent.. '\n| '..data.bluepercent.. '|}'		return ret_row end

--		If not any other return					-- local function conf_range(_c) if data.kills == 0 then return 0 end _c = _c / data.charms local _z = 1.64485 local zsq = _z * _z local _p = _c / data.kills local n1 = 2 * data.kills * _p + zsq local n2 = _z * math.sqrt( zsq + (4 * data.kills * _p * ( 1 - _p) ) ) local _d = 2 * ( data.kills + zsq ) local _l = ( 100 / _d ) * ( n1 - n2 ) local _u = ( 100 / _d ) * ( n1 + n2 ) local rnd if _l < 1 then rnd = 1 else rnd = 0 end local _lr = p._round(_l,rnd) local _ur = p._round(_u,rnd) if _lr == _ur then return _lr..'%' else return _lr..'—'.._ur..'%' end end

builder = mw.html.create('table') :addClass('wikitable') :css('text-align','center')

:wikitext('Charm drop numbers') :done :tag('tr') :tag('th') :wikitext('No charm') :done :tag('th') :wikitext(images.gold) :done :tag('th') :wikitext(images.green) :done :tag('th') :wikitext(images.crimson) :done :tag('th') :wikitext(images.blue) :done :done :tag('tr') :tag('td') :wikitext( conf_range(data.none) ) :done :tag('td') :wikitext( conf_range(data.gold) ) :done :tag('td') :wikitext( conf_range(data.green) ) :done :tag('td') :wikitext( conf_range(data.crimson) ) :done :tag('td') :wikitext( conf_range(data.blue) ) :done :done :tag('tr') :tag('td') :attr('colspan','5') :css({ ['font-size'] = 'smaller',				['text-align'] = 'left',				['line-height'] = '15px' }) :wikitext('Represents a 90% confidence range. '..				'Based on a sample of '..commas(data.kills)..' kills. '..				data.charms..' '..plural(data.charms,'charm')..' '..				plural(data.charms,'is','are')..' dropped at a time. '..				pls_add) :done :done

return builder end

function plural(n,s,p) if n == 1 then return s	else return p or s..'s'	end end

-- Wikipedia:Module:Math function p._round(value, precision) local rescale = math.pow(10, precision or 0); return math.floor(value * rescale + 0.5) / rescale; end

return p