From d89d68a920a9b1cab8e64479c5d870daec8f39fe Mon Sep 17 00:00:00 2001 From: Peter Eliasson Date: Wed, 14 Jan 2015 23:18:47 +0100 Subject: [PATCH] Added methods to highscore for getting data. Also added dps and hps fields to the database so they can be sorted by. --- highscore.lua | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 93 insertions(+), 10 deletions(-) diff --git a/highscore.lua b/highscore.lua index 10a7fc5..c19ec18 100644 --- a/highscore.lua +++ b/highscore.lua @@ -3,6 +3,7 @@ local addonName, addonTable = ... -- Global functions for faster access local tinsert = tinsert; local tContains = tContains; +local sort = sort; -- Set up module local addon = addonTable[1]; @@ -20,6 +21,22 @@ addon.dbDefaults.realm.modules["highscore"] = { ["*"] = { -- encounterId encounterName = nil, -- Note: Must be set. difficulties = { + -- playerParses is a list of objects: + --[[ + { + playerId = "", + playerName = "", + role = "", + specName = "", + itemLevel = 0, + damage = 0, + healing = 0, + dps = 0, + hps = 0, + duration = 0, + startTime = 0 + } + --]] ["LFR"] = {playerParses = {}}, ["Normal"] = {playerParses = {}}, ["Heroic"] = {playerParses = {}}, @@ -33,7 +50,7 @@ addon.dbDefaults.realm.modules["highscore"] = { } } -addon.dbVersion = addon.dbVersion + 1 +addon.dbVersion = addon.dbVersion + 2 -- Constants local TRACKED_ZONE_IDS = { @@ -62,16 +79,21 @@ 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, - role = player.role, - specName = player.specName, - itemLevel = player.itemLevel, - damage = player.damage, - healing = player.healing, - duration = duration, - startTime = startTime + playerId = player.id, + playerName = player.name, + role = player.role, + specName = player.specName, + itemLevel = player.itemLevel, + damage = player.damage, + healing = player.healing, + dps = dps, + hps = hps, + duration = duration, + startTime = startTime } tinsert(encounterTable.playerParses, parse); end @@ -114,6 +136,67 @@ function highscore:AddEncounterParsesForPlayers(guildName, encounter, players) end end +function highscore:GetParses(guildName, zoneId, encounterId, difficultyName, role, sortBy) + if (role ~= "TANK" and role ~= "HEALER" and role ~= "DAMAGER") then + return {}; + end + + if not sortBy then + if role == "TANK" or role == "DAMAGER" then + sortBy = "dps" + elseif role == "HEALER" then + sortBy = "hps" + end + end + + local encountersTable = self.db.guilds[guildName].zones[zoneId].encounters; + local parsesTable = encountersTable[encounterId].difficulties[difficultyName]; + + -- Get all parses for the specified role + local parses = {}; + for _, parse in ipairs(parsesTable.playerParses) do + if parse.role == role then + tinsert(parses, parse); + end + end + + -- Sort these parses by the sortBy field + sort(parses, function(a, b) + return a[sortBy] > b[sortBy]; + end) + return parses; +end + +-- Returns a list of encounters in the zone for the guild that +-- the guild has parses for. The returned value is a list of +-- {encounterId, encounterName}. +function highscore:GetEncounters(guildName, zoneId) + local encounters = {}; + local encountersTable = self.db.guilds[guildName].zones[zoneId].encounters; + for encounterId, encounter in pairs(encountersTable) do + tinsert(encounters, {encounterId, encounter.encounterName}); + end + return encounters; +end + +-- Returns a list of zones that the guild has encounters for. +-- The returned value is a list of {zoneId, zoneName}. +function highscore:GetZones(guildName) + local zones = {}; + for zoneId, zone in pairs(self.db.guilds[guildName].zones) do + tinsert(zones, {zoneId, zone.zoneName}); + end + return zones; +end + +function highscore:GetGuildNames() + local guildNames = {}; + for guildName, _ in pairs(self.db.guilds) do + tinsert(guildNames, guildName); + end + return guildNames; +end + function highscore:OnEnable() self.db = addon.db.realm.modules["highscore"]; -- 1.7.9.5