Update for CP and time

His Dad [10-02-16 - 08:19]
Update for CP and time
Filename
HistOffline.lua
history.lua
diff --git a/HistOffline.lua b/HistOffline.lua
index c018a49..c1e0802 100644
--- a/HistOffline.lua
+++ b/HistOffline.lua
@@ -193,7 +193,11 @@ for acc,_ in pairs(History_SV["Default"]) do
 			table.insert(accounts[acc].playerIDs_vet,playerID)
 		end
 		-- == Cumulative TimePlayed
-		me.timeplayed = math.floor(thischar.levels[level].time/60)
+		if thischar.timeplayed == nil then
+			me.timeplayed = math.floor(thischar.levels[level].time/60)
+		else
+			me.timeplayed = thischar.timeplayed
+		end
 	end
 end

diff --git a/history.lua b/history.lua
index 529b562..8a9be6d 100644
--- a/history.lua
+++ b/history.lua
@@ -1,6 +1,6 @@
 hist = {
 	name = "history",
-	version = "22",
+	version = "23",
 	initialised = false,
 	SV={},
 	L={},
@@ -8,7 +8,7 @@ hist = {
 	playerName = "",	-- might change
 	playerID = "",  	-- Unique through renames
 	tz_offset =0,
-	debug = false
+	debug = true
 }

 local function log_truncate(max)
@@ -155,7 +155,8 @@ local function new_map()
 end

 local function Activated()
-	hist.me.levels[hist.me.level].time = math.floor(GetSecondsPlayed() /60)
+	hist.me.timeplayed = math.floor(GetSecondsPlayed() /60)
+	hist.me.levels[hist.me.level].time = hist.me.timeplayed

 	local map = new_map()
 	if hist.me.maps[map].visit == nil then
@@ -166,7 +167,6 @@ local function Activated()
 end


-
 local function levelup(_, unitTag,_)
 	if unitTag == "player" then
 	local oldlevel = hist.me.level
@@ -211,6 +211,54 @@ local function levelup(_, unitTag,_)
 		end
 	end
 end
+
+
+local function CP_Levelup(_,unitTag, old, new)
+	if unitTag == "player" then
+		local oldlevel = hist.me.CP.level
+		hist.me.level =  GetUnitLevel("player") + GetUnitVeteranRank("player")
+		log("CP Gained was " .. old .. ",  now " .. new ..  ",  level: " .. hist.me.level)
+		hist.me.levels[hist.me.level] = {
+			begin = GetTimeStamp(),
+			time  = 0,		-- Accumulated time in Minutes
+			Ach_Points = GetEarnedAchievementPoints(),
+			deaths = 0
+		}
+		local map = new_map()
+		--Update Level data with map
+		if hist.me.levels[hist.me.level].map == nil then
+			hist.me.levels[hist.me.level].map = map
+
+			local msg = "First CP Gained in map " .. map
+			log (msg)
+			if hist.debug then
+				d(msg)
+			end
+		end
+
+
+
+		-- Duplication of info, but easier to work with
+		if hist.me.maps[map].timeslevel == nil then
+			hist.me.maps[map].timeslevel = 1
+			hist.me.maps[map].firstlevel = hist.me.level
+		else
+			hist.me.maps[map].timeslevel = hist.me.maps[map].timeslevel + 1
+		end
+
+		if hist.debug then
+		  d("Hist: CP Gained:.. ")
+			  d(hist.me.level)
+		end
+	end
+end
+
+local function CP_Achieved(_,_)
+	log("Achieved Champion Rank.")
+	hist.me.CP.FirstLocation = new_map()
+	hist.me.CP.FirstTime  = GetTimeStamp()
+
+end
 local function gendertext()
 	if (GetUnitGender("player") == GENDER_MALE)
 	then return "M"
@@ -236,6 +284,8 @@ local function setup_char()
 		hist.me.ach={}
 		hist.me.maps={}
 		hist.me.log={}
+		hist.me.CP={}
+		hist.me.CP.level =0
 		hist.me.levels = {}
 		hist.me.levels[hist.me.level] = {
 			begin = GetTimeStamp(),
@@ -292,7 +342,7 @@ function hist.Initialise(_, addOnName)
     -- Load the saved variables
     hist.SV = ZO_SavedVars:NewAccountWide("History_SV", 1, nil, nil)

-	if hist.SV.log == nil then
+	if hist.SV.log == nil then
 		hist.SV.log = {}
 		log("hist.SV.log created")
 	end
@@ -319,7 +369,7 @@ function hist.Initialise(_, addOnName)
 		end
 	-- Ok playerID may be nil, but we might still have data under player name
 	-- In which case we rename it, otherwise it is a new char.
-
+
 		if hist.SV.data[hist.playerName] == nil then
 			--No name, no ID, must be new.
 			setup_char()
@@ -328,7 +378,7 @@ function hist.Initialise(_, addOnName)
 			log("New data format, saving " .. hist.playerName .. " as " ..  hist.playerID)
 			hist.SV.data[hist.playerID] = hist.SV.data[hist.playerName]
 			hist.SV.data[hist.playerID].name = hist.playerName
-
+
 			hist.SV.data[hist.playerName] = nil		-- and on the eighth day God invented Lua
 		end
 	end
@@ -347,14 +397,19 @@ function hist.Initialise(_, addOnName)
 	if hist.me.maps == nil then
 		hist.me.maps = {}
 	end
-
+
+	if hist.me.CP == nil then
+		hist.me.CP = {}
+		hist.me.CP.level =0
+	end
+
 	if hist.me.log == nil then	-- special events we want to keep, like name changes
 		hist.me.log = {}
-	end
-
-
-
-
+	end
+
+
+
+
 	-- fix missed level, leveled up with addon disabled


@@ -410,11 +465,13 @@ function hist.Initialise(_, addOnName)
 		log("Changed Gender, Was " .. hist.me.Gender)
 		log_me("Changed Gender, Was " .. hist.me.Gender)
 		hist.me.Gender = gendertext()
-	end
-
-
+	end
+
+
 	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_ACHIEVEMENT_AWARDED, Achievement)
 	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_LEVEL_UPDATE, levelup)
+	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_CHAMPION_POINT_UPDATE, CP_Levelup)
+	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_CHAMPION_LEVEL_ACHIEVED , CP_Achieved)
 	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_VETERAN_RANK_UPDATE, levelup)
 	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_PLAYER_ACTIVATED, Activated)
 --	EVENT_MANAGER:RegisterForEvent(hist.name, EVENT_PLAYER_DEACTIVATED, Activated)