Module:Sandbox/MonkeysHK

local getArgs = require('Module:Arguments').getArgs local loader = require('Module:Loader')

local string, table, yesno = loader.require('String', 'Table', 'Yesno') local collectionData = loader.loadData('Collection/Data')

local p = {}

local function makeUI(params) return mw.getCurrentFrame:expandTemplate{ title = 'UI', args = params } end

- -- Template: Collection UI -- -- Creates a main collection UI for a given item - function p.collectionUI(frame) return p._collectionUI(table.deepCopy(getArgs(frame))) end

- -- _collectionUI Helper functions - local recipeAliases = { ['r'] = 'recipe', ['recipe'] = 'recipe', ['recipes'] = 'recipes', ['rs'] = 'recipes', ['rec'] = 'recipe', ['recs'] = 'recipes', ['recip'] = 'recipe', ['recips'] = 'recipes', ['res'] = 'recipes', ['re'] = 'recipe', ['up'] = 'upgrade', ['upg'] = 'upgrade', ['u'] = 'upgrade', ['upgr'] = 'upgrade', ['upgra'] = 'upgrade', ['upgrad'] = 'upgrade', ['upgrade'] = 'upgrade', }

-- 1/3 local variateStart = { -- { regex to match, subst } { '[Ee]xperience', '&8+' }, } -- 2/3 local function specialCaseColors(s) local matches = { ['[Mm]inion'] = 'blue', -- ['Enchanted Book'] = 'blue', ['[Uu]pgrade'] = 'green', ['[Cc]oming [Ss]oon'] = 'red', }

for k, v in pairs(matches) do		if s:match(k) then return uiText.getColorCode(v) end end if tonumber(s) then return uiText.getColorCode('dark_aqua') end end -- 3/3 local variateEnd = { -- { regex to match, subst } { {'[Nn]one', '[Uu]pgrade'}, '' }, { '[Cc][Oo][Mm][Ii][Nn][Gg] [Ss][Oo][Oo][Nn]', ' &8(&4COMING SOON&8)' }, { '[Dd]warven [Ff]orge [Rr]ecipe', ' &6Dwarven Forge Recipe' }, { '[Mm]inion', ' &rRecipes' } }

local function getStartEnd(_type, expressions) for _,v in ipairs(expressions) do		for _,_exp in ipairs(type(v[1]) == 'table' and v[1] or {v[1]}) do			if not not _type:match(_exp) then return v[2] end end end end

local function injectParamsBoolean(callStr, args) for param, value in pairs(args) do		if value then value = tostring(yesno(value)) callStr, n = callStr:gsub("|%s*"..param.."%s*=.-%f[|}]","|"..param.."="..value.."\n") if n == 0 then callStr = callStr:sub(0,callStr:len-2) .. "|"..param.."="..value.."}}" end end end return callStr end

local function injectParamsString(callStr, args) for param, value in pairs(args) do		if value then callStr, n = callStr:gsub("|%s*"..param.."%s*=.-%f[|}]","|"..param.."="..value.."\n") if n == 0 then callStr = callStr:sub(0,callStr:len-2) .. "|"..param.."="..value.."}}" end end end return callStr end

local function parseCollectionData(ui, rewards, collection) local rows = 1 local firstRowPositions = { { {5, 3} },		{ {4, 3}, {6, 3} },		{ {4, 3}, {5, 3}, {6, 3} },		{ {3, 3}, {4, 3}, {6, 3}, {7, 3} },		{ {3, 3}, {4, 3}, {5, 3}, {6, 3}, {7, 3} },		{ {2, 3}, {3, 3}, {4, 3}, {6, 3}, {7, 3}, {8, 3} },		{ {2, 3}, {3, 3}, {4, 3}, {5, 3}, {6, 3}, {7, 3}, {8, 3} },		{ {1, 3}, {2, 3}, {3, 3}, {4, 3}, {6, 3}, {7, 3}, {8, 3}, {9, 3} },	}	local total = #rewards for i, v in ipairs(rewards) do		local x		local y		if i > 21 then break; end

if total <= 8 then local pos = firstRowPositions[total][i] x, y = pos[2], pos[1] else x = 2+rows y = i%9 y = y == 0 and 9 or y		end if i%9 == 0 and i > 0 then rows = rows+1 end ui:setSlot(x, y, {			i == 1 and "Yellow Stained Glass Pane,"..i or 'Red Stained Glass Pane,'..i,			link = 'none',			title = table.concat{ i == 1 and '&e' or '&c', collection, ' ', string._toRoman(i) },			class = v._goto and 'goto-'..v._goto,			text = formatUIText(string.dedent			&7Progress: &e0&6%%			&f%s &e0&6\/&e%s&7			%s:			%s			&eClick to view rewards!			):format( ('-'):rep(20), string._formatShortNum(string._toNumber(v.amount)), #v > 1 and '&7Rewards' or '&7Reward', table.concat(table.map(v, function(val) local s = val[1] local start_ = val.type and getStartEnd(val.type, variateStart) local end_ = val.type and getStartEnd(val.type, variateEnd) return table.concat{ start_ or '', -- 1/3 val.rarity..s, -- 2/3 end_ or ' &r'..(val.type or '&rRecipe'), -- 3/3 }				end), '/') )		})	end end

local function parseTextList(s) local rewards = {} local count = 0 for i, v in ipairs(string.split((s:gsub('\\,', '\255')), '\n')) do		local _type v = string.methods(v):_trim if v:getValue ~= '' then if v:charAt(1) == "*" then if v:charAt(2) == "*" then _type = 'item' else _type = 'amount' end end if _type == 'amount' then local amount, _goto = unpack(string.split(v:sub(2), "%s*,%s*")) count = count+1 rewards[count] = {} rewards[count].amount = amount rewards[count]._goto = _goto and _goto:gsub('^goto%-', '') elseif _type == 'item' then local item, type, rarity = unpack(string.split(v:sub(3), '%s*,%s*')) item = item or v:sub(3) rarity = (rarity and rarity ~= "") and uiText.getFormatting(rarity) local tooltip = tooltips[item] local tooltipRarity = tooltip and tooltip.title and tooltip.title:match('^&([a-z0-9])') table.push(rewards[count], { 					item, 					rarity = '&'..(rarity or tooltipRarity or specialCaseColors(item) or 'f'),					type=(type and type ~= "") and string.ucfirst(recipeAliases[type:lower] or type)				}) end end end return rewards end - -- function: _collectionUI(args: table|frame) -- -- Invoked by. - function p._collectionUI(args) checkType(1, args, { 'table' }) assertTrue(args[1], 'No collection specified', 2) assertTrue(args[2], 'No collection rewards specified', 2) local slots = {} local collection = args[1] and args[1]:gsub('[Ee][Nn][Cc][Hh][Aa]?[Nn]?[Tt]?[Ee]?[Dd]? ?', ):gsub('[Bb][Ll][Oo][Cc][Kk] ?[Oo]?[Ff]? ?', ) local title = mw.title.getCurrentTitle local ui = createBlankUI(collection and collection..' Collection' or title.rootText, args['id'], { 		text=(args['return_text'] or args['goback']),		link=(args['return_text'] or args['goback']) and (args['return_text'] or args['goback']):gsub('%s*[Cc]ollection', ''),		id=args['return_id'],	}, args['hide']) ui:setSlot(1, 5, {		args.title or collection,		link='none', 		title='&e'..collection..' Collection',		text=formatUIText(string.dedent		&7View all your %s Collection 		&7progress and rewards!		&7Total Collected: &e0		:format(collection))	}) ui:setSlot(6, 4, 'link', '') parseCollectionData(ui, parseTextList(args[2]), collection) -- pipeline(ui, table.dump, 0, error) return (tostring(ui):gsub('\\(.)', '%1'):gsub('\255', ',')) end

- -- Template: Collection Rewards UI -- -- Creates a collection Rewards UI. - function p.collectionRewardsUI(frame) local args = getArgs(frame) return p._collectionRewardsUI(table.deepCopy(args)) end - -- function: _collectionRewardsUI(args) -- -- Invoked by - function p._collectionRewardsUI(args) checkType(1, args, { 'table' }) local collection = args[1] and args[1]:gsub('[Ee][Nn][Cc][Hh][Aa]?[Nn]?[Tt]?[Ee]?[Dd]? ?', ):gsub('[Bb][Ll][Oo][Cc][Kk] ?[Oo]?[Ff]? ?', ) local ui = createBlankUI(collection..' '..string._toRoman(args[2] or 1)..' Rewards', args['id'], {		text=collection..' Collection',		link=args['return_text'] and args['return_text']:gsub('%s*[Cc]ollection', ''),		id=args['return_id'],	}, args['hide']) local rewards = {} local items = table.filter(string.split(args[3], '\n'), function(v)		return string.trim(v) ~= ''	end) local amount = #items local posistions = { -- X/Y posistion entries for slot amounts 1-9 { {3, 5} },		{ {3, 4}, {3, 6} },		{ {3, 3}, {3, 5}, {3, 7} },		{ {3, 2}, {3, 4}, {3, 6}, {3, 8} },		{ 			{2, 2}, {2, 4}, {2, 6}, {2, 8},			{4, 5},		},		{ 			{2, 2}, {2, 4}, {2, 6}, {2, 8},			{5, 4}, {5, 6},		},		{ 			{2, 2}, {2, 4}, {2, 6}, {2, 8},			{3, 3}, {3, 5}, {3, 7},		},		{ 			{2, 2}, {2, 4}, {2, 6}, {2, 8},			{4, 2}, {4, 4}, {4, 6}, {4, 8},		},		{ 			{2, 4}, {2, 5}, {2, 6},			{3, 4}, {3, 5}, {3, 6},			{4, 4}, {4, 5}, {4, 6},		},		{ 			{2, 4}, {2, 5}, {2, 6}, {2, 7},			{3, 4}, {3, 5}, {3, 6},			{4, 4}, {4, 5}, {4, 6},		},		{ 			{2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7},			{3, 4}, {3, 5}, {3, 6},			{4, 4}, {4, 5}, {4, 6},		},	}	for i, line in ipairs(items) do		local item, id, title, desc = pipeline(line, string.trim, 2, string.sub, '%s*,%s*', string.unpackedSplit) local tooltip = tooltips[item] local itemText = desc or (tooltip and tooltip.text) local itemTitle = title or (tooltip and tooltip.title) local function addItem(x, y)			ui:setSlot(x, y, {				item, text=itemText and itemText..'//&eClick to view recipe!', 				title=itemTitle, class= id and 'goto-'..id:gsub('^goto%-', ),				link= id and 'none' or 			}) end addItem(unpack(posistions[amount][i])) end return tostring(ui) end

return p