Quantcast

Some cleanup/small features;

Peter Eliasson [01-13-15 - 20:53]
Some cleanup/small features;

* Clean up logging.
* Added startTime to parses.
* Added storing logging to saved vars.
Filename
highscore.lua
inspect.lua
main.lua
diff --git a/highscore.lua b/highscore.lua
index c65ca26..10a7fc5 100644
--- a/highscore.lua
+++ b/highscore.lua
@@ -61,7 +61,7 @@ local function getOrCreateEncounterTable(db, guildName, zoneId, zoneName, encoun
 	end
 end

-local function addEncounterParseForPlayer(encounterTable, duration, player)
+local function addEncounterParseForPlayer(encounterTable, startTime, duration, player)
 	local parse = {
 		playerId = player.id,
 		playerName = player.name,
@@ -70,7 +70,8 @@ local function addEncounterParseForPlayer(encounterTable, duration, player)
 		itemLevel = player.itemLevel,
 		damage = player.damage,
 		healing = player.healing,
-		duration = duration
+		duration = duration,
+		startTime = startTime
 	}
 	tinsert(encounterTable.playerParses, parse);
 end
@@ -82,6 +83,7 @@ function highscore:AddEncounterParsesForPlayers(guildName, encounter, players)
 	local encounterId = encounter.id;
 	local encounterName = encounter.name;
 	local difficultyName = encounter.difficultyName;
+	local startTime = encounter.startTime;
 	local duration = encounter.duration;

 	assert(guildName)
@@ -90,6 +92,7 @@ function highscore:AddEncounterParsesForPlayers(guildName, encounter, players)
 	assert(encounterId)
 	assert(encounterName)
 	assert(difficultyName)
+	assert(startTime)
 	assert(duration)
 	assert(players)

@@ -107,7 +110,7 @@ function highscore:AddEncounterParsesForPlayers(guildName, encounter, players)

 	for _, player in ipairs(players) do
 		self:Debug(format("addEncounterParseForPlayer: %s", player.name));
-		addEncounterParseForPlayer(encounterTable, duration, player)
+		addEncounterParseForPlayer(encounterTable, startTime, duration, player)
 	end
 end

diff --git a/inspect.lua b/inspect.lua
index a86f9c5..bf5b21a 100644
--- a/inspect.lua
+++ b/inspect.lua
@@ -90,6 +90,8 @@ end

 function inspect:StartNotifyInspectTimer()
 	if not self.notifyInspectTimer then
+		self:Debug("Starting notifyInspectTimer");
+
 		self.notifyInspectTimer = self:ScheduleRepeatingTimer(function()
 			self:NOTIFY_INSPECT_TIMER_DONE()
 		end, 1);
@@ -98,24 +100,15 @@ end

 function inspect:StopNotifyInspectTimer()
 	if self.notifyInspectTimer then
+		self:Debug("Stopping notifyInspectTimer");
+
 		self:CancelTimer(self.notifyInspectTimer);
 		self.notifyInspectTimer = nil;
 	end
 end

-function inspect:IsPlayerInInspectQueue(player)
-	local playerInQueue = false
-	for _, p in ipairs(self.notifyInspectQueue) do
-		if p.name == player.name then
-			playerInQueue = true;
-			break;
-		end
-	end
-	return playerInQueue;
-end
-
 function inspect:QueueInspect(player, callback)
-	self:Debug("QueueInspect " .. player.name)
+	self:Debug("QueueInspect", player.name)

 	if not self.inspectQueue[player.id] then
 		self.inspectQueue[player.id] = {player = player, callbacks = {}};
@@ -180,7 +173,7 @@ function inspect:ResolveInspect(playerId, success)
 	local callbacks = self.inspectQueue[playerId].callbacks;
 	self.inspectQueue[playerId] = nil;

-	self:Debug("ResolveInspect " .. player.name .. " " .. (success and "success" or "fail"))
+	self:Debug("ResolveInspect", player.name, (success and "success" or "fail"))

 	if success then
 		if not hasPlayerInspectCache(player.id) then
diff --git a/main.lua b/main.lua
index 01a0f90..3aa77b3 100644
--- a/main.lua
+++ b/main.lua
@@ -1,32 +1,37 @@
 local addonName, addonTable = ...

 local tinsert = tinsert;
+local tremove = tremove;

 -- Create ACE3 addon
 local addon = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0", "AceHook-3.0")

+tinsert(addonTable, addon);
+_G[addonName] = addon
+
 -- Set up a default prototype for all modules
 local modPrototype = { Debug = function(self, ...) addon:Debug(...) end }
 addon:SetDefaultModulePrototype(modPrototype)

-
 -- Db default settings
 addon.dbDefaults = {
 	realm = {
 		modules = {}
 	},
 	global = {
-		dbVersion = 1
+		dbVersion = 1,
+		debugLog = {}
 	}
 }

 -- 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
+addon.dbVersion = 2

+-- Constants
+DEBUG_PRINT = true;
+DEBUG_LOG = true;
+MAX_NUM_DEBUG_LOG_ENTRIES = 300;

 local function getDifficultyNameById(difficultyId)
 	if difficultyId == 7 or difficultyId == 17 then
@@ -43,7 +48,17 @@ local function getDifficultyNameById(difficultyId)
 end

 function addon:Debug(...)
-	self:Print(...)
+	if DEBUG_PRINT then
+		self:Print(...)
+	end
+	if DEBUG_LOG then
+		local logData = {date("%d/%m %H:%M:%S")};
+		for i=1, select('#', ...) do
+			local v = select(i, ...)
+			tinsert(logData, v);
+		end
+		tinsert(self.db.global.debugLog, logData);
+	end
 end

 function addon:UpdateMyGuildName()
@@ -62,7 +77,9 @@ function addon:UpdateCurrentZone()
 	local zoneName = GetRealZoneText();
 	self.currentZone = {id = zoneId, name = zoneName};

-	if not IsInInstance() then
+	if IsInInstance() then
+		self:Debug("UpdateCurrentZone", zoneId, zoneName)
+	else
 		self:UnsetCurrentEncounter();
 	end
 end
@@ -70,7 +87,8 @@ end
 function addon:SetCurrentEncounter(encounterId, encounterName, difficultyId, raidSize)
 	local difficultyName = getDifficultyNameById(difficultyId);

-	self:Debug("SetCurrentEncounter " .. encounterName .. " " .. difficultyName)
+	self:Debug("SetCurrentEncounter", encounterId, encounterName, difficultyId, raidSize, difficultyName);
+
 	if difficultyName then
 		self.currentEncounter = {
 			zoneId = self.currentZone.id,
@@ -85,7 +103,10 @@ function addon:SetCurrentEncounter(encounterId, encounterName, difficultyId, rai
 end

 function addon:UnsetCurrentEncounter()
-	self.currentEncounter = nil
+	if self.currentEncounter then
+		self:Debug("UnsetCurrentEncounter");
+		self.currentEncounter = nil
+	end
 end

 function addon:IsInMyGuild(playerName)
@@ -115,26 +136,19 @@ function addon:SetRoleForPlayers(players)
 	end
 end

-function addon:INSPECT_READY(evt, GUID)
-	self:Debug("INSPECT_READY (" .. GUID .. ")")
-
-	self.inspect:INSPECT_READY(evt, GUID);
-end
-
 function addon:PLAYER_GUILD_UPDATE(evt, unitId)
 	if unitId == "player" then
-		self:Debug("PLAYER_GUILD_UPDATE (player)");
 		self:UpdateMyGuildName()
 	end
 end

 function addon:ENCOUNTER_START(evt, encounterId, encounterName, difficultyId, raidSize)
-	self:Debug("ENCOUNTER_START " .. encounterId)
+	self:Debug("ENCOUNTER_START", encounterId, encounterName, difficultyId, raidSize)
 	self:SetCurrentEncounter(encounterId, encounterName, difficultyId, raidSize)
 end

 function addon:ENCOUNTER_END(evt, encounterId, encounterName, difficultyId, raidSize, endStatus)
-	self:Debug("ENCOUNTER_END")
+	self:Debug("ENCOUNTER_END", encounterId, encounterName, difficultyId, raidSize, endStatus)
 	if endStatus == 1 then -- Success
 		self:SetCurrentEncounter(encounterId, encounterName, difficultyId, raidSize)
 	else
@@ -143,27 +157,37 @@ function addon:ENCOUNTER_END(evt, encounterId, encounterName, difficultyId, raid
 end

 function addon:ZONE_CHANGED_NEW_AREA(evt)
-	self:Debug("ZONE_CHANGED_NEW_AREA");
 	self:UpdateCurrentZone();
 end

 function addon:EndSegment()
 	self:Debug("EndSegment")
-
+
+	-- Find the skada set matching the encounter
+	-- Looking only at the lastest set should make sense,
+	-- as that set should be the boss segment we just ended.
+	local _, skadaSet = next(Skada:GetSets());
+	local encounter = self.currentEncounter;
+
 	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")
+	if not encounter then
+		self:Debug("No current encounter");
 		return
 	end

-	local encounter = self.currentEncounter;
-	encounter.duration = Skada.last.time;
+	if not skadaSet or not skadaSet.gotboss or skadaSet.mobname ~= encounter.name then
+		self:Debug("No Skada set found for boss");
+		return;
+	end
+
+	encounter.duration = skadaSet.time;
+	encounter.startTime = skadaSet.starttime;

-	local players = self:GetGuildPlayersFromSet(Skada.last);
+	local players = self:GetGuildPlayersFromSet(skadaSet);
 	self:SetRoleForPlayers(players);
 	self.inspect:GetInspectDataForPlayers(players, function()
 		self.highscore:AddEncounterParsesForPlayers(self.guildName, encounter, players);
@@ -174,6 +198,8 @@ end

 function addon:OnInitialize()
 	self.db = LibStub("AceDB-3.0"):New("GuildSkadaHighScoreDB", addon.dbDefaults, true)
+
+	-- Make sure db version is in sync
 	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));
@@ -181,6 +207,13 @@ function addon:OnInitialize()
 		self.db:ResetDB();
 		self.db.global.dbVersion = self.dbVersion;
 	end
+
+	-- Purge old logs
+	local numLogsToPurge = (#self.db.global.debugLog - MAX_NUM_DEBUG_LOG_ENTRIES);
+	while numLogsToPurge >= 0 do
+		tremove(self.db.global.debugLog, 1)
+		numLogsToPurge = numLogsToPurge - 1;
+	end
 end

 function addon:OnEnable()