-
Notifications
You must be signed in to change notification settings - Fork 1
/
WowDiabetes.lua
executable file
·530 lines (463 loc) · 16.2 KB
/
WowDiabetes.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
-------------------------------------------------------------------------------
-- Utility functions and variables
-------------------------------------------------------------------------------
local L = WowDiabetesLocalization
-- Prints text with a specific color.
-- NOTE: Any inlined color changes (e.g. from links) will cancel out the color
--
local function ColorPrint(text, color)
color = color or "6F0948ff" -- Default to cyan
print("|c" .. color .. text)
end
-- Update interval for changing glucose
WowDiabetes_UpdateInterval = 1.0
local eventHandlers = {}
-------------------------------------------------------------------------------
-- Local variables
-------------------------------------------------------------------------------
-- Tells the addon to check the for the next reduction in food/drink
local playerIsAboutToEat = false
local playerIsAboutToDrink = false
-- Keeps track of the items in the player's bags
local bagCounts = {}
-- Keeps track of the last item eaten
local lastConsumed = 0
local glucoseValue = 0
-- Boolean to check if first time loading
local isFirstTime = true
-- timers
local meterTimer = 0
local combatTimer = 0
local insulinChance = 65
local inCombat = false
-- screen res
local screenRes = ""
-- minimap button
WowDiabetes_Settings = {
MinimapPos = 45 -- default position of the minimap icon in degrees
}
-- Glycemic Loads where g is the glicemic load of its food
local foodList = foodListTable
local drinkList = drinkListTable
local feastList = feastListTable
local regionList = regionTable
local dayTimer = 0
-- Scaling Variable
local scaleAmt = 5
-------------------------------------------------------------------------------
-- Main AddOn logic
-------------------------------------------------------------------------------
-- Called when the main frame first loads
function WowDiabetes_OnLoad(frame)
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("VARIABLES_LOADED")
-- Combat enter/leave
frame:RegisterEvent("PLAYER_REGEN_ENABLED")
frame:RegisterEvent("PLAYER_REGEN_DISABLED")
-- Food/drink consumption
frame:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
frame:RegisterEvent("UNIT_AURA")
-- Mouse handling
--frame:RegisterForClicks("RightButtonUp")
frame:RegisterForDrag("LeftButton")
screenRes = GetCVar("gxResolution")
end
-- Changes the color of the glucose bar depending on how well the player is doing
function ChangeGlucoseBarColor()
-- good glucose level
if glucoseLevel > 89 and glucoseLevel < 110 then
goodGlucose(UIErrorsFrame)
-- Slighty bad glucose level, character is dizzy
elseif glucoseLevel > 70 and glucoseLevel < 90 then
okayGlucose(UIErrorsFrame)
-- Very bad glucose level, character is about to pass out
elseif glucoseLevel > 110 and glucoseLevel < 130 then
okayGlucose(UIErrorsFrame)
-- the worst glucos level, low end
elseif glucoseLevel < 70 then
badGlucose(UIErrorsFrame)
-- the worst glucose level, high end
elseif glucoseLevel > 130 then
badGlucose(UIErrorsFrame)
end
end
-- Good Glucose Level
function goodGlucose(frame)
WowBlurryEffect:Hide()
WowDiabetesFrameGlucoseLevelBar:SetStatusBarColor(0,1,0,1)
if glucoseLevelString ~= "good" then
frame:AddMessage(GOOD_TEXT, 0, 1, 0)
glucoseLevelString = "good"
end
end
-- Okay Glucose Level
function okayGlucose(frame)
WowBlurryEffect:Show()
WowBlurryEffect:SetAlpha(.4)
WowDiabetesFrameGlucoseLevelBar:SetStatusBarColor(1,1,0,1)
if(glucoseLevelString ~= "okay") then
frame:AddMessage(OKAY_TEXT, 1, 1, 0)
glucoseLevelString = "okay"
end
end
-- Bad Glucose Level
function badGlucose(frame)
local frameH = frame:GetAttribute("height")
WowBlurryEffect:Show()
WowBlurryEffect:SetAlpha(.6)
WowDiabetesFrameGlucoseLevelBar:SetStatusBarColor(1,0,0,1)
if(glucoseLevelString ~= "bad") then
frame:AddMessage(BAD_TEXT, 1, 0, 0)
glucoseLevelString = "bad"
end
end
-- Called after the variables have loaded
function WowDiabetesGlucoseLevelBar_Setup(statusBar)
statusBar:SetMinMaxValues(40,180)
statusBar:SetValue(glucoseLevel)
statusBar:SetStatusBarTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill")
WowDiabetesFrameGlucoseLevelString:SetText(string.format("%.0f", statusBar:GetValue()) .. " mg/dL")
ChangeGlucoseBarColor()
end
-- Called whenever an event is triggered
function WowDiabetes_OnEvent(frame, event, ...)
return eventHandlers[event](frame, ...)
end
function eventHandlers.ADDON_LOADED(frame, ...)
if ... == "WowDiabetes" then
for bagId = 0, NUM_BAG_SLOTS do
WowDiabetes_ScanBag(bagId, false)
end
frame:UnregisterEvent("ADDON_LOADED")
frame:RegisterEvent("BAG_UPDATE")
if glucoseLevel == nil then
glucoseLevel = 90
glucoseLevelString = "good"
insulin = 10
insulinUsed = 0
foodEaten = 0
timeGood = 0
dayTimer = 0
elseif insulinUsed == nil then
insulinUsed = 0
foodEaten = 0
timeGood = 0
dayTimer = 0
end
end
end
function eventHandlers.VARIABLES_LOADED()
glucoseLevel = tonumber(glucoseLevel)
insulin = tonumber(insulin)
insulinUsed = tonumber(insulinUsed)
foodEaten = tonumber(foodEaten)
timeGood = tonumber(timeGood)
dayTimer = tonumber(dayTimer)
WowDiabetesFrameMedsAmountString:SetText(insulin)
WowDiabetesGlucoseLevelBar_Setup(WowDiabetesFrameGlucoseLevelBar)
function WowDiabetes_MinimapButton_Reposition()
WowDiabetes_MinimapButton:SetPoint("TOPLEFT","Minimap","TOPLEFT",52-(80*cos(WowDiabetes_Settings.MinimapPos)),(80*sin(WowDiabetes_Settings.MinimapPos))-52)
end
WowDiabetesGlucoseLevelBar_Setup(WowDiabetesFrameGlucoseLevelBar)
end
-- Called whenever the user clicks on the main WowDiabetes frame
function WowDiabetes_OnClickFrame()
-- ColorPrint("Frame clicked")
end
-- Called whenever the player enters combat
function eventHandlers.PLAYER_REGEN_DISABLED()
-- ColorPrint("Player entered combat!")
inCombat = true
combatTimer = 0
end
function WowDiabetes_ScaleActivity()
if(GetInstanceInfo() == "raid") then
scaleAmt = 60
elseif(GetInstanceInfo() == "party") then
scaleAmt = 45
else
scaleAmt = 5
end
end
-- Called whenever the player exits combat
function eventHandlers.PLAYER_REGEN_ENABLED()
local checkGluc
-- local insulinCheck = 0
-- ColorPrint("Player exited combat!")
insulinCheck = random(0,100)
-- ColorPrint(insulinCheck)
if insulinCheck > insulinChance then
insulin = insulin + 1
end
WowDiabetes_ScaleActivity()
-- ColorPrint(scaleAmt)
checkGluc = combatTimer % scaleAmt
newGlucose = combatTimer / scaleAmt
newGlucose = newGlucose * (-1)
-- ColorPrint(newGlucose .. " " .. checkGluc .. " " .. combatTimer)
if 0 > newGlucose then
newGlucose = checkGluc
end
if glucoseLevel > 46 then
changeGlucoseLevel(newGlucose)
end
combatTimer = 0
end
-- Called whenever a spell is cast, including usage of food/drink
function eventHandlers.UNIT_SPELLCAST_SUCCEEDED(frame, unitId, spell, rank, lineId, spellId)
if unitId == "player" then
-- Check for food/drink
if spell == L["DRINK_AURA_NAME"] or spell == L["WEAK_ALCOHOL_AURA_NAME"] or spell == L["STRONG_ALCOHOL_AURA_NAME"] then
--ColorPrint("Player is about to drink")
playerIsAboutToDrink = true
elseif spell == L["FOOD_AURA_NAME"] or spell == L["REFRESHMENT_AURA_NAME"] then
-- ColorPrint("Player is about to eat")
playerIsAboutToEat = true
end
end
end
function changeGlucoseLevel(value)
if value > 20 then
glucoseLevel = glucoseLevel + (value / 5)
else
glucoseLevel = glucoseLevel + value
end
WowDiabetesFrameGlucoseLevelBar:SetValue(glucoseLevel)
WowDiabetesFrameMedsAmountString:SetText(insulin)
foodEaten = foodEaten + 1
end
-- Called whenever someone's buffs/debuffs (auras) change
function eventHandlers.UNIT_AURA(frame, unitId)
if unitId == "player" then
--ColorPrint("Player's auras (buffs/debuffs) changed!")
foodName, rank, icon, count, dispelType, foodDuration, foodExpires, caster, isStealable, shouldConsolidate, foodSpellID = UnitAura(unitId, "Food")
drinkName, rank, icon, count, dispelType, drinkDuration, drinkExpires, caster, isStealable, shouldConsolidate, drinkSpellID = UnitAura(unitId, "Drink")
glucoseValue = feastList[tostring(foodSpellID)..":"..tostring(drinkSpellID)]
if (glucoseValue and playerIsAboutToEat == false and playerIsAboutToDrink == false) then
playerIsAboutToEat = true
playerIsAboutToDrink = true
lastConsumed = foodList[glucoseValue]
elseif (not glucoseValue and playerIsAboutToEat == true and playerIsAboutToDrink == true) then
changeGlucoseLevel(lastConsumed)
lastConsumed = 0
playerIsAboutToEat = false
playerIsAboutToDrink = false
end
end
end
-- Called whenever there is a change in bags
function eventHandlers.BAG_UPDATE(frame, bagId)
-- Update bag counts
local changedItems = WowDiabetes_ScanBag(bagId)
-- If necessary, print changed item(s)
--if playerIsAboutToEat or playerIsAboutToDrink then
for itemId, count in pairs(changedItems) do
local itemName, link = GetItemInfo(itemId)
-- ColorPrint(itemName)
if playerIsAboutToEat then
--ColorPrint("Player ate: " .. link .. ", change in count: " .. count)
local foodVal = foodList[itemId]
ColorPrint("Item: " .. itemName .. " Value: " .. foodVal)
changeGlucoseLevel(foodVal)
elseif playerIsAboutToDrink then
--ColorPrint("Player drank: " .. link .. ", change in count: " .. count)
local drinkVal = drinkList[itemId]
ColorPrint("Item: " .. itemName .. " Value: " .. drinkVal)
changeGlucoseLevel(drinkVal)
end
end
--glucoseLevel = glucoseLevel + 1
WowDiabetesFrameGlucoseLevelBar:SetValue(glucoseLevel)
--end
end
-- Counts and stores the number of items in each bag
-- @param bagId The bag index to check
-- @param returnChanges (Defaults to true) If true, will return a collection of items changed
-- where returnVal[itemId] == changeInCount
function WowDiabetes_ScanBag(bagId, returnChanges)
returnChanges = returnChanges or true
-- Count the number of each item in the bag
if not bagCounts[bagId] then
bagCounts[bagId] = {}
end
local itemCounts = {}
for slot = 0, GetContainerNumSlots(bagId) do
local texture, count, locked, quality, readable, lootable, link = GetContainerItemInfo(bagId, slot)
if texture then
local itemId = tonumber(link:match("|Hitem:(%d+):"))
if not itemCounts[itemId] then
itemCounts[itemId] = count
else
itemCounts[itemId] = itemCounts[itemId] + count
end
end
end
-- Compare against the old counts
local changedItems = {}
if returnChanges then
for itemId, oldCount in pairs(bagCounts[bagId]) do
local newCount = itemCounts[itemId] or 0
if oldCount ~= newCount then
changedItems[itemId] = newCount - oldCount
end
end
end
-- Store the new item counts
bagCounts[bagId] = itemCounts
if returnChanges then
return changedItems
end
end
-- If the frame has been completely open for longer than 15 seconds,
-- hide part of it so the player has to learn to keep track on their own
function WowDiabetes_OnUpdate(self, elapsed)
self.TimeSinceLastUpdate = self.TimeSinceLastUpdate + elapsed
while ( self.TimeSinceLastUpdate > WowDiabetes_UpdateInterval) do
if WowDiabetesFrameGlucoseLevelBar:IsShown() then
meterTimer = meterTimer + WowDiabetes_UpdateInterval
if meterTimer >= 10 then
WowDiabetesFrameGlucoseLevelBar:Hide()
WowDiabetesFrameGlucoseLevelString:Hide()
WowDiabetesFrameCloseButton:Hide()
WowDiabetesFrameCloseButton2:Show()
WowDiabetesFrameWebsiteButton:Hide()
WowDiabetesFrameWebsiteButton2:Show()
WowDiabetesFrame:SetSize(200, 138)
meterTimer = 0
end
end
--dayTimer = dayTimer + WowDiabetes_UpdateInterval
if glucoseLevelString == "good" then
timeGood = timeGood + WowDiabetes_UpdateInterval
end
combatTimer = combatTimer + WowDiabetes_UpdateInterval
self.TimeSinceLastUpdate = self.TimeSinceLastUpdate - WowDiabetes_UpdateInterval
end
end
-- Hide the frame entirely
function WowDiabetesCloseButton_OnClick()
WowDiabetesFrame:Hide()
end
-- Show the frame entirely
function WowDiabetes_MinimapButton_OnClick()
if WowDiabetesFrame:IsShown() then
WowDiabetesFrame:Hide()
else
WowDiabetesFrame:Show()
end
end
-- Shows the frame entirely so player can check glucose levels
function WowDiabetesGlucoseButton_OnClick()
if WowDiabetesFrameCloseButton2:IsShown() then
WowDiabetesFrame:SetSize(200, 185)
WowDiabetesFrameGlucoseLevelBar:Show()
WowDiabetesFrameGlucoseLevelString:Show()
WowDiabetesFrameCloseButton:Show()
WowDiabetesFrameCloseButton2:Hide()
WowDiabetesFrameWebsiteButton:Show()
WowDiabetesFrameWebsiteButton2:Hide()
else
WowDiabetesFrameGlucoseLevelBar:Hide()
WowDiabetesFrameGlucoseLevelString:Hide()
WowDiabetesFrameCloseButton:Hide()
WowDiabetesFrameCloseButton2:Show()
WowDiabetesFrameWebsiteButton:Hide()
WowDiabetesFrameWebsiteButton2:Show()
WowDiabetesFrame:SetSize(200, 138)
end
end
-- Raise your glucose level when medicine is used
function WowDiabetesMedicineButton_OnClick()
if insulin > 0 then
local insulinVal = math.random(8, 12)
glucoseLevel = glucoseLevel + insulinVal
WowDiabetesFrameGlucoseLevelBar:SetValue(glucoseLevel)
insulin = insulin - 1
insulinUsed = insulinUsed + 1
WowDiabetesFrameMedsAmountString:SetText(insulin)
end
end
-- Open the website panel for uploading/downloading your data
function WowDiabetesWebsiteButton_OnClick()
if WebsiteFrame:IsShown() then
WebsiteFrame:Hide()
else
WebsiteFrame:Show()
WowDiabetesUploadButton_OnClick()
end
end
-- Recreate the string if needed
function WowDiabetesUploadButton_OnClick()
local exportString = WowDiabetes_CreateUploadString()
WebsiteFrameEditBox:SetText(exportString)
WebsiteFrameEditBox:SetMultiLine(true)
WebsiteFrameEditBox:HighlightText()
end
-- Take the input string and save the data back in
function WowDiabetesDownloadButton_OnClick()
WowDiabetes_SaveDownloadInfo(WebsiteFrameEditBox:GetText())
ChangeGlucoseBarColor()
end
function WowDiabetes_SaveDownloadInfo(data)
if data == nil then
return "error"
end
local tempData = { strsplit(",", data) }
timeGood = tonumber(tempData[4])
dayTimer = tonumber(tempData[5])
glucoseLevel = tonumber(tempData[6])
insulin = tonumber(tempData[7])
insulinUsed = tonumber(tempData[8])
foodEaten = tonumber(tempData[9])
WowDiabetesFrameGlucoseLevelBar:SetValue(glucoseLevel)
WowDiabetesFrameMedsAmountString:SetText(insulin)
end
-- Create the String for uploading data
function WowDiabetes_CreateUploadString()
local tempName = GetUnitName("player", true)
local location = string.find(tempName, "-")
local region = regionList[GetCurrentRegion()]
if location ~= nil then
Name = string.sub(tempName, 1, location)
Server = string.sub(tempName, location)
else
Name = tempName
Server = GetRealmName()
end
UploadString = strjoin(",", Name, region, Server, timeGood, dayTimer, glucoseLevel, insulin, insulinUsed, foodEaten)
return UploadString
end
-- Update string above status bar with the new glucose level
function WowDiabetesGlucoseLevelBar_OnValueChanged()
WowDiabetesFrameGlucoseLevelString:SetText(string.format("%.0f", glucoseLevel) .. " mg/dL")
ChangeGlucoseBarColor()
end
-- Move the minimap button
function WowDiabetes_MinimapButton_DraggingFrame_OnUpdate()
local xpos,ypos = GetCursorPosition()
local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
xpos = xmin-xpos/UIParent:GetScale()+70 -- get coordinates as differences from the center of the minimap
ypos = ypos/UIParent:GetScale()-ymin-70
WowDiabetes_Settings.MinimapPos = math.deg(math.atan2(ypos,xpos)) -- save the degrees we are relative to the minimap center
WowDiabetes_MinimapButton_Reposition() -- move the button
end
-- Show minimap tooltip
function WowDiabetes_MinimapButton_OnEnter(self)
if self.dragging then
return
end
GameTooltip:SetOwner(self or UIParent, "ANCHOR_LEFT")
WowDiabetes_MinimapButton_Details(GameTooltip)
end
function WowDiabetes_MinimapButton_Details(tt, ldb)
tt:SetText(TITLE_TEXT)
end
SLASH_WOWDIABETES1, SLASH_WOWDIABETES2 = '/wowdiabetes', '/wd'
function SlashCmdList.WOWDIABETES(msg, editbox)
if msg == 'reset' then
glucoseLevel = 90
insulin = 10
else
WowDiabetesFrame:Show()
end
end