-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrindy.lua
149 lines (117 loc) · 3.26 KB
/
Grindy.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
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
local Grindy = CreateFrame("Frame");
local Player, Cache, Rate, Info, Colors;
Player = { StartTime = GetTime() }
Info = {}
Cache = {}
Rate = { Change = 0, Current = 0 }
Colors = {
green = "FF10C15C",
red = "FFD05353",
blue = "FF227C9D",
yellow = "FFFFCB77",
white = "FFFFFFFF"
}
function Colors:Color(str, color)
return "|C"..color..""..str..""
end
function Colors:Green(str)
return self:Color(str, Colors.green)
end
function Colors:Red(str)
return self:Color(str, Colors.red)
end
function Colors:Blue(str)
return self:Color(str, Colors.blue)
end
function Colors:Yellow(str)
return self:Color(str, Colors.yellow)
end
function Colors:White(str)
return self:Color(str, Colors.white)
end
function Cache:Init()
self = {}
end
function Cache:Add(key, time)
local total = self[key] ~= nil and self[key].Count + 1 or 1
self[key] = {
Count = total,
Time = time
}
end
function Info:From(message)
local PATTERN_XP_GAINED = "(%d+)"
local PATTERN_UNIT_NAME = "(.+).dies"
local amount = tonumber(string.match(message, PATTERN_XP_GAINED))
local name = string.match(message, PATTERN_UNIT_NAME)
local key = name:lower():gsub("%s", "_")
return name, key, amount
end
function Rate:Init()
self.Change = 0
self.Current = 0
end
function Rate:Update(rate)
self.Change = (rate - self.Current)
self.Current = rate
end
function Player:Init()
self.StartTime = GetTime()
end
function Player:Update(amount)
self.Current = UnitXP("player")
self.Max = UnitXPMax("player")
self.Latest = amount
self.Total = self.Total ~= nil and self.Total + amount or amount
self.ToLevel = self.Max - self.Current
end
local function format_number(amount)
return string.gsub(amount, "^(-?%d+)(%d%d%d)", '%1,%2')
end
function Grindy:Handle(...)
local message = ...
local name, key, amount = Info:From(message)
if amount > 0 then
Player:Update(amount)
local time = GetTime()
local rate = ceil(Player.Total / ((time - Player.StartTime) / 60 / 60))
local to_level = ceil(Player.ToLevel / amount)
Rate:Update(rate)
local to_level_str = format_number(to_level)
local rate_current_str = format_number(Rate.Current)
local rate_change_str = format_number(Rate.Change)
local change_color = Rate.Change == 0 and Colors.blue or
(Rate.Change < 0 and Colors.red or Colors.green)
Cache:Add(key, time)
self:AddMessage(""..Colors:Yellow(to_level_str).." "..Colors:White(name).." "..Colors:Blue("to level.").."")
self:AddMessage("Current Rate: "..Colors:Yellow(rate_current_str).." xp/h ("..Colors:Color(rate_change_str, change_color)..")")
end
end
function Grindy:AddMessage(message)
DEFAULT_CHAT_FRAME:AddMessage(""..Colors:Blue("[Grindy] ")..""..message.."")
end
function Grindy:Init()
Cache:Init()
Player:Init()
Rate:Init()
self:AddMessage("Initialised!")
end
function SlashHandler(arg1)
if arg1:lower():find("reset") then
Grindy:Init()
else
Grindy:AddMessage("Option not found.")
end
end
SlashCmdList["GrindyCommand"] = SlashHandler;
SLASH_GrindyCommand1 = "/grindy"
Grindy:RegisterEvent("PLAYER_LOGIN")
Grindy:RegisterEvent("CHAT_MSG_COMBAT_XP_GAIN")
Grindy:SetScript("OnEvent",
function(self, event, ...)
if event == "CHAT_MSG_COMBAT_XP_GAIN" then
self:Handle(...)
end
end
)
Grindy:Init()