Better handle battleground context

Sasky [06-09-14 - 02:14]
Better handle battleground context

This fixes some issues when switching campaigns where items wouldn't show initially.
Sometimes it still takes an event (under attack change) in order to get the scanning correct.
Filename
CyrHUD.lua
CyrHUD.xml
classes/Battle.lua
diff --git a/CyrHUD.lua b/CyrHUD.lua
index ad55cb4..e2bd308 100644
--- a/CyrHUD.lua
+++ b/CyrHUD.lua
@@ -61,6 +61,7 @@ function CyrHUD.eventAttackChange(_, keepID, battlegroundContext, underAttack)
     elseif CyrHUD.battles[keepID] ~= nil then
         CyrHUD.battles[keepID]:update()
     end
+    CyrHUD.battleContext = battlegroundContext
 end

 function CyrHUD.saveWindowPosition( window )
@@ -230,6 +231,8 @@ function CyrHUD.init()


     --Get initial scan
+    CyrHUD.battles = {}
+    CyrHUD.battleContext = GetCurrentCampaignId()
     CyrHUD.scanKeeps()

     --Add events
diff --git a/CyrHUD.xml b/CyrHUD.xml
index 5177562..3bfecae 100644
--- a/CyrHUD.xml
+++ b/CyrHUD.xml
@@ -19,7 +19,7 @@
                     <Anchor point="TOPLEFT" offsetX="70" />
                 </Texture>

-                <Label name="$(parent)_DC" font="ZoFontGameShadow" color="6666EE"
+                <Label name="$(parent)_DC" font="ZoFontGameShadow" color="688fb2"
                        wrapMode="ELLIPSIS" text="" horizontalAlignment="CENTER">
                     <Anchor point="TOPLEFT" offsetX="90" />
                 </Label>
@@ -29,7 +29,7 @@
                     <Anchor point="TOPLEFT" offsetX="140" />
                 </Texture>

-                <Label name="$(parent)_EP" font="ZoFontGameShadow" color="B22222"
+                <Label name="$(parent)_EP" font="ZoFontGameShadow" color="de5c4f"
                        wrapMode="ELLIPSIS" text="" horizontalAlignment="CENTER">
                     <Anchor point="TOPLEFT" offsetX="160" />
                 </Label>
@@ -39,7 +39,7 @@
                     <Anchor point="TOPLEFT" offsetX="210" />
                 </Texture>

-                <Label name="$(parent)_AD" font="ZoFontGameShadow" color="B2B222"
+                <Label name="$(parent)_AD" font="ZoFontGameShadow" color="c3aa4a"
                        wrapMode="ELLIPSIS" text="" horizontalAlignment="LEFT" opacity="0.85">
                     <Anchor point="TOPLEFT" offsetX="230" />
                 </Label>
diff --git a/classes/Battle.lua b/classes/Battle.lua
index 3d6f8c5..2cbe510 100644
--- a/classes/Battle.lua
+++ b/classes/Battle.lua
@@ -9,6 +9,8 @@ if CyrHUD == nil then
     CyrHUD = {}
 end

+local function n0(val) if val == nil then return 0 end return val end
+
 -- Setup class
 CyrHUD.Battle = {}
 CyrHUD.Battle.__index = CyrHUD.Battle
@@ -60,12 +62,11 @@ CyrHUD.Battle.new = function(keepID)
 end

 function CyrHUD.Battle:update()
-    local _, bc = GetKeepKeysByIndex(10)
-    self.defender = GetKeepAlliance(self.keepID, bc)
-    self.keepUA = GetKeepUnderAttack(self.keepID, bc)
-    self.siege[ALLIANCE_ALDMERI_DOMINION] = GetNumSieges(self.keepID, bc, ALLIANCE_ALDMERI_DOMINION)
-    self.siege[ALLIANCE_DAGGERFALL_COVENANT] = GetNumSieges(self.keepID, bc, ALLIANCE_DAGGERFALL_COVENANT)
-    self.siege[ALLIANCE_EBONHEART_PACT] = GetNumSieges(self.keepID, bc, ALLIANCE_EBONHEART_PACT)
+    self.defender = GetKeepAlliance(self.keepID, CyrHUD.battleContext)
+    self.keepUA = GetKeepUnderAttack(self.keepID, CyrHUD.battleContext)
+    self.siege[ALLIANCE_ALDMERI_DOMINION] = GetNumSieges(self.keepID, CyrHUD.battleContext, ALLIANCE_ALDMERI_DOMINION)
+    self.siege[ALLIANCE_DAGGERFALL_COVENANT] = GetNumSieges(self.keepID, CyrHUD.battleContext, ALLIANCE_DAGGERFALL_COVENANT)
+    self.siege[ALLIANCE_EBONHEART_PACT] = GetNumSieges(self.keepID, CyrHUD.battleContext, ALLIANCE_EBONHEART_PACT)

     if not self:isBattle() then
         if self.endBattle then
@@ -152,7 +153,7 @@ function CyrHUD.Battle:getAttSiege()
         color = CyrHUD.info[faction].color
     end
     if count == 0 then
-        if not self.keepUA and self.siege[self.defender] > 0 then
+        if not self.keepUA and n0(self.siege[self.defender]) > 0 then
             count = "?"
         else
             count = ""
@@ -178,7 +179,7 @@ end
     @see CyrHUD.info
 ]]
 function CyrHUD.Battle:getIcon()
-    if self.defender == ALLIANCE_NONE then
+    if CyrHUD.info[self.defender] == nil then
         return CyrHUD.info.noIcon
     end