Looking for a way to spot deprecated triggers

Posted by Kevnuke on Sun 21 Apr 2019 12:04 AM — 7 posts, 24,793 views.

USA #0
Since the last time I played Achaea heavily, a lot of things changed in the game. Some messages I have triggers for are simply no longer are used by the MUD. I know MUSHclient counts how many aliases, timers and triggers were fired during a session, but is there a way to do that on an individual trigger level?

What I'd like to do is have a running total of how many times all of my triggers have fired across sessions, beginning at some point in time. Ideally a button to reset the total. Maybe a column could be added to the aliases, timers, triggers etc that shows how many times they've been fired so I can spot which ones are useless now. I can't imagine adding a way to count that on all 1200+ triggers I have. There are a few obvious ones like the ones telling you when you can eat moss/potash and some other curatives again, but that's really unreliable for the less used ones.
Australia Forum Administrator #1
Template:function=GetTriggerInfo
GetTriggerInfo

The documentation for the GetTriggerInfo script function is available online. It is also in the MUSHclient help file.



That tells you how many times it matched (selector 21).

Now you can iterate over all triggers with this:

Template:function=GetTriggerList
GetTriggerList

The documentation for the GetTriggerList script function is available online. It is also in the MUSHclient help file.



You could put the match count into an array (keyed by trigger name) and then serialize that to the world variables so it is saved from one session to the next.
USA #2
I don't have names for most of my triggers though. The unnamed ones are just always on. Is there some way to make that info show in a column in Config->Triggers as an option in settings? then it could be sorted by value ascending or descending. Or just leave which selector gets added to the user. Make it a text field where the number (21 in this case) can be typed in.
Australia Forum Administrator #3
This plugin should tell you which triggers are unused, over time.


Template:saveplugin=Show_Unused_Triggers
To save and install the Show_Unused_Triggers plugin do this:
  1. Copy the code below (in the code box) to the Clipboard
  2. Open a text editor (such as Notepad) and paste the plugin code into it
  3. Save to disk on your PC, preferably in your plugins directory, as Show_Unused_Triggers.xml
    • The "plugins" directory is usually under the "worlds" directory inside where you installed MUSHclient.
  4. Go to the MUSHclient File menu -> Plugins
  5. Click "Add"
  6. Choose the file Show_Unused_Triggers.xml (which you just saved in step 3) as a plugin
  7. Click "Close"
  8. Save your world file, so that the plugin loads next time you open it.



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Show_Unused_Triggers"
   author="Nick Gammon"
   id="dd839eeea8735913421b7194"
   language="Lua"
   purpose="Shows unused triggers"
   save_state="y"
   date_written="2019-04-22 08:02:08"
   requires="5.00"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Shows a list of unused triggers over multiple sessions.

Usage:

show unused triggers   --> Shows unused triggers
clear unused triggers  --> Clears counts from previous client sessions
  
]]>
</description>

</plugin>


<!--  Aliases  -->

<aliases>
  <alias
   match="show unused triggers"
   enabled="y"
   sequence="100"
   script="ShowUnusedTriggers"
  >
  </alias>
  
  <alias
   match="clear unused triggers"
   enabled="y"
   sequence="100"
   script="ClearUnusedTriggers"
  >
  </alias>
    
</aliases>

<script>
<![CDATA[

require "serialize"  -- needed to serialize table to string

trigger_counts = trigger_counts or {}  -- ensure table exists, if not loaded from variable

-- on plugin install, convert variable into Lua table
function OnPluginInstall ()
  assert (loadstring (GetVariable ("trigger_counts") or "")) ()
end -- function OnPluginInstall

-- on saving state, convert Lua table back into string variable
function OnPluginSaveState ()
  
  -- add the counts from last session to the ones for this session
  local new_counts = { }
  
  local plugins = GetPluginList() or {}
  table.insert (plugins, "") -- add main world to plugins list
  
  for _, pluginID in pairs (plugins) do 

    local tl = GetPluginTriggerList(pluginID) or {}
    for id, name in ipairs (tl) do 
       local matchText = GetPluginTriggerInfo (pluginID, name, 1)
       local count = GetPluginTriggerInfo (pluginID, name, 21) + (trigger_counts [matchText] or 0)
       new_counts [matchText] = count
    end  -- for
    
  end -- of each plugin

  -- save the updated counts
  SetVariable ("trigger_counts", 
               "trigger_counts = " .. serialize.save_simple (new_counts))
end -- function OnPluginSaveState


function ShowUnusedTriggers (name, line, wildcards)

  local plugins = GetPluginList() or {}
  table.insert (plugins, "") -- add main world to plugins list
  local counter = 0
  
  for _, pluginID in pairs (plugins) do 
    local tl = GetPluginTriggerList (pluginID) or {}
    local pluginName = "main world file"
    if pluginID ~= "" then
       pluginName = GetPluginInfo (pluginID, 1)
    end -- of not main world file
    for id, name in ipairs (tl) do 
       local matchText = GetPluginTriggerInfo (pluginID, name, 1)
       local count = GetPluginTriggerInfo (pluginID, name, 21) + (trigger_counts [matchText] or 0)
       if count == 0 then
         ColourNote ("green", "", string.format ("Trigger '%s' in %s has not been used. Match text: ", name, pluginName))
         ColourNote ("darkgray",  "", "  " .. matchText)
         counter = counter + 1
       end -- if
    end  -- for
    
  end -- of each plugin
  ColourNote ("orange", "", string.format ("%d unused triggers found.", counter))
end -- ShowUnusedTriggers

function ClearUnusedTriggers (name, line, wildcards)
  trigger_counts = { }  -- no previous counts
  ColourNote ("orange", "", "Trigger counts from previous sessions cleared")
end -- ClearUnusedTriggers

]]>
</script>

</muclient>



Type in "show unused triggers" to see a list of triggers with a use count of zero. The plugin saves the counts for each session and reloads them next time (in the plugin save-state file) so that the counter will be cumulative. In other words, if a trigger is used today, then it will also be considered as having been used if you list them tomorrow.

You can type "clear unused triggers" to clear that saved information.

This plugin inspects all plugins, and the main world file triggers. This lets you check everything you have installed.

Example output:


Trigger '*trigger1102' in ATCP_Mapper has not been used. Match text: 
  There's water ahead of you. You'll have to * to make it through.
Trigger '*trigger1104' in ATCP_Mapper has not been used. Match text: 
  You'll have to swim to make it through the water in that direction.
Trigger '*trigger1106' in ATCP_Mapper has not been used. Match text: 
  ^There is a door in the way
Trigger '*trigger1108' in ATCP_Mapper has not been used. Match text: 
  Now now\, don\'t be so hasty\!$
Trigger '*trigger1110' in ATCP_Mapper has not been used. Match text: 
  ^You cannot walk through
Trigger '*trigger1112' in ATCP_Mapper has not been used. Match text: 
  As you stroll in, you feel your feet slipping on something slimy.
Trigger '*trigger1114' in ATCP_Mapper has not been used. Match text: 
  The door is locked.
Trigger '*trigger1116' in ATCP_Mapper has not been used. Match text: 
  You are regaining balance and are unable to move.
Trigger '*trigger1118' in ATCP_Mapper has not been used. Match text: 
  You are surrounded by a pocket of air and so must move normally through water.
Trigger '*trigger1120' in ATCP_Mapper has not been used. Match text: 
  You fumble about drunkenly.
Trigger '*trigger1122' in ATCP_Mapper has not been used. Match text: 
  You are asleep and can do nothing. WAKE will attempt to wake you.
Trigger '*trigger1124' in ATCP_Mapper has not been used. Match text: 
  You must be standing first.
Trigger '*trigger1126' in ATCP_Mapper has not been used. Match text: 
  You need to use a boat, fly, or swim underwater to go there.
Trigger '*trigger1128' in ATCP_Mapper has not been used. Match text: 
  You can't * while sitting.
Trigger '*trigger1130' in ATCP_Mapper has not been used. Match text: 
  ^You dream about 
Trigger '*trigger1132' in ATCP_Mapper has not been used. Match text: 
  There is no exit in that direction.
Trigger '*trigger1134' in ATCP_Mapper has not been used. Match text: 
  Alas, you cannot go that way.
Trigger '*trigger924' in main world file has not been used. Match text: 
  hello
18 unused triggers found.


The "match text" display lets you find a trigger, even if it is unnamed.
USA #4
Wow Nick! Thanks a lot! if I manually delete a trigger that has zero uses, should I "clear unused triggers" so it doesn't keep showing up in the results, or does the plugin detect that it no longer exists?
Australia Forum Administrator #5
It only lists triggers that exist.
USA #6
Thank you again. This'll help a lot