-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogression_system.lua
101 lines (79 loc) · 3.66 KB
/
progression_system.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
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
Progression = {
exponent = 1.15,
baseXP = 200
}
addEvent("Odin:Accounts:onPlayerLoggedIn", true)
--[[
Player Progression System
USAGE: See functions below
Introduces a leveling system that can be interpreted into any gaming platform with slight adjustments and enables players to gain levels when they perform an action.
]]
function Progression.onPlayerLogin()
local level = Accounts.getData(Accounts.getPlayerAccount(source), "level") or 0
local xp = Accounts.getData(Accounts.getPlayerAccount(source), "xp") or 0
source:setData("level", level, true)
source:setData("xp", xp, true)
end
addEventHandler("Odin:Accounts:onPlayerLoggedIn", root, Progression.onPlayerLogin)
function Progression.getPlayerXP(player)
if not player or not isElement(player) then return false end
if not Accounts.isPlayerLoggedIn(player) then return false end
local xp = Accounts.getData(Accounts.getPlayerAccount(player), "xp") or 0
return xp
end
function Progression.getPlayerLevel(player)
if not player or not isElement(player) then return false end
if not Accounts.isPlayerLoggedIn(player) then return false end
local level = Accounts.getData(Accounts.getPlayerAccount(player), "level") or 1
return level
end
function Progression.setPlayerXP(player, xp)
if not player or not isElement(player) then return false end
if not Accounts.isPlayerLoggedIn(player) then return false end
Accounts.setData(Accounts.getPlayerAccount(player), "xp", xp)
return true
end
function Progression.setPlayerLevel(player, level)
if not player or not isElement(player) then return false end
if not Accounts.isPlayerLoggedIn(player) then return false end
if not level or type(level) ~= "number" then return false end
Accounts.setData(Accounts.getPlayerAccount(player), "level", level)
player:setData("level", level, true)
return true
end
function Progression.givePlayerXP(player, xp)
if not player or not isElement(player) then return false end
if not Accounts.isPlayerLoggedIn(player) then return false end
if not xp or type(xp) ~= "number" then return false end
local playerXP = Progression.getPlayerXP(player)
local level = Progression.getPlayerLevel(player)
--Add XP before calling checkLevelUpProgress
Progression.setPlayerXP(player, playerXP+xp)
--Check progression of his leveling.
if (Progression.checkLevelUpProgress(player)) then --Level up!
--Calculate the excess for that level, before resetting.
local xpExcess = Progression.getNextLevelXP(level)
xpExcess = (playerXP+xp) - xpExcess --Excess should now be on there
Progression.setPlayerXP(player, xpExcess)
Progression.setPlayerLevel(player, level+1)
Player.createNotification(player, "Leveled up!", "You're now level "..Progression.getPlayerLevel(player).."!")
triggerEvent("Odin:Progression:onPlayerLeveledUp", player, Progression.getPlayerLevel(player), playerXP, Progression.getPlayerXP(player))
System.callClient(player, "Odin:Progression:onPlayerGainedXP", Progression.getPlayerLevel(player), 0, Progression.getPlayerXP(player))
else
System.callClient(player, "Odin:Progression:onPlayerGainedXP", Progression.getPlayerLevel(player), playerXP, Progression.getPlayerXP(player))
end
return true
end
function Progression.getNextLevelXP(level)
return Utils.round(Progression.baseXP * (level ^ Progression.exponent))
end
function Progression.checkLevelUpProgress(player)
local xp = Progression.getPlayerXP(player) or 0
local level = Progression.getPlayerLevel(player) or 0
--Check if the XP exceeds next level's XP. If so, return true to process level announcement
if (xp >= Progression.getNextLevelXP(level)) then
return true --Process level up in other function
else
return false --Not ready to level up, but add XP anyways.
end
end