aboutsummaryrefslogtreecommitdiff
path: root/init.lua
blob: 8097646246377a2eed3714ab3b1278279fe8e635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
karaoke = {}

local storage = minetest.get_mod_storage("karaoke")
local etime, lines

function karaoke.remaining()
	return lines
end

function karaoke.sing(title)
	local text = storage:get_string(title)

	if text == "" then
		return false, "Song not found"
	end

	lines = text:split("\n")
	etime = 0
end

minetest.register_chatcommand("kedit", {
	func = function(param)
		minetest.show_formspec("karaoke", [[
			size[9,5;]
			field[0.5,0.3;8.5,1;title;Song Title;]] .. param .. [[]
			textarea[0.5,1.1;8.5,4;text;Song Text;]] .. storage:get_string(param) .. [[]
			button_exit[3.3,4.5;2,1;save;Save]
		]])
	end,
})

minetest.register_chatcommand("kdelete", {
	func = function(param)
		storage:set_string(param, "")
		return true, "Song deleted: " .. param
	end,
})

minetest.register_chatcommand("ksing", {
	func = karaoke.sing,
})

minetest.register_chatcommand("kcancel", {
	func = function()
		etime, lines = nil
	end,
})

minetest.register_chatcommand("klist", {
	func = function()
		local songs = {}

		for k in pairs(storage:to_table().fields) do
			table.insert(songs, k)
		end

		return true, table.concat(songs, ", ")
	end,
})

minetest.register_on_formspec_input(function(formname, fields)
	if formname == "karaoke" and fields.title and fields.text and fields.title ~= "" and fields.text ~= "" then
		storage:set_string(fields.title, fields.text)
		print("Song saved: " .. fields.title)
	end
end)

minetest.register_globalstep(function(dtime)
	if lines then
		etime = etime - dtime

		if etime < 0 then
			local line = table.remove(lines, 1)

			if line then
				minetest.send_chat_message("/me " .. minetest.colorize("#C609FF", line) .. "")
				etime = line:len() * 0.1
			else
				etime, lines = nil
			end
		end
	end
end)