alias _msg {
  if ($u.out($1)) $iif($1-,$+(.,$u.out($1)),return) $$1-
  else .msg $$1-
}
alias _amsg scid -at1 .amsg $1-
on *:text:*:*:{
  if (!$chan) close -m $nick
  var %t = $left($1,1),%1 = $right($1,-1)
  if (%t !isalnum) {
    if ($hget($nick)) {
      if (%1 == drop) del_account $nick
      elseif (%1 == logout) logout $nick
      elseif (%1 == set) {
        if ($len($2) > 3) && ($2 isin password) {
          if ($3) {
            hadd $nick password $3
            _msg $nick Set your password to $qt($3)
          }       
          else _msg $nick Syntax: !set password <new password>
        }
        elseif ($istok($u.guns($nick),$2,44)) { 
          hadd $nick cgun $2
          _msg $nick You're now wielding your $2 $+ !
        }
      }
      elseif (%1 == guns) _msg $nick Guns: $iif($u.guns($nick),$v1,N/A)
      elseif (%1 == stats) _stats $nick $2-
    }
    else {
      if (%1 == register) add_account $nick $2
      elseif (%1 == login) login $nick $2
    }
  }
}
alias _stats {
  var %2 = $iif($2 == -a,$3,$2)
  var %n = $iif($hget(%2),%2,$1)
  if ($hget(%n)) {
    _msg $1 Name: $u.name(%n) Level: $u.level(%n) HP: $+($u.hp(%n),/,$u.fhp(%n)) Strength: $u.str(%n) Toughness: $u.tuff(%n) EXP: $+($u.exp(%n),/,$u.nexp(%n)) Money: $+($chr(36),$bytes($u.mon(%n),bd))
  }
  else _msg $1 You're not registered/logged in
}

alias _nick return $+($1,( $+ $u.name($1) $+ ))

; User shit 
alias u.name return $hget($1,name)
alias u.out return $hget($1,output)
alias u.pass return $hget($1,password)
alias u.str return $hget($1,strength)
alias u.tuff return $hget($1,toughness)
alias u.hp return $hget($1,hp)
alias u.fhp return $hget($1,fhp)
alias u.mon return $hget($1,money)
alias u.com return $hget($1,commands)
alias u.exp return $hget($1,exp)
alias u.nexp return $hget($1,nexp)
alias u.guns return $hget($1,guns)
alias u.mel return $hget($1,melee)
alias u.level return $hget($1,level)
alias u.cgun return $hget($1,cgun)
alias u.kills return $hget($1,kills)
alias u.deaths return $hget($1,Deaths)
; End

alias add_account {
  if (!$isfile($+($1,.hsh))) && (!$hget($1)) {
    var %a = hadd -m $1,%qp = $quick_pass,%s = $r(100,150)
    %a name $iif($2,$2,$1)
    %a password %qp
    %a output notice
    %a accuracy $r(1,5)
    %a strength $r(2,8)
    %a toughness $r(2,8)
    %a hp %s
    %a fhp %s
    %a nexp 100
    %a exp 0
    %a money $r(50,200)
    %a level 1
    %a kills 0
    %a deaths 0
    %a commands punch,kick
    hsave $1 $+($1,.hsh)
    _msg $1 You're now registered with the password %qp $+ .
    .signal -n 100 $1
  }
  else _msg $1 You're already registered
}
alias del_account {
  if ($hget($1)) {
    if ($isfile($+($1,.hsh))) .remove $+($1,.hsh)
    _msg $1 Your account has been deleted. 
    hfree $1
    .signal -n 101 $1
  }
}
alias login {
  if (!$hget($1)) {
    if ($isfile($+($1,.hsh))) {
      hmake $1 
      hload $1 $+($1,.hsh)
      if ($2 != $u.pass($1)) {
        hfree $1
        _msg $1 $iif(!$2,Syntax: !Login <password>,Invalid password)
      }
      else { _msg $1 You're now logged in | .signal -n 102 $1 }
    }
    else _msg $1 You're not registered
  }
  else _msg $1 You're already logged in
}
alias logout {
  if ($hget($1)) {
    hsave $1 $+($1,.hsh)
    _msg $1 You're now logged out
    hfree $1
    .signal -n 103 $1
  }
  else _msg $1 You're not logged in
}
alias quick_pass return $+($r(a,z),$r(a,z),$r(a,z),$r(a,Z),$r(1000,9999))

; Signal System
on *:SIGNAL:*:{
  if ($signal == 100) {
    ; created acct
  }
  elseif ($signal == 101) {
    ; deleted acct
  }
  elseif ($signal == 102) {
    ; logged in
  }
  elseif ($signal == 103) {
    ; logged out
  }
}
; End
