summaryrefslogtreecommitdiff
path: root/triggers.lua
blob: 0f985fed9217d4f3166ee7d95338ab01b226e99d (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
--	AWARDS
--	   by Rubenwardy, CC-BY-SA
-------------------------------------------------------
-- this is the trigger handler file for the awards mod
-------------------------------------------------------

-- Function and table holders for Triggers
awards.onDig={}
awards.onPlace={}
awards.onTick={}
awards.onDeath={}

-- Player functions

-- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger)
	local nodedug = string.split(oldnode.name, ":")

	local mod=nodedug[1]
	local item=nodedug[2]

	local playern = digger:get_player_name()

	if (playern~=nil and nodedug~=nil and mod~=nil and item~=nil) then
		--check the player's directory
		if not player_data[playern] then
        		player_data[playern]={}
			player_data[playern]['count']={}
			player_data[playern]['name']=playern
	        end

                --check player.count.mod
		if not player_data[playern]['count'][mod] then
        		player_data[playern]['count'][mod]={}
	        end

		--check player.count.mod.item
	        if not player_data[playern]['count'][mod][item] then
        		player_data[playern]['count'][mod][item]=0
	        end

		player_data[playern]['count'][mod][item]=player_data[playern]['count'][mod][item]+1

		print(" - "..mod..":"..item.." 's count is now "..(player_data[playern]['count'][mod][item]))
		
		-- Roll through the onDig functions
		local player=digger
		local data=player_data[playern]

		for i=1,# awards.onDig do
			local res=nil
			
			if type(awards.onDig[i]) == "function" then
				-- run the function
				print(i.." is a function")
				res=awards.onDig[i](player,data)
			elseif type(awards.onDig[i]) == "table" then
				-- handle table here
				print(i.." is a table")
				if not awards.onDig[i]['node'] or not awards.onDig[i]['target'] or not awards.onDig[i]['award'] then
					-- table running failed!
				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~=nil then
				awards.give_achievement(playern,res)
			end
		end
	end
end)

minetest.register_on_placenode(function(pos, newnode, placer)
	local nodedug = string.split(newnode.name, ":")

	local mod=nodedug[1]
	local item=nodedug[2]

	local playern = placer:get_player_name()

	if (playern~=nil and nodedug~=nil and mod~=nil and item~=nil) then
		--check the player's directory
		if not player_data[playern] then
        		player_data[playern]={}
			player_data[playern]['place']={}
			player_data[playern]['name']=playern
	        end

                --check player.count.mod
		if not player_data[playern]['place'][mod] then
        		player_data[playern]['place'][mod]={}
	        end

		--check player.count.mod.item
	        if not player_data[playern]['place'][mod][item] then
        		player_data[playern]['place'][mod][item]=0
	        end

		player_data[playern]['place'][mod][item]=player_data[playern]['place'][mod][item]+1

		print(" - "..mod..":"..item.." 's place is now "..(player_data[playern]['place'][mod][item]))
		
		-- Roll through the onDig functions
		local player=placer
		local data=player_data[playern]

		for i=1,# awards.onPlace do
			local res=nil

			if type(awards.onPlace[i]) == "function" then
				-- run the function
				print(i.." is a function")
				res=awards.onPlace[i](player,data)
			elseif type(awards.onPlace[i]) == "table" then
				-- handle table here
				print(i.." is a table")
				if not awards.onPlace[i]['node'] or not awards.onPlace[i]['target'] or not awards.onPlace[i]['award'] then
					-- table running failed!
				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~=nil then
				awards.give_achievement(playern,res)
			end
		end
	end
end)

minetest.register_on_dieplayer(function(player)
        player_data[player:get_player_name()]['deaths']=player_data[player:get_player_name()]['deaths']+1
	
	-- Set up the variables
	local playern=player:get_player_name()
	local data=player_data[playern]
	
	-- Roll through the onDeath functions
	for i=1,# awards.onDeath do
		local res=nil
		if type(awards.onDeath[i]) == "function" then
			-- run the function
			print(i.." is a function")
			res=awards.onDeath[i](player,data)
		elseif type(awards.onDeath[i]) == "table" then
			-- handle table here
			print(i.." is a table")
			if not awards.onDeath[i]['target'] or not awards.onDeath[i]['award'] then
				-- table running failed!
			else
				-- run the table

				if not data['deaths'] then
					-- table running failed!
				elseif data['deaths'] > awards.onDeath[i]['target']-1 then
					res=awards.onDeath[i]['award']
				end
			end
		end

		if res~=nil then
			awards.give_achievement(playern,res)
		end
	end
end)

minetest.register_on_newplayer(function(player)
	--Player data root
	player_data[player:get_player_name()]={}
	player_data[player:get_player_name()]['name']=player:get_player_name()
	player_data[player:get_player_name()]['deaths']=0
	
	--The player counter
	player_data[player:get_player_name()]['count']={}
	player_data[player:get_player_name()]['place']={}

	--Table to contain achievement records
	player_data[player:get_player_name()]['unlocked']={}
end)

minetest.register_on_shutdown(function()
    save_playerD()
end)