Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:CosmeticMachine

From MCC Island Wiki

Documentation for this module may be created at Module:CosmeticMachine/doc

local getArgs = require('Module:Arguments').getArgs
local getRarity = function(name)
	return require('Module:CosmeticInfo').getRarity({name=name})
end
local getType = function(name)
	return require('Module:CosmeticInfo').getType({name=name})
end

local machineData = mw.loadData('Module:CosmeticMachine/Data')

local function round(n)
	local frac = math.abs(n) % 1
	if frac == 0 then return n end
	local zeros = tostring(frac):match("%.(0*)") or ""
	local places = #zeros + 2
	local factor = 10 ^ places
	local sign = n < 0 and -1 or 1
	return math.floor(n * factor + 0.5) / factor
end

local p = {}

function p.getTotalUniqueCosmetics()
	local unique = {}
	for _, pullData in pairs(machineData) do
		for _, name in ipairs(pullData.cosmetics) do
			unique[name] = true
		end
	end
	local count = 0
	for _ in pairs(unique) do
		count = count + 1
	end
	return count
end

function p.getNoOfCosmeticsInPull(frame)
	local args = getArgs(frame)
	local cosmetics = machineData[args.pullType .. " Pull"].cosmetics
	
	local count = 0
	for _ in pairs(cosmetics) do
		count = count + 1
	end
	return count
end

function p.getPullChance(frame)
	local args = getArgs(frame)
	local cosmeticName = args.cosmeticName
	local pullType = args.pullType .. " Pull"
	
	local rarity = getRarity(cosmeticName)
	local cType = getType(cosmeticName) or "Standard"
	
	local pullData = machineData[pullType]
	if not pullData then return 0 end
	
	local baseRarityChance = (pullData.rarityChances[rarity]) / 100

	local function countEligible(filterFunc)
		local count = 0
		for _, name in ipairs(pullData.cosmetics) do
			if getRarity(name) == rarity and filterFunc(name) then
				count = count + 1
			end
		end
		return count
	end
	
	local chanceShare
	if cType == "Exclusive" then
		local exChance = pullData.exclusiveChance and (pullData.exclusiveChance[rarity] or pullData.exclusiveChance["All"] or 0)
		local exclusiveCount = countEligible(function(name)
			return getRarity(name) == rarity and getType(name) == cType
		end)
		if exclusiveCount == 0 then return 0 end
		
		chanceShare = (baseRarityChance * (exChance / 100)) / exclusiveCount
		
	elseif cType == "Arcane" and rarity == "Mythic" then
		local arcChance = pullData.arcaneChance and pullData.arcaneChance["Mythic"] or 0
		local arcaneCount = countEligible(function(name)
			return getRarity(name) == "Mythic" and getType(name) == "Arcane"
		end)
		if arcaneCount == 0 then return 0 end
		
		chanceShare = (baseRarityChance * (arcChance / 100)) / arcaneCount
		
	else
		local exclusiveCut = pullData.exclusiveChance and (pullData.exclusiveChance[rarity] or pullData.exclusiveChance["All"] or 0)
		local arcaneCut = (rarity == "Mythic" and pullData.arcaneChance and pullData.arcaneChance["Mythic"]) or 0
		local remainingChance = baseRarityChance * (1 - (exclusiveCut + arcaneCut) / 100)
		
		local standardCount = countEligible(function(name)
			return getRarity(name) == rarity and not getType(name)
		end)
		if standardCount == 0 then return 0 end
		
		chanceShare = remainingChance / standardCount
		
	end
	return round(chanceShare * 100) .. "%"
end

function p.displayPullCosmetics(frame)
	local args = getArgs(frame)
	local pullType = args.pullType
	local pullData = machineData[pullType .. " Pull"]
	if not pullData then return 'No data found for pull type: ' .. pullType end
	
	local out = {'<div class="tradeable-display">'}
	local count = 0
	
	for _, name in ipairs(pullData["cosmetics"]) do
		local chance = p.getPullChance({cosmeticName = name, pullType = pullType})
		local filename = name .. '.png'
		local cell = string.format(
			'<div><center><div class="afix" style="width: 100px">[[File:%s|link=%s]]</div></center><div style="word-wrap:break-word; text-align:center"><small>[[%s]]</small><br /><b>%s</b></div></div>',
			filename, name, name, chance)
		
		table.insert(out, cell)
		count = count + 1
	end
	
	table.insert(out, '</div>')
	return table.concat(out, '\n')
end

return p