Module:CharacterToken

--[=[ --LUA script replacement for wikitext template -- --   t-.png -- -- --Parameters: --   1: Token = (character,token type,count) --           count is optional --           Separate multiple items with exclamation mark "!" --           if the item is blank or "&", line break " " --                 will be inserted instead. --   2: Size in pixels --       optional, defaults to 25 -- --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")

---[==[ 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"] = {"The Child Hover Pram", 104}, ["3"] = {"The Child Ears Hat", 104} }, } --]===] local rarityType = { ["collection"]	= 0, ["common"]		= 1, ["uncommon"]	= 2, ["rare"]		= 3, ["epic"]		= 4, ["legendary"]	= 5, ["fabric"]		= 6, ["blueprint"]	= 7, ["relic"]		= 8, ["event"]		= 9, ["npc"]			= 10, }

-- Display Token Icon(s) local function tokenIcon(token, size) if not size or size == "" then size = "25" end token = mw.ustring.gsub(mw.ustring.lower(token),               "^%s*(.-)%s*$", "%1") or ''

local tAll = {} local space = ""

for tok in mw.text.gsplit(token, "%s*[!%$]%s*") do       if tok == "" or tok == "&" then space = " " else local tToken = {space, "", "t-", "", "",		   				"", ".png", "", "", "", }

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 tToken[ 4] = mw.ustring.gsub(tokenData[cName].F or cName, " ", "_") if tokenType ~= "0" then tToken[ 5] = "-" tToken[ 6] = tokenType end tToken[10] = tokenData[cName][tokenType][1] tToken[14] = tokenData[cName][tokenType][1] else tToken[ 3] = "" tToken[ 4] = "work_in_progress" tToken[10] = cName tToken[12] = tokenType or "" tToken[14] = "Work In Progress" tToken[15] = "" end if count and count ~= "" then tToken[16] = "'''" tToken[17] = count tToken[18] = "'''" end

table.insert(tAll, table.concat(tToken)) space = " " 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, "", "", "", 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 local stripend = tArgs[3] or ""

local multi, rarity = token:match("^(multi)(.*)$")

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

return p

-- --