<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
 name="Slain"
 author="Trevize"
 id="e3eadf556f60fd5acfde4baf"
 language="Lua"
 purpose="Slain Tracker"
 save_state="y"
 requires="4.40"
 version="1.20">

<description trim="y">
<![CDATA[

Slain Tracker 1.2.0

Do SLAIN HELP for commands.

Enjoy!

-Trevize

]]>
</description>

</plugin>


<aliases>
  <alias
   match="^\s*slain .+$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="101">
    <send>slain.help ()</send>
  </alias>

  <alias
   match="^\s*slain\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>slain.run ()</send>
  </alias>

  <alias
   match="^\s*slain\s+add\s+(\d+)\s+(.+?)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>
      slain.add ("%2", "%1")
      AnsiNote ("Added %1 \\"%2\\" to the slain tracker.")
      prompt.draw ()
    </send>
  </alias>

  <alias
   match="^\s*slain\s+delete\s+(.+?)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>slain.delete ("%1")</send>
  </alias>

  <alias
   match="^\s*slain\s+remove\s+(\d+)\s+(.+?)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>slain.remove ("%2", "%1")</send>
  </alias>

  <alias
   match="^\s*slain\s+reset(?:\s+(confirm))?\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>slain.reset ("%1")</send>
  </alias>

  <alias
   match="^\s*slain\s+search\s+(.+?)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>slain.run ("%1")</send>
  </alias>
</aliases>


<triggers>
  <trigger
   enabled="y"
   group="slain"
   keep_evaluating="y"
   match="^You have slain (.+)\, retrieving the corpse\.$"
   regexp="y"
   send_to="12"
   sequence="100">
    <send>slain.add ("%1")</send>
  </trigger>

  <trigger
   keep_evaluating="y"
   match="^\d+h(, \d+m)?(, \d+e)?(, \d+w)? [@cexkdb]*(?: Vote)?-"
   enabled="y"
   regexp="y"
   send_to="14"
   sequence="25">
    <send>prompt.data = TriggerStyleRuns</send>
  </trigger>
</triggers>


<script>
<![CDATA[

-- ============
--  foundation
-- ============

table.save = function (mushvar, luavar, luavarname)
  if type (luavar) == "table" then
    local var = ""
    for k, v in pairs (luavar) do
      if type (v) == "boolean" or type (v) == "number" or type (v) == "string" then
        if type (k) == "string" then k = string.format ("%q", k) end
        local key = "[" .. k .. "]"
        local val
        if type (v) == "string" then val = string.format ("%q", v) else val = tostring (v) end
        var = var .. luavarname .. key .. " = " .. val .. "\r\n"
      elseif type (v) == "table" then
        local keyname = k
        if type (keyname) == "string" then keyname = string.format ("%q", keyname) end
        var = var .. table.save (false, luavar[k], luavarname .. "[" ..keyname .. "]")
      end -- if
    end -- for
    if mushvar then SetVariable (mushvar, var) else return var end
  end -- if
end -- func

table.load = function (name)
  if GetVariable (name) then
    loadstring (GetVariable (name)) ()
    return true
  end -- if
end -- func

ansicolor = function (fore, back)
  local bold = false
  local fore = tonumber (fore) or 7
  if fore >= 0 and fore <= 7 then
    fore = fore + 30
  elseif fore >= 8 and fore <= 15 then
    fore = fore + 22
    bold = true
  else
    fore = 37
  end -- if
  local back = tonumber (back) or 0
  if back > 0 and back <= 7 then
    back = back + 40
  else
    back = 40
  end -- if
  if not bold then
    return ANSI (0, fore, back)
  else
    return ANSI (0, fore, 1, back)
  end -- if
end -- func

prompt = {}
prompt.data = false
prompt.draw = function ()
  if prompt.data then
    for k,v in ipairs (prompt.data) do
      ColourTell (RGBColourToName (v.textcolour),
                  RGBColourToName (v.backcolour),
                  v.text)
    end -- for
  end -- if
  Note ("")
end -- func



-- =======
--  slain
-- =======

slain = {

  current = {},

  add = function (item, num)
    slain.current[item] = (slain.current[item] or 0) + (num or 1)
    slain.save ()
  end, -- func

  remove = function (item, num)
    if (slain.current[item] - num) < 0 then
      AnsiNote (ansicolor (), "The list doesn't contain that many of that creature.")
    else
      AnsiNote (ansicolor (), string.format ("Removed %s \"%s\" from the slain tracker.", num, item))
      slain.current[item] = slain.current[item] - num
      if slain.current[item] == 0 then slain.current[item] = nil end
      slain.save ()
    end -- if
    prompt.draw ()
  end, -- func

  delete = function (item)
    if not slain.current[item] then
      AnsiNote (ansicolor (), "The list doesn't contain that many of that creature.")
    else
      AnsiNote (ansicolor (), string.format ("Removed all \"%s\" from the slain tracker.", item))
      slain.current[item] = nil
      slain.save ()
    end -- if
    prompt.draw ()
  end, -- func
 
  reset = function (confirmation)
    if string.lower (confirmation) == "confirm" then      
      AnsiNote (ansicolor (), "Slain tracker reset.")
      DeleteVariable ("slain")
      slain.current = {}
    else
      AnsiNote (ansicolor (), "Are you sure you wish to reset the slain tracker?")
      AnsiNote (ansicolor (), "If so, do SLAIN RESET CONFIRM.")
    end -- if
    prompt.draw ()
  end, -- func

  run = function (param)
    AnsiNote (ansicolor (), "You have slain:\n")
    if param then AnsiNote (ansicolor (8), " matching: \"" .. param .. "\"\n") end
    if next (slain.current) == nil then
      AnsiNote (ansicolor (), " nothing!")
    else
      local last = {}
      for k, v in pairs (slain.current) do
        if not param or string.find (string.lower(k), string.lower(param)) then
          if string.len (v) > 31 or string.len (v) + string.len (k) > 37 then
            AnsiNote (ansicolor (1), " [", ansicolor (7), string.format ("%6s", v),
                      ansicolor (1), "] ", ansicolor (7), k)
          elseif last[1] then
            AnsiNote (ansicolor (1), " [", ansicolor (7), string.format ("%6s", last[2]),
                      ansicolor (1), "] ", ansicolor (7), string.format ("%-31s", last[1]),
                      ansicolor (1), " [", ansicolor (7), string.format ("%6s", v),
                      ansicolor (1), "] ", ansicolor (7), k)
            last = {}
          else
            last = {k, v}
          end -- if
        end -- if
      end -- for
      if last[1] then
        AnsiNote (ansicolor (1), " [", ansicolor (7), string.format ("%6s", last[2]),
                  ansicolor (1), "] ", ansicolor (7), last[1])
      end -- if
    end -- if
    prompt.draw ()
  end, -- func

  help = function ()
    AnsiNote (ansicolor (), "Slain Tracker Commands:")
    AnsiNote (ansicolor (), "-----------------------")
    AnsiNote (ansicolor (), " SLAIN - see what you've slain")
    AnsiNote (ansicolor (), " SLAIN SEARCH <x> - search your slain tracker for something")
    AnsiNote (ansicolor (), " SLAIN ADD <#> <creature> - add something to the slain tracker")
    AnsiNote (ansicolor (), " SLAIN REMOVE <#> <creature> - remove something from the slain tracker")
    AnsiNote (ansicolor (), " SLAIN DELETE <creature> - remove all of something from the slain tracker")
    AnsiNote (ansicolor (), " SLAIN RESET - clear everything from the slain tracker")
    AnsiNote (ansicolor (), "\n Note: add, remove, and delete are case sensitive")
    prompt.draw ()
  end, -- func

  save = function ()
    table.save ("slain", slain.current, "slain.current")
  end, -- func
  }

table.load ("slain")

]]>
</script>

</muclient>
