Quantcast

Removed dps/hps, instead calc before giving away entries.

Peter Eliasson [01-14-15 - 22:58]
Removed dps/hps, instead calc before giving away entries.
Filename
highscore.lua
diff --git a/highscore.lua b/highscore.lua
index c19ec18..417aeb3 100644
--- a/highscore.lua
+++ b/highscore.lua
@@ -31,8 +31,6 @@ addon.dbDefaults.realm.modules["highscore"] = {
 										itemLevel 	= 0,
 										damage 		= 0,
 										healing 	= 0,
-										dps			= 0,
-										hps			= 0,
 										duration 	= 0,
 										startTime 	= 0
 									}
@@ -79,9 +77,6 @@ local function getOrCreateEncounterTable(db, guildName, zoneId, zoneName, encoun
 end

 local function addEncounterParseForPlayer(encounterTable, startTime, duration, player)
-	local dps = duration > 0 and (player.damage/duration) or 0;
-	local hps = duration > 0 and (player.healing/duration) or 0;
-
 	local parse = {
 		playerId 	= player.id,
 		playerName 	= player.name,
@@ -90,14 +85,32 @@ local function addEncounterParseForPlayer(encounterTable, startTime, duration, p
 		itemLevel 	= player.itemLevel,
 		damage 		= player.damage,
 		healing 	= player.healing,
-		dps 		= dps,
-		hps 		= hps,
 		duration 	= duration,
 		startTime 	= startTime
 	}
 	tinsert(encounterTable.playerParses, parse);
 end

+-- Function for convering a database representation of
+-- a parse to a parse that can be returned to users.
+-- Copies all values and adds calculated once (like dps)
+local function getReturnableParse(parse)
+	local parseCopy = {};
+	for key, val in pairs(parse) do
+		parseCopy[key] = val;
+	end
+
+	-- Calculate values
+	parseCopy["dps"] = 0;
+	parseCopy["hps"] = 0;
+	if parse.duration > 0 then
+		parseCopy["dps"] = parse.damage / parse.duration;
+		parseCopy["hps"] = parse.healing / parse.duration;
+	end
+
+	return parseCopy;
+end
+

 function highscore:AddEncounterParsesForPlayers(guildName, encounter, players)
 	local zoneId = encounter.zoneId;
@@ -152,16 +165,17 @@ function highscore:GetParses(guildName, zoneId, encounterId, difficultyName, rol
 	local encountersTable = self.db.guilds[guildName].zones[zoneId].encounters;
 	local parsesTable = encountersTable[encounterId].difficulties[difficultyName];

-	-- Get all parses for the specified role
+	-- Get a copy of all parses for the specified role
 	local parses = {};
 	for _, parse in ipairs(parsesTable.playerParses) do
 		if parse.role == role then
-			tinsert(parses, parse);
+			local parseCopy = getReturnableParse(parse);
+			tinsert(parses, parseCopy);
 		end
 	end

 	-- Sort these parses by the sortBy field
-	sort(parses, function(a, b)
+	sort(parses, function(a, b)
 		return a[sortBy] > b[sortBy];
 	end)
 	return parses;