Module:Archetype

From Wavu Wiki, the 🌊 wavy Tekken wiki
Revision as of 12:33, 21 January 2022 by RogerDodger (talk | contribs)

Documentation for this module may be created at Module:Archetype/doc

local G = require("Module:G")
local p = {}

local json = mw.title.makeTitle("User", "RogerDodger/archetypes.json"):getContent()
local data = mw.text.jsonDecode(json)

local labels = { "Poke", "Mixup", "Okiwaza", "Whiff punish", "Okizeme", "Pressure", "Defense", "Cheese" }
local ranks = {}
for _,fighter in ipairs(data["unsorted"]) do
	ranks[fighter] = {}
end
for _,label in ipairs(labels) do
	for i,fighter in ipairs(data[label]) do
		ranks[fighter][label] = i
	end
end

p.all = function()
	local wikitable = mw.html.create('table'):addClass('punishers sortable')
	
	-- Headings
	local heads = mw.html.create('tr')
	heads:node(mw.html.create('th'):wikitext("Character"))
	for _,label in ipairs(labels) do
		heads:node(mw.html.create('th'):wikitext(label))
	end
	heads:node(mw.html.create('th'):wikitext("Total"))
	wikitable:node(heads)
	
	-- Body
	for _,fighter in ipairs(data["unsorted"]) do
		local row = mw.html.create('tr')
		local n = 0
		row:node(mw.html.create('td'):wikitext("[[" .. fighter .. "]]"))
		for _,label in ipairs(labels) do
			local i = ranks[fighter][label]
			n = n + i
			row:node(mw.html.create('td'):wikitext(i):addClass(G._main(i,1,50,true)))
		end
		row:node(mw.html.create('td'):wikitext(n))
		wikitable:node(row)
	end
	
	local radars = ''
	for _,fighter in ipairs(data["unsorted"]) do
		radars = radars .. "{{Label|" .. fighter .. "|\n" .. p._radar(fighter) .. "}}\n"
	end
	
	return tostring(wikitable) .. "\n\n" .. 
		mw.getCurrentFrame():preprocess("{{Row|" .. radars .. "}}")
end

p.radar = function(frame) 
	local fighter = frame.args[1]
	return frame:preprocess( p._radar(fighter) )
end

p._radar = function(fighter)
	local a = ranks[fighter]
	if a == nil then error("Unknown character: " .. fighter) end
	
	-- Standardize
	local sum = 0
	for _,label in ipairs(labels) do
		sum = sum + a[label]
	end
	local mu = sum / #labels
	-- local sigma_sq = 0
	-- for _,label in ipairs(labels) do
	-- 	sigma_sq = sigma_sq + math.pow(mu - a[label], 2)
	-- end
	-- local sigma = math.sqrt(sigma_sq)
	
	local std = {}
	for _,label in ipairs(labels) do
		-- std[label] = (a[label] - mu) / sigma
		std[label] = 1 - (a[label] - (mu - 25)) / 50
	end
	
	-- -- Normalize
	-- local mn = math.huge
	-- local mx = -math.huge
	-- for _,label in ipairs(labels) do
	-- 	if a[label] < mn then mn = a[label] end
	-- 	if a[label] > mx then mx = a[label] end
	-- end
	
	-- local norm = {}
	-- for _,label in ipairs(labels) do
	-- 	norm[label] = 1 - (a[label] - mn) / (mx - mn)
	-- end
	
	local points = ""
	for _,label in ipairs(labels) do
		points = points .. string.format("%.5f", std[label]) .. " " .. label .. "\n"
	end
	return "<radar size=\"280\">\n" .. points .. "</radar>"
end

return p