summaryrefslogtreecommitdiff
path: root/triggers.lua
blob: f4fb994ce092dcfb1004d7efc21aaf2c0b3b09d2 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
-- AWARDS
--    by Rubenwardy
-------------------------------------------------------
-- this is the trigger handler file for the awards mod
-------------------------------------------------------

-- Function and table holders for Triggers
awards.onDig = {}
awards.onPlace = {}
awards.onCraft = {}
awards.onChat = {}
awards.onDeath = {}
awards.onJoin = {}

-- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger)
	if not digger or not pos or not oldnode then
		return
	end
	local nodedug = string.split(oldnode.name, ":")
	if #nodedug ~= 2 then
		--minetest.log("error","Awards mod: "..oldnode.name.." is in wrong format!")
		return
	end
	local mod = nodedug[1]
	local item = nodedug[2]
	local playern = digger:get_player_name()

	if (not playern or not nodedug or not mod or not item) then
		return
	end
	awards.assertPlayer(playern)
	awards.tbv(awards.players[playern].count, mod)
	awards.tbv(awards.players[playern].count[mod], item, 0)

	-- Increment counter
	awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item] + 1

	-- Run callbacks and triggers
	local player=digger
	local data=awards.players[playern]
	for i=1,# awards.onDig do
		local res = nil
		if type(awards.onDig[i]) == "function" then
			-- Run trigger callback
			res = awards.onDig[i](player,data)
		elseif type(awards.onDig[i]) == "table" then
			-- Handle table trigger
			if not awards.onDig[i].node or not awards.onDig[i].target or not awards.onDig[i].award then
				-- table running failed!
				print("[ERROR] awards - onDig trigger "..i.." is invalid!")
			else
				-- run the table
				local tnodedug = string.split(awards.onDig[i].node, ":")
				local tmod=tnodedug[1]
				local titem=tnodedug[2]
				if tmod==nil or titem==nil or not data.count[tmod] or not data.count[tmod][titem] then
					-- table running failed!
				elseif data.count[tmod][titem] > awards.onDig[i].target-1 then
					res=awards.onDig[i].award
				end
			end
		end

		if res then
			awards.give_achievement(playern,res)
		end
	end
end)

minetest.register_on_placenode(function(pos,node,digger)
	if not digger or not pos or not node or not digger:get_player_name() or digger:get_player_name()=="" then
		return
	end
	local nodedug = string.split(node.name, ":")
	if #nodedug ~= 2 then
		--minetest.log("error","Awards mod: "..node.name.." is in wrong format!")
		return
	end
	local mod=nodedug[1]
	local item=nodedug[2]
	local playern = digger:get_player_name()

	-- Run checks
	if (not playern or not nodedug or not mod or not item) then
		return
	end
	awards.assertPlayer(playern)
	awards.tbv(awards.players[playern].place, mod)
	awards.tbv(awards.players[playern].place[mod], item, 0)

	-- Increment counter
	awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item] + 1

	-- Run callbacks and triggers
	local player = digger
	local data = awards.players[playern]
	for i=1,# awards.onPlace do
		local res = nil
		if type(awards.onPlace[i]) == "function" then
			-- Run trigger callback
			res = awards.onPlace[i](player,data)
		elseif type(awards.onPlace[i]) == "table" then
			-- Handle table trigger
			if not awards.onPlace[i].node or not awards.onPlace[i].target or not awards.onPlace[i].award then
				print("[ERROR] awards - onPlace trigger "..i.." is invalid!")
			else
				-- run the table
				local tnodedug = string.split(awards.onPlace[i].node, ":")
				local tmod = tnodedug[1]
				local titem = tnodedug[2]
				if tmod==nil or titem==nil or not data.place[tmod] or not data.place[tmod][titem] then
					-- table running failed!
				elseif data.place[tmod][titem] > awards.onPlace[i].target-1 then
					res = awards.onPlace[i].award
				end
			end
		end

		if res then
			awards.give_achievement(playern,res)
		end
	end
end)

minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
	if not player or not itemstack then
		return
	end
	local itemcrafted = string.split(itemstack:get_name(), ":")
	if #itemcrafted ~= 2 then
		--minetest.log("error","Awards mod: "..itemstack:get_name().." is in wrong format!")
		return
	end
	local mod = itemcrafted[1]
	local item = itemcrafted[2]
	local playern = player:get_player_name()

	if (not playern or not itemcrafted or not mod or not item) then
		return
	end
	awards.assertPlayer(playern)
	awards.tbv(awards.players[playern].craft, mod)
	awards.tbv(awards.players[playern].craft[mod], item, 0)

	-- Increment counter
	awards.players[playern].craft[mod][item]=awards.players[playern].craft[mod][item] + 1

	-- Run callbacks and triggers
	local data=awards.players[playern]
	for i=1,# awards.onCraft do
		local res = nil
		if type(awards.onCraft[i]) == "function" then
			-- Run trigger callback
			res = awards.onDig[i](player,data)
		elseif type(awards.onCraft[i]) == "table" then
			-- Handle table trigger
			if not awards.onCraft[i].item or not awards.onCraft[i].target or not awards.onCraft[i].award then
				-- table running failed!
				print("[ERROR] awards - onCraft trigger "..i.." is invalid!")
			else
				-- run the table
				local titemcrafted = string.split(awards.onCraft[i].item, ":")
				local tmod=titemcrafted[1]
				local titem=titemcrafted[2]
				if tmod==nil or titem==nil or not data.craft[tmod] or not data.craft[tmod][titem] then
					-- table running failed!
				elseif data.craft[tmod][titem] > awards.onCraft[i].target-1 then
					res=awards.onCraft[i].award
				end
			end
		end

		if res then
			awards.give_achievement(playern,res)
		end
	end
end)

minetest.register_on_dieplayer(function(player)
	-- Run checks
	local name = player:get_player_name()
	if not player or not name or name=="" then
		return
	end
	
	-- Get player	
	awards.assertPlayer(name)
	local data = awards.players[name]

	-- Increment counter
	data.deaths = data.deaths + 1
	
	-- Run callbacks and triggers
	for _,trigger in pairs(awards.onDeath) do
		local res = nil
		if type(trigger) == "function" then
			res = trigger(player,data)
		elseif type(trigger) == "table" then
			if trigger.target and trigger.award then
				if data.deaths and data.deaths >= trigger.target then
					res = trigger.award
				end
			end
		end
		if res ~= nil then
			awards.give_achievement(name,res)
		end
	end
end)

minetest.register_on_joinplayer(function(player)
	-- Run checks
	local name = player:get_player_name()
	if not player or not name or name=="" then
		return
	end
	
	-- Get player	
	awards.assertPlayer(name)
	local data = awards.players[name]

	-- Increment counter
	data.joins = data.joins + 1
	
	-- Run callbacks and triggers
	for _,trigger in pairs(awards.onJoin) do
		local res = nil
		if type(trigger) == "function" then
			res = trigger(player,data)
		elseif type(trigger) == "table" then
			if trigger.target and trigger.award then
				if data.joins and data.joins >= trigger.target then
					res = trigger.award
				end
			end
		end
		if res ~= nil then
			awards.give_achievement(name,res)
		end
	end
end)

minetest.register_on_chat_message(function(name, message)
	-- Run checks
	local idx = string.find(message,"/")
	if not name or (idx ~= nil and idx <= 1)  then
		return
	end

	-- Get player
	awards.assertPlayer(name)
	local data = awards.players[name]
	local player = minetest.get_player_by_name(name)
	
	-- Increment counter
	data.chats = data.chats + 1
	
	-- Run callbacks and triggers	
	for _,trigger in pairs(awards.onChat) do
		local res = nil
		if type(trigger) == "function" then
			res = trigger(player,data)
		elseif type(trigger) == "table" then
			if trigger.target and trigger.award then
				if data.chats and data.chats >= trigger.target then
					res = trigger.award
				end
			end
		end
		if res ~= nil then
			awards.give_achievement(name,res)
		end
	end
end)

minetest.register_on_newplayer(function(player)
	local playern = player:get_player_name()
	awards.assertPlayer(playern)
end)

minetest.register_on_shutdown(function()
	awards.save()
end)