<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<!--
dfsf
-->

<muclient>
<plugin
   name="RiftList"
   author="Keldar"
   id="b0a0404437ae272e2ac6641b"
   language="Lua"
   purpose="Displaying the rift contents"
   save_state="y"
   date_written="2007-07-21"
   requires="4.00"
   version="1.0"
   >
<description trim="y">
<![CDATA[
sdf
]]>
</description>

</plugin>


<aliases>
  <alias
   script="RiftList"
   match="^\s*ir\s*$"
   enabled="y"
   regexp="y"
   ignore_case="y"
   sequence="100"
  >
  </alias>
</aliases>

<triggers>
  <trigger
   enabled="y"
   match="^You (remove|store) \d+ (.*), bringing the total(?: in the Rift)? to (\d+)\.$"
   regexp="y"
   script="UpdateRift"
   sequence="100"
  >
  </trigger>
</triggers>

<!--  Script  -->


<script>
<![CDATA[
require "flc.kwait"
require "tprint"

rift = {}
rift.herbs = {}
rift.misc = {}
rift.comms = {}
rift.crystals = {}
rift.inks = {}

herbs = {"bloodroot", "moss", "bayberry", "bellwort", 
		"cohosh", "echinacea", "ginger", "ginseng", 
		"goldenseal", "hawthorn", "kelp", "kola",
		"kuzu", "lady's slipper", "lobelia", "myrrh",
		"ash", "pear", "sileris", "skullcap", "elm",
		"valerian", "weed"}
herb_dict = {["lady's slipper"] = "slipper"}

misc = {"venom sac", "sidewinder skin", "feather", "diamond dust"}


comms = {"bone", "coal", "cloth", "gems", "^%s*gold%s*$", "ice",
		"iron", "leather", "obsidian", "platinum", "rope",
		"silver bar", "steel", "wood"}
comm_dict = {["silver bar"] = "silver", ["^%s*gold%s*$"] = "gold"}
	
inks = {"green ink", "blue ink", "red ink", "yellow ink", 
		"purple ink", "gold ink"}

crystals = {"cube", "cylinder", "crystal diamond", "disc", "egg",
			"pentagon", "polyhedr", "pyramid", "sphere",
			"spiral", "torus"}
crystal_dict = {["crystal diamond"] = "diamond", ["polyhedr"] = "polyhedron"}

parse_order = {}
parse_order.herbs = {herbs, herb_dict}
parse_order.misc = {misc, {}}
parse_order.comms = {comms, comm_dict}
parse_order.inks = {inks, {}}
parse_order.crystals = {crystals, crystal_dict}


-- initialize the rift table with zeros
for k,v in pairs(parse_order) do
	for i,item in ipairs(v[1]) do
		if v[2][item] then
			rift[k][v[2][item]] = 0
		else
			rift[k][item] = 0
		end
	end
end


function UpdateRift(name, outp, wildcs)
	
end

function RiftList(name, outp, wildcs)
	kwait.make(function()
	Send("config pagelength 250")
	Send("ir")
	
	local line,wildcs = kwait.regexp([[^Your current pagelength\: 250$]], 5, true)
	if line == nil then return end
	
	line, wildcs = kwait.regexp([[^Glancing into the Rift, you see\:$]], 10, true)
	if line == nil then return end
	
	while true do
		line,wildcs = kwait.regexp([[^(?:\s+\[\s*([0-9]+)\]([ \'a-z]+)(?:\[\s*([0-9]+)\]([ \'a-z]+)(?:\[\s*([0-9]+)\]([ \'a-z]+)|)|)|^\d+h, \d+m(?:, \d+e|)(?:, \d+w|) c?e?x?k?d?b?\@?-$)$]], 10, true)
		if (line ~= nil) and (string.find(line, "^%d+h, %d+m") == nil) then
			parse_rift(wildcs)
		else
			break
		end
	end
	
	if string.find(line, "^%d+h, %d+m") then
		display_results()
	end
	Send("config pagelength 30")
	line,wildcs = kwait.regexp([[^Your current pagelength\: 30$]], 5, true)
	end)
end

function parse_rift(wildcs)
	
	local find_entry = function(tab, dict_tab, wildcs)
		if wildcs[2] then
			for _,v in ipairs(tab) do
				if string.find(wildcs[2], v) then
					if dict_tab[v] then
						return dict_tab[v], tonumber(wildcs[1])
					else
						return v, tonumber(wildcs[1])
					end
				end
			end
		end
		return nil
	end
	
	for i=2,6,2 do
		if not wildcs[i] then break end
		local found = false
		for k,v in pairs(parse_order) do
			local name, num = find_entry(v[1], v[2], {wildcs[i-1], wildcs[i]})
			if name ~= nil then
				rift[k][name] = num
				found = true
				break
			end
		end
		if not found then print("Not found riftable: ", wildcs[i]) end
	end
end

function display_results()
	ColourNote("green", "", "Glancing into the Rift, you see:\n")
	local cat_labels = {["herbs"] = "Concoctions",
						["comms"] = "Commodities",
						["crystals"] = "Crystals",
						["inks"] = "Inks",
						["misc"] = "Miscellaneous"
						}
	local display_order = {"herbs", "misc", "comms", "inks", "crystals"}
	
	
	for _,order in ipairs(display_order) do
		local cat, data = order, rift[order]
		-- flatten the rift table
		local t = {}
		if data then
			for k,v in pairs(data) do
				table.insert(t, {k,v})
			end
		end
		
		if #t > 0 then
			
			local col_width = 15
			
			-- print the category label
			local dash = 12
			local padding = (((col_width*3)+27)-(dash*2)-2-#cat_labels[cat]-2)/2
			ColourNote("silver", "", string.rep(" ", padding),
					"teal", "", string.rep("-", dash) .. "[",
					"silver", "", " ",
					"green", "", cat_labels[cat],
					"silver", "", " ",
					"teal", "", "]" .. string.rep("-", dash))
							
			-- sort the flat table
			table.sort(t, function(a,b) return a[2] > b[2] end)
	
			local col = 0

			for _,v in ipairs(t) do
				if col == 3 then Note(); col = 0 end
					col = col + 1
					ColourTell("silver", "", "  ",
						"blue", "", "[",
						"silver", "", string.rep(" ", 4-#tostring(v[2])) .. tostring(v[2]),
						"blue", "", "]",
						"silver", "", " " .. v[1] .. string.rep(" ", col_width-#v[1]))
			end
			Note()

		end
	end
end
]]>
</script>


<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="RiftList:help"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
function OnHelp ()
  world.Note (world.GetPluginInfo (world.GetPluginID (), 3))
end
]]>
</script> 

</muclient>
