Code generator

Discussion in 'CommandHelper Scripts' started by extradessert, Sep 22, 2012.

  1. extradessert

    extradessert Guest

    I needed to make a code generator for a registration script I was writing, and thought I'd share it with you guys!

    - Generates a random code when the proc is called. (Defaults to code length of 7)
    - Adjustable length of code.
    (To do this call _codegen(NUMBER) )
    - Choose what type of characters show up in the generated code. (Defaults to "full": all character types)
    (To do this call _codegen(NUMBER, type1, [type2]) where type1 is the first type of characters you want, and optionally type2 is the 2nd type of characters you want.)

    How to use:
    1. Paste this proc in
    2. Then in your script call: assign(@code, _codegen())
    That will assign @code to a random code in your script, then just reference @code wherever you need the generated code.
    3. [optional] If you need to adjust the code length, call _codegen(NUMBER).
    4. [optional] If you need to adjust the character type, call _codegen(NUMBER, type1, [type2]). Accepted types are "lower" for lowercase, "upper" for uppercase, "number" for numbers, and "full" for all 3 types.


    #Random code generator
    proc(_codegen, @codelength, @codetype, @codetype2,
    #version 1.1
       assign(@string, '')
        if(@codelength == '',
            assign(@codelength, 7)
        if(@codetype == '',
            assign(@codetype, full)
    #the characters
       assign(@lower, array(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z))
        assign(@upper, array(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z))
        assign(@number, array(1,2,3,4,5,6,7,8,9))
        assign(@full, array(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,1,2,3,4,5,6,7,8,9))
    #set 1
            assign(@chars, array_get(@lower)),
            assign(@chars, array_get(@upper)),
            assign(@chars, array_get(@number)),
            assign(@chars, array_get(@full)),
        die('Fail: Invalid code type given')
    #set 2
       if(@codetype2 !== '',
                assign(@chars2, array_get(@lower)),
                assign(@chars2, array_get(@upper)),
                assign(@chars2, array_get(@number)),
                assign(@chars2, array_get(@full)),
            die('Fail: Invalid code type given')
            foreach(@chars2, @i,
                array_push(@chars, @i)
    #generating code
       for(assign(@i, 0), @i < @codelength, @i++,
            assign(@string[@i], @chars[rand(array_size(@chars))])
        assign(@string, array_implode(@string, ''))
    - option to allow lowercase, uppercase, and numbers in code (added in version 1.1)
    - fix nasty looking array?

    version 1.1:
    - Add option to choose what type of characters show up in the generated code.

    Old versions:
    ... 1.1 | 1.0
  2. zIMoNsTeRz

    zIMoNsTeRz Member

    Wow thank you so much! I was just trying to do this yesterday lol, but this is way better.

    This is what I used it for:
    bind(player_chat, null, null, @event,
    assign(@key, import('numbertest'))
    assign(@player, @event['player'])
        if(@key != null) {
        if(@key['status'] == true && @event['message'] == @key['number']) {
            broadcast(_ba('&a'[email protected]player.'&7 was the fastest typer and won!'))
            export('numbertest', array('status': false))
            msg(_colorize('&7Generating your reward...'))
                set_timeout(2000, closure(
                    if(@item == null) {
                        assign(@item, 98)
                    runas('~console', sconcat('/give', @player, rand(1,100), rand(2,8)))
                    msg(_colorize('&aHere you go. &7'[email protected]qty.' '.data_name(@item).'&a!'))
    set_interval(_minutes(20), closure(
    assign(@number, _codegen(rand(10,15)))
    broadcast(_ba('&7First to type &a&o'[email protected]number.' &7gets a reward!'))
        export('numbertest', array(
                'number': @number,
                'status': true,
            set_timeout(30000, closure(
                if(import('numbertest')['status'] == true) {
                    broadcast(_ba('&cSpeed test ended! No one could type fast enough.'))
                    export('numbertest', false)
  3. extradessert

    extradessert Guest

    That's a interesting use, reward for typing random crap. I would have never thought of that.
  4. raGan

    raGan Member

    How about doing md5 of random numbers as strings and getting random substrings ? I use that to generate player-specific passwords. (using their names)
  5. LadyCailin

    LadyCailin Administrator Developer

    As far as cleaning up the arrays, the letter ones don't really have a better solution, but for the numeric arrays, you can use range() to quickly generate that. Also, you should quote the letters :p
  6. XeonPowder

    XeonPowder New Member

    I made a file called random.msa and i put it in my LocalPackages folder. Is that the wrong place, because its not working D: <---- nub
  7. PseudoKnight

    PseudoKnight Well-Known Member

    *.msa is for command aliases in LocalPackages. Use for things that should run automatically like event binds.
  8. extradessert

    extradessert Guest

    Yes I know wraith, quote everything... IDK it still works.

    @raGan: post example?
    I had made this code gen script for my registration script, using a randomly generated password one-time in a specific /invite <player>. It's supposed to get players out of my 'guest' permissions group without admin interaction (guest group can't break or place blocks, or chat after they spam too much).
  9. raGan

    raGan Member

    msg(color(13) . 'Your password is ' . substr(md5(player() . 'secret'), 0, 6) . '.')
    This way password is (almost !) always different and everyone have to get it themself and can't simply pass the code to other players, since it will work only for them. I display this to player when he clicks special blok holding special item.
  10. extradessert

    extradessert Guest

  11. IronyBeasty

    IronyBeasty Member

    This one doesn't work either :(
  12. CypherNova139

    CypherNova139 New Member

    Interesting... a lot of potential uses for this in other scripts, so I decided to give this script a update!

    I added a couple features:
    - Aliases to character types. (Surprised this isn't in the original script as switch() is being used)
    - /codegen command - Print a random code on demand!

    And fixed a couple bugs:
    - Fixed passing a non-numeric length, now it just uses the default number instead of throwing an exception when attempting to generate the random code
    - Switched syntax to use brackets

    Download .MSLP file or view paste