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

Module:CosmeticInfo: Difference between revisions

From MCC Island Wiki
Added isInGamePassCollection function
Refactored code + added two new functions to easily sort cosmetics by global number owned (list input or by field value!!!)
Line 8: Line 8:
local p = {}
local p = {}


function p.getDescription(frame)
local function getCosmetic(args)
return cosmeticData["cosmetics"][args.name]
end
 
local function formatGlyphs(description)
local glyphs = {
local glyphs = {
["<glyph:\"mcc:icon.tooltips.veteran\"> "] = "<br />[[File:Icon-Veteran.png|16px]] <span style=\"color: #52C8FF; font-weight: bold\">",
["<glyph:\"mcc:icon.tooltips.veteran\"> "] = "<br />[[File:Icon-Veteran.png|16px]] <span style=\"color: #52C8FF; font-weight: bold\">",
Line 14: Line 18:
["<glyph:\"mcc:icon.info_blue\"> "] = "<br />[[File:Icon-Info-Blue.png|16px]] <span style=\"color: #55FF55; font-weight: bold\">",
["<glyph:\"mcc:icon.info_blue\"> "] = "<br />[[File:Icon-Info-Blue.png|16px]] <span style=\"color: #55FF55; font-weight: bold\">",
}
}
local args = getArgs(frame)
local description = cosmeticData["cosmetics"][args.name]["description"]
local openSpan = false
local openSpan = false
for key, val in pairs(glyphs) do
for key, val in pairs(glyphs) do
if string.find(description, key) then
if description:find(key) then
if openSpan == false then
description = description:gsub(key, (openSpan and val or "</span>" .. val))
description = description:gsub(key, "</span>" .. val)
else
description = description:gsub(key, val)
end
openSpan = true
openSpan = true
end
end
end
end
return openSpan and (description .. "</span>") or description
if openSpan then
end
description = description .. "</span>"
 
local function getSimpleField(field)
return function(frame)
local args = getArgs(frame)
local cosmetic = getCosmetic(args)
return cosmetic and cosmetic[field]
end
end
return description
end
end


function p.getType(frame)
local function isInAllowedCollection(allowedCollections)
local args = getArgs(frame)
return function(frame)
return cosmeticData["cosmetics"][args.name]["type"]
local args = getArgs(frame)
local cosmetic = getCosmetic(args)
return cosmetic and allowedCollections[cosmetic.collection]
end
end
end


function p.getCollection(frame)
function p.getDescription(frame)
local args = getArgs(frame)
local args = getArgs(frame)
return cosmeticData["cosmetics"][args.name]["collection"]
local cosmetic = getCosmetic(args)
if not cosmetic then return "No data found" end
return formatGlyphs(cosmetic.description)
end
end


function p.isInCrateCollection(frame)
p.getType = getSimpleField("type")
local args = getArgs(frame)
p.getCollection = getSimpleField("collection")
local collection = cosmeticData["cosmetics"][args.name]["collection"]
p.getRarity = getSimpleField("rarity")
p.isColorable = getSimpleField("colorable")
local allowed = {
p.getTrophiesAwarded = getSimpleField("trophies")
["Oceanic"] = true,
p.isBonusTrophies = getSimpleField("isBonusTrophies")
["Mechanical"] = true,
p.canBeDonated = getSimpleField("canBeDonated")
["Natural"] = true,
p.getGlobalNumberOwned = getSimpleField("globalNumberOwned")
["Magical"] = true
 
}
p.isInCrateCollection = isInAllowedCollection({
["Oceanic"] = true,
return allowed[collection]
["Mechanical"] = true,
["Natural"] = true,
["Magical"] = true
})
 
p.isInGamePassCollection = isInAllowedCollection({
["Battle Box"] = true,
["Dynaball"] = true,
["HITW"] = true,
["Parkour Warrior"] = true,
["Sky Battle"] = true,
["TGTTOS"] = true
})
 
function p.getLastUpdatedDate()
local lang = mw.language.getContentLanguage()
return lang:formatDate( 'd F Y, h:i a', '@' .. cosmeticData['last_updated'], false ) .. ' UTC'
end
end


function p.isInGamePassCollection(frame)
function p.lastUpdatedIcon()
local args = getArgs(frame)
return string.format(
local collection = cosmeticData["cosmetics"][args.name]["collection"]
'<sup><abbr title="Data updated every 12 hours from MCC Island\'s API. Last updated on: %s" tabindex="0">ⓘ</abbr></sup>',
p.getLastUpdatedDate()
local allowed = {
)
["Battle Box"] = true,
["Dynaball"] = true,
["HITW"] = true,
["Parkour Warrior"] = true,
["Sky Battle"] = true,
["TGTTOS"] = true
}
return allowed[collection]
end
end


function p.getRarity(frame)
local function getOwnedNumber(raw)
local args = getArgs(frame)
raw = tostring(raw or "0"):gsub(",", ""):gsub("%+", "")
return cosmeticData["cosmetics"][args.name]["rarity"]
return tonumber(raw) or 0
end
end


function p.isColorable(frame)
local function sortCosmetics(list, order)
local args = getArgs(frame)
table.sort(list, function(a, b)
return cosmeticData["cosmetics"][args.name]["colorable"]
return order == "desc" and a.owned > b.owned or a.owned < b.owned
end)
end
end


function p.getTrophiesAwarded(frame)
local function formatCosmeticList(cosmetics)
local args = getArgs(frame)
local result = {}
return cosmeticData["cosmetics"][args.name]["trophies"]
for _, c in ipairs(cosmetics) do
table.insert(result, string.format("* %s (%s)", c.name, c.display or c.owned))
end
return table.concat(result, "\n")
end
end


function p.isBonusTrophies(frame)
p.sortListByNumberOwned = function(frame)
local args = getArgs(frame)
local args = getArgs(frame)
return cosmeticData["cosmetics"][args.name]["isBonusTrophies"]
local names = mw.text.split(args.names or "", ",%s*")
local order = args.order or "asc"
 
local cosmetics = {}
for _, name in ipairs(names) do
local data = cosmeticData["cosmetics"][name]
if data and data.globalNumberOwned then
table.insert(cosmetics, {
name = name,
owned = getOwnedNumber(data.globalNumberOwned),
display = data.globalNumberOwned
})
end
end
 
sortCosmetics(cosmetics, order)
return formatCosmeticList(cosmetics)
end
end


function p.canBeDonated(frame)
p.getNumberOwnedListByFieldValue = function(frame)
local args = getArgs(frame)
local args = getArgs(frame)
return cosmeticData["cosmetics"][args.name]["canBeDonated"]
local field = args.field
end
local target = args.value
local order = args.order or "asc"


function p.getGlobalNumberOwned(frame)
local filterValue = ({
local args = getArgs(frame)
["true"] = true,
return cosmeticData["cosmetics"][args.name]["globalNumberOwned"]
["false"] = false
end
})[target] or target


function p.getLastUpdatedDate()
local filtered = {}
local lang = mw.language.getContentLanguage()
for name, data in pairs(cosmeticData["cosmetics"]) do
return lang:formatDate( 'd F Y, h:i a', '@' .. cosmeticData['last_updated'], false ) .. ' UTC'
if data[field] == filterValue and data.globalNumberOwned then
end
table.insert(filtered, {
name = name,
owned = getOwnedNumber(data.globalNumberOwned),
display = data.globalNumberOwned
})
end
end


function p.lastUpdatedIcon()
sortCosmetics(filtered, order)
    return string.format('<sup><abbr title="Data updated every 12 hours from MCC Island\'s API. Last updated on: %s" tabindex="0">ⓘ</abbr></sup>', p.getLastUpdatedDate())
return formatCosmeticList(filtered)
end
end


return p
return p

Revision as of 06:21, 11 July 2025

This module uses info gathered from the API and stored in the data page to:

It is also invoked by other modules, such as Module:CosmeticMachine and Module:CosmeticCrates.


local getArgs = require('Module:Arguments').getArgs

local cosmeticData = mw.loadData('Module:CosmeticInfo/Data')
if not cosmeticData then
	return "Error: Unable to load data"
end

local p = {}

local function getCosmetic(args)
	return cosmeticData["cosmetics"][args.name]
end

local function formatGlyphs(description)
	local glyphs = {
		["<glyph:\"mcc:icon.tooltips.veteran\"> "] = "<br />[[File:Icon-Veteran.png|16px]] <span style=\"color: #52C8FF; font-weight: bold\">",
		["<glyph:\"mcc:icon.tooltips.squidtek\"> "] = "<br />[[File:Icon-Squidtek.png|16px]] <span style=\"color: #52C8FF; font-weight: bold\">",
		["<glyph:\"mcc:icon.info_blue\"> "] = "<br />[[File:Icon-Info-Blue.png|16px]] <span style=\"color: #55FF55; font-weight: bold\">",
	}
	
	local openSpan = false
	for key, val in pairs(glyphs) do
		if description:find(key) then
			description = description:gsub(key, (openSpan and val or "</span>" .. val))
			openSpan = true
		end
	end
	return openSpan and (description .. "</span>") or description
end

local function getSimpleField(field)
	return function(frame)
		local args = getArgs(frame)
		local cosmetic = getCosmetic(args)
		return cosmetic and cosmetic[field]
	end
end

local function isInAllowedCollection(allowedCollections)
	return function(frame)
		local args = getArgs(frame)
		local cosmetic = getCosmetic(args)
		return cosmetic and allowedCollections[cosmetic.collection]
	end
end

function p.getDescription(frame)
	local args = getArgs(frame)
	local cosmetic = getCosmetic(args)
	if not cosmetic then return "No data found" end
	return formatGlyphs(cosmetic.description)
end

p.getType = getSimpleField("type")
p.getCollection = getSimpleField("collection")
p.getRarity = getSimpleField("rarity")
p.isColorable = getSimpleField("colorable")
p.getTrophiesAwarded = getSimpleField("trophies")
p.isBonusTrophies = getSimpleField("isBonusTrophies")
p.canBeDonated = getSimpleField("canBeDonated")
p.getGlobalNumberOwned = getSimpleField("globalNumberOwned")

p.isInCrateCollection = isInAllowedCollection({
	["Oceanic"] = true,
	["Mechanical"] = true,
	["Natural"] = true,
	["Magical"] = true
})

p.isInGamePassCollection = isInAllowedCollection({
	["Battle Box"] = true,
	["Dynaball"] = true,
	["HITW"] = true,
	["Parkour Warrior"] = true,
	["Sky Battle"] = true,
	["TGTTOS"] = true
})

function p.getLastUpdatedDate()
	local lang = mw.language.getContentLanguage()
	return lang:formatDate( 'd F Y, h:i a', '@' .. cosmeticData['last_updated'], false ) .. ' UTC'
end

function p.lastUpdatedIcon()
	return string.format(
		'<sup><abbr title="Data updated every 12 hours from MCC Island\'s API. Last updated on: %s" tabindex="0">ⓘ</abbr></sup>',
		p.getLastUpdatedDate()
	)
end

local function getOwnedNumber(raw)
	raw = tostring(raw or "0"):gsub(",", ""):gsub("%+", "")
	return tonumber(raw) or 0
end

local function sortCosmetics(list, order)
	table.sort(list, function(a, b)
		return order == "desc" and a.owned > b.owned or a.owned < b.owned
	end)
end

local function formatCosmeticList(cosmetics)
	local result = {}
	for _, c in ipairs(cosmetics) do
		table.insert(result, string.format("* %s (%s)", c.name, c.display or c.owned))
	end
	return table.concat(result, "\n")
end

p.sortListByNumberOwned = function(frame)
	local args = getArgs(frame)
	local names = mw.text.split(args.names or "", ",%s*")
	local order = args.order or "asc"

	local cosmetics = {}
	for _, name in ipairs(names) do
		local data = cosmeticData["cosmetics"][name]
		if data and data.globalNumberOwned then
			table.insert(cosmetics, {
				name = name,
				owned = getOwnedNumber(data.globalNumberOwned),
				display = data.globalNumberOwned
			})
		end
	end

	sortCosmetics(cosmetics, order)
	return formatCosmeticList(cosmetics)
end

p.getNumberOwnedListByFieldValue = function(frame)
	local args = getArgs(frame)
	local field = args.field
	local target = args.value
	local order = args.order or "asc"

	local filterValue = ({
		["true"] = true,
		["false"] = false
	})[target] or target

	local filtered = {}
	for name, data in pairs(cosmeticData["cosmetics"]) do
		if data[field] == filterValue and data.globalNumberOwned then
			table.insert(filtered, {
				name = name,
				owned = getOwnedNumber(data.globalNumberOwned),
				display = data.globalNumberOwned
			})
		end
	end

	sortCosmetics(filtered, order)
	return formatCosmeticList(filtered)
end

return p