Module:NPC

--[=[-- Lua script to get NPC Detail

getNPC: get generic NPC infomation Parameters: 1: NPC = NPC name 2: Include Name/Text Link (Optional) N = NPC Name with image F = File Name E = NPC Name only L = NPC Name Link only X = NPC/Display Name with image D = Descriptive/Display Name (Text Only) T = Text Only NPC Name A = Alternate Text (Parameter 3) Z = File Name ONLY 3: Alternate Text (Optional) 4: Size in pixels (Optional) default 25 5: Event Version (Optional) -- The list of available NPCs is kept in the module data -- Unknown is returned if NPC is not in data.

getNPCList: get a List of NPC in horizontal format Parameters: None

getFirstUse: get the fixed format text about first LTE using the NPC Parameters: 1:	NPC Name --]=]--

local p = {}

local npcData = mw.loadData( 'Module:NPC/data' ) local util = require('Module:Utility') local EC = require("Module:EC").getEC local TOK = require("Module:CharacterToken").getToken

local function getRewards(item) local char = mw.text.split(item, "%s*%$%s*") local rewards = "" local space = ""

for idx = 1, #char do		if char[idx]:match("^EC") then rewards = rewards .. space .. EC({char[idx]}) space = ", " else rewards = rewards .. space .. TOK({char[idx], nil, "B"}) space = ", " end end

return rewards end

function p.getNPC(frame) local tArgs = util.getArgs(frame) local keyName = tArgs[1] or "" local inclName = tArgs[2] or "" local altText = tArgs[3] or "" local size    = tArgs[4] or "" local version = string.lower(tArgs[5] or "")

if inclName ~= "" then if not inclName:match("^[NEATDXGLFZ]$") then inclName = "N" end end

if size == "" then size = "25" end

local outText = "" local tFile = "work_in_progress" local tSize = ".png|x" .. size .. "px|" local tDisp = keyName local tLink = "Work In Progress"

if npcData[keyName] then local npcItem = npcData[keyName] tFile = "npc-" .. string.lower(keyName) tSize = ".png|x" .. size .. "px|" tDisp = keyName tLink = keyName if npcItem["N"] then tLink = npcItem["N"] tDisp = tLink end if npcItem["D"] then tDisp = npcItem["D"] end

if npcItem["I"] then tFile = npcItem["I"] end if npcItem["L"] then tLink = npcItem["L"] end if inclName == "G" then version = npcItem["G"] end

if version ~= "" and npcItem["V"] and npcItem["V"][version] then local text = ""

text = npcItem["V"][version][2] if text and text ~= "" then tLink = "Gold Coins" else tLink = keyName end text = npcItem["V"][version][1] if inclName == "F" then if npcItem["V"][version][4] and npcItem["V"][version][4] ~= "" then tDisp = npcItem["V"][version][4] else tDisp = ":Category:" .. text .. "|" .. text end elseif text and text ~= "" then tDisp = text .. " themed " .. tLink end

if text and text == "Disney Magic Kingdoms" then tFile = "npc-" .. string.lower(tLink) else tFile = "npc-" .. string.lower(tLink) .. "-" .. (npcItem["V"][version][3] or version) end end end

local nameLink = "" local bracket = ""

if inclName == "N" then nameLink = "" .. tLink .. " ("       bracket = ")" elseif inclName == "L" then nameLink = "" .. tDisp       end        nameLink = nameLink .. "" elseif inclName == "X" then nameLink = "" .. tDisp       end        nameLink = nameLink .. " ("        bracket = ")" elseif inclName == "A" then nameLink = "" .. altText       end        nameLink = nameLink .. " ("        bracket = ")" elseif inclName == "E" then nameLink = tLink elseif inclName == "T" then nameLink = tLink .. " ("       bracket = ")" elseif inclName == "D" then nameLink = tDisp .. " ("       bracket = ")" elseif inclName == "F" then return tFile .. ".png|" .. tDisp .. " themed gold trophy\n" elseif inclName == "Z" then return tFile .. ".png" end

if inclName ~= "L" then if inclName ~= "E" then nameLink = nameLink .. ""       .. tFile .. tSize .. tDisp        .. "" .. bracket end end return nameLink end

function p.getEventNPC(frame) local tArgs = util.getArgs(frame) local eName = mw.ustring.lower(tArgs[1] or "") local eType = tArgs[2] or ""

local tIndex = {}

for npcName, v in pairs(npcData) do       if eType == "T" then if v["TC"] then for oneName in mw.text.gsplit(v["TC"], "%s*$%s*") do                   if oneName == eName then table.insert(tIndex, {string.lower(npcName), npcName}) end end end elseif v["E"] then for oneName in mw.text.gsplit(v["E"], "%s*$%s*") do               if oneName == eName then table.insert(tIndex, {string.lower(npcName), npcName}) end end end end

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

local outText = "" local space = ""

for _, name in ipairs(tIndex) do       outText = outText .. space .. "" .. name[2] .. " " space = " " end

return outText end

function p.getFirstUse(frame) local tArgs = util.getArgs(frame)

local npcName = tArgs[1] or "" npcName = npcName:gsub("^%s*(.-)%s*$", "%1") or ''

local outText = {}

if npcData[npcName] then local npcItem = npcData[npcName]

-- assemble output string if LTE name present if npcItem["E"] then local eName = mw.text.split(npcItem["E"], "%s*$%s*") if eName[1] == "0" then return "" end outText = { "* ",			   	npcItem["D"] or npcName,			    	" first appeared during the ", require("Module:LTEvent").getLTE({eName[1], "N", "D"}), ".\n", }		end end return table.concat(outText) end

function p.getNPCinfo(frame) local tArgs = util.getArgs(frame) local npcName = tArgs[1] or mw.title.getCurrentTitle.text local meData = mw.loadData("Module:MiniEvent/data") local lte = require("Module:LTEvent").getLTE local VL = mw.ext.VariablesLua if npcName == "Gold Trophies" then VL.vardefine("file", p.getNPC({npcName, "G"})) end

local latest = "" local lData = {} local lRewards = nil local events = {} local eventUse = {} for meName, data in pairs(meData) do		if (data.NPC and data.NPC == npcName) or (data.NPC2 and data.NPC2 == npcName) then if not eventUse[meName] then table.insert(events, "" .. data.Name .. " Mini Event" .. "") end eventUse[meName] = true

-- If the latest has no rewards but earlier mini-event has rewards -- and earlier mini-event is processed after the latest mini-event if not lRewards then lRewards = data.Rewards end

for x, xData in pairs(data) do				if type(x) == "number" then if xData[1] > latest then latest = xData[1] lData = xData lRewards = data.Rewards or lRewards end end end end end

-- undefine variables VL.vardefine("ltenpc") VL.vardefine("eventlist") VL.vardefine("spawnrate") VL.vardefine("rewards") if tArgs["rewards"] then VL.vardefine("eventrewards", getRewards(tArgs["rewards"])) end if tArgs["rewards 2"] then VL.vardefine("eventrewards2", getRewards(tArgs["rewards 2"])) end

if npcData[npcName] then local npcItem = npcData[npcName] -- Check if NPC is from LTE if npcItem.E then local eName = mw.text.split(npcItem.E, "%s*$%s*") VL.vardefine("ltenpc", lte({eName[1], "N", "K"})) end -- NPC image file name VL.vardefine("npcfile", (npcItem.I or ("npc-" .. npcName:lower:gsub(" ", "_")))					.. ".png") if npcItem.T then VL.vardefine("npctokens", TOK({npcName .. ",1$" .. npcName .. ",2", "I"})) end end

-- check if NPS was used in mini-event(s) if events[1] then table.sort(events) VL.vardefine("eventlist", table.concat(events, " ")) end

-- check if last event had re-spawn if lData[6] then local durUnit = "hours" if lData[8] and lData[8] == "m" then durUnit = "minutes" end -- Spawn Rate for last mini-event VL.vardefine("spawnrate", table.concat({lData[6], "every", lData[7], durUnit}, " ")) end

-- Rewards for last mini-event (or the last time rewards specified) if lRewards then VL.vardefine("rewards", getRewards(lRewards)) end

return "" end return p