Elf programming example #2

monitoring sleep

Task: recognise when we start and stop sleeping. Recognise when the tick elapses, automatically waking and standing up. If no key is pressed while sleeping, then an enter is sent to th mud every 5 seconds, to receive an updated prompt (so that tick recognition can take place).
View the source code now.

To achieve this task we need several things:

Let's define a numeric variable named @sleeping. When we sleep its value will be 1, otherwise it will be 0.

Let's define a numeric variable named @LastToTick. It will contain the number of seconds to the tick. When its value will raise, it will mean that a tick has been recognised and that we have to wake up.

Let's define a numeric variable named @LastIdle. It will contain the number of seconds since last keypress. It is used to avoid sending a lot of ENTERs.

The trigger to recognise when we fall asleep is quite easy.
The trigger will be
*you fall asleep*

The commands related to this trigger are the following:
@sleeping=1
@LastToTick=@ToTick+1

They simply signal that we started sleeping and remember when we started sleeping.

The trigger that actually monitorise our sleep has to work continuously.
Therefore, the trigger will be
*

We also need to set the flag enable re-checking on the same line to let the trigger be checked even if nothing arrives from the mud.

The commands related to this trigger are the following:
@NLocal num
@num=@ToTick
@Skip (@sleeping=0)*7
@Skip (@LastToTick<@num)*2
<SendEnters
@Skip 4
wake
stand
@Beep
@sleeping=0
@LastToTick=@num

In a few words: the estimated number of seconds to the tick is stored in the variable @num to avoid it from changing during subsequent tests.
If we are not sleeping, nothing else is done. In fact, @sleeping=0 evaluates to 1, thus executing @Skip 7. If the number of seconds to the tick raised compared to the last time we examined them, it means that a tick was rcognised. In such case, we wake, stand up and signal that we are sleeping no longer.
On the other end, if we are still sleeping, we call a user defined alias named SendEnters that will simulate the ENTER key every 5 seconds that we don't press a key on the keyboard.

The trigger to recognise if we manually wake up is quite easy too.
The trigger will be
*you stand*

The commands related to this trigger are the following:
@sleeping=0

It simply signals that we are no longer sleeping.

Now we must write the alias that actually sends ENTERs every 5 seconds if no key is pressed.

The alias will be activated by the pattern
SendEnters

the commands related to this alias are the following:
@Skip (@OnLine=0)*99
@NLocal i
@i=@Idle
@Skip (@i=@LastIdle)*99
@LastIdle=@i
@If (@LastIdle#5)=4
@Send "#13"

The first line avoids sending ENTERs if we are not on-line.
The following lines send an ENTER (ANSI code 13) to the mud every 5 seconds while no key is pressed.
Please note the use of @LastIdle. If it has the same value it had the last time the alias was executed, then nothing else is done. This is necessary because elf is fast enough to force the execution of the trigger calling this alias several times in a second.

Here is the necessary programming for this task:
 
variable sleeping
type numeric
 
variable LastToTick
type numeric
 
variable LastIdle
type numeric
 
trigger Felt asleep
trigger *you fall asleep*
flags none
commands @sleeping=1
@LastToTick=@ToTick+1
 
trigger Monitorise sleep
trigger *
flags enable re-checking on the same line
commands @NLocal num
@num=@ToTick
@Skip (@sleeping=0)*7
@Skip (@LastToTick<@num)*2
<SendEnters
@Skip 4
wake
stand
@Beep
@sleeping=0
@LastToTick=@num
 
trigger Awaken!
trigger *you stand*
flags none
commands @sleeping=0
 
alias Send enters
pattern SendEnters
flags none
commands @Skip (@OnLine=0)*99
@NLocal i
@i=@Idle
@Skip (@i=@LastIdle)*99
@LastIdle=@i
@If (@LastIdle#5)=4
@Send "#13"
 

These pages were developed with Netscape Composer 4.03 by Alfredo Milani-Comparetti  
Last modified on 8 nov 1997