Lag Macro

From Gwen Morse's Wiki
Jump to: navigation, search
; This triggers show the local lag for your MUD
;
; Maybe the triggers have to be modified to your local MUD. My MUD sends me
; lines like:
; You tell <other player>: blah blah
; <other player> tells you: blah blah
;
; Just copy these lines in your ~/.tfrc file, modify the player
; variable to your characters name and you should be off
;
; For problems contact me at <brueckner at respublica dot de>
;
; Syntax:
;    /lag on     - Turns the triggers on
;    /lag off    - Turns them off
;    /lag show   - Displayes the last $[lagccm] collected times
;    /lag time x - Sets the delay between sending probes to x seconds

/set pedantic=on

/require alias.tf
/require tick.tf

/set player=charactername

; -----------------------------------------------------------------------------
; Tell triggers
; -----------------------------------------------------------------------------
/alias re tell $[lasttell] %{*}

/def -p1 -mregexp -t' tells you: ' tell01 = \
	/set lasttell2=$[tolower({PL})]%; \
	/if (strcmp(lasttell2, player)) \
		/beep%; \
		/set lasttell=$[lasttell2]%; \
	/endif


; -----------------------------------------------------------------------------
; Ticker settings
; -----------------------------------------------------------------------------
/tickoff
/def -i tick_warn =
/def -i tick_action = \
	/if (lagreport) \
		/set lagtime=$[time()]%; \
		/send tell %{player} -- %{lagsnr} %{lagtime} --%; \
		/set lagsnr=$[lagsnr + 1]%; \
	/endif


; -----------------------------------------------------------------------------
; Lag trigger
; -----------------------------------------------------------------------------

/set lagreport=0
/set lagsnr=1
/set lagwait=1
/set lagmax=0
/set lagsum=0
/set lagcc=1
/set lagccm=18
/def -i lag = \
	/if ({#} == 1) \
		/if ({1} =~ "on") \
			/ticksize 10%; \
			/tickon%; \
			/set lagreport=1%; \
			/set lagsnr=1%; \
			/set lagwait=1%; \
			/set lagmax=0%; \
			/set lagsum=0%; \
			/set lagcc=1%; \
			/echo % Lag is on.%; \
		/elseif ({1} =~ "off") \
			/ticksize 60%; \
			/tickoff%; \
			/set lagreport=0%; \
			/echo % Lag is off.%; \
		/elseif ({1} =~ "show") \
			/set lagtmp1=$[lagcc - 1]%; \
			/if (lagtmp1 < 1) \
				/set lagtmp1=$[lagccm]%; \
			/endif%; \
			/set lagtmp2=1%; \
			/lag%; \
			/eval /echo % $[strrep("-", 35)]%; \
			/while (lagtmp2 <= lagccm) \
				/set lagtmp3=laghistory_$[lagtmp1]%; \
				/eval /set lagtmp3=\$[$[lagtmp3]]%; \
				/if (strlen(lagtmp3) > 0) \
					/echo % %{lagtmp3}%; \
				/endif%; \
				/set lagtmp1=$[lagtmp1 - 1]%; \
				/if (lagtmp1 < 0) \
					/set lagtmp1=$[lagccm]%; \
				/endif%; \
				/set lagtmp2=$[lagtmp2 + 1]%; \
			/done%; \
			/eval /echo % $[strrep("-", 35)]%; \
		/elseif ({1} =~ "time") \
			/ticksize {2}%; \
		/else \
			/echo % Syntax: /lag [on|off|show|time]%; \
		/endif%; \
	/else \
		/echo % Last probe sent:     %{lagsnr}%; \
		/eval /echo % Last probe received: $[lagwait - 1]%; \
		/echo % Maximum lag:         %{lagmax}s%; \
		/eval /echo % Average lag:         $[lagsum / lagwait]s%; \
	/endif

/def -p999 -mregexp -ag -t'^You tell .*: -- [0-9]+ [0-9]+ --$' lag01

/def -p999 -mregexp -ag -t' tells you: -- ([0-9]+) ([0-9]+) --$' lag02 = \
	/set lagt1=%{P1}%; \
	/set lagt2=%{P2}%; \
	/set lagdiff=$[time() - lagt2]%; \
	/set lagsum=$[lagsum + lagdiff]%; \
	/set lagwait=$[lagt1]%; \
	/if (lagdiff > lagmax) \
		/set lagmax=$[lagdiff]%; \
	/endif%; \
	/if (lagdiff < 30) \
		/set laghistory_$[lagcc]=$[strcat(strrep("#", lagdiff), strrep(" ", 30 - lagdiff + 1), lagdiff, "s")]%; \
	/else \
		/set laghistory_$[lagcc]=$[strcat(strrep("#", 30), " ", lagdiff, "s")]%; \
	/endif%; \
	/set lagcc=$[lagcc + 1]%; \
	/if (lagcc > lagccm) \
		/set lagcc=1%; \
	/endif%; \
	/if (lagdiff > 3) \
		/echo % Lag: #%{[lagt1} %{lagdiff}s, max %{lagmax}s.%; \
	/endif