Module:Music map: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
(Created page with "local var = mw.ext.VariablesLua local p = {} -- Property:Name for Map: mw.pages that should be in/excluded in the smw='hist' map. -- By default, excluded are all Map: mw.pages with multiple music tracks, -- and included are all Map: mw.pages with a single music track. local include_historic = { ['Prifddinas'] = true, } local exclude_historic = { ['Alone'] = true, } -- Convert a link to a "Play track" link -- Use text='%1' to use the music file's filename without the...")
 
No edit summary
 
Line 3: Line 3:
local p = {}
local p = {}


-- Property:Name for Map: mw.pages that should be in/excluded in the smw='hist' map.
-- Property:Name for Map: pages that should be in/excluded in the smw='hist' map.
-- By default, excluded are all Map: mw.pages with multiple music tracks,
-- By default, excluded are all Map: pages with multiple music tracks,
-- and included are all Map: mw.pages with a single music track.
-- and included are all Map: pages with a single music track.
local include_historic = {
local include_historic = {
['Prifddinas'] = true,
['Prifddinas'] = true,

Latest revision as of 11:23, 17 October 2024

Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Music map/doc. [edit]
Module:Music map's function main is invoked by Template:Music map.

local var = mw.ext.VariablesLua

local p = {}

-- Property:Name for Map: pages that should be in/excluded in the smw='hist' map.
-- By default, excluded are all Map: pages with multiple music tracks,
-- and included are all Map: pages with a single music track.
local include_historic = {
	['Prifddinas'] = true,
}
local exclude_historic = {
	['Alone'] = true,
}

-- Convert a link to a "Play track" link
-- Use text='%1' to use the music file's filename without the .ogg extension.
function playTrack(link, text)
	if link == nil then
		return ''
	end
	if type(link) ~= 'table' then
		link = {link}
	end
	local ret = ''
	for _, l in ipairs(link) do
		ret = ret .. l:gsub('%[%[:?File:([^%|%]]+)%.ogg%|?.*%]%]', '<div>[[:File:%1.ogg|'..text..']]')..'</div>'
	end
	return ret
end

function p.main( frame )
    return p._main( frame:getParent().args )
end

function p._main( args )
	local nopreprocess = args['nopreprocess']
	local jsonObj = {
		type = "FeatureCollection",
		features = {},
	}
	local feat = jsonObj['features']
	local cgs = args['categories'] or ''
	if args.smw == 'hist' then
		cgs = cgs .. '[[Category:%s]][[Location JSON::+]] OR [[Category:%s]][[Historic Location JSON::+]]' .. cgs
	elseif args.smw == 'onlyhist' then
		cgs = '[[Category:%s]][[Historic Location JSON::+]]' .. cgs
	else
		cgs = '[[Category:%s]][[Location JSON::+]]' .. cgs
	end
	local query = {
		string.format(cgs, "Music tracks", "Music tracks"),
		'?=#',
		'?Music number',
		'?Music track',
		'limit=5000'
	}
	local query2 = {
		string.format(cgs, "Music track maps", "Music track maps"),
		'?=#',
		'?Music track',
		'?Plays music',
		'?Name',
		'limit=5000'
	}
	if args.smw ~= 'onlyhist' then
		table.insert(query, '?Location JSON')
		table.insert(query2, '?Location JSON')
	end
	if args.smw == 'hist' or args.smw == 'onlyhist' then
		table.insert(query, '?Historic Location JSON')
		table.insert(query2, '?Historic Location JSON')
	end
	local songs = mw.smw.ask(query)
	local clusters = mw.smw.ask(query2)
	local polys = songs or {}
	if type(clusters) == 'table' then
		for _,c in ipairs(clusters) do
			table.insert(polys, c)
		end
	end
	math.randomseed(args['randomseed'] or 1)
	local i = 0
	local noid = -1
	local tracklist = {}
	if polys then
		for _, coords in ipairs(polys) do
			local track = coords[1]
			local name = coords['Name']
			local musicID = coords['Music number']
			-- replace |foo]] with |Play track]], or ]] with |Play track:
			local tracklink
			if type(coords['Music track']) == 'table' then
				tracklink = playTrack(coords['Music track'], '%1')
			else
				tracklink = playTrack(coords['Music track'], 'Play track')
			end
			if musicID then
				musicID = tonumber(musicID)
				tracklist[musicID] = {}
			else
				musicID = noid
				tracklist[musicID] = {}
				noid = noid - 1
			end
			local json = coords['Location JSON']
			if args.smw == 'onlyhist' then
				json = coords['Historic Location JSON']
			elseif args.smw == 'hist' then
				if coords['Historic Location JSON'] then
					json = coords['Historic Location JSON']
				elseif exclude_historic[name] or (type(coords['Music track']) == 'table' and not include_historic[name]) then
					-- multiple music tracks means that the coords are for a combined music track area.
					json = nil
				end -- else: keep json = coord['Location JSON']; i.e. do nothing
			end
			if type(json) == 'string' then
				json = {json}
			end
			local paren = track:find(' %(music track%)')
			local colon = track:find(':') or 0
			local link
			if paren then
				base = track:sub(colon, paren)
				link = string.format('[[%s (music track)|%s]]', base, name and '(?)' or base)
			else
				link = string.format('[[%s|%s]]', track, name and '(?)' or track)
			end
			if name then
				link = string.format('[[%s]] <sup>%s</sup>', name, link)
			end
			--colors = {'#3388ff', '#ff3333', '#88ff33', '#ffff33'}
			--colors[math.random(4)]
			if json then
				for _, jsonstr in ipairs(json) do
					mw.logObject({name, jsonstr})
					local obj = mw.text.jsonDecode(jsonstr)
					local prop = obj['properties']
					
					if prop then
						prop['title'] = link
						prop['description'] = tracklink
						prop['mapID'] = "-1"
						prop['plane'] = 0
						table.insert(feat, obj)
						table.insert(tracklist[musicID], i)
					end
					
					i = i + 1
				end
			end
		end
	end
	var.vardefine('tracklist', trackdata(tracklist))
	local jsonstr = mw.text.jsonEncode(jsonObj)
	jsonstr = jsonstr:gsub('}}', '} }'):gsub('}}', '} }')
	return jsonstr
end

function trackdata(tracklist)
	if tracklist == nil then
		return ''
	end
	local div = mw.html.create('div'):css('display', 'none')
	for track, ids in pairs(tracklist) do
		div:tag('data'):attr('value', table.concat(ids, ' ')):wikitext(track)
	end
	return tostring(div)
end

return p

--[[Debug copypaste:
="{{#tag:mapframe|"..p._main({width=1000, height=850, zoom=0, smw="onlyhist"}).."|width=500|height=500|mapid=-1}}"
--]]