Refactor class and data into separate files

Scott Yeskie [06-04-14 - 10:38]
Refactor class and data into separate files
Filename
CyrHUD.lua
CyrHUD.txt
classes/Battle.lua
classes/Info.lua
diff --git a/CyrHUD.lua b/CyrHUD.lua
index 9aaa716..0186531 100644
--- a/CyrHUD.lua
+++ b/CyrHUD.lua
@@ -3,7 +3,10 @@
   * Author: Sasky
 ]]--

-CyrHUD = {}
+if CyrHUD == nil then
+    --noinspection GlobalCreationOutsideO
+    CyrHUD = {}
+end

 ----------------------------------------------
 -- Utility
@@ -42,35 +45,6 @@ function CyrHUD.updateScore()
 end

 ----------------------------------------------
--- Data
-----------------------------------------------
-CyrHUD.info = {}
-CyrHUD.info[ALLIANCE_ALDMERI_DOMINION] = {}
-CyrHUD.info[ALLIANCE_ALDMERI_DOMINION].color = { hex = "B2B222", r = .698, g = .698, b = .133 }
-CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_KEEP] = {}
-CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_KEEP].icon = "/esoui/art/mappins/ava_largekeep_aldmeri.dds"
-CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_KEEP].iconua = "/esoui/art/mappins/ava_largekeep_aldmeri_underattack.dds"
-CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_OUTPOST] = {}
-CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_OUTPOST].icon = "/esoui/art/mappins/ava_outpost_aldmeri.dds"
-CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_OUTPOST].iconua = "/esoui/art/mappins/ava_outpost_aldmeri.dds"
-CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT] = {}
-CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT].color = { hex = "6666EE", r = .4, g = .4, b = .933 }
-CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_KEEP] = {}
-CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_KEEP].icon = "/esoui/art/mappins/ava_largekeep_daggerfall.dds"
-CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_KEEP].iconua = "/esoui/art/mappins/ava_largekeep_daggerfall_underattack.dds"
-CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_OUTPOST] = {}
-CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_OUTPOST].icon = "/esoui/art/mappins/ava_outpost_daggerfall.dds"
-CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_OUTPOST].iconua = "/esoui/art/mappins/ava_outpost_daggerfall.dds"
-CyrHUD.info[ALLIANCE_EBONHEART_PACT] = {}
-CyrHUD.info[ALLIANCE_EBONHEART_PACT].color = { hex = "B22222", r = .698, g = .133, b = .133 }
-CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_KEEP] = {}
-CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_KEEP].icon = "/esoui/art/mappins/ava_largekeep_ebonheart.dds"
-CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_KEEP].iconua = "/esoui/art/mappins/ava_largekeep_ebonheart_underattack.dds"
-CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_OUTPOST] = {}
-CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_OUTPOST].icon = "/esoui/art/mappins/ava_outpost_ebonheart.dds"
-CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_OUTPOST].iconua = "/esoui/art/mappins/ava_outpost_ebonheart.dds"
-
-----------------------------------------------
 -- Notification UI pool
 ----------------------------------------------
 CyrHUD.entryCount = 0
@@ -98,7 +72,7 @@ CyrHUD.createEntry = function()
     entry.name:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 35, 5)
     entry.name:SetFont(fontMain)

-    local fontSmall = "EsoUI/Common/Fonts/univers67.otf|14|soft-shadow-thin"
+    --local fontSmall = "EsoUI/Common/Fonts/univers67.otf|14|soft-shadow-thin"
     entry.defSiege = WINDOW_MANAGER:CreateControl(entryName .. "defSiege", CyrHUD_UI, CT_LABEL)
     entry.defSiege:SetDimensions(30,30)
     entry.defSiege:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 220, 5)
@@ -147,7 +121,7 @@ CyrHUD.reset = function()
 end

 CyrHUD.print = function(battle)
-    entry = CyrHUD.getEntry()
+    local entry = CyrHUD.getEntry()
     entry.icon:SetTexture(battle:getIcon())
     entry.name:SetText(battle.keepName)
     local color = CyrHUD.info[battle.defender].color
@@ -204,116 +178,6 @@ CyrHUD.scanKeeps = function()
 end

 ------------------------------------------------------------------------
--- Battle class
-------------------------------------------------------------------------
-CyrHUD.Battle = {}
-CyrHUD.Battle.__index = CyrHUD.Battle
-
-setmetatable(CyrHUD.Battle, {
-    __call = function (cls, ...)
-        return cls.new(...)
-    end,
-})
-
-CyrHUD.Battle.new = function(keepID)
-    local _, bc = GetKeepKeysByIndex(10)
-    local self = setmetatable({}, CyrHUD.Battle)
-    self.startBattle = GetTimeStamp()
-    self.endBattle = nil
-    self.keepID = keepID
-    self.keepName = GetKeepName(keepID)
-    self.keepType = GetKeepType(keepID)
-    self:update()
-    return self
-end
-
-function CyrHUD.Battle:update()
-    local _, bc = 2,2 -- GetKeepKeysByIndex(10)
-    self.defender = GetKeepAlliance(self.keepID, bc)
-    self.keepUA = GetKeepUnderAttack(self.keepID, bc)
-    self.siege = {}
-    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)
-
-    if not self:isBattle() then
-        if self.endBattle then
-            --Remove after time
-            if GetDiffBetweenTimeStamps(GetTimeStamp(), self.endBattle) > 15 then
-                CyrHUD.battles[self.keepID] = nil
-            end
-        else
-            self.endBattle = GetTimeStamp()
-        end
-    end
-end
-
-function CyrHUD.Battle:getBGColor()
-    if self.endBattle then
-        if self.defender == GetUnitAlliance("player") then
-            return .2,.5,.2,.6
-        end
-        return .5,.5,.5,.3
-    end
-    local delta = GetDiffBetweenTimeStamps(GetTimeStamp(), self.startBattle)
-    if delta < 60 then
-        return (60-delta)/120,0,0,.3
-    end
-
-    return 0,0,0,.3
-end
-
-function CyrHUD.Battle:getDuration()
-    local time = self.endBattle or GetTimeStamp()
-    local delta = GetDiffBetweenTimeStamps(time, self.startBattle)
-    local out = CyrHUD.formatTime(delta)
-    return out
-end
-
-function CyrHUD.Battle:getDefSiege()
-    local siege = self.siege[self.defender]
-    --d(self.keepName .. ":" ..  siege .. " (" .. self.defender .. ")")
-    if siege == 0 then siege = "" end
-    return siege, CyrHUD.info[self.defender].color
-end
-
-function CyrHUD.Battle:getAttSiege()
-    local siege = self.siege
-    siege[self.defender] = nil
-    local count, faction = 0, nil
-    for f,c in pairs(siege) do
-        count = count + c
-        if c > 0 then
-            if faction == nil then
-                faction = f
-            else
-                faction = 0
-            end
-        end
-    end
-    local color = { hex = "CCCCCC", r=.8, g=.8, b=.8 }
-    if faction and faction ~= 0 then
-        color = CyrHUD.info[faction].color
-    end
-    if count == 0 then count = "" end
-    return count, color
-end
-
-function CyrHUD.Battle:isBattle()
-    if self.keepUA then return true end
-    local count = (self.siege[ALLIANCE_ALDMERI_DOMINION] + self.siege[ALLIANCE_DAGGERFALL_COVENANT] + self.siege[ALLIANCE_EBONHEART_PACT])
-    return count > 0
-end
-
-function CyrHUD.Battle:getIcon()
-    if self.keepUA then
-        return CyrHUD.info[self.defender][self.keepType].iconua
-    else
-        return CyrHUD.info[self.defender][self.keepType].icon
-    end
-end
-
-------------------------------------------------------------------------
 -- Initialization
 ------------------------------------------------------------------------
 local function init()
diff --git a/CyrHUD.txt b/CyrHUD.txt
index 2130177..d75c621 100644
--- a/CyrHUD.txt
+++ b/CyrHUD.txt
@@ -3,5 +3,8 @@
 ## Version: 1.0
 ## Author: Sasky

+classes/Battle.lua
+classes/Info.lua
+
 CyrHUD.lua
 CyrHUD.xml
\ No newline at end of file
diff --git a/classes/Battle.lua b/classes/Battle.lua
new file mode 100644
index 0000000..d5dc882
--- /dev/null
+++ b/classes/Battle.lua
@@ -0,0 +1,116 @@
+--[[
+  * CyrHUD.Battle class
+  * Author: Sasky
+  * Contains all information about a conflict
+]]--
+
+if CyrHUD == nil then
+    --noinspection GlobalCreationOutsideO
+    CyrHUD = {}
+end
+
+CyrHUD.Battle = {}
+CyrHUD.Battle.__index = CyrHUD.Battle
+
+setmetatable(CyrHUD.Battle, {
+    __call = function (cls, ...)
+        return cls.new(...)
+    end,
+})
+
+CyrHUD.Battle.new = function(keepID)
+    local self = setmetatable({}, CyrHUD.Battle)
+    self.startBattle = GetTimeStamp()
+    self.endBattle = nil
+    self.keepID = keepID
+    self.keepName = GetKeepName(keepID)
+    self.keepType = GetKeepType(keepID)
+    self:update()
+    return self
+end
+
+function CyrHUD.Battle:update()
+    local _, bc = GetKeepKeysByIndex(10)
+    self.defender = GetKeepAlliance(self.keepID, bc)
+    self.keepUA = GetKeepUnderAttack(self.keepID, bc)
+    self.siege = {}
+    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)
+
+    if not self:isBattle() then
+        if self.endBattle then
+            --Remove after time
+            if GetDiffBetweenTimeStamps(GetTimeStamp(), self.endBattle) > 15 then
+                CyrHUD.battles[self.keepID] = nil
+            end
+        else
+            self.endBattle = GetTimeStamp()
+        end
+    end
+end
+
+function CyrHUD.Battle:getBGColor()
+    if self.endBattle then
+        if self.defender == GetUnitAlliance("player") then
+            return .2,.5,.2,.6
+        end
+        return .5,.5,.5,.3
+    end
+    local delta = GetDiffBetweenTimeStamps(GetTimeStamp(), self.startBattle)
+    if delta < 60 then
+        return (60-delta)/120,0,0,.3
+    end
+
+    return 0,0,0,.3
+end
+
+function CyrHUD.Battle:getDuration()
+    local time = self.endBattle or GetTimeStamp()
+    local delta = GetDiffBetweenTimeStamps(time, self.startBattle)
+    local out = CyrHUD.formatTime(delta)
+    return out
+end
+
+function CyrHUD.Battle:getDefSiege()
+    local siege = self.siege[self.defender]
+    --d(self.keepName .. ":" ..  siege .. " (" .. self.defender .. ")")
+    if siege == 0 then siege = "" end
+    return siege, CyrHUD.info[self.defender].color
+end
+
+function CyrHUD.Battle:getAttSiege()
+    local siege = self.siege
+    siege[self.defender] = nil
+    local count, faction = 0, nil
+    for f,c in pairs(siege) do
+        count = count + c
+        if c > 0 then
+            if faction == nil then
+                faction = f
+            else
+                faction = 0
+            end
+        end
+    end
+    local color = { hex = "CCCCCC", r=.8, g=.8, b=.8 }
+    if faction and faction ~= 0 then
+        color = CyrHUD.info[faction].color
+    end
+    if count == 0 then count = "" end
+    return count, color
+end
+
+function CyrHUD.Battle:isBattle()
+    if self.keepUA then return true end
+    local count = (self.siege[ALLIANCE_ALDMERI_DOMINION] + self.siege[ALLIANCE_DAGGERFALL_COVENANT] + self.siege[ALLIANCE_EBONHEART_PACT])
+    return count > 0
+end
+
+function CyrHUD.Battle:getIcon()
+    if self.keepUA then
+        return CyrHUD.info[self.defender][self.keepType].iconua
+    else
+        return CyrHUD.info[self.defender][self.keepType].icon
+    end
+end
\ No newline at end of file
diff --git a/classes/Info.lua b/classes/Info.lua
new file mode 100644
index 0000000..3a3aad1
--- /dev/null
+++ b/classes/Info.lua
@@ -0,0 +1,36 @@
+--[[
+  * CyrHUD.info table
+  * Author: Sasky
+  * Contains texture mapping and colors
+]]--
+
+if CyrHUD == nil then
+    --noinspection GlobalCreationOutsideO
+    CyrHUD = {}
+end
+
+CyrHUD.info = {}
+CyrHUD.info[ALLIANCE_ALDMERI_DOMINION] = {}
+CyrHUD.info[ALLIANCE_ALDMERI_DOMINION].color = { hex = "B2B222", r = .698, g = .698, b = .133 }
+CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_KEEP] = {}
+CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_KEEP].icon = "/esoui/art/mappins/ava_largekeep_aldmeri.dds"
+CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_KEEP].iconua = "/esoui/art/mappins/ava_largekeep_aldmeri_underattack.dds"
+CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_OUTPOST] = {}
+CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_OUTPOST].icon = "/esoui/art/mappins/ava_outpost_aldmeri.dds"
+CyrHUD.info[ALLIANCE_ALDMERI_DOMINION][KEEPTYPE_OUTPOST].iconua = "/esoui/art/mappins/ava_outpost_aldmeri.dds"
+CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT] = {}
+CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT].color = { hex = "6666EE", r = .4, g = .4, b = .933 }
+CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_KEEP] = {}
+CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_KEEP].icon = "/esoui/art/mappins/ava_largekeep_daggerfall.dds"
+CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_KEEP].iconua = "/esoui/art/mappins/ava_largekeep_daggerfall_underattack.dds"
+CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_OUTPOST] = {}
+CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_OUTPOST].icon = "/esoui/art/mappins/ava_outpost_daggerfall.dds"
+CyrHUD.info[ALLIANCE_DAGGERFALL_COVENANT][KEEPTYPE_OUTPOST].iconua = "/esoui/art/mappins/ava_outpost_daggerfall.dds"
+CyrHUD.info[ALLIANCE_EBONHEART_PACT] = {}
+CyrHUD.info[ALLIANCE_EBONHEART_PACT].color = { hex = "B22222", r = .698, g = .133, b = .133 }
+CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_KEEP] = {}
+CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_KEEP].icon = "/esoui/art/mappins/ava_largekeep_ebonheart.dds"
+CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_KEEP].iconua = "/esoui/art/mappins/ava_largekeep_ebonheart_underattack.dds"
+CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_OUTPOST] = {}
+CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_OUTPOST].icon = "/esoui/art/mappins/ava_outpost_ebonheart.dds"
+CyrHUD.info[ALLIANCE_EBONHEART_PACT][KEEPTYPE_OUTPOST].iconua = "/esoui/art/mappins/ava_outpost_ebonheart.dds"