Modulo:Sandbox/M.casanova/Prova

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
require('strict')
local getArgs = require('Module:Arguments').getArgs
local mWikidata = require('Module:Wikidata')
local errorCategory = '[[Categoria:Voci con errori del modulo GiocoMusicale]]'

local p = {}

-- Error handler per xpcall, formatta l'errore.
local function errhandler(msg)
	local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
	return string.format('<span class="error">%s</span>%s', msg, cat)
end

local function bach(dati)
	local mua =
{{
	"e''4 g''4 e''4 c''4", "r8 e''8 [ g''8 f''8 ] e''8 [ g''8 e''8 c''8 ]", "e''2. c''4", "c''4 g'4 e''4 c''4", "r4 c''8 [ d''8 ] e''4 c''4", "e''8 [ f''8 g''8 f''8 ] e''8 [ d''8 e''8 c''8 ]", "c''2 e''2", "r8 c''8 [ c''8 d''8 ] e''8 [ e''8 d''8 c''8 ]", "c''4 c''8 [ d''8 ] e''8 [ d''8 e''8 c''8 ]"
},{
	"d''4 g'4 g''2", "g''8 [ a''8 g''8 fis''8 ] g''2", "d''8 [ g'8 g''8 fis''8 ] g''2", "d''4 d''4 g''2", "d''4 g''8 [ fis''8 ] g''2", "d''2 g''2", "g''8 [ g'8 g''8 g''8 ] g''2", "d''8 [ d''8 g''8 fis''8 ] g''2", "g''4 g'4 g''2"
},{
	"g''8 [ a''8 b''8 c'''8 ] f''2", "g''4 c''4 f''2", "g''8 [ c''8 d''8 e''8 ] f''4 f''4", "g''8 [ c''8 b'8 c''8 ] f''2", "g''8 [ g''8 c'''8 g''8 ] f''4 f''4", "g''2 f''2", "g''8 [ g''8 f''8 e''8 ] f''2", "g''8 [ e''8 d''8 c''8 ] f''4 f''4", "g''8 [ c''8 f''8 e''8 ] f''2"
},{
	"f''4 f''4 e''8 [ c''8 a''8 c''8 ]", "f''4 e''8 [ d''8 ] e''4 c''4", "f''8 [ f''8 e''8 d''8 ] e''8 [ e''8 d''8 c''8 ]", "f''4 e''8 [ d''8 ] e''8 [ g''8 e''8 c''8 ]", "f''4 f''4 f''8 [ e''8 d''8 c''8 ]", "f''4. f''8 e''8 [ d''8 e''8 c''8 ]", "f''4 f''2 e''4", "f''4 f''4 e''4 a''4", "f''8 [ b'8 c''8 d''8 ] e''8 [ g''8 f''8 e''8 ]"
},{
	"d''2. c''8 [ d''8 ]", "d''4 g''8 [ a''8 ] g''4. f''8", "d''2 d''2", "d''1", "d''4 g''2 f''8 [ g''8 ]", "d''4 d''8 [ c''8 ] d''8 [ e''8 ] f''4", "d''4 g'4 g''4 f''4", "d''4 g''4 g'4 f''4", "d''4 g''8 [ a''8 ] g''8 [ a''8 f''8 g''8 ]"
},{
	"e''8 [ f''8 d''8 e''8 ] c''2", "e''8 [ f''8 e''8 d''8 ] c''2", "e''4 d''8 [ e''8 ] c''2", "e''8 [ d''8 e''8 d''8 ] c''2", "e''8 [ f''8 g''8 e''8 ] c''2", "e''8 [ g''8 e''8 d''8 ] c''2", "e''8 [ c''8 e''8 d''8 ] c''2", "e''4. d''8 c''2", "e''8 [ c'''8 g''8 e''8 ] c''2"
}}
	local mub =
{{
	"r2 c'4 c'4", "c'2. c'4", "c'4 e'8 [ d'8 ] c'4 c'4", "c2 c'2", "r2 c'2", "r4 e8 [ d8 ] c4 c'4", "c'1", "c4 e8 [ g8 ] c'4 c'4", "c'8 [ c8 e8 g8 ] c'4 c'4"
},{
	"c'4 b8 [ a8 ] b4 g4", "c'4 c'4 b4 b4", "c'8 [ c'8 b8 a8 ] b8 [ b8 a8 g8 ]", "c'4 b8 [ a8 ] b8 [ d'8 b8 g8 ]", "c'4 c'2 b4", "c'4 c'4 b4 e'4", "c'4 b8 [ a8 ] b8 [ c'8 d'8 e'8 ]", "c'4 b4 e'8 [ d'8 c'8 b8 ]", "c'8 [ c'8 b8 a8 ] c'8 [ b8 a8 g8 ]"
},{
	"a2 d'8 [ c'8 b8 a8 ]", "a2. as4", "a4 a4 a8 [ c'8 a8 f8 ]", "a2 a8 [ b8 c'8 d'8 ]", "a2 d8 [ f8 e8 d8 ]", "a2. d'4", "a4 a4 a8 [ a8 g8 f8 ]", "a4 a2 d'4", "a2 a8 [ c'8 b8 a8 ]"
},{
	"g4 a8 [ b8 ] c'2", "g2 c'4 c'4", "g4 g4 c'2", "g8 [ g8 a8 b8 ] c'2", "g8 [ d'8 c'8 b8 ] c'4 c'4", "g4 g4 c'4 c'4", "g8 [ g8 c'8 b8 ] c'4 c'4", "g4 c'8 [ b8 ] c'2", "g2 c4 c'4"
},{
	"c'4 b8 [ a8 ] b2", "c'4 b8 [ a8 ] b4 a8 [ b8 ]", "c'4 b4 b8 [ a8 ] b4", "c'8 [ a8 b8 c'8 ] b8 [ c'8 a8 b8 ]", "c'4 c'4 b4 b4", "c'4 b2 a8 [ b8 ]", "c'2 b2", "c'2 b4 a8 [ b8 ]", "c'8 [ c'8 b8 a8 ] b8 [ b8 a8 b8 ]"
}}
	local numeri = {}
	math.randomseed(os.time())
	local t = -1
	if not (dati == false) then
		for i in string.gmatch(dati, '%d+') do
			t = tonumber(i)
			if (t == nil or t<1 or t>9) then
				t = math.random(9)
			end
			table.insert(numeri,t)
		end
	end
	local tl = #numeri
	if (tl < 11) then
		for i=tl+1,11 do
			numeri[i] = math.random(9)
		end
	end
	local mdati = { "\\header { tagline = ##f } upper = { \\clef treble \\key c \\major \\time 4/4 \\tempo 4 = 168 \\set Staff.midiInstrument = #\"flute\" " }
	for n=1,6 do
		table.insert(mdati,mua[n][numeri[n]])
		table.insert(mdati,' | ')
	end
	table.insert(mdati,"} lower = { \\clef bass \\key c \\major \\time 4/4 \\set Staff.midiInstrument = #\"viola\" ")
	for n=1,5 do
		table.insert(mdati,mub[n][numeri[n+6]])
		table.insert(mdati,' | ')
	end
	table.insert(mdati," c'1 | ")
	table.insert(mdati,"} \\score { \\new PianoStaff << \\new Staff = \"upper\" \\upper \\new Staff = \"lower\" \\lower >> \\layout { \\context { \\Score \\remove \"Metronome_mark_engraver\" } } \\midi { } }")
	return table.concat(mdati,'\n')
end

function p._gioco(args)
	local num0 = args.numeri or false
	local m_args = { raw= "1", vorbis="1" }
	local m_dati = bach(num0)
	if args.debug then
		local mdebug = mw.text.jsonEncode(args)
		return string.format('<pre>%s\n\n%s</pre>', mdebug, m_dati)
	else
		return mw.getCurrentFrame():extensionTag('score', m_dati, m_args)
	end
end

-- Funzione per il template {{Mappa OSM}}.
function p.gioco(frame)
	return select(2, xpcall(function()
		return p._gioco(getArgs(frame, { parentOnly = true }))
	end, errhandler))
end

return p