Module:API info/flavor

From Warcraft Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:API info/flavor/doc

local bit = require "bit32"
local data
local m = {}

-- github forks are not searchable unless they have more stars, just cloned it instead
-- https://stackoverflow.com/questions/33626326/how-to-search-a-github-fork-for-code-using-the-github-search-api
local flavors = {
	mainline = {
		label = "mainline",
		flag = 0x1,
		icon = {name = "Dragonflight-Icon-Inline.png", size = 36},
		url = "https://github.com/search?q=repo:Gethe/wow-ui-source+%s&type=code",
	},
	vanilla = {
		label = "vanilla",
		flag = 0x2,
		icon = {name = "WoW Icon update.png", size = 36},
		url = "https://github.com/search?q=repo:Ketho/wow-ui-source-vanilla+%s&type=code",
	},
	wrath = {
		label = "wrath",
		flag = 0x4,
		icon = {name = "Wrath-Logo-Small.png", size = 36},
		url = "https://github.com/search?q=repo:Ketho/wow-ui-source-wrath+%s&type=code",
	},
	mainline_beta = {
		label = "war within",
		flag = 0x8,
		icon = {name = "TheWarWithin-Icon-Inline.png", size = 36},
		url = "https://github.com/search?q=repo:Ketho/wow-ui-source-tww+%s&type=code",
	},
	cata = {
		label = "cata",
		flag = 0x10,
		icon = {name = "Cata-Logo-Small.png", size = 35}, -- 36px does not show an icon
		url = "https://github.com/search?q=repo:Ketho/wow-ui-source-cata+%s&type=code",
	},
}

local function GetData(apiType)
	local api_types = {
		a = "api",
		e = "event",
	}
	if api_types[apiType] then
		return {[apiType] = mw.loadData("Module:API_info/flavor/"..api_types[apiType])}
	end
end

function m:GetFlavorInfo(apiType, name)
	data = data or GetData(apiType)
	local flags = data[apiType][name]
	if flags then
		local mainline = bit.band(flags, flavors.mainline.flag) > 0
		local vanilla = bit.band(flags, flavors.vanilla.flag) > 0
		local wrath = bit.band(flags, flavors.wrath.flag) > 0
		local mainline_beta = bit.band(flags, flavors.mainline_beta.flag) > 0
		local cata = bit.band(flags, flavors.cata.flag) > 0
		return flags, mainline, vanilla, wrath, mainline_beta, cata
	end
end

local function InsertTableInfo(tbl, name, info)
	table.insert(tbl, {
		icon = info.icon.name,
		iconsize = info.icon.size,
		url = info.url:format(name),
		text = info.label,
	})
end

function m:GetFlavors(apiType, name)
	data = data or GetData(apiType)
	local flags, mainline, vanilla, wrath, mainline_beta, cata = self:GetFlavorInfo(apiType, name)
	if flags then
		local t = {}
		if mainline_beta then
			InsertTableInfo(t, name, flavors.mainline_beta)
		end
		if mainline then
			InsertTableInfo(t, name, flavors.mainline)
		end
		if cata then
			InsertTableInfo(t, name, flavors.cata)
		end
		if wrath then
			InsertTableInfo(t, name, flavors.wrath)
		end
		if vanilla then
			InsertTableInfo(t, name, flavors.vanilla)
		end
		return t
	end
end

return m