诚邀有MediaWiki开发维护经验的朋友和我们一起共建英雄联盟中文Wiki平台,期待你的加入! 甜咖啡(QQ:815798492)(微信:glf101012) 请备注来意,并附带您的GitHub/Gitee主页或作品链接。

模块:Profile-Icons/V1

来自英雄联盟维基百科

可在模块:Profile-Icons/V1/doc创建此模块的帮助文档

-- <pre>

local lib       = require('Module:Feature')
local FN        = require('Module:Filename') --fn is what its called in every other module; pls dont change it
local getter    = require("Module:Profile-Icons/V1/getter")
local builder   = require("Module:SimpleHTMLBuilder")

local availability = {
	['Available'] ="[[File:Available Icon.png|24px|link=]] '''Available'''",
	['Unreleased'] = "[[File:Circle.svg|24px|link=]] '''Unreleased'''",
	['Unavailable'] = "[[File:Unavailable Icon.png|24px|link=]] '''Unavailable'''",
	['Limited'] = "'''Limited'''",
	['Unlocked'] = "[[File:Open Lock Icon.png|24px]] '''Unlocked'''",
	['Unknown'] = "'''Unknown'''"
}

local rarity = {
	[0] = "'''Others'''",
	[1] = "[[File:Standard Skin.png|20px|link=]] '''Rare'''", --size different because this image file has no empty border
	[2] = "[[File:Epic Skin.png|20px|link=]] '''Epic'''",
	[3] = "[[File:Legendary Skin.png|20px|link=]] '''Legendary'''",
	[4] = "[[File:Hextech Skin.png|20px|link=]] '''Mythic'''",
	[5] = "[[File:Ultimate Skin.png|20px|link=]] '''Ultimate'''"
}

local function getIconById(id)
	local filename = FN.profileiconV1({id});
	if (filename == nil) then return userError("no such icon with id "..id) end

	local root = builder.create('div')
		:addClass('profile-icons-v1')
		:attr("data-id", id)
		:attr("data-region", "riot");
	root:wikitext("[[file:"..FN.profileiconV1({id}).."|128px]]");
	return tostring(root);
end


local function getIdByTitle(title)
	local index = mw.loadData("Module:Profile-Icons/V1/index/title")
	local ids = index[title]
	if (ids == nil) then return nil end
	return ids[1]-- just return first ¯\_(ツ)_/¯
end

local function getIconByTitle(title)
	local id = getIdByTitle(title)
	if (id == nil) then return userError('no such icon with title "'..title..'"') end
	return getIconById(id);
end

local function getIdFromTitle(title)
	if tonumber(title) ~= nil then
		return tonumber(title) -- can convert, id => just convert to number
	else
		local index = mw.loadData("Module:Profile-Icons/V1/index/title")
		local ids = index[title]
		if (ids == nil) then return userError("") end
		local smallestId = math.huge -- lua way of saying "infinity"
		for _, id in ipairs(index[title]) do
			if id < smallestId then
				smallestId = id
			end
		end
		return smallestId --always returns smallest Id, not the perfect solution but this is a predictable behaviour so i guess its fine
	end
end

local function getIconSets(data)
	local sets = {}
	local esport = (data.esportsTeam ~= nil)
	for i, set in ipairs(data.sets) do
		esport = (set ~= "Esports Teams") and esport
		table.insert(sets, set)
	end
	if esport then table.insert(sets, "Esports Teams") end
	return sets;
end

local function tooltip(id, region)
	local data = mw.loadJsonData("Module:Profile-Icons/V1/icon/"..id..".json")
	-- local function localget(datatype) -- necessary, because some data fields are a bit more complicated (looking at you, skin sets >:/  )
	-- 	return getter[datatype](id, region)
	-- end
	
    local root = builder.create("div")
    	:css("--padding", "1em")
    	:css("--secondary-color", "#f1e6d0")
    	:css("--primary-color", "#ceb57c")
    	:addClass('blue-tooltip') --TODO: remove this, this is only for debugging
		:css("width", "25em") --we use relative units just cuz it web standard (actually accessibility)
		:css("display", "flex")
		:css("flex-direction", "column")
		:css("align-items", "center")
		:css("padding", "var(--padding)")
		:css("gap", "var(--padding)")
		
	local divider = '<hr style="width: 100%; margin: 0; border-color: var(--secondary-color)">'

	-- Availability, Legacy-ness and Rarity
	local header = root:tag("div")
		:css("display", "grid")
		:css("align-self", "stretch")
		-- :css("text-align", "center")
		:css("font-family", "BeaufortLoL")
		:css("text-transform", "uppercase")
		:css("color", "var(--secondary-color)")
		:css("grid-auto-columns", "1fr")
		:css("grid-auto-flow", "column");
	
	--header	
	do
		local style = {
			["display"] = "flex",
		    ["align-items"] = "center",
		    ["justify-content"] = "center",
		    ["column-gap"] = "0.5em"
		}
		
		header:tag("span"):css(style):wikitext(
			availability[(data.availability and data.availability[region]) or "Available"]
			--availability[data.availability[region] or "Available"] --TODO: Available is default, because meta fields dont work rn. Fix later
		):done()
	
		if data.isLegacy then
			header:tag("span"):css(style):wikitext("[[File:Legacy skin.png|24px|link=]] Legacy"):done()
		end
		
		header:tag("span"):css(style):wikitext(
			rarity[(data.rarities[region] and data.rarities[region].rarity) or 0]
		):done()
	
		header:done()
		root:wikitext(divider)
	end
	
	
	-- Icon (image)
	root:tag("div")
		:css("box-shadow", "#0a1827 0px 0px 25px 15px")
		:wikitext("[[File:"..FN.profileiconV1({id}).."|96px|border]]")
		:done()
		
	-- title
	root:tag("span")
		:css("font-size", "1.5em")
		:css("font-family", "BeaufortLoL")
		:css("text-transform", "uppercase")
		:css("color", "var(--secondary-color)")
		:css("text-align", "center")
		:wikitext(data.title)
		:done()
		
	-- Description of icon
	root:tag("span")
		:css("color", "var(--secondary-color)")
		:css("text-align", "center")
		:wikitext((data.descriptions[region] and data.descriptions[region].description) or "😅")
		:done();
	
	root:wikitext(divider)
	
	
	-- esports
	if data.esportsTeam or data.esportsRegion or data.esportsEvent then
		local style = {
			["font-family"] = "BeaufortLoL",
			["text-transform"] = "uppercase",
			["color"] = "var(--secondary-color)"
		}
		local container = root:tag("div")
			:css("display", "grid")
			:css("grid-template-columns", "1fr 2fr")
			:css("align-self", "stretch")
			:css("text-align", "center")

		if data.esportsTeam then container
			:tag("span"):css(style):wikitext("team:"):done()
			:tag("span"):wikitext(data.esportsTeam):done()
		end
		if data.esportsRegion then container
			:tag("span"):css(style):wikitext("region:"):done()
			:tag("span"):wikitext(data.esportsRegion):done()
		end
		if data.esportsEvent then container
			:tag("span"):css(style):wikitext("event:"):done()
			:tag("span"):wikitext(data.esportsEvent):done()
		end
		
		container:done():wikitext(divider)
	end
	
	--'Account Creation', 'Bundle', 'Code', 'Event Shop', 'Merch Store', 'Mission', 'Riot', 'Store', 'Unavailable', 'Unknown'
	local footer = root:tag("div")
		:css("display", "grid")
		:css("grid-template-columns", "repeat(2, auto 1fr)")
		:css("align-self", "stretch")
		:css("align-items", "center")
		:css("row-gap", "var(--padding)")
		:css("column-gap", "0.5em")
	
	footer
		:tag("span"):css("text-align", "center"):wikitext("[[File:Release.png|24px|link=]]"):done()
		:tag("span"):wikitext(data.yearReleased):done()
	
	footer
		:tag("span"):css("text-align", "center"):wikitext("[[File:Availability.png|28px|link=]]"):done()
		:tag("span"):wikitext('Source'):done(); --TODO: not working, meta not json / localget('source')
	
	--row 2
	local set = getIconSets(data)
	if table.getn(set) > 0 then footer
		:tag("span"):css("text-align", "center"):wikitext("[[File:Set piece.png|24px|link=]]"):done()
		:tag("span"):wikitext(table.concat(set, ', ')):done()
	end
	
	footer:tag("span")
		:css("font-family", "BeaufortLoL")
		:css("text-transform", "uppercase")
		:css("color", "var(--primary-color)")
		:css("text-align", "center")
		:css("font-weight", "500")
		:wikitext("ID")
	:done():tag("span"):wikitext(id):done()
	
	return tostring(root)
end



local p = {}

function p.getIdFromTitle(frame)
    local args = lib.frameArguments(frame)
    args['title'] = args['title'] or args[1]
    
	return getIdFromTitle(args['title'])
end

--questionable function
function p.get(frame)
    local args = lib.frameArguments(frame)

    args['icon']     = args['icon']     or args[1]
    args['datatype'] = args['datatype'] or args[2]
    args['output']   = args['output']   or args[3] or nil
    args['region']   = args['region']	or args[4] or 'riot' --riot is the default value for all regions, unless overwritten by localized value
    
    -- checks, whether args/icon is an id or a title. 
	if tonumber(args['icon']) == nil then
		args['icon'] = getIdFromTitle(args['icon']) -- cant convert, string => get id from title
	else
		args['icon'] = tonumber(args['icon']) -- can convert, id => just convert to number
	end
    
    -- TODO: if icon is string and not id: get ID from string
    local result = getter[args['datatype']](args['icon'], args['region'])

    if args['output'] ~= nil and type(result) == "table" then
        if(args["index"]) then
            local i = tonumber(args["index"])
            if(result[i]) then 
                return frame:preprocess(result[i])
            else
                return ""
            end
        end
        if args['output'] == "csv" then
            return lib.tbl_concat{result}
        elseif args['output'] == "custom" then 
            return frame:preprocess(lib.tbl_concat{result, prepend = args['prepend'], append = args['append'], separator = args['separator'], index = args["index"]})
        elseif args['output'] == "template" then 
            return frame:preprocess(lib.tbl_concat{result, prepend = "{{" .. (args['t_name'] or "ii") .. "|", append = "}}", separator = args['separator']})
        end
    elseif type(result) == "string" then
        return frame:preprocess(result)
    else
        return result
    end
end

function p.getIconById(frame)
	local args = lib.frameArguments(frame);
	if (args[1] == nil) then return userError("1 argument required, but only 0 present.") end;
	local id = tonumber(args[1]);
	return frame:preprocess(getIconById(id));
end

function p.getIconByTitle(frame)
	local args = lib.frameArguments(frame);
	if (args[1] == nil) then return userError("1 argument required, but only 0 present.") end;
	local title = args[1];
	return frame:preprocess(getIconByTitle(title));
end

function p.getIconList(frame)
	local args = lib.frameArguments(frame)
	-- local builder = require("Module:SimpleHTMLBuilder")
    local index = mw.loadJsonData('Module:Profile-Icons/V1/index.json')
    local root = builder.create("div")
		:css("display", "flex")
		:css("flex-wrap", "wrap")
		:css("gap", "1em")
		:css("justify-content", "center");
		
    for id, _ in pairs(index) do
		root:wikitext(getIconById(id));
    end
    return frame:preprocess(tostring(root))
end

--local result = getter[args['datatype']](args['icon'], args['region'])
function p.tooltip(frame)
	local args = lib.frameArguments(frame)
	-- local iconId = getIdFromTitle(args[1])
	if (args[1] == nil) then return userError("1 argument required, but only 0 present.") end;
	local id = tonumber(args[1]);
	if (id == nil) then return userError("invalid id") end;
	
	local region = args['region'] or args[2] or 'riot'
	
	-- normal output
    --return frame:preprocess(tostring(root))
    return tooltip(id, region)
    --return frame:preprocess(tooltip(id, region))
end

return p

--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
-- EVERYTHING AFTER THIS IS COPY/PASTED FROM Module:IconData AND IS NOT WORKING. JUST FOR REFERENCE WHILE IM STILL CODING THIS
-- thank you, TheTimebreaker
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------


-- function p.exists(frame)
--     local args = lib.frameArguments(frame)
	
-- 	if getIconData(args[1]) then
--     	return true
-- 	end
-- 	return false
-- end


-- function p.idCompare()
--     local avatarList = builder.create('ul')

--     local avatars = {}
--     local idtable = {}
    
--     for x in pairs(iconData) do
--         table.insert(avatars, x)
--     end
   
--     function comp(a, b)
--         if iconData[a].id == nil then a = -1 else a = iconData[a].id end
--         if iconData[b].id == nil then b = -1 else b = iconData[b].id end
--         if a < b then
--             return true
--         end
--     end
--     table.sort(avatars, comp)
    
--     s=""
--     local avatarListItems = builder.create('ol')
--     for _, j in pairs(avatars) do
--         --s = s .. '* [[:File:ProfileIcon' .. string.format("%04d", tonumber(iconData[j].id or 99999)) .. '.png]]\n'
    
--         avatarListItems
--             :tag('li')
--             :wikitext('[[File:ProfileIcon' .. string.format("%04d", tonumber(iconData[j].id or 99999)) .. '.png|80px]] == [[File:' .. mw.ustring.gsub(j or "", ':', '-') .. ' profileicon.png|80px]]')
--         :done()
--     end
--     avatarList
--         :node(avatarListItems)
    
--     return avatarList
-- end


-- function p.getSeticons(frame)
--     local args = lib.frameArguments(frame)
        
--     local avatarList = builder.create('ul')
--     local avatars = {}
--     local result = false
    
--     for x in pairs(iconData) do
--         table.insert(avatars, x)
--     end
--     table.sort(avatars)

--     for _, avatarname in pairs(avatars) do
--         local hit = false
--         local t = iconData[avatarname]
        
--         if (t.set ~= nil) then
--             for _, subset in pairs(t.set) do
--                 if subset == args[1] then
--                     hit = true
--                     result = true
--                 end
--             end
--         end
--         if hit == true then
--             avatarList
--                 :tag('li')
--                     :tag('div')
--                         :addClass('avatar-icon')
--                         :attr('data-icon', avatarname)
--                         :wikitext('[[File:' .. FN.profileicon{avatarname} .. '|20px|alt=' .. avatarname .. '|link=]] ' .. avatarname)
--                     :done()
--                 :done()
--                 :newline()
--         end
--     end
    
--     if result == false then 
--         avatarList
--             :tag('li')
--                 :wikitext('No match found for ' .. args[1] .. '.')
--             :done()
--     end
    
--     return avatarList
-- end



-- function p.getEsportslist(frame)
--     local avatarList = builder.create('ol')
    
--     avatarList
--         :addClass('champion_roster')
--         :newline()
    
--     local avatars = {}
    
--     for x in pairs(iconData) do
--         table.insert(avatars, x)
--     end
--     table.sort(avatars)
    
--     esportsbyyear (avatars, avatarList, 2018)
--     avatarlist
--         :wikitext(';' .. v)
--         :newline()
--     esportsbyyear (avatars, avatarList, 2017)
--     avatarlist
--         :wikitext(';' .. v)
--         :newline()
--     esportsbyyear (avatars, avatarList, 2016)
--     avatarlist
--         :wikitext(';' .. v)
--         :newline()
--     esportsbyyear (avatars, avatarList, 2015)
--     avatarlist
--         :wikitext(';' .. v)
--         :newline()
--     esportsbyyear (avatars, avatarList, 2014)
--     avatarlist
--         :wikitext(';' .. v)
--         :newline()
--     esportsbyyear (avatars, avatarList, 2013)
--     avatarlist
--         :wikitext(';' .. v)
--         :newline()
--     esportsbyyear (avatars, avatarList, 2012)
--     avatarlist
--         :wikitext(';' .. v)
--         :newline()
    
--     return avatarList
-- end

-- function p.getEsportsyear(frame)
--     local args = lib.frameArguments(frame)
        
--     avatarList = builder.create('ol')
    
--     avatarList
--         :addClass('champion_roster')
--         :newline()
    
--     local avatars = {}
    
--     for x in pairs(iconData) do
--         table.insert(avatars, x)
--     end
--     table.sort(avatars)
    
--     esportsbyyear(avatars, avatarList, tonumber(args[1]))
    
--     return avatarList
-- end

-- function esportsbyyear(avatars, avatarlist, v)
--     for _, avatarname in pairs(avatars) do
--         local current = iconData[avatarname]
        
--         local hash = false
--         if type(current.set) == "table" then
--             for _, setname in pairs(current.set) do
--                 if setname == "eSports" then
--                     hash = true
--                 end
--             end
--         end
        
--         if hash == true then
--             if current.release == v then
                
--                 local region = ""
--                 if current.country ~= nil then
--                     region = current.country .. ", " .. getRegion(current.country)
--                 end
                
--                 local event = ""
--                 if current.tournament ~= nil then
--                     event = current.tournament .. ", " .. getEvent(current.tournament)
--                 end
                
--                 avatarList
--                     :tag('li')
--                         :tag('div')
--                             :addClass('esports-icon')
--                             :attr('data-icon', avatarname)
--                             :attr('data-search', avatarname .. lib.ternary(current.nicknames ~= nil, ", " .. tostring(current.nicknames), ""))
--                             :attr('data-region', region)
--                             :attr('data-tournament', event)
--                             :wikitext('[[File:' .. FN.profileicon{avatarname} .. '|48px|alt=' .. avatarname .. ']]')
--                         :done()
--                     :done()
--                     :newline()
--             else
--                 --do nothing
--             end    
--         else
--             --do nothing
--         end
--     end

--     return avatarList
    
-- end

-- function getRegion(regionname)
--     local regionlist = {
--         {"US",    "NA"},
--         {"DE",    "EU"},
--         {"DK",    "EU"},
--         {"ES",    "EU"},
--         {"FR",    "EU"},
--         {"LT",    "EU"},
--         {"SW",    "EU"},
--         {"UA",    "EU"},
--         {"UK",    "EU"},
--         {"CO",    "LAN"},
--         {"CR",    "LAN"},
--         {"MX",    "LAN"},
--         {"PE",    "LAN"},
--         {"AR",    "LAS"},
--         {"CL",    "LAS"},
--         {"AU",    "OCE"},
--         {"ID",    "SEA"},
--         {"HK",    "SEA"},
--         {"MY",    "SEA"},
--         {"PH",    "SEA"},
--         {"SG",    "SEA"},
--         {"TH",    "SEA"},
--         {"TW",    "SEA"},
--         {"VN",    "SEA"}
--     }
    
--     for _, value in ipairs(regionlist) do 
--         if regionname == value[1] then
--             return value[2]
--         end
--     end

--     return "undefined"
-- end

-- function getEvent(eventname)
--     local eventslist = {
--         {"MSI",         "International"},
--         {"Worlds",      "International"},
--         {"All-Star",    "International"},
--         {"Rift Rivals", "International"},
--         {"NALCS",       "Regional"},
--         {"EULCS",       "Regional"},
--         {"CBLOL",       "Regional"},
--         {"CLS",         "Regional"},
--         {"CNC",         "Regional"},
--         {"GPL",         "Regional"},
--         {"LAN",         "Regional"},
--         {"LCK",         "Regional"},
--         {"LCL",         "Regional"},
--         {"LJL",         "Regional"},
--         {"LMS",         "Regional"},
--         {"LPL",         "Regional"},
--         {"LLN",         "Regional"},
--         {"OPL",         "Regional"},
--         {"TCL",         "Regional"},
--         {"VCS",         "Regional"},
--         {"OGN",         "Special"},
--         {"SLTV",        "Special"}
--     }
    
--     for _, value in ipairs(eventslist) do 
--         if eventname == value[1] then
--             return value[2]
--         end
--     end

--     return "undefined"
-- end

-- return p

-- </pre>
-- [[Category:Lua]]