<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE muclient>


<muclient>
<plugin
   name="SlainCounter"
   author="Keldar"
   id="8be822713b112f6073308e9b"
   language="Lua"
   purpose="Keeps track of the number of different mobs slain"
   save_state="y"
   date_written="2007-07-16"
   requires="4.00"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Commands:

slainshow   -  shows a table of all kills so far
slainreset  -  resets the plugin, clearing all data

]]>
</description>

</plugin>

<triggers>
  <trigger
   enabled="y"
   match="^You have slain (.+?)\, retrieving the corpse\.$"
   regexp="y"
   script="AddSlain"
   sequence="100"
  >
  </trigger>
</triggers>

<aliases>
  <alias
   script="SlainShow"
   match="^\s*slainshow\s*$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>

<aliases>
  <alias
   script="ResetSlain"
   match="^\s*slainreset\s*$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>


<!--  Script  -->


<script>
<![CDATA[
require "serialize"

slain_table = {}
if GetVariable("slain") ~= nil then
	loadstring(GetVariable("slain"))()
end

function save()
	SetVariable("slain", serialize.save("slain_table"))
end

function AddSlain(name,outp,wildcs)
	local victim = wildcs[1]
	if slain_table[victim ] ~= nil then
		slain_table[victim] = slain_table[victim] + 1
	else
		slain_table[victim] = 1
	end
	save()
end

function ResetSlain()
	slain_table = {}
	save()
	Note("SlainCounter data reset.")
end

function SlainShow()
	ColourNote("green", "", "You have slain:\n")
	
	-- flatten the slain table
	local t = {}
	for k,v in pairs(slain_table) do
		table.insert(t, {k,v})
	end
	
	-- sort the flattened table
	table.sort(t, function(a,b) return a[2] > b[2] end)
	
	-- go through the sorted table, displaying each result
	local name_width = 25
	local col = 0
	for _,v in ipairs(t) do
		local name, num = string.sub(v[1], 1, name_width), tostring(v[2])
		if col == 1 then
			ColourNote("blue", "", "    " .. "[", "silver", "", string.rep(" ", 6-#num) .. num, "blue", "", "]",
						"silver", "", " " .. name)
			col = 0
		elseif col == 0 then
			ColourTell("blue", "", "  " .. "[", "silver", "", string.rep(" ", 6-#num) .. num, "blue", "", "]",
						"silver", "", " " .. name .. string.rep(" ", name_width-#name))
			col = 1
		end
	end
	Note()
end

]]>


</script>


<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="TestVariables:help"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
function OnHelp ()
  world.Note (world.GetPluginInfo (world.GetPluginID (), 3))
end
]]>
</script> 

</muclient>
