-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersonalRollLoot.lua
238 lines (218 loc) · 7.82 KB
/
PersonalRollLoot.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
-- namespace
local _, ns = ...;
-- imports
local Instances = ns.Instances
local ITEM_LIST = ns.ITEM_LIST
local Items = ns.Items
local Players = ns.Players
local Raids = ns.Raids
local Roles = ns.Roles
local utils = ns.utils
local MasterUI = ns.MasterUI
local MemberUI = ns.MemberUI
-- ------------------------------------------------------- --
-- slash commands --
-- ------------------------------------------------------- --
local COMMANDS = {
["add-player"] = Players.add,
["remove-player"] = Players.remove,
["player-info"] = function(arg)
local player = Players.get(arg)
if (player) then
player:print()
end
end,
["add-role"] = function(arg)
arg = arg or ""
local arg1, arg2 = strsplit(" ", arg, 2)
local player = Players.get(arg1)
if (player) then
local name = player.name
local role = arg2
if (Roles.checkRoleId(role)) then
-- add role to player
player.roles[role] = true
print("> Added role '"..role.."' to player '"..name.."'.")
end
end
end,
["remove-role"] = function(arg)
arg = arg or ""
local arg1, arg2 = strsplit(" ", arg, 2)
local player = Players.get(arg1)
if (player) then
local name = player["name"]
local role = arg2
if (Roles.checkRoleId(role)) then
-- remove role from player
player["roles"][role] = nil
print("> Removed role '"..role.."' from player '"..name.."'.")
end
end
end,
["add-item"] = function(arg)
arg = arg or ""
local arg1, arg2 = strsplit(" ", arg, 2)
local player = Players.get(arg1)
if (player) then
local item = Items.get(arg2)
if (item) then
-- add item to player
if (not player:addItem(item)) then
error("> Item '"..item.name.."' ("..item.itemId..") is not assigned to the class '"..player.class.."'.", 0)
end
print("> Added item '"..item.name.."' ("..item.itemId..") to player '"..player.name.."'.")
end
end
end,
["remove-item"] = function(arg)
arg = arg or ""
local arg1, arg2 = strsplit(" ", arg, 2)
local player = Players.get(arg1)
if (player) then
local item = Items.get(arg2)
if (item) then
local name = player.name
-- remove item from player
if (player:removeItem(item)) then
print("> Removed item '"..item.name.."' ("..item.itemId..") from player '"..name.."'.")
else
print("> The item '"..item.name.."' ("..item.itemId..") was not on the need-list for player '"..name.."'.")
end
end
end
end,
["create-instance"] = function(arg)
arg = arg or ""
local name, raidName = strsplit(" ", arg, 2)
Instances.create(name, { [raidName] = true })
end,
["delete-instance"] = Instances.delete,
["instance-info"] = function(arg)
-- no arguments, print all instances
if (not arg) then
local empty = true
for name,instance in pairs(ns.DB.INSTANCE_LIST) do
instance:print()
empty = false
end
if (empty) then print("> No instances found.") end
else
local instance = Instances.get(arg)
if (instance) then
instance:print()
end
end
end,
["active-instance"] = Instances.activate,
["invite"] = Instances.invite,
["roll"] = function(arg)
local rollOrder = Instances.roll(arg)
if (rollOrder) then
rollOrder:print()
end
end
}
-- ------------------------------------------------------- --
-- register the slash commands and call the command table --
-- ------------------------------------------------------- --
SLASH_PersonalRollLootMember1 = "/prl"
SLASH_PersonalRollLootMember2 = "/personal"
SlashCmdList["PersonalRollLootMember"] = function(s)
MemberUI.toggleUI()
end
SLASH_PersonalRollLootMaster1 = "/prlm"
SLASH_PersonalRollLootMaster2 = "/personalmaster"
SlashCmdList["PersonalRollLootMaster"] = function(s)
local cmd, args = strsplit(" ", s, 2)
local c = COMMANDS[cmd]
if c then
local status, err = pcall(c, args)
if (not status) then print(err) end
else
-- no command specified, open the UI
MasterUI.toggleUI()
end
end
-- ------------------------------------------------------- --
-- event handling --
-- ------------------------------------------------------- --
local function updateLootItems()
if (IsInGroup()) then
local lootItems = Items.getLootItems()
if (UnitIsGroupLeader("player")) then
-- add the items to the loot history of the current instance
if (ns.DB.activeInstance) then
local instance = ns.DB.INSTANCE_LIST[ns.DB.activeInstance]
if (instance) then
for itemId, item in pairs(lootItems) do
instance:addItem(item)
end
end
end
end
-- update the UI
MasterUI.setLootItems(lootItems)
MemberUI.setLootItems(lootItems)
end
end
local function parseLootMessage(msg, member)
if (msg and member) then
-- remove the realm part from the member
local playerName = strsplit("-", member, 2)
local player = ns.DB.PLAYER_LIST[playerName]
if (player) then
local itemId = Items.getItemIdsFromChat(msg)
if (itemId) then
local item = ITEM_LIST[itemId]
if (item and not item.repeatable) then
Items.removeFromPlayer(player, item)
end
end
end
end
end
local function inspectTarget()
local unit = "target"
local playerName = UnitName(unit)
if (playerName) then
local player = ns.DB.PLAYER_LIST[playerName]
if (player and CheckInteractDistance(unit,1) and CanInspect(unit)) then
for i = 1, 24 do
local itemId = GetInventoryItemID(unit, i)
if (itemId) then
local item = ITEM_LIST[itemId]
Items.removeFromPlayer(player, item)
end
end
end
end
end
-- for testing
local test
SLASH_PersonalRollLootTest1 = "/test"
SlashCmdList["PersonalRollLootTest"] = function(s) test(s) end
test = function()
print("test function")
end
-- create an event frame
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("VARIABLES_LOADED")
eventFrame:RegisterEvent("INSPECT_READY")
eventFrame:RegisterEvent("CHAT_MSG_LOOT")
eventFrame:RegisterEvent("LOOT_OPENED")
eventFrame:RegisterEvent("LOOT_SLOT_CLEARED")
function eventFrame:OnEvent(event, arg1, arg2, arg3, arg4, arg5, ...)
if (event == "VARIABLES_LOADED") then
ns.loadSavedVariables()
elseif (event == "LOOT_OPENED" or event == "LOOT_SLOT_CLEARED") then
updateLootItems()
elseif (event == "CHAT_MSG_LOOT") then
-- arg5 "playerName2" - Name of player who received the loot
parseLootMessage(arg1, arg5)
elseif (event == "INSPECT_READY") then
inspectTarget()
end
end
eventFrame:SetScript("OnEvent", eventFrame.OnEvent)
print("Addon loaded... PersonalRollLoot "..GetAddOnMetadata("PersonalRollLoot", "Version"))