mIRC Homepage
Posted By: sparta on ban event - 26/06/07 09:39 PM
I rewrote it, now everything working, but it must be a cleaner way to write it? would be nice if someone could explain or show me? smile
Code:
on *:ban:#: {
  if ($me isop $chan) && ($banmask iswm $address($me,5)) && ($banmask != *!*@*) && ($banmask != *!~*@*) && (%ban.pro == Enabled) {
    mode $chan -bo $banmask $nick
    .notice $nick Don't ban me!!
  }
  if ($me !isop $chan) && ($banmask iswm $address($me,5)) && (%ban.pro == Enabled) {
    echo -a Not oped in $chan $+ , can't unban my self.
  }
  else {
    if ($me isop $chan) && ($banmask != *!*@*) && ($banmask != *!~*@*) && (%kickonban == Enabled) {
      var %banmask = $banmask
      if ($ial(%banmask,0).nick == 0) { return }
      if ($banmask iswm $address($me,5)) { return }
      var %totbu = $ial(%banmask,0).nick
      var %stb = 1
      while (%stb <= %totbu) {
        echo -a working -> $ial(%banmask,%stb).nick
        if (%stb == %totbu) { halt }
        inc %stb 
      }
    }
  }
}
Posted By: Bekar Re: on ban event - 27/06/07 05:16 AM
Just a few comments on the code in general.

1) Why do you put '$banmask' into '%banmask' ? You aren't changing it anywhere, so this is pointless. Just use '$banmask'.

2) As this is a channel-based event, you can drop the '$chan' in the if tests, i.e. 'if ($me isop)'.

3) The if's look to be checking the same things a few times, so can be re-structured to not re-check so many times.

4) Unless you've got code after this, why are you doing an extra 'if' within the while? It's superflouous.
Posted By: sparta Re: on ban event - 27/06/07 09:23 AM
I fixed 1 and 2, dunno how to fix the last 2.

3) The if's look to be checking the same things a few times, so can be re-structured to not re-check so many times.

4) Unless you've got code after this, why are you doing an extra 'if' within the while? It's superflouous.
Posted By: 5618 Re: on ban event - 27/06/07 09:33 AM
Code:
else {
  if {

equals: elseif {

Code:
while (%stb <= %totbu) {
  echo -a working -> $ial(%banmask,%stb).nick
  if (%stb == %totbu) { halt }
  inc %stb
}

Why use "if (%stb == %totbu) { halt }" at all? The while statement will automatically stop when %stb equals %totbu since that *is* your while statement.
Posted By: Riamus2 Re: on ban event - 27/06/07 11:19 AM
Originally Posted By: 5618
Why use "if (%stb == %totbu) { halt }" at all? The while statement will automatically stop when %stb equals %totbu since that *is* your while statement.


As mentioned, what you have there is unnecessary as your WHILE statement will automatically complete when those are equal. Beyond that, if you *did* want to halt the full script, you'd want to use RETURN instead of HALT, like you did previously. Or, if you just want to stop the WHILE loop, but want the script to continue running, then use BREAK.
Posted By: RoCk Re: on ban event - 27/06/07 12:18 PM
Originally Posted By: Bekar
2) As this is a channel-based event, you can drop the '$chan' in the if tests, i.e. 'if ($me isop)'.

eh? That don't work.
Posted By: sparta Re: on ban event - 27/06/07 12:30 PM
Quote:
2)As this is a channel-based event, you can drop the '$chan' in the if tests, i.e. 'if ($me isop)'.

it could have been working, on @*:ban:, then i could remove that line, but since i also use if ($me !isop $chan) { do stuff } it dosent work that well in my code wink
Posted By: Bekar Re: on ban event - 27/06/07 12:51 PM
You're right.. Why did I think that worked ?!

Anybody know if using '#' is faster than '$chan' in a channel-based event?

Code:
if ($me isop #) {
Posted By: RoCk Re: on ban event - 27/06/07 01:09 PM
I don't know if it's faster or not but I normally use # over $chan in those types of situations, just a habit I guess. It's 4 less characters though smile
Posted By: Wims Re: on ban event - 27/06/07 07:28 PM
i make some test and it show that # is a little bit faster than $chan ( 10 ms with 5000 iteration ) but not tested with an event
Posted By: RoCk Re: on ban event - 27/06/07 08:03 PM
Maybe it's a little faster because with # there's no chance for parameters and properties so no need to check? I'm not sure if that's true or not, but it looks good. Could be true. confused
Posted By: Riamus2 Re: on ban event - 27/06/07 11:28 PM
I kind of have to laugh about such questions regarding speed. Yes, speed is important... to a point. Unless you have a huge script that does a LOT all at once, even a couple hundred miliseconds isn't going to be noticeable. It's just my opinion that worrying over that little bit of speed change is not worth the effort, of course. Now, if we're talking about seconds of difference, then it's worth caring about.
Posted By: RoCk Re: on ban event - 27/06/07 11:57 PM
One identifier may not make a noticeable difference, but add that one unnoticeable difference together with all the other unnoticeable differences and suddenly you have a noticeable difference. But you're right, 10ms is nothing to worry about.
Posted By: Bekar Re: on ban event - 28/06/07 12:22 AM
Yeah, I agree.

Was just curious..
Posted By: Riamus2 Re: on ban event - 28/06/07 12:49 AM
Originally Posted By: RoCk
One identifier may not make a noticeable difference, but add that one unnoticeable difference together with all the other unnoticeable differences and suddenly you have a noticeable difference. But you're right, 10ms is nothing to worry about.


Only if it's all done at once. And in most cases, that is not true. There was discussion over how not using {}'s could make it "noticeably" faster. I have a LOT of scripts running and many of them use TEXT events in channels with a lot of text. The majority of code in my scripts use {}'s and there isn't enough of a slowdown to even notice without trying to detect it with a script. Everything happens fast enough that it might as well be called "instant." Sure, if you went through and ran all of my scripts without those {}'s and tested it with a script, you'd find that not using them would be faster... but when actually using the scripts as they are currently written, there isn't anywhere near enough slowdown to notice.

It would really take a lot of things happening all at once for it to really be noticeable, or else some loop that needs to loop a LOT of times before getting a result, or something similar. That's all I'm saying. I believe in optimizing code, but there are just some things that really aren't worth bothering with and {}'s or ()'s or #/$chan are some of them. As such, I would use:

Code:
if (this == that) { do this in $chan }


Rather than:

Code:
if (this == that) do this in #


Or:

Code:
if this == that { do this in # }


Or whatever. Again, it's just my opinion. Besides, I personally find the first one much easier to read quickly without having to think about it. That makes for easier debugging.
© mIRC Discussion Forums