Topic: How HELP's !random works...

Sometimes ppls think !random is just unfair. All i can do is sharing the code how the command works.

The command itself:

    /**
     * Random command
     * 
     * @return boolean
     */
    private boolean cmdRandom() {
        if (!hasCommand("random") || char_ != '!') return false;
        int max = 1000;
        String seps = ",\\.\\-\\|\\:\\/\\\\\\# ";
        String string = param1+' '+param2+' '+param3+' '+message;
        Matcher matcher;
        Map<Integer,String> map = new HashMap<Integer,String>();
        ArrayList<String> players = new ArrayList<String>();
        
        // Max value
        matcher = regInteger.matcher(string);
        if (matcher.find()) {
            max = Integer.parseInt(matcher.group(1));
            string = string.replace(matcher.group(1),"");
        } else max = 1000;
        string = string.replaceAll("[ ]+"," ").trim();
        
        // List of players
        matcher = Pattern.compile("([a-zA-Z"+seps+"]+)").matcher(string);
        if (matcher.find()) {
            String[] split = matcher.group(1).trim().split("["+seps+"]+");
            for (String p : split) {
                if (!p.isEmpty() && !players.contains(p.toUpperCase())) players.add(p.toUpperCase());
            }
        }
        
        // Random
        if (players.size() > 0) {
            for (String p : players) map.put(Utility.Random.Integer(0,max),p);
            List<Integer> rnds = new ArrayList<Integer>(map.keySet());
            Collections.sort(rnds,Collections.reverseOrder());
            string = "";
            for (Integer rnd : rnds) string += " "+map.get(rnd)+": "+rnd;
            draw(lng.get("%0 randoms for [%1]...%2",player,max,string));
        } else draw(lng.get("%0 randoms [%1] and gets a [%2]",player,max,Utility.Random.Integer(0,max)));
        return true;
    }

The Utility.Random.Integer(int min,int max):

        /**
         * Integer
         * 
         * @param min
         * @param max
         * @return int
         */
        public static int Integer(int min, int max) {
            return min + (int)(Math.random() * ((max - min) + 1));
        }

Math.random() is an internal java thing, i have no control on that. I also dont set any kind of seed or whatever would change the behavior of that method.

PS: Feel free to post a better algorithm if you can tongue

Amethyst        T0    Pyr     30
Cisco                T4    Sco    90
Delphi              T1    Eng    21
Neuropath      T0    Tel     10

Re: How HELP's !random works...

You forgot the line

if (player = Mothra)  Make random = top tongue

Re: How HELP's !random works...

What scope do the following vars have?
- char_
- param1
- param2
- param3
- message
- regInteger

People can still `/random 1000` if they have doubts.

Re: How HELP's !random works...

All are in protected scope.

They have todo with parsing ppls input.
char_ only stores if you used a ! or / command on help.
param1 - 3 are the extracted parameters form input, message the rest on right side.
regInteger is a Regular Expression Pattern which is used to check that the random max value is really a number

PS: Yes ppls can still do /random but !random were specially invented for doing one random for a group of ppls, i just posted it to let ppls know how the results are calculated. Anyone could now say, why not implementing that on server side, simple its just to complicated to code a parser that cant be exploited through trying to get on top of the results.

Amethyst        T0    Pyr     30
Cisco                T4    Sco    90
Delphi              T1    Eng    21
Neuropath      T0    Tel     10

Re: How HELP's !random works...

cisco211 wrote:

All are in protected scope.

They have todo with parsing ppls input.
char_ only stores if you used a ! or / command on help.
param1 - 3 are the extracted parameters form input, message the rest on right side.
regInteger is a Regular Expression Pattern which is used to check that the random max value is really a number

PS: Yes ppls can still do /random but !random were specially invented for doing one random for a group of ppls, i just posted it to let ppls know how the results are calculated. Anyone could now say, why not implementing that on server side, simple its just to complicated to code a parser that cant be exploited through trying to get on top of the results.

Why would this be complicated on the server side? It would be no more or less exploitable than your HELP bot, surely?

Re: How HELP's !random works...

It has several reasons why its not implemented on server side.

(Thx to angus to telling me that)
1. The server itself is a lot of pure C (not much C++)
2. The server is very portable and stuff like this would require adding libboost (There are a lot another reasons why not using this)
3. Its known that C sux balls when working with Strings, thats where HELP comes in, he were made to extend with commands that are a lot of work in C  implementing/maintaining it.
4. Atm you cant directly see that but helps chat line parsing is a complicated token parser which makes use of regular expressions to ensure type safety including a lot of custom parsing inside each command. (Written as C code it would take a lot of time to get it working on server and this would also decrease the performance a lot)
5. Real life... we also have another things todo than spending 100% of our free time to HSR.
6. Using Help allows quicker development/deployment.

And a personal meaning about C, lez say programming languages looks like pictures, it would look like this:

Languages like C: http://devmag.org.za/blog/wp-content/up … bezier.png
Many languages that clearly not C: http://jaced.com/blogpix/2007/trisquarecircle/002.gif

So... Instead having 10yrs+ experience of C i can do much more complicated stuff in other languages with much lesser time (this is what counts).
Talking about speed/accuracy... C isnt the best everytime, i can get faster/better results (on lowlevel) with pure basic.

It should also be common to choose languages depending on their strength compared to your problem you want to resolve. And chat parsing is one of the things that are ugly work on C.

Amethyst        T0    Pyr     30
Cisco                T4    Sco    90
Delphi              T1    Eng    21
Neuropath      T0    Tel     10

Re: How HELP's !random works...

I'm not a C programmer either; I wasn't disputing that it may be more difficult to implement. You don't need to go on the defensive wink