mIRC Homepage
Posted By: kwell Someone can help me optimize this code? - 08/11/09 11:11 AM
<nick-1> !Add alpha
-nick-2- You are in the Alpha group
<nick-1> !groups
<nick-2> Group Alpha 1 users
<nick-2> Group Beta 0 users
<nick-1> !Add beta
-nick-2- You are in the Beta group
<nick-1> !groups
<nick-2> Group Alpha 0 users
<nick-2> Group Beta 1 users

Code:
on $69:text:$(/^[!](groups)$/Si):#: {
  if $regml(1) = groups { msg # Group Alpha $numtok(%alpha,32) users | msg # Group Beta $numtok(%beta,32) users }
}

on *:text:*:#:{
  if $strip($1) = !add && !$3 { 
    if $strip($2) = alpha {
      if $istok(%beta,$nick,32) { set %beta $remtok(%beta,$nick,1,32) }
      if !$istok(%alpha,$nick,32) { 
        set %alpha $addtok(%alpha,$nick,32)
        notice $nick You are in the Alpha group
      }
    }
    elseif $strip($2) = beta { 
      if $istok(%alpha,$nick,32) { set %alpha $remtok(%alpha,$nick,1,32) } 
      if !$istok(%beta,$nick,32) { 
        set %beta $addtok(%beta,$nick,32)
        notice $nick You are in the Beta group
      }
    }
    else { msg # Group nonexistent. Use: !group  }
  } 
}

Thanks!!!
Posted By: RusselB Re: Someone can help me optimize this code? - 08/11/09 09:27 PM
Suggestion:
Code:
on *:text:*:#:{
  tokenize 32 $strip($1-)
  if ($1 == !add) { 
    if $istok(alpha beta,$2,32) {
      var %temp = $iif($2 == alpha,beta,alpha)
      set $+(%,%temp) $remtok($($+(%,%temp),2),$iif($3,$3,$nick),1,32)
      var %temp = $iif(%temp == alpha,beta,alpha)
      set $+(%,%temp) $addtok($($+(%,%temp),2),$iif($3,$3,$nick),32)
    } 
    else { msg # Group nonexistent. Use: !group alpha or !group beta }
  }
}

Posted By: s00p Re: Someone can help me optimize this code? - 09/11/09 09:43 AM
RusselB, I'm not 100% sure about that suggestion... If I may,...
Code:
on *:text:*:#:{
  tokenize 32 $strip($1-)
  if ($1 == !add) { 
    if !$istok(alpha beta,$2,32) {
      msg # Group nonexistent. Use: !group alpha or !group beta
      return
    }

    var %temp = % $+ $iif($2 == alpha,beta,alpha)
    var %nick = $iif($3,$3,$nick)
    set $+(%temp) $remtok($(%temp,2),%nick,1,32)

    %temp = % $+ $2
    set $+(%temp) $addtok($(%temp,2),%nick,32)
  }
}


Optimize for... speed, or size?
Code:
alias init_commands {
  hmake commands
  hadd commands !add cmd_add
  hadd commands !groups cmd_groups

  hmake commands_add
  hadd commands_add alpha cmd_add_alpha
  hadd commands_add beta cmd_add_beta

  hmake alpha
  hmake beta
}

alias cmd_add {
  if ($hget(commands_add,$1)) {
    $v1
  }
  else {
    msg # Group nonexistent. Use: !group
  }
}

alias cmd_groups {
  msg # Group Alpha $hget(alpha,0).item users
  msg # Group Beta $hget(beta,0).item users
}

alias cmd_add_alpha {
  if $hget(beta,$nick) { hdel beta $nick }

  ; you could store other data here... 
  hadd alpha $nick $true
}

alias cmd_add_beta {
  if $hget(alpha,$nick) { hdel alpha $nick }

  ; you could store other data here... 
  hadd beta $nick $true
}

on 1:LOAD: { init_commands }
on 1:START: { init_commands }

on *:text:*:#:{
  if ($3) { return }
  if ($hget(commands,$strip($1))) { $v1 $strip($2) }
}
Posted By: s00p Re: Someone can help me optimize this code? - 14/11/09 06:21 AM
I hope you've read my previous message. Here's the next step to reducing needless whitespacing:
Code:
on *:text:*:#:{
  tokenize 32 $strip($1-)
  if ($1 != !add) { 
    return
  }
  else if !$istok(alpha beta,$2,32) {
    msg # Group nonexistent. Use: !group alpha or !group beta
    return
  }

  var %temp = % $+ $iif($2 == alpha,beta,alpha)
  var %nick = $iif($3,$3,$nick)
  set $+(%temp) $remtok($(%temp,2),%nick,1,32)

  %temp = % $+ $2
  set $+(%temp) $addtok($(%temp,2),%nick,32)
}
Posted By: RusselB Re: Someone can help me optimize this code? - 14/11/09 07:09 AM
Your latest suggestion requires the first word to be !add, yet if the 2nd word isn't alpha or beta, the outgoing message says to use !group

Thus following the format from the outgoing message, the initial command would never be executed, since !add != !group
Posted By: s00p Re: Someone can help me optimize this code? - 14/11/09 07:44 AM
Correct. That's the same behaviour as your suggestion.
Posted By: RusselB Re: Someone can help me optimize this code? - 14/11/09 09:54 AM
Opps.. my mistake and my apologies.
Posted By: gooshie Re: Someone can help me optimize this code? - 14/11/09 05:31 PM

<any_nick> !Add alpha
-Your_Bot- You are in the Alpha group
<any_nick> !groups
<Your_Bot> [Group] Alpha 1 users [Group] Beta 0 users
<any_nick> !Add beta
-Your_Bot- You are in the Beta group
<any_nick> !groups
<Your_Bot> [Group] Alpha 0 users [Group] Beta 1 users

Code:
on $*:text:/^!groups$/iS:#:msg # [Group] Alpha $numtok(%alpha,32) users [Group] Beta $numtok(%beta,32) users

on $*:text:/^!add\b/iS:#:{
  tokenize 32 $strip($1-)
  if $istok(alpha beta,$2,32) {
    set $+(%,$2) $addtok($($+(%,$2),2),$nick,32)
    var %2 = $iif($2 == alpha,beta,alpha)
    set $+(%,%2) $remtok($($+(%,%2),2),$nick,1,32)
    notice $nick You are in the $2 group
  } 
  else { msg # Group nonexistent. Use: !add alpha or !add beta }
}

Posted By: s00p Re: Someone can help me optimize this code? - 15/11/09 03:46 AM
Code:
on $*:text:/^!groups$/iS:#:msg # [Group] Alpha $numtok(%alpha,32) users [Group] Beta $numtok(%beta,32) users

on $*:text:/^!add\b/iS:#:{
  tokenize 32 $strip($1-)
  ; remove the else branch, since you can expect it not to be executed most of the time.
  var %g1, %g2
  if ($2 == alpha || $2 == beta) {
    goto $2
  }

  msg # Group nonexistent. Use: !add alpha or !add beta
  return

  :alpha
  set %alpha $addtok(%alpha,$nick,32)
  set %beta $remtok(%beta,$nick,1,32)
  goto end

  :beta
  set %alpha $remtok(%alpha,$nick,1,32)
  set %beta $remtok(%beta,$nick,32)
  goto end

  :end
  notice $nick You are in the $2 group
}


wink
Posted By: gooshie Re: Someone can help me optimize this code? - 15/11/09 05:18 PM
Code:
on $*:text:/^!add\b/iS:#:{
  var %g $strip($2)
  $iif(!$3 && (%g == alpha || %g == beta),goto %g)

  msg # Group nonexistent. Use: !add alpha or !add beta
  halt

  :alpha
  %alpha = $addtok(%alpha,$nick,32)
  %beta = $remtok(%beta,$nick,1,32)
  goto end

  :beta
  %beta = $addtok(%beta,$nick,32)
  %alpha = $remtok(%alpha,$nick,1,32)

  :end
  notice $nick You are in the %g group
}
Posted By: gooshie Re: Someone can help me optimize this code? - 15/11/09 05:38 PM
<nick_one> !Add alpha
-Your_Bot- You are in the Alpha group
<any_nick> !group
<Your_Bot> [Group] Alpha 1 users [Group] Beta 0 users
<nick_one> !Add beta
-Your_Bot- You are in the Beta group
<any_nick> !groups
<Your_Bot> [Group] Alpha 0 users [Group] Beta 1 users

Code:
on $*:text:/^!add (alpha|beta)$/iS:#:{
  var %g $regml(1)
  notice $nick You are in the %g group
  set $+(%,%g) $addtok($($+(%,%g),2),$nick,32)
  %g = $iif(%g = alpha,beta,alpha)
  set $+(%,%g) $remtok($($+(%,%g),2),$nick,1,32)
}
on $*:text:/^!add\b/iS:#:msg # Invalid Parameters. Use: !add alpha or !add beta
on $*:text:/^!group(s)?$/iS:#:msg # [Group] Alpha $numtok(%alpha,32) users [Group] Beta $numtok(%beta,32) users
Posted By: Tomao Re: Someone can help me optimize this code? - 16/11/09 05:27 AM
gooshie, why another two text events when you can do:
Code:
on *:text:*:#:{
  if ($strip($1) == !add) && (!$istok(alpha beta,$strip($2),32)) { 
    msg # Invalid Parameters. Use: !add alpha or !add beta 
  }
  if ($regex($1-,/^!(add) (alpha|beta)$/iS)) {
    var %g $regml(2)
    notice $nick You are in the %g group
    set $+(%,%g) $addtok($($+(%,%g),2),$nick,32)
    %g = $iif(%g = alpha,beta,alpha)
    set $+(%,%g) $remtok($($+(%,%g),2),$nick,1,32)
  }
  if ($regex($1-,/^!group(s)?$/iS)) { 
    msg # [Group] Alpha $numtok(%alpha,32) users [Group] Beta $numtok(%beta,32) users
  }
}
Posted By: s00p Re: Someone can help me optimize this code? - 16/11/09 01:05 PM
Didn't you listen to my lecture on excess tabbing? Your solution goes about doing the same comparison twice in an incredibly inefficient manner. Please don't insult gooshie like that.

Here's my suggestion for improvement:
Code:
alias alpha {
  set %alpha $addtok(%alpha,$nick,32)
  set %beta $remtok(%beta,$nick,1,32)
}

alias beta {
  set %alpha $remtok(%alpha,$nick,1,32)
  set %beta $addtok(%beta,$nick,32)
}

on $*:text:/^!add (alpha|beta)$/iS:#:{
  var %g $regml(1)
  notice $nick You are in the %g group
  %g
}

on $*:text:/^!add\b/iS:#:msg # Invalid Parameters. Use: !add alpha or !add beta
on $*:text:/^!group(s)?$/iS:#:msg # [Group] Alpha $numtok(%alpha,32) users [Group] Beta $numtok(%beta,32) users
Posted By: Tomao Re: Someone can help me optimize this code? - 16/11/09 06:25 PM
Originally Posted By: s00p
Please don't insult gooshie like that.
Excuse me, s00p. Please don't be melodramatic. Insult is too strong of a word for me to begin with. I was just offering my take on it. You sound, to me, as if one should feel insulted to post against another person's suggestion. For God's sake, this is a learning forum where people are free to give it a shot with their ideas.
Posted By: gooshie Re: Someone can help me optimize this code? - 16/11/09 11:56 PM
Tomao asked, "why another two text events..."
Answer: Because compared to your example it is six lines shorter,
over a hundred bytes smaller and only triggers on what it should.

would this be better?

Code:
on $*:text:/^!(add\b|groups?\b) ?(alpha\b|beta\b)?/giS:#:{
  var %c $regml(1),%g $regml(2)
  if !$3 && %g && %c = add {
    notice $nick You are in the %g group
    set $+(%,%g) $addtok($($+(%,%g),2),$nick,7)
    %g = $iif(%g = alpha,beta,alpha)
    set $+(%,%g) $remtok($($+(%,%g),2),$nick,1,7)
  }
  elseif %c = add { msg # Invalid Parameters. Use: !add alpha or !add beta }
  elseif !$2 { msg # [Group] Alpha $numtok(%alpha,7) users [Group] Beta $numtok(%beta,7) users }
}
Posted By: gooshie Re: Someone can help me optimize this code? - 16/11/09 11:57 PM
s00p

Code:
on $*:text:/^!add (alpha|beta)$/iS:#:notice $nick You are in the $regml(1) group | $regml(1)
on $*:text:/^!add\b/iS:#:msg # Invalid Parameters. Use: !add alpha or !add beta
on $*:text:/^!groups?$/iS:#:msg # [Group] Alpha $numtok(%alpha,7) users [Group] Beta $numtok(%beta,7) users
alias alpha %alpha = $addtok(%alpha,$nick,7) | %beta = $remtok(%beta,$nick,1,7)
alias beta %alpha = $remtok(%alpha,$nick,1,7) | %beta = $addtok(%beta,$nick,7)
Posted By: gooshie Re: Someone can help me optimize this code? - 17/11/09 01:01 AM
Two more methods using hash tables:

Code:
on $*:text:/^!(add\b|groups?\b) ?(alpha\b|beta\b)?/giS:#:{
  var %g $regml(2)
  if !$3 && $regml(1) = add && %g {
    hadd -m %g $nick
    notice $nick You are in the %g group
    %g = $iif(%g = alpha,beta,alpha)
    $iif($hget(%g),hdel %g $nick)
  }
  elseif ($regml(1) = add) msg # Invalid Parameters. Use: !add alpha or !add beta
  elseif (!$2) msg # [Group] Alpha $hget(alpha,0).item users [Group] Beta $hget(beta,0).item users
}


Smaller and more precise:

Code:
on $*:text:/^!add (alpha|beta)$/iS:#:{
  var %g $regml(1)
  hadd -m %g $site
  notice $nick You are in the %g group
  %g = $iif(%g = alpha,beta,alpha)
  $iif($hget(%g),hdel %g $site)
}
on $*:text:/^!add\b/iS:#:msg # Invalid Parameters. Use: !add alpha or !add beta
on $*:text:/^!groups?$/iS:#:msg # [Group] Alpha $hget(alpha,0).item users [Group] Beta $hget(beta,0).item users
Posted By: s00p Re: Someone can help me optimize this code? - 20/11/09 04:41 AM
Originally Posted By: gooshie
s00p

Code:
on $*:text:/^!add (alpha|beta)$/iS:#:notice $nick You are in the $regml(1) group | $regml(1)
on $*:text:/^!add\b/iS:#:msg # Invalid Parameters. Use: !add alpha or !add beta
on $*:text:/^!groups?$/iS:#:msg # [Group] Alpha $numtok(%alpha,7) users [Group] Beta $numtok(%beta,7) users
alias alpha %alpha = $addtok(%alpha,$nick,7) | %beta = $remtok(%beta,$nick,1,7)
alias beta %alpha = $remtok(%alpha,$nick,1,7) | %beta = $addtok(%beta,$nick,7)


'smaller' isn't always 'more optimal'.
Posted By: gooshie Re: Someone can help me optimize this code? - 20/11/09 01:00 PM
would this be better?

Code:
on $*:text:/^!add (alpha|beta)$/iS:#:{
  if $regml(1) = beta { %alpha = $remtok(%alpha,$nick,1,7) | %beta = $addtok(%beta,$nick,7) }
  else { %alpha = $addtok(%alpha,$nick,7) | %beta = $remtok(%beta,$nick,1,7) }
  notice $nick You are in the $regml(1) group
}
on $*:text:/^!add\b/iS:#:msg # Invalid Parameters. Use: !add alpha or !add beta
on $*:text:/^!groups?$/iS:#:msg # [Group] Alpha $numtok(%alpha,7) users [Group] Beta $numtok(%beta,7) users
© mIRC Discussion Forums