All pastes #736968 Raw Edit

Hihg/low

public text v1 · immutable
·published 2007-10-14 23:45 UTC
rendered paste body
; Premium High/Low Game, Orgins: N/A
on *:TEXT:!guess &:#: { hl_guess $2 }
on *:TEXT:!join:#:{ hl_join }
on *:TEXT:!hl *:#:{ msg $chan $hl_com }
on *:TEXT:!start *:#:{ hl_init $2- }
on *:TEXT:!stop:#:{ hl_stop $network $chan }
on *:INPUT:#:{
  if ($1 == !guess) && ($2) { msg $chan $1- | hl_guess $2 | halt }
  elseif ($1 == !join) { msg $chan $1- | hl_join | halt }
  elseif ($1 == !hl) { msg $chan $1 | msg $chan Use $hl_com | halt }
  elseif ($1 == !start) { msg $chan $1- | hl_init $2- | halt }
  elseif ($1 == !stop) { msg $chan $1- | hl_stop $network $chan | halt }
}
alias hl_init {
  var %range = $1, %players = $2, %secs = $3, %timeout = $4
  if (%range == $null) { var %range = $+(1,-,$calc(100 * $rand(1,100))) }
  else { if ($hl_checkrange(%range) == FAIL) { goto refuse } | var %range = $v1 }
  if (%players !isnum 2-) || ($int(%players) != %players) { var %players = 1, %secs = 0, %timeout = 0 }
  else {
    if (%timeout !isnum 5-60) || ($int(%timeout) != %timeout) { var %timeout = 20 }
    if (%secs !isnum 1-60) || ($int(%secs) != %secs) { var %secs = $calc(%players * 5) }
  }
  tokenize 45 %range
  if ($calc($2 - $1) < 2) {
    describe $chan turns out a leg of $nick($chan,$rand(1,$nick($chan,0))) and slaps $nick $+ 's skull inwards with it. Range %range - is -that- guessing??
    return
  }
  goto ok
  :refuse | msg $chan Please use correct format, $nick $+ : $hl_com | return
  :ok
  var %h = $+(hl_,$network,$chr(44),$chan) | if ($hget(%h)) { hfree %h | .timer $+ $+(hl_*,$chr(44),$network,$chr(44),$chan) OFF }
  hmake %h 30 | hadd %h maxplayers %players | hadd %h range %range | tokenize 45 %range | hadd %h num $rand($1,$2) | hadd %h timeout %timeout
  hadd %h player1, $+ $hl_site $nick
  if (%players > 1) { 
    var %tn = $+(hl_join,$chr(44),$network,$chr(44),$chan) | .timer $+ %tn 1 %secs hl_jointimeout $network $chan
    msg $chan $nick started higher/lower game %range $+ . Max %players players. %secs secs time to sign in with !join. | return
  }
  msg $chan Game started. | hl_next $network $chan
}
alias hl_jointimeout { var %h = $+(hl_,$1,$chr(44),$2) | if ($me ison $2) { hl_start $1 $2 Time to join has passed. Players: } | else { hadd %h start 1 } }
alias hl_join {
  var %h = $+(hl_,$network,$chr(44),$chan) | if (!$hget(%h)) { msg $chan No game started yet! Use $hl_com | return }
  var %site = $hl_site
  if ($hget(%h,next)) {
    var %com = msg $chan Guessing already started, $nick $+ .
    if ($hfind(%h,$+(player*,$chr(44),%site),1,w)) {
      if ($hfind(%h,$+(player,$hget(%h,next),$chr(44),%site),1,w)) { %com It's your turn, please use !guess <number>. }
      elseif ($hfind(%h,$+(player,$hget(%h,next),$chr(44),*),1,w)) { %com It's now $hget(%h,$v1) $+ 's turn. }
      return
    } 
    %com You're too late to join. | return
  }
  if ($hfind(%h,$+(player*,$chr(44),%site),1,w)) { msg $chan You're already in the playerlist, $nick $+ ! | return }
  var %i = 1, %max = 0 | while ($hfind(%h,$+(player*,$chr(44),*),%i,w)) { tokenize 44 $v1 | if ($remove($1,player) > %max) { var %max = $v1 } | inc %i }
  inc %max | hadd %h $+(player,%max,$chr(44),%site) $nick | inc %total
  if (%total < $hget(%h,maxplayers)) { msg $chan Thanks $nick $+ ! %total player(s) signed up so far. | return }
  .timer $+ $+(hl_join,$chr(44),$network,$chr(44),$chan) OFF | msg $chan Red lantern is $nick $+ ! | hl_start $network $chan Players:
}
alias hl_start {
  var %net = $1, %chan = $2, %h = $+(hl_,$1,$chr(44),$2), %i = 1, %chunk = $3-, %i = 1
  while ($hfind(%h,$+(player*,$chr(44),*),%i,w)) {
    var %new = %chunk $hget(%h,$v1) | if ($len(%new) > 400) { msg %chan %chunk | var %chunk } | else { var %chunk = %new }
    inc %i
  }
  if (%chunk != $null) { msg %chan %chunk }
  if ($hfind(%h,$+(player*,$chr(44),*),0,w) > 1) { msg %chan Time available for a guess: $hget(%h,timeout) secs. }
  hl_next %net %chan
}
alias hl_stop { var %h = $+(hl_,$1,$chr(44),$2) | if ($hget(%h)) { hfree %h | .timer $+ $+(hl_*,$chr(44),$1,$chr(44),$2) OFF } }
alias hl_next {
  var %net = $1, %chan = $2, %h = $+(hl_,$1,$chr(44),$2), %i = 1, %w = @hl_temp, %l | window -sh %w
  while ($hfind(%h,$+(player*,$chr(44),*),%i,w)) {
    var %hi = $v1, %nick = $hget(%h,%hi) | if (%nick !ison %chan) { inc %i | continue }
    tokenize 44 %hi | var %nr = $base($remove($1,player),10,10,10) | aline %w %nr %hi | inc %i
  }
  var %l
  if ($hget(%h,next) != $null) {
    var %total = $line(%w,0), %i = 1, %l | while (%i <= %total) { tokenize 32 $line(%w,%i) | if ($1 > $hget(%h,next)) { var %l = %i | break } | inc %i }
  }
  if (!%l) { var %l = 1 } | tokenize 32 $line(%w,%l) | window -c %w | var %hi = $2
  tokenize 44 %hi | hadd %h next $remove($1,player) | tokenize 45 $hget(%h,range)
  if ($1 != $2) { msg %chan $hget(%h,%hi) $+ , number within $hget(%h,range) $+ . }
  else { msg %chan $hget(%h,%hi) $+ , type !guess $1 $+ . }
  if ($hfind(%h,$+(player*,$chr(44),*),0,w) > 1) { .timer $+ $+(hl_wait,$chr(44),%net,$chr(44),%chan) 1 $hget(%h,timeout) hl_guesstimeout %net %chan }
}
alias hl_guesstimeout {
  var %h = $+(hl_,$1,$chr(44),$2) | if ($me !ison $2) { return }
  var %hi = $hfind(%h,$+(player,$hget(%h,next),$chr(44),*),1,w) | msg $2 Time is up, $hget(%h,%hi) $+ . 
  hinc %h timeouts | if ($hget(%h,timeouts) > 20) { msg $2 Too lazy to stop me eh? I'll do it myself! | hl_stop $1 $2 } | else { hl_next $1 $2 }
}
alias hl_guess {
  var %h = $+(hl_,$network,$chr(44),$chan) | if (!$hget(%h)) { msg $chan No game started yet! Use $hl_com | return }
  var %site = $hl_site, %tn = $+(hl_join,$chr(44),$network,$chr(44),$chan)
  if ($timer(%tn)) {
    if (!$hfind(%h,$+(player*,$chr(44),%site),1,w)) { msg $chan Hey you gotta 1) !join 2) !guess. You have $timer(%tn).secs secs left for step 1) }
    else { msg $chan Please wait, $nick $+ , $timer(%tn).secs secs left before starting. Grab a gun to convince people! }
    return
  }
  var %higuess = $hfind(%h,$+(player*,$chr(44),%site),1,w), %hiturn = $hfind(%h,$+(player,$hget(%h,next),$chr(44),*),1,w)
  if (%higuess != %hiturn) { msg $chan Hey $nick $+ , it's $hget(%h,%hiturn) $+ 's turn not yours! | return }
  var %guess = $1 | hinc %h guesscount | hdec %h timeouts | tokenize 45 $hget(%h,range)
  if (%guess !isnum $hget(%h,range)) { 
    if ($1 != $2) { msg $chan Hey $nick $+ , the range is now $hget(%h,range) $+ . You wasted this turn! } | else { describe $chan sighs. }
    goto next
  }
  if (%guess > $hget(%h,num)) { msg $chan LOWER, $nick $+ . | dec %guess | hadd %h range $+($1,-,%guess)  | goto next }
  if (%guess < $hget(%h,num)) { msg $chan HIGHER, $nick $+ . | inc %guess | hadd %h range $+(%guess,-,$2) | goto next }
  msg $chan 04CORRECT $nick $+ ! The number was %guess $+ . Needed $hget(%h,guesscount) guesses. $hl_com | hl_stop $network $chan | return
  :next | hl_next $network $chan
}

alias hl_checkrange {
  if ($left($1,1) !isnum) || ($numtok($1,45) > 2) { return FAIL }
  var %n1 = $gettok($1,1,45), %n2 = $gettok($1,2,45)
  if (%n1 < 0) { return FAIL }
  if (%n2 == $null) { var %n2 = %n1 }
  if (%n1 !isnum) || (%n2 !isnum) || ($int(%n1) != %n1) || ($int(%n2) != %n2) { return FAIL }
  if (%n2 < %n1) { var %t = %n1, %n1 = %n2, %n2 = %t }
  return $+(%n1,-,%n2)
}
alias hl_site { if ($nick != $me) { return $site } | else { return $gettok($address($me,2),2,64) } }
alias hl_com { return !start [min-max] [maxplayers] [secs to join] [secs to guess] }
alias hl_netcid {
  var %total = $scon(0), %i = 1
  while (%i <= %total) { if ($scon(%i).network) == $1) && ($scon(%i).status == connected) { return $scon(%i) } | inc %i }
  return NOTCON 
}
on *:JOIN:#: { 
  var %h = $+(hl_,$network,$chr(44),$chan) | if (!$hget(%h)) { return }
  if ($nick == $me) { if (!$chan($chan).inwho) { who $chan | hadd %h inwho 1 } }
  if ($hfind(%h,$+(player*,$chr(44),$site),1,w) == $null) { return }
  var %hi = $v1 | if ($hget(%h,%hi) != $nick) { hadd %h %hi $nick }
  var %hi = $hfind(%h,$+(player,$hget(%h,next),$chr(44),*),1,w)), %nick = $hget(%h,%hi)
  if (%nick == $nick) { msg $chan Welcome back $nick $+ , it's your turn, number within $hget(%h,range) $+ . }
  else { msg $chan Welcome back $nick $+ , it's now %nick $+ 's turn. }
}
RAW 352:*:{ var %h = $+(hl_,$network,$chr(44),$2) | if ($hget(%h,inwho)) { haltdef } }
RAW 315:*:{ 
  var %h = $+(hl_,$network,$chr(44),$2) | if (!$hget(%h,inwho)) || ($me !ison $2) { return } 
  haltdef | hdel %h inwho | if ($hget(%h,start)) { hl_start $network $2 | hdel %h %hi start | return }
  var %hi = $hfind(%h,$+(player,$hget(%h,next),$chr(44),*),1,w)) | if (%hi == $null) { return }
  if ($hget(%h,%hi) != $me) {

    var %tn = $+(hl_wait,$chr(44),$network,$chr(44),$2) | if (!$timer(%tn)) { hl_next $network $2 }
  }
}
on *:NICK: {
  var %i = 1
  while ($comchan($newnick,%i)) {
    var %chan = $v1, %h = $+(hl_,$network,$chr(44),%chan) | if (!$hget(%h)) { inc %i | continue }
    if ($hfind(%h,$+(player*,$chr(44),$site),1,w) == $null) { inc %i | continue }
    var %hi = $v1 | if ($hget(%h,%hi) != $newnick) { hadd %h %hi $newnick }
    inc %i
  }
}
on *:QUIT: {
  var %i = 1
  while ($comchan($nick,%i)) {
    var %chan = $v1, %h = $+(hl_,$network,$chr(44),%chan) | if (!$hget(%h)) { inc %i | continue }
    if ($hfind(%h,$+(player,$hget(%h,next),$chr(44),$site),1,w) != $null) { hl_next $network %chan }
    inc %i
  }
}