-
Notifications
You must be signed in to change notification settings - Fork 21
/
Comms.lua
129 lines (102 loc) · 4.49 KB
/
Comms.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
local Util = WarpDeplete.Util
-- Format: "current|type"
-- current: number
-- type: "blizz" or "gettime", depending on whether or not the time
-- was retrieved from the built-in blizzard timer or the GetTime method.
local timerResponsePrefix = "WDP_TimerRes"
local timerRequestPrefix = "WDP_TimerReq"
-- Format: "objTime1|objectTime2|objTimeN"
-- objTime: Objective completion times separated by |, as number of seconds from start.
-- Should be -1 if the objective has not been completed.
local objectiveResponsePrefix = "WDP_ObjRes"
local objectiveRequestPrefix = "WDP_ObjReq"
-- Format: "time|name|class"
-- time: Death time in seconds
-- name: Player name as string
-- class: Class ID of the player (as returned by `select(2, UnitClass("player"))`)
local deathBroadcastPrefix = "WDP_Death"
local requestMessage = "pls"
function WarpDeplete:RegisterComms()
self:RegisterComm(timerRequestPrefix, "OnTimerSyncRequest")
self:RegisterComm(timerResponsePrefix, "OnTimerSyncResponse")
self:RegisterComm(objectiveRequestPrefix, "OnObjectiveSyncRequest")
self:RegisterComm(objectiveResponsePrefix, "OnObjectiveSyncResponse")
self:RegisterComm(deathBroadcastPrefix, "OnDeathBroadcast")
end
function WarpDeplete:RequestTimerSync()
self:PrintDebug("Requesting timer sync")
self:SendCommMessage(timerRequestPrefix, requestMessage, "PARTY", nil, "ALERT")
end
function WarpDeplete:RequestObjectiveSync()
self:PrintDebug("Requesting objective sync")
self:SendCommMessage(objectiveRequestPrefix, requestMessage, "PARTY", nil, "ALERT")
end
function WarpDeplete:OnTimerSyncRequest(prefix, message, dist, sender)
if message ~= requestMessage or sender == GetUnitName("player", false) then return end
local text = ("%d"):format(self.timerState.current)
if self.timerState.isBlizzardTimer then
text = text .. "|blizz"
else
text = text .. "|gettime"
end
self:SendCommMessage(timerResponsePrefix, text, "WHISPER", sender, "ALERT")
end
function WarpDeplete:OnTimerSyncResponse(prefix, message, dist, sender)
local currentRaw, typeRaw = strsplit("|", message)
local isBlizzard = typeRaw == "blizz"
local current = tonumber(currentRaw)
self:PrintDebug("Received time from " .. sender .. ": "
.. tonumber(current) .. ", type: " .. typeRaw)
if self.timerState.isBlizzardTimer and not isBlizzard then
self:PrintDebug("Updating timer")
local deaths = C_ChallengeMode.GetDeathCount()
self.timerState.current = current
self.timerState.deaths = deaths
local trueTime = current - deaths * self.keyDetailsState.deathPenalty
self.timerState.startOffset = trueTime
self.timerState.startTime = GetTime()
self.timerState.isBlizzardTimer = false
end
end
function WarpDeplete:OnObjectiveSyncRequest(prefix, message, dist, sender)
if message ~= requestMessage or sender == GetUnitName("player", false) then return end
local completionTimes = {}
local hasAny = false
for i, obj in ipairs(self.objectivesState) do
hasAny = obj.time ~= nil or hasAny
completionTimes[i] = ("%d"):format(obj.time or -1)
end
-- We only send if we have any times saved, since we might also be in
-- the process of getting times from other users
if not hasAny then return end
local text = Util.joinStrings(completionTimes, "|")
self:SendCommMessage(objectiveResponsePrefix, text, "WHISPER", sender, "ALERT")
end
function WarpDeplete:OnObjectiveSyncResponse(prefix, message, dist, sender)
local parts = {strsplit("|", message)}
for i, objTimeRaw in ipairs(parts) do
local objTime = tonumber(objTimeRaw)
if self.objectivesState[i] and objTime >= 0 then
self.objectivesState[i].time = objTime
end
end
self:UpdateTimings()
self:UpdateObjectivesDisplay()
end
function WarpDeplete:BroadcastDeath()
local time = self.timerState.current
local playerName = UnitName("player")
local playerClass = select(2, UnitClass("player"))
self:AddDeathDetails(time, playerName, playerClass)
local messageTable = {tostring(time), playerName, playerClass}
local message = Util.joinStrings(messageTable, "|")
self:PrintDebug("Sending death broadcast")
self:SendCommMessage(deathBroadcastPrefix, message, "PARTY", nil, "ALERT")
end
function WarpDeplete:OnDeathBroadcast(prefix, message, dist, sender)
if message ~= requestMessage or sender == GetUnitName("player", false) then return end
self:PrintDebug("Received death broadcast from other player: " .. message)
local timeRaw, name, class = strsplit("|", message)
local time = tonumber(timeRaw)
self:AddDeathDetails(time, name, class)
end