-
Notifications
You must be signed in to change notification settings - Fork 8
/
xp_priv.lua
75 lines (61 loc) · 2.04 KB
/
xp_priv.lua
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
local privs = {
{ xp = 10000, name = "areas_protect" },
{ xp = 10000, name = "moon_access" },
{ xp = 15000, name = "blockexchange_protected_upload" },
{ xp = 20000, name = "asteroids_access" },
{ xp = 40000, name = "lavastone_remove" },
{ xp = 50000, name = "mars_access" },
{ xp = 50000, name = "train_operator" },
{ xp = 50000, name = "train_place" },
{ xp = 50000, name = "track_builder" },
{ xp = 50000, name = "railway_operator" },
{ xp = 100000, name = "warzone_access" },
{ xp = 200000, name = "spacecannon_unrestricted" },
{ xp = 300000, name = "missions_teleport" },
{ xp = 450000, name = "jumpdrive_land" },
{ xp = 500000, name = "areas_high_limit" }
}
-- playername -> time_of_last_check
local last_check_map = {}
local function check_autogrant(playername, xp)
local now = os.time()
local last_check = last_check_map[playername]
if last_check and (now - last_check) < 2 then
-- check was executed shortly before, wait a bit to re-check again
-- in case player is using a digtron or a fast tool
return
end
last_check_map[playername] = now
-- get player priv map
local player_privs = minetest.get_player_privs(playername)
-- dirty flag
local changed = false
-- loop over privs and grant priv if xp reached and not granted yet
for _, p in ipairs(privs) do
if xp > p.xp and not player_privs[p.name] then
-- xp threshold exceeded and priv not yet granted
player_privs[p.name] = true
changed = true
minetest.chat_send_player(playername, "XP Granted priv acquired: " .. p.name)
end
end
if changed then
-- apply changed privs
minetest.set_player_privs(playername, player_privs)
end
end
-- add xp_redo hook
xp_redo.register_hook({
xp_change = check_autogrant
})
-- check privs upon joining
minetest.register_on_joinplayer(function(player)
local playername = player:get_player_name()
local xp = xp_redo.get_xp(playername)
check_autogrant(playername, xp)
end)
-- clear last_check cache
minetest.register_on_leaveplayer(function(player)
local playername = player:get_player_name()
last_check_map[playername] = nil
end)