Quantcast

Database changes;

Peter Eliasson [01-12-15 - 21:22]
Database changes;

* Added a dbVersion and clears db if not the correct version.
* Made parses also belong to a guild in the database.
* Changed database "schema" to use more default values as it makes it easier to get an overview.
Filename
highscore.lua
main.lua
diff --git a/highscore.lua b/highscore.lua
index 66c21b3..c65ca26 100644
--- a/highscore.lua
+++ b/highscore.lua
@@ -11,79 +11,72 @@ addon.highscore = highscore;

 -- db defaults
 addon.dbDefaults.realm.modules["highscore"] = {
-	["zones"] = {
-	--[[
-		["*"] = { -- zoneId
-			zoneName = "Unknown",
-			encounters = {
-				["*"] = { -- encounterId
-					encounterName = "Unknown",
-					difficulties = {
-						["**"] = {
-							playerParses = {} -- List of objects "{playerInfo, role, dps, hps}"
-						},
-						["LFR"] = {},
-						["Normal"] = {},
-						["Heroic"] = {},
-						["Mythic"] = {}
+	["guilds"] = {
+		["*"] = { -- Guild Name
+			["zones"] = {
+				["*"] = { -- zoneId
+					zoneName = nil, -- Note: Must be set.
+					encounters = {
+						["*"] = { -- encounterId
+							encounterName = nil, -- Note: Must be set.
+							difficulties = {
+								["LFR"] 	= {playerParses = {}},
+								["Normal"] 	= {playerParses = {}},
+								["Heroic"] 	= {playerParses = {}},
+								["Mythic"] 	= {playerParses = {}}
+							}
+						}
 					}
 				}
 			}
 		}
-	--]]
 	}
 }

+addon.dbVersion = addon.dbVersion + 1

-local trackedZoneIds = {994}
+-- Constants
+local TRACKED_ZONE_IDS = {
+	994 -- Highmaul
+}

-function highscore:GetOrCreateEncounterTable(zoneId, zoneName, encounterId, encounterName, difficultyName)
-	if not self.db.zones[zoneId] then
-		self.db.zones[zoneId] = {zoneName = zoneName, encounters = {}}
-	end
-
-	local zone = self.db.zones[zoneId]
-	if not zone.encounters[encounterId] then
-		zone.encounters[encounterId] = {
-			encounterName = encounterName,
-			difficulties = {
-				LFR = {playerParses = {}},
-				Normal = {playerParses = {}},
-				Heroic = {playerParses = {}},
-				Mythic = {playerParses = {}}
-		}};
+
+local function getOrCreateEncounterTable(db, guildName, zoneId, zoneName, encounterId, encounterName, difficultyName)
+	local guildTable = db.guilds[guildName];
+	local zoneTable = guildTable.zones[zoneId];
+	local encounterTable = zoneTable.encounters[encounterId];
+
+	if not zoneTable.zoneName then
+		zoneTable.zoneName = zoneName;
 	end

-	local encounter = zone.encounters[encounterId]
-	if not encounter.difficulties[difficultyName] then
+	if not encounterTable.encounterName then
+		encounterTable.encounterName = encounterName;
+	end
+
+	if not encounterTable.difficulties[difficultyName] then
 		return nil
 	else
-		return encounter.difficulties[difficultyName]
+		return encounterTable.difficulties[difficultyName]
 	end
 end

-function highscore:AddEncounterParseForPlayer(zoneId, zoneName, encounterId, encounterName, difficultyName, duration, player)
-	local encounterTable = self:GetOrCreateEncounterTable(zoneId,
-		zoneName, encounterId, encounterName, difficultyName);
-
-	self:Debug(format("AddEncounterParseForPlayer (%s, %s, %s, %s)", zoneName, encounterName, difficultyName, player.name));
-
-	if encounterTable then
-		local parse = {
-			playerId = player.id,
-			playerName = player.name,
-			role = player.role,
-			specName = player.specName,
-			itemLevel = player.itemLevel,
-			damage = player.damage,
-			healing = player.healing,
-			duration = duration
-		}
-		tinsert(encounterTable.playerParses, parse);
-	end
+local function addEncounterParseForPlayer(encounterTable, duration, player)
+	local parse = {
+		playerId = player.id,
+		playerName = player.name,
+		role = player.role,
+		specName = player.specName,
+		itemLevel = player.itemLevel,
+		damage = player.damage,
+		healing = player.healing,
+		duration = duration
+	}
+	tinsert(encounterTable.playerParses, parse);
 end

-function highscore:AddEncounterParsesForPlayers(encounter, players)
+
+function highscore:AddEncounterParsesForPlayers(guildName, encounter, players)
 	local zoneId = encounter.zoneId;
 	local zoneName = encounter.zoneName;
 	local encounterId = encounter.id;
@@ -91,6 +84,7 @@ function highscore:AddEncounterParsesForPlayers(encounter, players)
 	local difficultyName = encounter.difficultyName;
 	local duration = encounter.duration;

+	assert(guildName)
 	assert(zoneId and zoneId > 1)
 	assert(zoneName)
 	assert(encounterId)
@@ -99,16 +93,25 @@ function highscore:AddEncounterParsesForPlayers(encounter, players)
 	assert(duration)
 	assert(players)

-	if not tContains(trackedZoneIds, zoneId) then
-		self:Debug("Current zone not not in tracked zones");
+	if not tContains(TRACKED_ZONE_IDS, zoneId) then
+		self:Debug("AddEncounterParsesForPlayers: Current zone not not in tracked zones");
+		return
+	end
+
+	local encounterTable = getOrCreateEncounterTable(self.db, guildName, zoneId, zoneName, encounterId, encounterName, difficultyName);
+
+	if not encounterTable then
+		self:Debug("AddEncounterParsesForPlayers: Could not get encounterTable")
 		return
 	end

 	for _, player in ipairs(players) do
-		self:AddEncounterParseForPlayer(zoneId, zoneName, encounterId, encounterName, difficultyName, duration, player)
+		self:Debug(format("addEncounterParseForPlayer: %s", player.name));
+		addEncounterParseForPlayer(encounterTable, duration, player)
 	end
 end

+
 function highscore:OnEnable()
 	self.db = addon.db.realm.modules["highscore"];
 end
diff --git a/main.lua b/main.lua
index 4939eef..01a0f90 100644
--- a/main.lua
+++ b/main.lua
@@ -14,9 +14,16 @@ addon:SetDefaultModulePrototype(modPrototype)
 addon.dbDefaults = {
 	realm = {
 		modules = {}
+	},
+	global = {
+		dbVersion = 1
 	}
 }

+-- The current db version. Clear (migrate?) the database if
+-- version of database doesn't match this version.
+addon.dbVersion = 1
+
 tinsert(addonTable, addon);
 _G[addonName] = addon

@@ -143,6 +150,11 @@ end
 function addon:EndSegment()
 	self:Debug("EndSegment")

+	if not self.guildName then
+		self:Debug("Not in a guild");
+		return;
+	end
+
 	if not self.currentEncounter or not Skada.last.gotboss then
 		self:Debug("Not a boss")
 		return
@@ -154,7 +166,7 @@ function addon:EndSegment()
 	local players = self:GetGuildPlayersFromSet(Skada.last);
 	self:SetRoleForPlayers(players);
 	self.inspect:GetInspectDataForPlayers(players, function()
-		self.highscore:AddEncounterParsesForPlayers(encounter, players);
+		self.highscore:AddEncounterParsesForPlayers(self.guildName, encounter, players);
 	end)

 	self:UnsetCurrentEncounter();
@@ -162,6 +174,13 @@ end

 function addon:OnInitialize()
 	self.db = LibStub("AceDB-3.0"):New("GuildSkadaHighScoreDB", addon.dbDefaults, true)
+	if self.db.global.dbVersion ~= self.dbVersion then
+		self:Debug(format("Found not matching db versions: db=%d, addon=%d",
+			self.db.global.dbVersion, self.dbVersion));
+		self:Debug("Resetting db");
+		self.db:ResetDB();
+		self.db.global.dbVersion = self.dbVersion;
+	end
 end

 function addon:OnEnable()