aboutsummaryrefslogtreecommitdiff
path: root/ch_core/timers.lua
blob: 59a0dcb0501d23dda079a74ecfd0675f2eb2eea2 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
ch_core.open_submod("timers", {data = true, hud = true, chat = true})

ch_core.count_of_ch_timer_hudbars = 4

local default_timer_bar_icon = "default_snowball.png"
local default_timer_bar_bar = "hudbars_bar_timer.png"

-- timer_def - podporované klíče:
--  - label -- textový popis pro HUD, může být prázdný řetězec (volitelná)
--  - func -- funkce bez parametrů, která má být spuštěna po vypršení časovače; může být nil
--  - hudbar_icon -- ikona pro HUD (volitelná)
--  - hudbar_bgicon -- ikona pro HUD na pozadí (volitelná)
--  - hudbar_bar -- textura pro HUD (volitelná)
--  - hudbar_text_color -- barva textu pro HUD (volitelná)
function ch_core.start_ch_timer(online_charinfo, timer_id, delay, timer_def)
	local timers = online_charinfo.ch_timers
	if not timers then
		timers = {}
		online_charinfo.ch_timers = timers
	elseif timers[timer_id] then
		return false -- timer is already running
	end
	if delay < 0.0 then
		error("Negative delay at start_ch_timer()!")
	end
	local now = ch_core.cas
	local new_timer = timer_def or {}
	new_timer = table.copy(new_timer)
	new_timer.started_at = now
	new_timer.run_at = now + delay
	timers[timer_id] = new_timer
	local player = minetest.get_player_by_name(online_charinfo.player_name)
	local hudbar_id = player and ch_core.try_alloc_hudbar(player)
	if hudbar_id then
		new_timer.hudbar = hudbar_id
		new_timer.last_hud_value = math.ceil(delay)
		hb.change_hudbar(player,
			hudbar_id,
			new_timer.last_hud_value,
			new_timer.last_hud_value,
			new_timer.hudbar_icon or default_timer_bar_icon,
			new_timer.hudbar_bgicon,
			new_timer.hudbar_bar or default_timer_bar_bar,
			new_timer.label or "časovač",
			new_timer.hudbar_text_color or 0xFFFFFF)
		if delay >= 0.1 then
			hb.unhide_hudbar(player, hudbar_id)
		end
	end
	return true
end

function ch_core.is_ch_timer_running(online_charinfo, timer_id)
	local timers = online_charinfo.ch_timers
	if timers and timers[timer_id] then
		return true
	else
		return false
	end
end

function ch_core.get_ch_timer_info(online_charinfo, timer_id)
	local timers = online_charinfo.ch_timers
	return timers and timers[timer_id]
end

function ch_core.cancel_ch_timer(online_charinfo, timer_id)
	local timers = online_charinfo.ch_timers
	local timer = timers and timers[timer_id]
	if not timer then
		return false
	end
	if timer.hudbar then
		local player = minetest.get_player_by_name(online_charinfo.player_name)
		hb.hide_hudbar(player, timer.hudbar)
		ch_core.free_hudbar(player, timer.hudbar)
	end
	timers[timer_id] = nil
	return true
end

local def = {
	params = "[početsekund]",
	description = "Odpočítá zadaný počet sekund. Při použití bez parametru jen zruší probíhající odpočet.",
	privs = {},
	func = function(player_name, param)
		local online_charinfo = ch_data.online_charinfo[player_name]
		if param == "" then
			ch_core.cancel_ch_timer(online_charinfo, "custom_timer")
			return true
		end
		local sekund = param:gsub(",", ".")
		sekund = tonumber(sekund)
		if not sekund or sekund < 0 then
			return false, "Neplatné zadání!"
		end
		if not online_charinfo then
			return false, "Vnitřní chyba serveru."
		end
		ch_core.cancel_ch_timer(online_charinfo, "custom_timer")
		local timer_func = function()
			local player = minetest.get_player_by_name(player_name)
			if player ~= nil then
				ch_core.systemovy_kanal(player_name, "Nastavený časovač vypršel.")
				minetest.sound_play("chat3_bell", {to_player = player_name, gain = 1.0}, true)
			end
		end
		local timer_def = {
			label = "odpočet",
			func = timer_func,
		}
		local watch_def = minetest.registered_items["orienteering:watch"]
		if watch_def ~= nil and watch_def.inventory_image ~= nil then
			timer_def.hudbar_icon = watch_def.inventory_image
		end
		ch_core.start_ch_timer(online_charinfo, "custom_timer", sekund, timer_def)
		return true
	end,
}
minetest.register_chatcommand("odpočítat", def)
minetest.register_chatcommand("odpocitat", def)

ch_core.close_submod("timers")