Quantcast

Added a second loop to try invalid items again

Kevin Lyles [12-11-10 - 15:43]
Added a second loop to try invalid items again
Filename
ItemScanner.lua
diff --git a/ItemScanner.lua b/ItemScanner.lua
index 4881580..ddee57b 100644
--- a/ItemScanner.lua
+++ b/ItemScanner.lua
@@ -133,48 +133,75 @@ end
 local function itemParseCoroutine(start)
 	local chunkStart, chunkEnd = chunkSize * math.floor(start / chunkSize), chunkSize * math.floor((start + chunkSize) / chunkSize) - 1
 	IS_status.items.scan_active = true
+	IS_status.items.finished = false
 	coroutine.yield()
 	local startTime = GetTime()
 	local roundStartTime = startTime
 	local numProcessed, roundNumProcessed, valid, roundValid = 0, 0, 0, 0
 	print(string.format("ItemScanner: starting scan at item %d", start))
-	for i = start, math.min(chunkEnd, maxItem) do
-		if not IS_status.items.invalid[i] or (type(IS_status.items.invalid[i]) == "number" and IS_status.items.invalid[i] < CONSECUTIVE_INVALID_TO_IGNORE) then
-			IS_status.items.last_scanned = i
-			local itemStartTime = GetTime()
-			if scanItemLink("item:" .. i .. ":0:0:0:0:0:0:0:85") then
-				IS_status.items.last_valid = i
-				-- Reset invalid count
-				IS_status.items.invalid[i] = false
-				valid, roundValid = valid + 1, roundValid + 1
+
+	local function scan(i, scanningAll)
+		IS_status.items.last_scanned = i
+		local itemStartTime = GetTime()
+		if scanItemLink("item:" .. i .. ":0:0:0:0:0:0:0:85") then
+			if scanningAll then
+				if IS_status.items.invalid[i] ~= false then
+					valid, roundValid = valid + 1, roundValid + 1
+				end
 			else
-			-- start with 3 if no previous value
-				IS_status.items.invalid[i] = (IS_status.items.invalid[i] or 2) + 1
+				if IS_status.items.scan_active then
+					IS_status.items.last_valid = i
+				end
+				valid, roundValid = valid + 1, roundValid + 1
 			end
+			-- Reset invalid count
+			IS_status.items.invalid[i] = false
+		else
+			-- start with 3 if no previous value
+			IS_status.items.invalid[i] = (IS_status.items.invalid[i] or 2) + 1
+		end
+		numProcessed = numProcessed + 1
+		roundNumProcessed = roundNumProcessed + 1
+		while GetTime() - itemStartTime < VALID_DELAY do
+			coroutine.yield()
+		end
+
+		local currentTime = GetTime()
+		if currentTime - roundStartTime >= 120 then
+			local elapsedTime = currentTime - startTime
+			print(string.format("ItemScanner: at item %d, %d in %.1f sec. (%.2f/min.) (%d valid), %d this round (%d valid)", i, numProcessed, elapsedTime, numProcessed / elapsedTime * 60, valid, roundNumProcessed, roundValid))
+			roundStartTime = roundStartTime + 120
+			roundNumProcessed, roundValid = 0, 0
+		end
+	end
+
+	for i = start, math.min(chunkEnd, maxItem) do
+		if not IS_status.items.invalid[i] or (type(IS_status.items.invalid[i]) == "number" and IS_status.items.invalid[i] < CONSECUTIVE_INVALID_TO_IGNORE) then
+			scan(i, false)
 			if not IS_status.items.scan_active then
 				break
 			end
-			numProcessed = numProcessed + 1
-			roundNumProcessed = roundNumProcessed + 1
-			while GetTime() - itemStartTime < VALID_DELAY do
-				coroutine.yield()
-			end
-			local currentTime = GetTime()
-			if currentTime - roundStartTime >= 120 then
-				local elapsedTime = currentTime - startTime
-				print(string.format("ItemScanner: at item %d, %d in %.1f sec. (%.2f/min.) (%d valid), %d this round (%d valid)", i, numProcessed, elapsedTime, numProcessed / elapsedTime * 60, valid, roundNumProcessed, roundValid))
-				roundStartTime = roundStartTime + 120
-				roundNumProcessed, roundValid = 0, 0
-			end
 		end
 	end
+	IS_status.items.finished = true
+	local chunkName = chunkStart .. "-" .. chunkEnd
+	while IS_status.items.scan_active do
+		local i = (IS_status.items.last_invalid[chunkName] or chunkStart - 1)
+		repeat
+			i = i + 1
+			if i > chunkEnd or i > maxItem then
+				i = chunkStart
+			end
+		until IS_status.items.invalid[i] ~= true
+		IS_status.items.last_invalid[chunkName] = i
+		scan(i, true)
+	end

 	local elapsedTime = GetTime() - startTime
 	if elapsedTime == 0 then
 		elapsedTime = 0.001
 	end
 	print(string.format("ItemScanner: scan stopped at item %d, %d in %.1f sec. (%.2f/min.) (%d valid)", IS_status.items.last_scanned, numProcessed, elapsedTime, numProcessed / elapsedTime * 60, valid))
-	IS_status.items.scan_active = false

 	return true
 end
@@ -247,12 +274,20 @@ if not IS_status.items.invalid then
 	IS_status.items.invalid = {}
 end

+if not IS_status.items.last_invalid then
+	IS_status.items.last_invalid = {}
+end
+
 SLASH_ITEMSCANNER1="/is"
 SLASH_ITEMSCANNER2="/itemscanner"
 SlashCmdList["ITEMSCANNER"] = commandHandler

 -- Autoresumes on login
 local function resume()
+	if not IS_status.items.last_invalid then
+		IS_status.items.last_invalid = {}
+	end
+
 	if IS_status.items.scan_active then
 		if IS_status.items.finished then
 			commandHandler("items next-chunk")