Signed-off-by: His Dad <John@hisdad.org>

His Dad [10-17-16 - 17:48]
Signed-off-by: His Dad <John@hisdad.org>
Filename
history.lua
diff --git a/history.lua b/history.lua
index 660a37a..1d8b215 100644
--- a/history.lua
+++ b/history.lua
@@ -8,7 +8,7 @@ hist = {
 	playerName = "",	-- might change
 	playerID = "",  	-- Unique through renames
 	tz_offset =0,
-	debug = false
+	debug = true
 }

 local function log_truncate(max)
@@ -38,8 +38,8 @@ local function log_me(text)	-- per char log
 							})
 end

-local Keep_Acheivement = function (cat) -- is this an acheivement we want to save?
-	if cat == 8 or cat == 3 or cat == 5 then
+local Keep_Achievement = function (cat) -- is this an achievement we want to save?
+	if cat == 9 or cat == 8 or cat == 3 or cat == 4 or cat == 5 or cat == nil then
 		return false
 	end
 	return true
@@ -75,19 +75,18 @@ end

 local function Achievement(_, name, points, id, link)
 	local description
-	local cat  = GetCategoryInfoFromAchievementId(id)
+	local category_id  = GetCategoryInfoFromAchievementId(id)
 	local category_name =GetAchievementCategoryInfo(category_id)
 		_,description,_,_,_,_,_= GetAchievementInfo(id)
-	if Keep_Acheivement(cat)  then
-	hist.me.ach[id]={
-		["Category_ID"] = cat,
-		["Category"] = category_name,
-		["time"] = GetTimeStamp(),
-		["points"] = points,
-		["name"] = zo_strformat(name),
-		["description"] = zo_strformat(description),
-		["link"] = link
-		}
+	if Keep_Achievement(category_id)  then
+		hist.me.ach[id] = {}
+		hist.me.ach[id].Category_ID = category_id
+		hist.me.ach[id].Category = category_name
+		hist.me.ach[id].time = GetTimeStamp()
+		hist.me.ach[id].points = points
+		hist.me.ach[id].name = zo_strformat(name)
+		hist.me.ach[id].description = zo_strformat(description)
+		hist.me.ach[id].link = link

       if hist.debug then
         d("Achievement Awarded:.. ")
@@ -101,52 +100,66 @@ local function  log_clear()
 	log("Cleared")
 end
 function load_history()
--- load up historic Achievements
+-- load up historic Achievements.
+-- Note some achievements like "Level 40 Hero" have become obsolete. CategoryID is nil
 	log("Request to load historic Dungeon Achievements")
 	local name,points,description,completed,adate,atime
-  local cat
-  local count = 0
+	local count = 0
+	local reject = 0
+	local flag = 0
 	for i = 1,2000 do
 		name,description,points,_,completed,adate,atime= GetAchievementInfo(i)
 		if completed then
-			cat,_,_ =GetCategoryInfoFromAchievementId(i)
-			if Keep_Acheivement(cat)  then
+			local category_id = GetCategoryInfoFromAchievementId(i)	-- nill if bad
+			if Keep_Achievement(category_id) then
 				if hist.me.ach[i] == nil then
-				--	log("Added historic achievement " ..i .. " " .. name)
-						local category_id  = GetCategoryInfoFromAchievementId(i)
-						local category_name =GetAchievementCategoryInfo(category_id)
-						hist.me.ach[i]={
-							["Category_ID"] = category_id,
-							["Category"] = category_name,
-							["time"] = (luatz_esodate(adate .. " " .. atime)+ hist.tz_offset),
-							["points"] = points,
-							["name"] = zo_strformat(name),
-							["description"] = zo_strformat(description),
-							["link"] = GetAchievementLink(i)
-							}
+						hist.me.ach[i] = {}
+						hist.me.ach[i].Category_ID = category_id
+						hist.me.ach[i].Category = GetAchievementCategoryInfo(category_id)
+						hist.me.ach[i].time = (luatz_esodate(adate .. " " .. atime)+ hist.tz_offset)
+						hist.me.ach[i].points = points
+						hist.me.ach[i].name = zo_strformat(name)
+						hist.me.ach[i].description = zo_strformat(description)
+						hist.me.ach[i].link = GetAchievementLink(i)
 						count = count + 1
 				end
+			else
+				reject = reject +1
 			end
 		end
 	end
 	log("Added " .. tostring(count) .. " historic achievements.")
-	d("reloaded achievements")
+	if hist.debug then
+		d("Added " .. count .. " achievements.")
+		d("Rejected " .. reject .. " achievements.")
+	end
 end

 local function fix()  -- fix links etc for any old data
 local description
 	hist.me.ach_updates = nil
 	log("Request to fix Achievement data")
- for i,_ in pairs(hist.me.ach) do
- 		_, description,_,_,_,_,_= GetAchievementInfo(i)
+	local obsolete = 0
+	for i,_ in pairs(hist.me.ach) do
+		_, description,_,_,_,_,_= GetAchievementInfo(i)
 		local category_id  = GetCategoryInfoFromAchievementId(i)
 		local category_name =GetAchievementCategoryInfo(category_id)
-		hist.me.ach[i]["Category_ID"] = category_id
-		hist.me.ach[i]["Category"] = category_name
-		hist.me.ach[i]["description"] = zo_strformat(description)
-		hist.me.ach[i]["link"] = GetAchievementLink(i)
- end
-
+		if category_id ~= nil then
+			hist.me.ach[i]["Category_ID"] = category_id
+			hist.me.ach[i]["Category"] = category_name
+			hist.me.ach[i]["description"] = zo_strformat(description)
+			hist.me.ach[i]["link"] = GetAchievementLink(i)
+		else	--Obsolete
+			hist.me.ach[i] = nil
+			obsolete = obsolete +1
+		end
+	end
+	local msg
+	msg = obsolete .. " obsolete achievments removed."
+	log (msg)
+	if hist.debug then
+		d(msg)
+	end
 end

 local function new_map()
@@ -328,14 +341,21 @@ local function Dead()
 end

 del_old_categories = function()
+	local count = 0
 	log("Unwanted Category information Purged for all chars.")
 	for playerID, player in pairs (hist.SV.data)  do
 		for ID, ach in pairs  (player.ach) do
-			if Keep_Acheivement (ach["Category_ID"]) then
+			if  not (Keep_Achievement (ach["Category_ID"])) then
 				player.ach[ID] = nil
+				count = count +1
 			end
 		end
 	end
+	local msg = count .. " achievement records deleted."
+	log(msg)
+	if hist.debug then
+		d(msg)
+	end
 end

 function hist.Initialise(_, addOnName)
@@ -354,7 +374,7 @@ function hist.Initialise(_, addOnName)
     -- Load the saved variables
     hist.SV = ZO_SavedVars:NewAccountWide("History_SV", 1, nil, nil)

-	--remove old acheivement categories
+	--remove old achievement categories

 	if hist.SV.catcleaned == nil then
 		del_old_categories()
@@ -530,7 +550,7 @@ function hist.Initialise(_, addOnName)
 	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_PLAYER_ACTIVATED, Activated)
 --	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_PLAYER_DEACTIVATED, Activated)
 	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_PLAYER_DEAD, Dead)
-	EVENT_MANAGER:RegisterForEvent(hist.name, EVEN_ZONE_CHANGED, new_map)
+	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_ZONE_CHANGED, new_map)
 	hist.initialised = true
 end

@@ -540,5 +560,6 @@ SLASH_COMMANDS["/histload"] = load_history
 SLASH_COMMANDS["/histfix"] = fix
 SLASH_COMMANDS["/histclear"] = log_clear
 SLASH_COMMANDS["/histstart"] = get_start
+SLASH_COMMANDS["/histcleancat"] = del_old_categories

 EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_ADD_ON_LOADED, hist.Initialise)