FANDOM


-- <pre>
-- Implements [[Template:Charm data]]
--
 
local p = {}
local commas = require('Module:Addcommas')._add
 
local images = {
	gold = '[[File:Gold charm.png|link=Gold charm]]',
	green = '[[File:Green charm.png|link=Green charm]]',
	crimson = '[[File:Crimson charm.png|link=Crimson charm]]',
	blue = '[[File:Blue charm.png|link=Blue charm]]'
}
 
function p.main(frame)
	local args = frame:getParent().args
	local view = string.lower(args.view or '')
	local monster = string.gsub(args.monster or '', '_' , ' ')
	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',
			action = args.action,
		}
	max_charms = charm_data.kills * charm_data.charms
 
	charm_data.any = charm_data.gold + charm_data.green +
				charm_data.crimson + charm_data.blue
 
	charm_data.none = max_charms - charm_data.any
 
	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,charm_data)
end
 
function p._main(view,monster,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 data.rare then
		min = 15
	else
		min = 50
	end
 
	local builder
	local url = tostring(mw.uri.fullUrl(mw.title.getCurrentTitle().fullText,{
				action = 'view',
				-- For User:Joeytje50/Dropadd.js to look for
				log_edit = 'true' }))
	local pls_add
	if #monster > 1 then
		pls_add = '['..url..' Add data to the log] (requires JavaScript).'
	else
		pls_add = ''
	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..'%'..
			'\n|-\n'
		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')
		:addClass('charmtable')
		:attr('data-monster', monster)
		:css({ ['text-align'] = 'center' })
	: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)..' '..(data.action or 'kills')..'.<br />'..
				data.charms..' '..plural(data.charms,'charm is','charms are')..' dropped at a time.<br />'..
				pls_add)
		:done()
	:done()
	local ns = mw.title.getCurrentTitle().namespace
	if ns == 0 and data.kills < 250 then
		return tostring(builder)..'[[Category:Monsters with too little charm data]]'
	elseif ns == 114 then
		local properties = {
				'{{#set:',
				string.format('Gold charm percent=%s|',data.goldpercent),
				string.format('Green charm percent=%s|',data.greenpercent),
				string.format('Crimson charm percent=%s|',data.crimsonpercent),
				string.format('Blue charm percent=%s',data.bluepercent),
				'}}'
		}
		return tostring(builder)..'[[Category:Charm logs]]'..mw.getCurrentFrame():preprocess(table.concat(properties,''))
	else
		return builder
	end
end
 
function plural(n,s,p)
	if n == 1 then
		return s
	else
		return p or s..'s'
	end
end
 
-- From 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

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.