Quantcast
local item_info = {}

loadfile("scanned/gem-info.lua")()

local function strsplit(delimiter, subject, pieces)
	delimiter = string.gsub(delimiter, "([%[%]+?()%%*-])", "%%%1")
	subject = string.gsub(subject, delimiter, "\a")
	local iterator = string.gmatch(subject, "[^\a]+")
	local result = {}
	if pieces then
		for i = 1, pieces do
			table.insert(result, iterator())
		end
	else
		local str = iterator()
		while str do
			table.insert(result, str)
			str = iterator()
		end
	end
	return unpack(result)
end

local function getPadLength(num)
	return tostring(num):len()
end

local function padInteger(num, len)
	return string.rep("0", len - getPadLength(num)) .. num
end

i = 0
local start = padInteger(i, 5)
local finish = padInteger(i + 999, 5)

local func = loadfile(string.format("scanned/item-info-%s_%s.lua", start, finish))
while func do
	func()

	for k, v in pairs(_G[string.format("IS_item_info_%s_%s", start, finish)]) do
		item_info[k] = v
	end

	i = i + 1000
	start = padInteger(i, 5)
	finish = padInteger(i + 999, 5)
	func = loadfile(string.format("scanned/item-info-%s_%s.lua", start, finish))
end

local gem_ids_to_item_info = {}

for gemId, gemInfo in pairs(IS_gem_info) do
	local _, itemId = strsplit("|", gemInfo[2])
	local pieces = { strsplit(":", itemId) }
	pieces[1] = pieces[1]:sub(2)
	itemId = table.concat(pieces, ":", 1, 10)
	gem_ids_to_item_info[gemId] = item_info[itemId]
	if not item_info[itemId] then
		io.write(string.format("Unhandled item: %s\n", itemId))
	end
end

loadfile("sort.lua")()

io.output("gem-ids-to-item-info.lua")
sort(gem_ids_to_item_info, "gem_ids_to_item_info")
io.close()