-
Notifications
You must be signed in to change notification settings - Fork 1
/
Battleground.lua
133 lines (110 loc) · 4.35 KB
/
Battleground.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
local ModuleName, Public, Private, AddonName, Namespace = 'Battleground', {}, {}, ...
local Addon = Namespace.Addon
local Module = Addon:NewModule(ModuleName, 'AceEvent-3.0', 'AceTimer-3.0', 'AceComm-3.0')
local L = Namespace.Libs.AceLocale:GetLocale(AddonName)
local GroupType = Namespace.Utils.GroupType
local GetGroupType = Namespace.Utils.GetGroupType
local GetBattlefieldStatus = GetBattlefieldStatus
local GetInstanceInfo = GetInstanceInfo
local GetMaxBattlefieldID = GetMaxBattlefieldID
local GetActiveMatchState = C_PvP.GetActiveMatchState
local PvPMatchState = Enum.PvPMatchState
local pairs = pairs
Namespace.Battleground = Public
local QueueStatus = {
Queued = 'queued',
Confirm = 'confirm',
Active = 'active',
None = 'none',
Error = 'error',
}
local Zones = {
[30] = L['Alterac Valley'],
[2197] = L['Alterac Valley (Korrak\'s Revenge)'],
[1191] = L['Ashran'],
[2118] = L['Battle for Wintergrasp'],
[628] = L['Isle of Conquest'],
[2107] = L['Arathi Basin'],
[529] = L['Arathi Basin (Classic)'],
[1681] = L['Arathi Basin (Winter)'],
[2177] = L['Arathi Basin Comp Stomp'],
[1105] = L['Deepwind Gorge'],
[566] = L['Eye of the Storm'],
[968] = L['Eye of the Storm (Rated)'],
[1803] = L['Seething Shore'],
[727] = L['Silvershard Mines'],
[607] = L['Strand of the Ancients'],
[998] = L['Temple of Kotmogu'],
[761] = L['The Battle for Gilneas'],
[726] = L['Twin Peaks'],
[489] = L['Warsong Gulch'],
[1280] = L['Southshore vs. Tarren Mill'],
}
Namespace.Battleground.QueueStatus = QueueStatus
Namespace.Battleground.Zones = Zones
local Memory = {
currentZoneId = nil,
queueState = {
--[0] = {
-- status = QueueStatus,
-- queueSuspended = boolean
-- queueId = number
--},
},
queueStateChangeListeners = {},
}
--- per name callback with the argument being {previousState, newState, mapName}
--- @param callback
function Public.RegisterQueueStateListener(listenerName, callback)
Memory.queueStateChangeListeners[listenerName] = callback
end
function Public.InActiveBattleground()
local state = GetActiveMatchState()
if state ~= PvPMatchState.Waiting and state ~= PvPMatchState.StartUp and state ~= PvPMatchState.Engaged then
return false
end
local groupType = GetGroupType()
return groupType == GroupType.InstanceParty or groupType == GroupType.InstanceRaid
end
function Private.InitCurrentZoneId()
local _, instanceType, _, _, _, _, _, currentZoneId = GetInstanceInfo()
if instanceType == 'none' then currentZoneId = 0 end
Memory.currentZoneId = currentZoneId
end
function Public.GetCurrentZoneId()
if Memory.currentZoneId == nil then Private.InitCurrentZoneId() end
return Memory.currentZoneId
end
-- Getting ready to enter, or already in a bg, so other queues will pause
function Public.AllowQueuePause()
for _, queueState in pairs(Memory.queueState) do
if queueState.status == QueueStatus.Active or queueState.status == QueueStatus.Confirm then
return true
end
end
return false
end
function Module:UPDATE_BATTLEFIELD_STATUS(_, queueId)
local previousState = Memory.queueState[queueId] or { status = QueueStatus.None, suspendedQueue = false, queueId = queueId }
local status, mapName, _, _, suspendedQueue = GetBattlefieldStatus(queueId)
local newState = { status = status, suspendedQueue = suspendedQueue, queueId = queueId }
if newState.status == previousState.status and newState.suspendedQueue == previousState.suspendedQueue then return end
for _, listener in pairs(Memory.queueStateChangeListeners) do
listener(previousState, newState, mapName)
end
Memory.queueState[queueId] = newState
end
function Module:OnEnable()
self:RegisterEvent('UPDATE_BATTLEFIELD_STATUS')
self:RegisterEvent('PLAYER_ENTERING_WORLD')
end
function Module:PLAYER_ENTERING_WORLD(_, isLogin, isReload)
Private.InitCurrentZoneId()
if not isLogin and not isReload then return end
-- when logging in or reloading mid-queue, the first queue status mutation
-- is inaccurate if not set before it happens
for queueId = 1, GetMaxBattlefieldID() do
local status, _, _, _, suspendedQueue = GetBattlefieldStatus(queueId)
Memory.queueState[queueId] = { status = status, suspendedQueue = suspendedQueue }
end
end