Disney Magic Kingdoms Wiki
Disney Magic Kingdoms Wiki

Description

This module is the Lua back-end for {{Token}}, to display character token icons.


Usage

{{#invoke:CharacterToken|getToken|<parameter list>}}

For parameter details see Template:Token/doc


--[=[
Lua script replacement for wikitext template

    [[File:t-{{{1}}}{{#if:{{{2|}}}|-{{{2}}}|}}.png
    |x25px|{{{3}}} Token|link={{{3}}} Token]]

Parameters:
    1: Token = (character,token type,count)
        count is optional
        Separate multiple items with dollar sign "$"
        if the item is blank or "&", line break "<br />"
            will be inserted instead.
        if the string "multi" is input then lists all tokens
        	of rarity specified in parameter 2
    2: Size in pixels (or token rarity in case of "multi")
        optional, defaults to 25
        If equal to "N", includes the token Name Link also
        If equal to "L", includes the token Name Link only
    3: Option
    	For normal, if non-blank, will automatically insert
    		line breaks "<br />" after every 4 tokens. Any
    		specified line breaks ("&") are ignored.
    	For "multi", if non-blank, specifies the string to
    		strip from the end of the token name.
    4: Skip
    	Name of the token to be skipped or  removed from displayed
    	list, on pages other than token pages.

The list of available tokens is kept in the module data

Unknown Token is returned if Token Type is not in data.
--]=]

local p = {}

local util = require("Module:Utility")

---[==[ For testing purposes
local tokenData = mw.loadData('Module:CharacterToken/data')
--[===[]==]
local tokenData = {
-- Toy Story --
    ["ip-ts"] = {
    	["F"] = "ip-toy story",
    	["0"] = {"Pixar Ball", 0}
    },
    ["jessie"] = {
    	["2"] = {"Lasso", 2},
    	["3"] = {"Jessie Ears Hat", 3}
    },
    ["sarge"] = {
    	["2"] = {"Sarge's Bucket", 2},
    	["3"] = {"Sarge Ears Hat", 3}
    },
    ["woody"] = {
    	["2"] = {"Sheriff Star", 2},
    	["3"] = {"Woody Ears Hat", 2}
    },
    ["the child"] = {
    	["2"] = {"Grogu Hover Pram", 104},
    	["3"] = {"Grogu Ears Hat", 104}
    },
}
--]===]
local rarityType = {
	["collection"]		= 0,
	["common"]			= 1,
	["uncommon"]		= 2,
	["rare"]			= 3,
	["epic"]			= 4,
	["legendary"]		= 5,
	["fabric"]			= 6,
	["blueprint"]		= 7,
	["common relic"]	= 81,
	["uncommon relic"]	= 82,
	["rare relic"]		= 83,
	["event"]			= 9,
	["npc"]				= 10,
}

-- Display Token Icon(s)
local function tokenIcon(token, size, addbreaks, skip)
	local outType = ""

	if not tonumber(size) then
		outType = size
		size = 25
	end

	local autoBreaks = false
	if addbreaks and addbreaks ~= "" then
		autoBreaks = true
	end

    token = mw.ustring.gsub(mw.ustring.lower(token),
                "^%s*(.-)%s*$", "%1") or ''

    local tAll = {}
    local space = ""

	-- get the current page/token name
	local pagename = mw.title.getCurrentTitle().text:match("(.+)%s+Token$") or ""
	-- use the name of token to skip if specified
	if skip then pagename = skip end
	-- count of icons on 1 row to insert line break
	local iconCount = 0

    for tok in mw.text.gsplit(token, "%s*[!%$]%s*") do
        if tok == "" or tok == "&" then
        	if not autoBreaks then
				space = "<br />"
			end
        else
        	local addThis = false
		    local tToken = {space, "", "", "", "",
		    				"", "[[File:", "t-", "", "",
		    				"", ".png|x", size, "px|", "",
		    				" Token|link=", "", " Token]]", "", "", 
		    				"", ""
		    				}

            local cName, tokenType, count = mw.ustring.match(tok, 
                                "^([^,]-)%s*,%s*(%d+)%s*,*%s*([\/%d%?]*)$")
    
            if not cName then
                cName, count = mw.ustring.match(tok, "^([^,]-)%s*,%s*,*%s*([\/%d]*)$")
                if not cName then cName = tok end
            end
    
            if not tokenType or tokenType == "" then
                tokenType = "0"
            end
            
            if tokenData[cName] and tokenData[cName][tokenType] then
            	local tokName = tokenData[cName][tokenType][1]
        		if pagename ~= tokName then
        			addThis = true -- not current token so add to output
					if outType == "N" then
						tToken[ 2] = "[["
						tToken[ 3] = tokName
						tToken[ 4] = " Token|"
						tToken[ 5] = tokName
						tToken[ 6] = "]] ("
						tToken[19] = ")"
					end
					if outType == "L" then
						tToken[ 7] = "[["
						tToken[ 8] = tokName
						tToken[12] = " Token]]"
						tToken[13] = ""
						tToken[14] = ""
						tToken[16] = ""
						tToken[18] = ""
					end
					if outType ~= "L" then
						tToken[ 9] = mw.ustring.gsub(tokenData[cName].F or cName, " ", "_")
						if tokenType ~= "0" then
							tToken[10] = "-"
							tToken[11] = tokenType
						end
						tToken[15] = tokName
						tToken[17] = tokName
					end
				end
        	else
				addThis = true
				tToken[ 8] = ""
				tToken[ 9] = "work_in_progress"
				tToken[15] = mw.ustring.upper(cName)
				tToken[17] = "Work In Progress"
        	end
        
            if count and count ~= "" then
            	if size == "50" or outType == "L" then
            	tToken[20] = "'''"
            	tToken[22] = "'''"
            	end
            	tToken[21] = count
            end

			if addThis then
				if autoBreaks and iconCount >= 3 then
					tToken[ 1] = "<br />"
					iconCount = 0
				end
				iconCount = iconCount + 1

				table.insert(tAll, table.concat(tToken))
				if outType == "N" or (count and count ~= "" and size ~= "50") then
					space = ", " 
				else 
					space = " " 
				end
			end
        end
    end
    return table.concat(tAll)
end

-- Dislplay list if token links
local function tokenList(rarity, stripend)
	local nRarity = tonumber(rarity)
	if not nRarity then
		if rarityType[string.lower(rarity or "")] then
			nRarity = rarityType[string.lower(rarity)]
		end
	end

	local tAll = {}

	for _, data in pairs(tokenData) do
		for _, tok in pairs(data) do
			if type(tok) == "table" then
				if not nRarity or (tok[2] and nRarity == math.mod(tok[2], 100)) then
					local sortkey = tok[1]:lower():gsub("[\"']", "")
					table.insert(tAll, {sortkey, tok[1], tok[2]})
				end
			end
		end
	end

    table.sort(tAll, function(a,b) return a[1] < b[1] end)

	local tOut = {}
	local space = ""

	for _, data in ipairs(tAll) do
		local limited = ""
		if data[3] and math.floor(data[3]/100) == 1 then
			limited = "''"
		end
		tLink = {space, limited, "[[", "", " Token", "|", "", "]]", limited}
		tLink[4] = data[2]
		local name = data[2]
		for strip in mw.text.gsplit(stripend, "%s*%$%s*") do
			name = name:gsub(" ".. strip .. "$", "")
		end
		
		tLink[7] = name
		table.insert(tOut, table.concat(tLink))
		space = " '''·''' "
	end
	return table.concat(tOut)
end

-- External Invokable Functions
function p.getToken(frame)
    local tArgs = util.getArgs(frame)

    local token = tArgs[1] or ""
    -- size is rarity for multi token list
    local size = tArgs[2]
    -- for multi token list the string to strip from end of name
    -- otherwise, if present then automatically add line breaks
    local stripend = tArgs[3] or ""
    -- Name of token to remove on non-token pages
    local skip = tArgs[4]

	if token:lower():match("^multi") then
		return tokenList(size, stripend)
	else
		return tokenIcon(token, size, stripend, skip)
	end
end

function p.tokenType()
	local token = mw.title.getCurrentTitle().prefixedText
	token = token:gsub(" Token", "")
	
	local nRarity = ""

	for _, data in pairs(tokenData) do
		for _, tok in pairs(data) do
			if type(tok) == "table" then
				if tok[1] == token then
					nRarity = tok[2]
				end
			end
		end
	end
	
	if nRarity ~= "" and math.floor(nRarity/100) == 1 then
		nRarity = math.mod(nRarity, 100)
	end
	
	for rar, numb in pairs(rarityType) do
		if numb == nRarity then
			if rar:match("relic") or rar == "blueprint" then
				return "ae"
			end
			return rar
		end
	end
	
	return "Unknown"
end

return p