Module:Building

--[==[-- -- Lua script to get Building Information -- building: get Building data based on input parameters 1 = Building Name 2 = Enchantment Number 3 = Event Name / IP Short Name

Unknown is returned if building is not in data. --]==]

local p = {}

---[=[ For testing remove 1 '-' from start of line local buildData = mw.loadData('Module:Building/data') --[==[]=] local buildData = { ["Under the Sea"] = { ["W"] = true, ["A"] = "Jamming with a Hot Crustacean Band", ["R"] = { ["magic"] = {"4h", 12, 155, "IP-TLM", "Sebastian,3", "King triton,2", "Ariel,3", "Ursula,2", T=true}, ["tlm"] = {"4h", 23, 32, "", "", "", "", "", true}, ["wir"] = {"4h", 12, 50, "IP-TLM", "Sebastian,2", "King triton,3", "Ariel,2", "Ursula,3"}, }   }, } --]==]

local EC = require("Module:EC").getEC local TOK = require("Module:CharacterToken").getToken local NPC = require("Module:NPC").getNPC local lte = require("Module:LTEvent").getLTE local util = require("Module:Utility")

function oneBuilding(buildName, enchLvl, event, outType, pagename) buildName = buildName:gsub("^%s*(.-)%s*$", "%1") or '' if buildName == "" then buildName = mw.title.getCurrentTitle.text else buildName = mw.text.decode(buildName, false) end enchLvl = tostring(enchLvl) or 0 enchLvl = enchLvl:upper:gsub("^%s*(.-)%s*$", "%1") or '' enchLvl = enchLvl:gsub("[^0-5]", "")

event = event:lower:gsub("^%s*(.-)%s*$", "%1") or ''

if not outType:match("^[DRM]$") and outType ~= "ME" then outType = "D" end

local data = {}

if buildData[buildName] then data = buildData[buildName] else data = { ["A"] = "?", ["R"] = { ["magic"] = {"?h", "?", "?"}, }		}		buildName = "?" end

local reward = ""

if event ~= "" and data["R"][event] then reward = event else reward = "magic" -- current event (no magic entry yet) -- pick the first reward key (should be for current event) if not data["R"]["magic"] then for k, v in pairs(data["R"]) do				if k ~= "after" then reward = k					break end end end end

bData = data["R"][reward]

local tFunc = { -- Magic Reward only ["M"] = function if bData[1] ~= "" then return bData[3] end end, -- Rewards only ["R"] = function if data["R"][reward][1] ~= "" then local tRet = {" Rewards (Every ", data["R"][reward][1], ") '''", "", "", "", EC({reward}), data["R"][reward][3], "", "", "'''"								}						if bData[2] ~= 0 then tRet[4] = EC({"XP"}) tRet[5] = data["R"][reward][2] tRet[6] = ", " end if data["R"][reward]["T"] and event == "" then tRet[ 9] = ", " if reward ~= "magic" then tRet[10] = NPC({"Gold Trophies", nil, nil, nil, reward}) else tRet[10] = NPC({"Gold Trophies", "G"}) end end return table.concat(tRet) else return "" end end, -- Default (token rewards table row) ["D"] = function local tRet = {"|-\n| style=\"text-align: center;\"|", "", "", "", "", "\n|", "", ": ", "", "\n|", "", '\n| style="text-align: center;" data-sort-value="', "", '"|', "", "\n| nowrap style=\"text-align: center;\"|", "", "", ", ", "", "", "", "", "", ""							}					local buildType = "Ba" if data[1] then buildType = "Bc" end

tRet[ 2] = " " if enchLvl ~= "" and enchLvl ~= "0" then tRet[ 3] = " ''Ench. Level " tRet[ 4] = tostring(enchLvl) tRet[ 5] = "'' " end tRet[ 7] = buildName tRet[ 9] = data["A"] if data["W"] then tRet[11] = NPC({"Wish Granter"}) end if bData[1] ~= "" then local durNumb, durHMS = string.match(bData[1], "^([%d%?]+)([hms])$") local durNumb = tonumber(durNumb) if durNumb then if durHMS == "h" then durNumb = durNumb * 3600 elseif durHMS == "m" then durNumb = durNumb * 60 end else durNumb = 99900 end tRet[13] = durNumb tRet[15] = bData[1] if bData[2] ~= 0 then tRet[17] = EC({"XP"}) tRet[18] = bData[2] end tRet[20] = EC({reward}) tRet[21] = bData[3] else tRet[19] = "" end if bData["T"] and event == "" then tRet[22] = ", " if reward ~= "magic" then tRet[23] = NPC({"Gold Trophies", nil, nil, nil, event}) else tRet[23] = NPC({"Gold Trophies", "G"}) end end if event == "" and ((bData[4] and bData[4] ~= "") or (bData[5] and bData[5] ~= "")) then tRet[24] = "\n| nowrap style=\"text-align: center;\"|" local tokens = (bData[4] or "") if enchLvl == "1" then tokens = (bData[4] or "") .. "$" .. (bData[5] or "") elseif enchLvl == "2" then tokens = (bData[4] or "") .. "$" .. (bData[5] or "") .. "$" .. (bData[6] or "") elseif enchLvl == "3" then tokens = (bData[4] or "") .. "$" .. (bData[5] or "") .. "$" .. (bData[6] or "") .. "$" .. (bData[7] or "") elseif enchLvl == "4" or enchLvl == "5" then tokens = (bData[4] or "") .. "$" .. (bData[5] or "") .. "$" .. (bData[6] or "") .. "$" .. (bData[7] or "") .. "$" .. (bData[8] or "") end tRet[25] = TOK({tokens, "", "B"}) end return table.concat(tRet) end, -- Mini Event (token rewards table row) ["ME"] = function local tRet = {"|-\n| style=\"text-align: center;\"|", "", "", "", "", "\n|", "", ": ", "", "\n|", "", '\n| style="text-align: center;" data-sort-value="', "", '"|', "" }					local buildType = "Ba" if data[1] then buildType = "Bc" end

tRet[ 2] = " " if enchLvl ~= "" and enchLvl ~= "0" then tRet[ 3] = " ''Ench. Level " tRet[ 4] = tostring(enchLvl) tRet[ 5] = "'' " end tRet[ 7] = buildName tRet[ 9] = data["A"] if data["W"] then tRet[11] = NPC({"Wish Granter"}) end if bData[1] ~= "" then local durNumb, durHMS = string.match(bData[1], "^([%d%?]+)([hms])$") local durNumb = tonumber(durNumb) if durNumb then if durHMS == "h" then durNumb = durNumb * 3600 elseif durHMS == "m" then durNumb = durNumb * 60 end else durNumb = 99900 end tRet[13] = durNumb tRet[15] = bData[1] else tRet[19] = "" end return table.concat(tRet) end, }	return tFunc[outType] end

function p.building(frame) local tArgs = util.getArgs(frame) local pagename = tArgs["P"] or mw.title.getCurrentTitle.text or "" local tOut = ""

local buildName = tArgs[1] or "" local enchLvl = tArgs[2] or "" local event = tArgs[3] or "" local outType = tArgs[4] or ""

if buildName ~= "" then for build in mw.text.gsplit(buildName, "%s*$%s*") do tOut = tOut .. (tOut == "" and "" or "\n") .. oneBuilding(build, enchLvl, event, outType, pagename) end elseif outType == "G" then local attractions = {} local reward = "magic" for item, data in pairs(buildData) do			if not data["R"][reward] then for k, v in pairs(data["R"]) do					if k ~= "after" then reward = k						break end end end if data["R"][reward]["T"] then local durNumb, durHMS = string.match(data["R"][reward][1], "^([%d%?]+)([hms])$") if not durNumb or durNumb == "?" then durNumb = 0 else durNumb = tonumber(durNumb) if durHMS == "m" then durNumb = durNumb / 60 end end table.insert(attractions, {durNumb, item}) end end table.sort(attractions, function(a,b) return a[1] < b[1] end) for _, item in pairs(attractions) do tOut = tOut .. oneBuilding(item[2], "", "T", "D") .."\n" end elseif outType == "W" then local attractions = {} local reward = "magic" for item, data in pairs(buildData) do			if not data["R"][reward] then for k, v in pairs(data["R"]) do					if k ~= "after" then reward = k						break end end end if data["W"] then local durNumb, durHMS = string.match(data["R"][reward][1], "^([%d%?]+)([hms])$") if not durNumb or durNumb == "?" then durNumb = 0 else durNumb = tonumber(durNumb) if durHMS == "m" then durNumb = durNumb / 60 end end table.insert(attractions, {durNumb, item}) end end table.sort(attractions, function(a,b) return a[1] < b[1] end) for _, item in pairs(attractions) do tOut = tOut .. oneBuilding(item[2], "", "T", "D") .."\n" end elseif event ~= "" then local eventBuild = {} for build, data in pairs(buildData) do			for name, x in pairs(data["R"]) do				if event:lower == name then local durNumb, durHMS = string.match(x[1], "^([%d%?]+)([hms])$") local durNumb = tonumber(durNumb) if durNumb then if durHMS == "h" then durNumb = durNumb * 3600 elseif durHMS == "m" then durNumb = durNumb * 60 end else durNumb = 99900 end table.insert(eventBuild, {(data[1] and "0" or "1"), durNumb, build}) end end end table.sort(eventBuild, function(a,b)			if a[1] == b[1] then				if a[2] == b[2] then					return a[3] < b[3]				else					return a[2] < b[2]				end			else				return a[1] < b[1]			end		end) for _, item in pairs(eventBuild) do tOut = tOut .. (tOut == "" and "" or "\n") .. oneBuilding(item[3], "", event, "D") end else local tokenBuild = {} for build, data in pairs(buildData) do			for event, rewards in pairs(data["R"]) do				for k, tokens in pairs(rewards) do					if (event == "magic" or event == "after") and tonumber(k) and k > 3 then for token in mw.text.gsplit(tokens, "%s*$%s*") do							if pagename == TOK({token, "T"}) then local durNumb, durHMS = string.match(rewards[1], "^([%d%?]+)([hms])$") local durNumb = tonumber(durNumb) if durNumb then if durHMS == "h" then durNumb = durNumb * 3600 elseif durHMS == "m" then durNumb = durNumb * 60 end else durNumb = 99900 end table.insert(tokenBuild, {durNumb, build, k-4}) end end end end end end table.sort(tokenBuild, function(a,b)			if a[1] == b[1] then				return a[2] < b[2]			else				return a[1] < b[1]			end		end) for _, item in pairs(tokenBuild) do tOut = tOut .. oneBuilding(item[2], item[3], "", "D") .. "\n" end end

return tOut end

function p.getBuildinginfo(frame) local tArgs = util.getArgs(frame) local buildName = tArgs[1] or mw.title.getCurrentTitle.text buildName = buildName:gsub("^%s*(.-)%s*$", "%1") or '' local VL = mw.ext.VariablesLua local data = {}

if buildData[buildName] then data = buildData[buildName] else data = { ["A"] = "?", ["R"] = { ["magic"] = {"?h", "?", "?"}, }		}	end

local activity = data["A"] if data["W"] then activity = NPC({"Wish Granter"}) .. " " .. activity end VL.vardefine("activity", activity)

local reward = "magic" if not data["R"]["magic"] then for k, v in pairs(data["R"]) do			if k ~= "after" then reward = k				VL.vardefine("ltename", lte({k, nil, "K"})) break end end end

local time = "" local rewards = "" local chancerewards = ""

if data["R"][reward][1] ~= "" then time = data["R"][reward][1] if data["R"][reward][2] ~= 0 then rewards = EC({"XP"}) .. data["R"][reward][2] .. ", "		end rewards = rewards .. EC({reward}) .. data["R"][reward][3] if data["R"][reward]["T"] then rewards = rewards .. ", " .. NPC({"Gold Trophies", "G"}) end

local tokens = (data["R"][reward][4] or "") .. "$" .. (data["R"][reward][5] or "") .. "$" .. (data["R"][reward][6] or "") .. "$" .. (data["R"][reward][7] or "") .. "$" .. (data["R"][reward][8] or "") chancerewards = TOK({tokens, "I"}) end

VL.vardefine("time", time) VL.vardefine("rewards", rewards) VL.vardefine("chancerewards", chancerewards)

local eventinfo = {}

for k, v in pairs(data["R"]) do		if k ~= "magic" then table.insert(eventinfo, {(v["E"] or 1), k, v}) end end

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

for _, data in pairs(eventinfo) do		if reward ~= data[2] then VL.vardefine("ltename" .. data[1], lte({data[2], nil, "K"})) VL.vardefine("eventtime" .. data[1], data[3][1]) if data[3][2] ~= 0 then eventrewards = EC({"XP"}) .. data[3][2] .. ", "			end eventrewards = eventrewards .. EC({data[2]}) .. data[3][3] if data[3]["T"] then eventrewards = eventrewards .. ", " .. NPC({"Gold Trophies", nil, nil, nil, data[2]}) end VL.vardefine("eventrewards" .. data[1], eventrewards) if data[3][4] and data[3][4] ~= "" then local eventchancerewards = data[3][4] .. "$" .. (data[3][5] or "") .. "$" .. (data[3][6] or "") .. "$" .. (data[3][7] or "") .. "$" .. (data[3][8] or "") VL.vardefine("eventchancerewards" .. data[1], TOK({eventchancerewards, "I"})) end end end

return "" end

return p