Module:Sandbox/Lua/User:ThetaZero: Difference between revisions

From WIDEVERSE Wiki
Jump to navigation Jump to search
(Created page with "-- <nowiki> local p = {} local matnames = mw.loadData('Module:Disassemble/mats') local perknames = mw.loadData('Module:Perks/data') local perkmats = mw.loadData('Module:Perks/...")
 
m (1 revision imported)
 
(No difference)

Latest revision as of 21:23, 4 November 2021

Documentation for this module may be created at Module:Sandbox/Lua/User:ThetaZero/doc

-- <nowiki>
local p = {}
local matnames = mw.loadData('Module:Disassemble/mats')
local perknames = mw.loadData('Module:Perks/data')
local perkmats = mw.loadData('Module:Perks/materials')
local yn = require('Module:Yesno')
local hc = require('Module:Paramtest').has_content

local gizmosmap = {
	weapon = 'Weapon',
	armour = 'Armour',
	tool = 'Tool'
}

function plink(mat)
	if mat then
		local matplain = string.lower(mat)
				:gsub('components?', '')
				:gsub('parts?', '')
		mat = matplain
		mw.text.trim(mat)
		
		mat = matnames[mat]
		if mat then
			local class = ''
			if mat:find('parts') then
				class = 'gizmo-interface-parts'
			end
			return string.format('[[File:%s.png|link=%s]]', mat, mat), class, matplain, mat
		end
	end
	
	return ''
end

function perklink(perk)
	local working = mw.text.split(perk, '%[')
	local a = working[1]
	local b = working[2] or ''
	local c
	if b == '' then
		working = mw.text.split(a, '%(')
		a = working[1]
		c = working[2] or ''
	else
		working = mw.text.split(b, '%(')
		b = working[1] or ''
		c = working[2] or ''
	end
	
	b = b:gsub('%]','')
		:gsub('-', '–')
	c = c:gsub('%)', '')
	
	
	local name, rankmin, rankmax
	local rankstr = ''
	local comment = ''
	
	if a then
		a = mw.text.trim(a:lower())
		name = perknames[a].name
		rankstr = ' ' .. mw.text.trim(b)
		if c ~= '' then
			comment = string.format(' (%s)', mw.text.trim(c))
		end
	else
		return nil
	end


	local span = mw.html.create('span')
	span:css({
		background = '#09222F',
		['border-radius'] = '111px',
		width = '35px',
		height = '35px',
		['line-height'] = '34px',
		['text-align'] = 'center',
		display = 'inline-table',
		})
		:wikitext(string.format('[[File:%s.png|25x25px|link=%s]]', name, name))
	return string.format('\n* %s [[%s|%s]]%s%s', tostring(span), name, name:gsub('%(perk%)', ''), rankstr, comment), a


end

function otherperks(mats, perks, gizmos)
	local allperks = {}
	local fperks = {}
	local ret = {}
	for m,n in pairs(mats) do
		if perkmats[m] then
			for p,t in pairs(perkmats[m].perks) do
				if t[n] == nil then
					allperks[p] = true
				elseif t[n][1] > 0 and t[n][2] > 0 then
					allperks[p] = true
				end
			end
		end
	end

	--detect gizmos
	local appgizmos = {
		weapon = true,
		armour = true,
		tool = true
		}
	local p2
	for p,_ in pairs(perks) do
		p2 = perknames[p]
		for i,v in pairs(appgizmos) do
			appgizmos[i] = v and p2[i]
		end
	end
	
	-- overrides
	for i,v in pairs(gizmos) do
		appgizmos[i] = v
	end
	
	-- test for issues
	if not appgizmos.weapon and not appgizmos.armour and not appgizmos.tool then
		--table.insert(ret, "\n:* '''Error: perks selected can't be in the same gizmo. Defaulting to showing all'''")
		appgizmos = {
			weapon = true,
			armour = true,
			tool = true,
			}
	end
	
		
	
	--filter allperks
	for p,_ in pairs(allperks) do
		p2 = perknames[p]
		for i,v in pairs(appgizmos) do
			if v and p2[i] then
				fperks[p] = p2.name
				break
			end
		end
	end
		
	
	-- make string
	for p,n in pairs(fperks) do
		if not perks[p] then
			table.insert(ret, string.format('\n:* [[%s|%s]]',n,n:gsub(' %(perk%)','')))
		end
	end

	return ret
end

function makeCalcLink(gizmo, mats, ancient)
	if #gizmo == 0 then
		return ''
	end
	local ret = {}
	for i,v in ipairs(gizmo) do
		local link = tostring(mw.uri.fullUrl('Calculator:Perks', {
			type = v,
			ancient = ancient,
			top = mats[1],
			left = mats[2],
			middle = mats[3],
			right = mats[4],
			bottom = mats[5],
			["top-left"] = mats[6],
			["top-right"] = mats[7],
			["bottom-left"] = mats[8],
			["bottom-right"] = mats[9]
		}))
		local _v = v
		if ancient then
			_v = 'Ancient '..string.lower(v)
		end
		table.insert(ret, string.format('[[File:%s gizmo shell.png|link=%s]] [%s %s]', _v, link, link, _v))
	end
	return string.format('\n<div style="clear:left;" class="plainlinks">\'\'\'Open in calculator:\'\'\' %s</div>', table.concat(ret, ', '))
end

function p.main(frame)
	local args = frame:getParent().args
	
	local ret = {}
	local matnums = {}
	local mati
	local numofmats = 0
	for i=1,9,1 do
		mati = 'mat' .. i
		ret[mati] = { plink(args[mati] or args[i]) }
		if ret[mati][1] ~= '' then
			numofmats = numofmats + 1
			matnums[ret[mati][3]] = (matnums[ret[mati][3]] or 0) + 1
		end
	end
	
	local perks = mw.text.split(args.perks or args.Perks, '%s*,%s*')
	local perkl = {}
	local perkplain = {}
	local pl, pplain
	for i,v in ipairs(perks) do
		pl, pplain = perklink(v)
		if pl then
			table.insert(perkl, pl)
			perkplain[pplain] = true
		end
	end
	
	ret.perks = table.concat(perkl, '')
	
	local gizmos = {
		weapon = yn(args.weapon),
		armour = yn(args.armour),
		tool = yn(args.tool),
		}
	
	if not yn(args.otherperks or 'yes') then
		ret.other = ''
	else
		ret.other = table.concat(otherperks(matnums, perkplain, gizmos), '')
	end
	
	ret.comment = args.comment or args.Comment
	
	ret.calcgizmo = {}
	for v in mw.text.gsplit(args.gizmo or args.Gizmo or '', '%s*,%s*') do
		if gizmosmap[v] then
			table.insert(ret.calcgizmo, gizmosmap[v])
		end
	end
	ret.ancient = false
	if yn(args.ancient) or numofmats > 5 then
		ret.ancient = true
	end
	
	return p._main(ret)
end

function p._main(args)
	local ret = mw.html.create('tr')
	local comment = args.comment
	if hc(comment) then
		comment = comment .. '<br/>'
	end
	
	local calclink = makeCalcLink(args.calcgizmo, {
		args.mat1[4],
		args.mat2[4],
		args.mat3[4],
		args.mat4[4],
		args.mat5[4],
		args.mat6[4],
		args.mat7[4],
		args.mat8[4],
		args.mat9[4]
	}, args.ancient)
	
	ret:tag('td')
			:tag('table')
				:addClass('gizmo-interface')
				:addClass(args.ancient and 'gizmo-ancient' or '')
				:tag('tr')
					:tag('td')
						:addClass(args.mat6[2])
						:wikitext(args.mat6[1])
					:done()
					:tag('td')
						:addClass(args.mat1[2])
						:wikitext(args.mat1[1])
					:done()
					:tag('td')
						:addClass(args.mat7[2])
						:wikitext(args.mat7[1])
					:done()
				:done()
				:tag('tr')
					:tag('td')
						:addClass(args.mat2[2])
						:wikitext(args.mat2[1])
					:done()
					:tag('td')
						:addClass(args.mat3[2])
						:wikitext(args.mat3[1])
					:done()
					:tag('td')
						:addClass(args.mat4[2])
						:wikitext(args.mat4[1])
					:done()
				:done()
				:tag('tr')
					:tag('td')
						:addClass(args.mat8[2])
						:wikitext(args.mat8[1])
					:done()
					:tag('td')
						:addClass(args.mat5[2])
						:wikitext(args.mat5[1])
					:done()
					:tag('td')
						:addClass(args.mat9[2])
						:wikitext(args.mat9[1])
					:done()
				:done()
			:done()
		:done()
	local commentcell = ret:tag('td')
	commentcell:wikitext(comment)
			:wikitext(args.perks)
		:done()
	if args.other ~= '' then
		commentcell
			:wikitext('\n')
			:tag('div')
				:addClass('mw-collapsible mw-collapsed')
				:attr({
						['data-expandtext'] = 'show',
						['data-collapsetext'] = 'hide',
					})
				:wikitext('\n* Other possible perks:\n')
				:tag('div')
					:addClass('mw-collapsible-content')
					:wikitext(args.other)
				:done()
			:done()
		:done()
	end
	commentcell:wikitext(calclink)
		
	return ret
end


return p