@program cmd-laston.muf
1 1000 d
i
( cmd-laston.muf by Natasha@HLM
  A laston for Fuzzball 6.

  Forked from cmd-whodoing for Fuzzball 6. Factored table code into $lib/table.

  Copyright 2002 Natasha Snunkmeox. Copyright 2002 Here Lie Monsters.
  "@view $box/mit" for license information.

  Version history
  1.001, 16 March 2003: don't use .rtimestr macro, use rtimestr in
    $lib/timestr. Default USE_ATLASTON to on.
  1.002, 25 Feb 2004: handle discons from crashes more gracefully
)
$author Natasha Snunkmeox <natmeox@neologasm.org>
$version 1.002
$note A Fuzzball 6 laston.

$include $lib/strings
$include $lib/table
$include $lib/timestr
$iflib $lib/alias $include $lib/alias $endif  ( Honor aliases issue417 Natasha@HLM 9 January 2003 )

$def prop_wsobject "_prefs/ws/object"
$def prop_wflist "_prefs/con_announce_list"
$def prop_def "_prefs/laston/def"
$def prop_laston "@/last/on"
$def prop_lastoff "@/last/off"

( Uncomment if a _connect event sets users' @/laston properties. )
$def USE_ATLASTON

var lineformat
var metadata


: rtn-lasttime  ( db strProp -- intTime }  Gets the systime in the given property. If the program is not configured to use @/laston properties, the player's last usetime time of connection} is used instead. )
$ifdef USE_ATLASTON
    getpropstr  ( strTime )
    dup number? if atoi else pop -1 then  ( intTime )
$else
    pop timestamps pop -3 rotate pop pop  ( intTime )
$endif
;

: rtn-lastcon  ( db -- intTime }  Gets the systime when the given dbref most recently connected. )
    prop_laston rtn-lasttime
;
: rtn-lastdiscon  ( db -- intTime }  Gets the systime when the given dbref most recently disconnected. Use this time for when a player was last 'on.' )
    prop_lastoff rtn-lasttime
;


( ** data getters  { db -- str }  )
: get-name name ;
: get-con rtn-lastcon "%R %e %b %Y" swap timefmt ;
: get-discon rtn-lastdiscon "%R %e %b %Y" swap timefmt ;

: get-laston[ db:who -- str ]
    who @ awake? if "\[[1;32mOnline now\[[0m" exit then
    who @ prop_lastoff rtn-lasttime dup -1 = if  ( intTime )
        pop "\[[1;33mNever connected\[[0m" exit  ( str )
    then  ( intLastoff )

    dup who @ prop_laston rtn-lasttime < if
        ( This player was disconnected last before connecting, yet is disconnected! )
        ( Was it a crash? )
        #0 "_sys/startuptime" getpropval  ( intLastoff intStartup )
        over over < if swap then pop  ( intLastoff' )
    then

    systime swap - rtimestr  ( str )
;


( ** list makers  { str -- arr }  )
: rtn-names2list  ( arrNames -- arrDbs )
    0 array_make  ( arrNames arrDbs )
    swap foreach swap pop  ( arrDbs strName )
        .noisy_pmatch dup ok? if  ( arrDbs db )
            swap array_appenditem  ( arrDbs )
        else pop then  ( arrDbs )
    repeat  ( arrDbs )
;
: do-help pop .showhelp "exit" abort ;
: do-all
    dup not if do-help then
$iflib $lib/alias
    me @ swap alias-expand  ( Honor aliases issue417 Natasha@HLM 9 January 2003 )
$else
    .noisy_pmatch
    dup ok? if 1 array_make else pop "exit" abort then
$endif
;
: do-watchfor pop me @ prop_wflist getpropstr " " explode_array rtn-names2list ;
: do-room  ( strY -- arr )
    pop loc @ contents_array  ( arrContents )
    0 array_make swap foreach swap pop  ( arrPlayers db )
        dup player? if swap array_appenditem else pop then
    repeat  ( arrPlayers )
;


$define dict_commands {
    0 'do-all
    "room" 'do-room
    "here" 'do-room
    "wf" 'do-watchfor
    "wat" 'do-watchfor
    "watchfor" 'do-watchfor
    "help" 'do-help
}dict $enddef


: main  ( str -- )
    dup not if pop me @ prop_def getpropstr then  ( str )

    STRparse pop  ( strX strY )
    swap dict_commands over array_getitem  ( strY strX ? )
    dup address? not if  ( strY strX ? )
        pop "I don't know what you mean by '#%s'." fmtstring .tellbad  ( strY )
        pop exit  (  )
    then  ( strY strX adr )
    swap pop  ( strY adr )

    "%-16.16[name]s %-18.18[laston]s %-20.20[con]s %-20.20[discon]s" lineformat !
    {
        "name"   'get-name
        "laston" 'get-laston
        "con"    'get-con
        "discon" 'get-discon
    }dict metadata !  ( strY adr )

    2 try
        execute  ( arr )
    catch exit endcatch  ( arr )
    dup array_count not if  ( arr )
        "No one to show." .tellbad  ( arr )
        pop exit  (  )
    then  ( arr )

    lineformat @  ( arr strLinefmt )
    { "name" "\[[1;37mName" "laston" "\[[1;37mLast on" "con" "\[[1;37mLast connected" "discon" "\[[1;37mLast disconnected" }dict  ( arr strLinefmt dictHeaders )
    metadata @  ( arr strLinefmt dictHeaders dictProcessors )
    4 rotate  ( strLinefmt dictHeaders dictProcessors arr )

    table-table  (  )
;
.
c
q

lsedit cmd-laston=_help
.del 1 $
laston <names>
laston #wf
laston #here

List the last time the people with the given names were online. If #wf is given, lists everyone in your watchfor, which can be quite a few people. If #here is given, lists everyone in the room with you at the time.
.format 5=78
.end
