-
Notifications
You must be signed in to change notification settings - Fork 0
/
PayDay.lua
496 lines (448 loc) · 14.9 KB
/
PayDay.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
-- Used https://github.com/Gethe/wow-ui-source/blob/2.4.3/AddOns/Blizzard_TimeManager/Blizzard_TimeManager.lua
-- as a refernce.
SLASH_PAYDAY1 = "/payday"
SLASH_PAYDAY2 = "/pd"
local match = meowth.Match:New()
local stats = meowth.Stats:New()
local matchStarted = false
local maxRoll = false
local minRoll = false
local channelId = 2
local minimapPos = 10
local messages = {}
local iFrame = {} -- interface frame, the frame in Interface/AddOns
-- PayDay AddOn
function PrintChat(msg)
local header = messages.header.text
if channelId == 1 then
SendChatMessage(header..msg, "SAY", nil, GetChannelName("SAY"))
elseif channelId == 2 then
SendChatMessage(header..msg, "PARTY", nil, GetChannelName("PARTY"))
elseif channelId == 3 then
SendChatMessage(header..msg, "RAID", nil, GetChannelName("RAID"))
end
end
function EndMatch()
matchStarted = false
match = false
PayDayFrame:UnregisterEvent("CHAT_MSG_SAY")
PayDayFrame:UnregisterEvent("CHAT_MSG_PARTY")
PayDayFrame:UnregisterEvent("CHAT_MSG_RAID")
PayDayFrame:UnregisterEvent("CHAT_MSG_RAID_LEADER")
PayDayFrame:UnregisterEvent("CHAT_MSG_SYSTEM") -- this is where rolls come in
PayDayFrameButtonStartRoll:Disable()
PayDayFrameButtonEndMatch:Disable()
PayDayFrameButtonLastCall:Disable()
PayDayFrameButtonReminder:Disable()
PayDayFrameButtonStartMatch:Enable()
end
function PrintTieMembers()
PrintChat(messages.askToRoll.text)
local m = ""
for i, v in ipairs(match:GetWaitingList()) do
m = m..v..", "
end
if string.len(m) > 1 then
PrintChat(string.sub(m, 1, string.len(m) - 2))
end
end
function PayDay_OnLoad()
local version = GetAddOnMetadata("PayDay", "version")
DEFAULT_CHAT_FRAME:AddMessage(string.format("Pay Day loaded, version %s", version))
end
function PayDay_OnSlashCmd(msg, editBox)
local debugGamblers = {"Cartman", "Stan", "Kyle", "Kenny", "Randy", "Sharon", "Butters"}
if msg == "dj" then -- debug join
local outStr = ""
for i, v in ipairs(debugGamblers) do
PayDayFrame_OnEvent(PayDayFrame, "CHAT_MSG_SAY", "1", 0, 0, 0, v)
outStr = string.format("%s %s,", outStr, v)
end
outStr = string.sub(outStr, 1, string.len(outStr) - 1) -- Drop the extra comma.
SELECTED_CHAT_FRAME:AddMessage(string.format("%s joined the match", outStr))
elseif msg == "dr" then -- debug roll
local outStr = ""
for i, v in ipairs(debugGamblers) do
local roll = string.format("%s rolls %d (%d-%d)", v, random(minRoll, maxRoll), minRoll, maxRoll)
outStr = string.format("%s %s, ", outStr, roll)
PayDayFrame_OnEvent(PayDayFrame, "CHAT_MSG_SYSTEM", roll)
end
outStr = string.sub(outStr, 1, string.len(outStr) - 1) -- Drop the extra comma.
-- combined to reduce number of messages to server otherwise I get muted
SELECTED_CHAT_FRAME:AddMessage(outStr)
elseif msg == "version" then
local version = GetAddOnMetadata("PayDay", "version")
SELECTED_CHAT_FRAME:AddMessage(string.format("PayDay version %s", version))
else
PayDayFrame:Show()
end
end
function PayDay_ParseChat(name, msg)
if not matchStarted then return end
if msg == "1" then
match:AddGambler(name)
if #match:GetGamblers() >= 2 then
PayDayFrameButtonStartRoll:Enable()
end
elseif msg == "-1" then
match:RemoveGambler(name)
if #match:GetGamblers() < 2 then
PayDayFrameButtonStartRoll:Disable()
end
else
return
end
end
function PayDay_ParseRoll(msg)
-- returns true if the roll was used, false if it was dropped
if not (match.phase == "roll" or match.phase == "high tie" or match.phase == "low tie") then
return false
end
local i, j = string.find(msg, "rolls")
if i == nil then return false end
local name, rolls, mins, maxs = string.match(msg, "^(%w+)%srolls%s(%d+)%s%((%d+)-(%d+)%)")
local roll, min, max = tonumber(rolls), tonumber(mins), tonumber(maxs)
if min ~= minRoll or max ~= maxRoll then return false end
return match:AddRoll(name, roll)
end
function PayDay_CheckComplete(prevPhase, prevWaitCount)
if prevWaitCount == nil then
prevWaitCount = 0
end
if prevPhase == "roll" then
if match.phase == "high tie" then
PrintChat(messages.onHighTie.text)
PrintTieMembers()
elseif match.phase == "low tie" then
PrintChat(messages.onLowTie.text)
PrintTieMembers()
elseif match.phase == "all match" then
PrintChat(messages.onStandoff.text)
match:Reroll()
end
elseif prevPhase == "high tie" and match.phase ~= "complete"
and prevWaitCount - 1 ~= #match:GetWaitingList() then
if match.phase == "high tie" then
PrintChat(messages.on2ndHighTie.text)
PrintTieMembers()
elseif match.phase == "low tie" then
PrintChat(messages.onAlsoLowTie.text)
PrintTieMembers()
end
elseif prevPhase == "low tie" and match.phase ~= "complete"
and prevWaitCount - 1 ~= #match:GetWaitingList() then
if match.phase == "low tie" then
PrintChat(messages.on2ndLowTie.text)
PrintTieMembers()
end
end
if match.phase == "complete" then
PrintChat(messages.onMatchComplete.text)
local diff = match.highRoll - match.lowRoll
PrintChat(string.format(messages.onMatchCompleteOwe.text, match.lowGambler, diff, match.highGambler))
stats:AddMatch(match)
EndMatch()
end
end
-- PayDay Minimap
function PayDayButtonMinimap_OnLoad(self)
self:RegisterForDrag('LeftButton')
self:SetFrameLevel(8)
PayDayButtonMinimap_SetPosition(minimapPos)
end
function PayDayButtonMinimap_OnClick(self)
PayDayFrame_Toggle()
end
function PayDayButtonMinimap_OnDragStart(self)
self.dragging = true
self:LockHighlight()
self:SetScript('OnUpdate', PayDayButtonMinimap_UpdatePosition)
end
function PayDayButtonMinimap_OnDragStop(self)
self.dragging = nil
self:SetScript('OnUpdate', nil)
self:UnlockHighlight()
end
function PayDayButtonMinimap_UpdatePosition(self)
-- Most of this came from minimap.lua in the Bongos addon.
local mx, my = Minimap:GetCenter()
local px, py = GetCursorPosition()
local scale = Minimap:GetEffectiveScale()
px, py = px / scale, py / scale
minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360
local angle = math.rad(minimapPos)
local cos = math.cos(angle)
local sin = math.sin(angle)
local minimapShape = GetMinimapShape and GetMinimapShape() or 'ROUND'
local round = false
if minimapShape == 'ROUND' then
round = true
elseif minimapShape == 'SQUARE' then
round = false
elseif minimapShape == 'CORNER-TOPRIGHT' then
round = not(cos < 0 or sin < 0)
elseif minimapShape == 'CORNER-TOPLEFT' then
round = not(cos > 0 or sin < 0)
elseif minimapShape == 'CORNER-BOTTOMRIGHT' then
round = not(cos < 0 or sin > 0)
elseif minimapShape == 'CORNER-BOTTOMLEFT' then
round = not(cos > 0 or sin > 0)
elseif minimapShape == 'SIDE-LEFT' then
round = cos <= 0
elseif minimapShape == 'SIDE-RIGHT' then
round = cos >= 0
elseif minimapShape == 'SIDE-TOP' then
round = sin <= 0
elseif minimapShape == 'SIDE-BOTTOM' then
round = sin >= 0
elseif minimapShape == 'TRICORNER-TOPRIGHT' then
round = not(cos < 0 and sin > 0)
elseif minimapShape == 'TRICORNER-TOPLEFT' then
round = not(cos > 0 and sin > 0)
elseif minimapShape == 'TRICORNER-BOTTOMRIGHT' then
round = not(cos < 0 and sin < 0)
elseif minimapShape == 'TRICORNER-BOTTOMLEFT' then
round = not(cos > 0 and sin < 0)
end
local x, y
if round then
x = cos*80
y = sin*80
else
x = math.max(-82, math.min(110*cos, 84))
y = math.max(-86, math.min(110*sin, 82))
end
self:SetPoint('CENTER', x, y)
end
function PayDayButtonMinimap_SetPosition(angle)
local cos = math.cos(angle)
local sin = math.sin(angle)
local x = cos*80
local y = sin*80
PayDayButtonMinimap:SetPoint('CENTER', x, y)
end
-- PayDayFrame
function PayDayFrame_OnLoad(self)
PayDayFrame:RegisterEvent("ADDON_LOADED")
PayDayFrame:RegisterEvent("PLAYER_LOGOUT")
UIDropDownMenu_Initialize(PayDayFrameDropDownChannel, PayDayFrameDropDownChannel_Initialize)
UIDropDownMenu_SetWidth(70, PayDayFrameDropDownChannel)
UIDropDownMenu_SetSelectedID(PayDayFrameDropDownChannel, channelId);
PayDayFrameButtonStartRoll:Disable()
PayDayFrameButtonEndMatch:Disable()
PayDayFrameButtonPrintStats:Disable()
PayDayFrameButtonResetStats:Disable()
PayDayFrameButtonReminder:Disable()
PayDayFrameButtonLastCall:Disable()
PayDayFrameEditBoxMaxRoll:SetText("50")
PayDayFrameEditBoxMinRoll:SetText("1")
PayDayFrameButtonPrintStats:RegisterForClicks("LeftButtonUp", "RightButtonUp")
end
function PayDayFrame_OnUpdate(self)
if stats.totalMatches > 0 and not matchStarted then
PayDayFrameButtonPrintStats:Enable()
PayDayFrameButtonResetStats:Enable()
else
PayDayFrameButtonPrintStats:Disable()
PayDayFrameButtonResetStats:Disable()
end
end
function PayDayFrame_OnEvent(self, event, ...)
if event == "ADDON_LOADED" then
arg1 = ...
if arg1 == "PayDay" then -- Is it for the PayDay AddOn?
if not PayDaySettings or PayDaySettings.settingsVersion == nil
or PayDaySettings.settingsVersion ~= 3 then
PayDaySettings = {
settingsVersion = 2,
minimapPos = 10,
messages = PAYDAY_DEFAULT_MESSAGES
}
end
minimapPos = PayDaySettings.minimapPos
messages = PayDaySettings.messages
PayDayButtonMinimap_UpdatePosition(PayDayButtonMinimap)
PayDayButtonMinimap_SetPosition(minimapPos)
PayDayButtonMinimap_UpdatePosition(PayDayButtonMinimap)
PayDayCreateMessagesFrame(messages)
end
elseif event == "PLAYER_LOGOUT" then
PayDaySettings.minimapPos = minimapPos
PayDaySettings.messages = messages
end
if not matchStarted then return end
if event == "CHAT_MSG_SAY" and channelId == 1 then
local msg, _, _, _, name = ...
PayDay_ParseChat(name, msg)
elseif event == "CHAT_MSG_PARTY" and channelId == 2 then
local msg, _, _, _, name = ...
PayDay_ParseChat(name, msg)
elseif (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER") and channelId == 3 then
local msg, _, _, _, name = ...
PayDay_ParseChat(name, msg)
elseif event == "CHAT_MSG_SYSTEM" then
local msg = ...
if match.phase == "join" then return end
local prevPhase = match.phase
local prevWaitCount = #match:GetWaitingList()
if PayDay_ParseRoll(tostring(msg)) then
PayDay_CheckComplete(prevPhase, prevWaitCount)
end
end
end
function PayDayFrame_Toggle()
if (PayDayFrame:IsShown()) then
PayDayFrame:Hide()
else
PayDayFrame:Show()
end
end
function PayDayFrameCloseButton_OnClick()
PlaySound("igMainMenuQuit")
PayDayFrame:Hide()
end
function PayDayFrameDropDownChannel_Initialize()
-- |c000a49fd |c00e36b09
local info = UIDropDownMenu_CreateInfo()
info.text = "Say"
info.value = "say"
info.justifyH = "RIGHT"
info.checked = nil
info.func = PayDayFrameDropDownChannel_OnClick
UIDropDownMenu_AddButton(info)
info.text = "Party"
info.value = "party"
info.justifyH = "RIGHT"
info.checked = nil
info.func = PayDayFrameDropDownChannel_OnClick
UIDropDownMenu_AddButton(info)
info.text = "Raid"
info.value = "raid"
info.justifyH = "RIGHT"
info.checked = nil
info.func = PayDayFrameDropDownChannel_OnClick
UIDropDownMenu_AddButton(info)
end
function PayDayFrameDropDownChannel_OnClick()
if matchStarted then return end
UIDropDownMenu_SetSelectedID(PayDayFrameDropDownChannel, this:GetID())
channelId = this:GetID()
end
function PayDayFrameButtonStartMatch_OnClick(self)
matchStarted = true
local max = tonumber(PayDayFrameEditBoxMaxRoll:GetText())
local min = tonumber(PayDayFrameEditBoxMinRoll:GetText())
if max == nil or min == nil then return end
if min >= max then return end
PlaySoundFile("Interface\\AddOns\\PayDay\\res\\lion1.ogg", "Master")
maxRoll = max
minRoll = min
match = meowth.Match:New(maxRoll, minRoll)
PrintChat(string.format(messages.rollingToFrom.text, min, max))
PrintChat(messages.join.text)
if channelId == 1 then
PayDayFrame:RegisterEvent("CHAT_MSG_SAY")
elseif channelId == 2 then
PayDayFrame:RegisterEvent("CHAT_MSG_PARTY")
elseif channelId == 3 then
PayDayFrame:RegisterEvent("CHAT_MSG_RAID")
PayDayFrame:RegisterEvent("CHAT_MSG_RAID_LEADER")
else
return
end
PayDayFrame:RegisterEvent("CHAT_MSG_SYSTEM") -- this is where rolls come in
PayDayFrameButtonStartMatch:Disable()
PayDayFrameButtonLastCall:Enable()
PayDayFrameButtonEndMatch:Enable()
PayDayFrameButtonLastCall:Enable()
end
function PayDayFrameButtonEndMatch_OnClick(self)
if not matchStarted then return end
PrintChat(messages.onMatchEnd.text)
EndMatch()
end
function PayDayFrameButtonStartRoll_OnClick(self)
if not matchStarted then return end
match:Start()
if match.phase ~= "roll" then return end
PayDayFrameButtonStartRoll:Disable()
PayDayFrameButtonReminder:Enable()
PayDayFrameButtonLastCall:Disable()
PrintChat(string.format(messages.onStartRoll.text, minRoll, maxRoll))
end
function PayDayFrameButtonPrintStats_OnClick(self, button, down)
-- top and bottom is how many of the top you want to display
-- and how many of the bottom you want to display. -1 means all
if button == "LeftButton" then
local count = #stats:GetGamblersSorted()
local topAndBottom = 3
local top = 0
local bot = 0
local outputStr = ""
if topAndBottom == -1 then
for i, v in ipairs(stats:GetGamblersSorted()) do
outputStr = outputStr..(string.format(" %s %d;", v, stats.totals[v]))
end
else
for i, v in ipairs(stats:GetGamblersSorted()) do
if top < topAndBottom then
top = top + 1
outputStr = outputStr..(string.format(" %s %d;", v, stats.totals[v]))
elseif top == topAndBottom and bot == 0 and count > 2 * topAndBottom then
outputStr = outputStr.." ... "
top = top + 1
elseif top >= topAndBottom and bot < topAndBottom and i > count - topAndBottom then
bot = bot + 1
outputStr = outputStr..(string.format(" %s %d;", v, stats.totals[v]))
end
end
end
-- Drop the last semicolon and print the statistics.
PrintChat(string.sub(outputStr, 1, string.len(outputStr) - 1))
else
local menu = {
{text = "Select a Gambler", isTitle = true}
}
for i, v in ipairs(stats:GetGamblersSortedbyName()) do
table.insert(menu, {text = v, func = function()
PrintChat(string.format("%s %d", v, stats.totals[v]))
end
})
end
local menuFrame = CreateFrame("Frame", "GamblerStatsSelectFrame", UIParent, "UIDropDownMenuTemplate")
EasyMenu(menu, menuFrame, "cursor", 0 , 0, "MENU")
end
end
function PayDayFrameButtonResetStats_OnClick()
if matchStarted then return end
SELECTED_CHAT_FRAME:AddMessage("Resetting statistics.")
PayDayFrameButtonPrintStats:Disable()
PayDayFrameButtonResetStats:Disable()
stats = meowth.Stats:New()
end
function PayDayFrameButtonReminder_OnClick(self)
if not matchStarted then return end
PrintTieMembers()
end
function PayDayFrameButtonLastCall_OnClick(self)
if not matchStarted then return end
if match.phase ~= "join" then return end
PrintChat(messages.onLastCall.text)
end
-- PayDayInterfaceOptions
function PayDayInterfaceOptions_OnLoad(panel)
panel.name = "Pay Day"
InterfaceOptions_AddCategory(panel)
end
function ScrollFrameTest_OnLoad(panel)
panel.name = "Scroll"
panel.parent = "Pay Day"
InterfaceOptions_AddCategory(panel)
end
function PayDayInterfaceOptionsMessagesParent_OnLoad(panel)
-- Need to make it so that panel.okay callback will apply the changes and cancel will just drop the changes
panel.name = "Messages"
panel.parent = "Pay Day"
InterfaceOptions_AddCategory(panel)
end
SlashCmdList["PAYDAY"] = PayDay_OnSlashCmd