;----------------------------------------------------
;Addon: Lag Graph (LagGraph.mrc)
;Author: ian
;----------------------------------------------------
on *:LOAD: { 
  echo -a Loaded Lag Graph addon by ian 
  .timerLAG.CHECK 0 25 if ( $!server ) { .raw PING $!ticks }
  lag.win
}
on *:CONNECT: { if (!$timer(LAG.CHECK)) { .timerLAG.CHECK 0 25 if ( $!server ) { .raw PING $!ticks } } | lag.win } 
on *:DISCONNECT: { if ($timer(LAG.CHECK)) { .timerLAG.CHECK off } } 
on ^*:PONG: {  lag.update.disp $round($calc(($ticks - $2) /1000),3) |  haltdef }
alias prcnt { return  $int($calc($1 * $calc(100 / $2))) }
alias lag.win { 
  if (!$timer(LAG.CHECK)) { .timerLAG.CHECK 0 25 if ( $!server ) { .raw PING $!ticks } }
  window -fpk0 +fts @LAG -1 -1 110 102
  drawrect -fn @LAG 1 1 0 0  200 200
  var %ctr 0 | while (%ctr <= 10) { drawline -nr @LAG $rgb(62,62,62) 0 1 $calc(%ctr *10) 110 $calc(%ctr *10) | inc %ctr }
  drawtext -n @LAG 0 Tahoma 13 1 0 5
  drawtext -n @LAG 0 Tahoma 13 1 20 4
  drawtext -n @LAG 0 Tahoma 13 1 40 3
  drawtext -n @LAG 0 Tahoma 13 1 60 2
  drawtext -n @LAG 0 Tahoma 13 1 80 1
  drawscroll @LAG
}
alias lag.update.disp {
  if ($window(@LAG)) { 
    drawscroll - @LAG 4 0 11 0 100 101
    drawrect -fn @LAG 1 1 11 0 3 105
    drawcopy -t @LAG 1 8 1 1 100 @lag 11 1 3 100
    drawrect -rf @LAG $rgb($calc(50 + ($prcnt($1,5) * 2)),$calc(50 + ($prcnt($1,5) * 2)),$calc(50 + ($prcnt($1,5) * 2))) 1 11 $calc(100 - $prcnt($1,5)) 3 $prcnt($1,5)
    drawscroll @LAG
    titlebar @LAG - $1 $+ s
  }
}
menu menubar {
  -
  =[Lag Graph]=: { lag.win } 
}
