Aardwolf AFK detection plugin

Posted by Nick Gammon on Sat 12 Jul 2008 11:34 PM — 3 posts, 18,288 views.

Australia Forum Administrator #0
The plugin below detects if you are AFK on the client side. That is, if you have wandered away from the keyboard.

The reason for this is that some of the other plugins I have developed - in particular the spellups one - keep casting spells quite frequently, which can make it look to the MUD that you are there in person, and thus the MUD never times you out. However this is confusing for your friends who wonder why you are not responding, as you appear to be standing there, casting spells, not AFK, but not replying to them.

What it does is detect when you last typed a command, and reset an internal timer. If the timer is not reset, it puts you into "client-side AFK" mode. Other plugins can detect that, and stop issuing commands (like spellups) until you return to the keyboard.


To use it, download the code below and save as AFK_Detector.xml in the Aardwolf subdirectory below your Plugins directory. If you haven't used any Aardwolf plugins before you may need to make the Aardwolf directory.

Alternatively, RH click the link below and choose "Save Link As" (or "Save Linked File As") to save the linked file.

http://www.gammon.com.au/mushclient/plugins/Aardwolf/AFK_Detector.xml

In a standard MUSHclient installation these two files should to into this directory:


C:\Program Files\MUSHclient\worlds\plugins\Aardwolf\




<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient [
  <!ENTITY timer_mins "5" > 
  <!ENTITY timer_secs "0" > 
]>
<!-- Saved on Saturday, June 30, 2007, 10:48  -->
<!-- MuClient version 4.13 -->

<muclient>
<plugin
   name="xAFK_Detector"
   author="Nick Gammon"
   id="0e191dc7829ff2ac2433c2d8"
   language="Lua"
   purpose="Detects if you are AFK"
   date_written="2008-06-24"
   requires="4.28"
   version="1.0"
   >
<description trim="y">

[FOR PLUGIN AUTHORS ONLY]

Detects if you are AFK, for the benefit of other plugins.

Example:

function OnPluginBroadcast (msg, id, name, text)
  if msg == 1 and id == "0e191dc7829ff2ac2433c2d8" then
    AFK = text == "y"
   end -- AFK status changed
end

</description>

</plugin>


<triggers>

  <trigger
   enabled="y"
   match="You are now in AFK mode."
   script="gone_afk"
   sequence="100"
  >
  </trigger>
  
 <trigger
   enabled="y"
   match="You cannot use that command while AFK."
   script="gone_afk"
   sequence="100"
  >
  </trigger>
  
  <trigger
   enabled="y"
   match="^AFK mode removed\."
   script="not_afk"
   regexp="y"
   sequence="100"
  >
  </trigger>
  
</triggers>

<timers>
  <timer name="afk_timer" 
         second="&timer_secs;"  
         minute="&timer_mins;"  
         send_to="12"    
         enabled="y"
>
  <send>
    ColourNote ("salmon", "", "You are now AFK (client-side).")
    SetStatus ("AFK")
    check (EnableTimer ("afk_timer", 0))
    BroadcastPlugin (1, "y")
  </send>
  </timer>
</timers>

<!--  Script  -->


<script>
<![CDATA[

function gone_afk (name, line, wildcards)
  SetStatus ("AFK")
  check (EnableTimer ("afk_timer", 0))
  BroadcastPlugin (1, "y")
end -- gone_afk

function not_afk (name, line, wildcards)
  FixTimer ()
end -- not_afk

-- when they type something, reset AFK status

function OnPluginCommand (sText)
  FixTimer ()
  return true  -- process the command
end

-- when you connect to the MUD, presumably you are not AFK

function OnPluginConnect ()
  FixTimer ()
end

-- shared routine to handle turning AFK off

function FixTimer ()
  if GetTimerOption ("afk_timer", "enabled") == 0 then
    ColourNote ("salmon", "", "You are no longer AFK  (client-side).")
    SetStatus ("Ready")
    BroadcastPlugin (1, "n")
  end

-- turn timer back on
  check (EnableTimer ("afk_timer", 1))

-- make sure the full time interval elapses
  check (ResetTimer ("afk_timer"))

end

assert (GetOption ("enable_timers") == 1, "Timers not enabled")

]]>
</script>
</muclient>


#1
I have a problem when i add this script:

Run-time error
Plugin: xAFK_Detector (called from world: aardwolf)
Function/Sub: OnPluginCommand called by Plugin xAFK_Detector
Reason: Executing plugin xAFK_Detector sub OnPluginCommand
[string "Plugin"]:34: attempt to call global 'check' (a nil value)
stack traceback:
[string "Plugin"]:34: in function 'FixTimer'
[string "Plugin"]:14: in function <[string "Plugin"]:13>
Error context in script:
30 : BroadcastPlugin (1, "n")
31 : end
32 :
33 : -- turn timer back on
34*: check (EnableTimer ("afk_timer", 1))
35 :
36 : -- make sure the full time interval elapses
37 : check (ResetTimer ("afk_timer"))
38 :
Australia Forum Administrator #2
Recent versions of MUSHclient have the "check" function built in.

I suggest you download version 4.33, see this page:

http://mushclient.com/forum/?id=8765

If this post is older than a week or so, check for an even later version by going to:

http://mushclient.com/forum/?bbtopic_id=1

Most of the Aardwolf plugins require the new functionality in the latest version.

This particular one can probably be corrected by adding one line:


<script>
<![CDATA[

require "check"  --> add this line



However upgrading MUSHclient is the safer option, if you are planning to use the other plugins.