-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxp.moon
145 lines (124 loc) · 4.39 KB
/
xp.moon
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
-- Copyright (c) 2014 by Adam Hellberg <[email protected]>.
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
NAME, T = ...
{
comm_manager: comm
event_manager: em
localization: L
:log
misc:
:fix_name, :is_any_nil
} = T
{
:GetExpansionLevel, :GetRealmName, :GetNumSubgroupMembers
:IsInGroup
:LE_PARTY_CATEGORY_HOME
:next
:UnitLevel, :UnitName, :UnitXP, :UnitXPMax
} = _G
T.xp_manager =
max_levels:
[0]: 60 -- Classic/Vanilla
[1]: 70 -- TBC
[2]: 80 -- WotLK
[3]: 85 -- Cataclysm
[4]: 90 -- MoP
[5]: 100 -- WoD
max_level: 255 -- Placeholder/fallback value replaced at load
data: {}
has_data: =>
next(@data) != nil
get_self: =>
current_xp = UnitXP 'player'
max_xp = UnitXPMax 'player'
{
level: UnitLevel 'player'
max_level: @max_level
:current_xp
:max_xp
percentage: math.floor (current_xp / max_xp) * 100 + 0.5
}
update: (name, current_level, max_level, current_xp, max_xp) =>
name = fix_name name
current_level = tonumber current_level
max_level = tonumber max_level
current_xp = tonumber current_xp
max_xp = tonumber max_xp
if is_any_nil current_level, max_level, current_xp, max_xp
return log\error L.xp.invalid_data, name
unless @data[name]
@data[name] = {}
realm = name\match '-(.+)$'
local_realm = GetRealmName!\gsub '%s', ''
@data[name].friendly_name = (realm == local_realm) and name\match('(.+)-') or name
data = with @data[name]
.current_level = current_level
.max_level = max_level
.current_xp = current_xp
.max_xp = max_xp
.percentage = math.floor (current_xp / max_xp) * 100 + 0.5
em\fire 'SHAREXP_XP_UPDATED'
log\debug L.xp_updated, data.friendly_name
send_xp: =>
lvl = UnitLevel 'player'
xp = UnitXP 'player'
max_xp = UnitXPMax 'player'
comm\send 'PARTY', 'xpupdate', lvl, @max_level, xp, max_xp
log\debug L.xp_sent_update
request_xp: =>
comm\send 'PARTY', 'xprequest'
log\debug L.xp_sent_request
check_group: =>
num = GetNumSubgroupMembers LE_PARTY_CATEGORY_HOME
group = {}
for i = 1, num
unit = "party#{i}"
name, realm = UnitName unit
name = fix_name name, realm
group[name] = true
for k, _ in pairs @data
unless group[k]
log\debug L.xp_removing, k
@data[k] = nil
em\fire 'SHAREXP_XP_UPDATED'
xp = T.xp_manager
comm\register_callback 'xpupdate', (kind, channel, sender, ...) ->
-- ARGS:
-- 1: current level
-- 2: max level
-- 3: current xp
-- 4: max xp
log\debug L.xp_received_update, channel, sender
xp\update sender, ...
comm\register_callback 'xprequest', (kind, channel, sender) ->
log\debug L.xp_received_request, channel, sender
xp\send_xp!
T.PLAYER_ENTERING_WORLD = ->
xp\send_xp!
xp\request_xp! if IsInGroup!
T.PLAYER_XP_UPDATE = ->
xp\send_xp!
T.GROUP_ROSTER_UPDATE = ->
xp\send_xp!
xp\check_group!
T.SHAREXP_LOADED = ->
expansion_level = GetExpansionLevel!
max_lvl = xp.max_levels[expansion_level]
xp.max_level = max_lvl