Akusu's scripting emporium

Discussion in 'Server and Client Scripting' started by Akusu, May 28, 2010.

  1. Akusu

    Akusu Guest

    Working scripts will be featured here. Suggestions on what to add are welcome, as I'll be working on these on and off. For now look at these as examples to incorporate into more complete scripts.

    Test scripts will be archived in following posts and moved here when completed.

    Password-Protected server 1.1.1
    Features: If a password is set, auto-mutes until UNREGISTERED_TIMEOUT seconds have passed, then boots. Allows auth at or above the auth level set in PASSWORD_SET_LEVEL to set a new password. When a password is reset, the memberlist is cleared. Registering using the password leaves that player registered until next password reset. Added blinding of non-members (thanks to Astruvius figuring out how to get around the broadcasts).
    Future upgrades: Suggestions?
    Code (text):
    1.  
    2. ({
    3. UNREGISTERED_TIMEOUT : 30,
    4. PASSWORD_SET_LEVEL : 2,
    5. MIN_PASSWORD_LENGTH : 6,
    6.  
    7. PROP_PASSWORD : "PASSWORD",
    8. PROP_MEMBERS : "members",
    9.  
    10. serverStartUp : function() {
    11.     this.init();
    12. }
    13. ,
    14.  
    15. securityMessage : function(tar, message)
    16. {
    17.     sys.sendMessage(tar, "~~Security: " + message);
    18. }
    19. ,
    20.  
    21. scriptMessage : function(tar, message)
    22. {
    23.     sys.sendMessage(tar, "~~Script: " + message);
    24. }
    25. ,
    26.  
    27. afterNewMessage : function (message) {
    28.     if (message == "Script Check: OK") {
    29.         sys.sendAll("Scripts were updated!");
    30.         this.init();
    31.     }
    32. }
    33. ,
    34.  
    35. afterLogOut : function (src) {
    36.     delete loggedin[src]; }
    37. ,
    38.  
    39. beforeLogIn: function(src){
    40.     var playerName = sys.name(src);
    41.     if (sys.getVal(this.PROP_PASSWORD) != undefined && sys.getVal(this.PROP_MEMBERS).indexOf("|" + playerName + "|") == -1)
    42.     {
    43.         muted[src] = true;
    44.         this.securityMessage(src, "You have entered a password-protected server. Please enter the password using /password [password] within " + this.UNREGISTERED_TIMEOUT + " seconds.");
    45.         this.securityMessage(src, "You will not have to do this again unless the password changes.");
    46.         sys.callLater("if (sys.getVal(" + this.PROP_MEMBERS + ").indexOf('|' + " + playerName + " + '|') == -1) { sys.sendMessage(" + src + ", 'Sorry, time is up.'); sys.kick( '" + src + "' );}", this.UNREGISTERED_TIMEOUT);
    47.     }
    48.     else
    49.     {
    50.         loggedin[src] = src;
    51.     }
    52. }
    53. ,
    54.  
    55. beforeChatMessage : function(src, message){
    56.     sys.stopEvent();
    57.     if ((message[0] == '/' || message[0] == '!') && message.length > 1)
    58.     {  
    59.         var args = message.substring(1).toLowerCase().split(' ');
    60.         if (this.commands[args[0]] != undefined)
    61.         {              
    62.             this.commands[args[0]](this, sys, src, args);
    63.         }
    64.         else
    65.         {
    66.             this.scriptMessage(src, 'The command "' + args[0] + '" does not exist.');
    67.         }
    68.     }
    69.     else if (sys.auth(src) < 2 && muted[src] == true)
    70.     {
    71.         this.securityMessage(src, "You are muted.");
    72.         return;
    73.     }
    74.     else if ( sys.getVal(this.PROP_PASSWORD) != undefined)
    75.     {
    76.         this.sendLoggedIn(src, message);
    77.     }
    78.     else
    79.     {
    80.         this.sendAll(sys.name(src) + ":" + message);
    81.     }
    82. }
    83.  
    84. ,
    85.  
    86. /**
    87. *   Password commands.
    88. */
    89. password : function(context, sys, src, args)
    90. {
    91.     if (args[1] != undefined && args[1] == sys.getVal(context.PROP_PASSWORD))
    92.     {
    93.         var members = sys.getVal(context.PROP_MEMBERS);
    94.         members = members + sys.name(src) + "|";
    95.         sys.saveVal(context.PROP_MEMBERS, members);
    96.         muted[src] = false;
    97.         loggedin[src] = src;
    98.         context.securityMessage(src, "Password accepted.");
    99.         context.securityMessage(src, "You are now unmuted.");
    100.     }
    101.     else
    102.     {
    103.         context.securityMessage(src, "Access denied.");
    104.         sys.kick(src);
    105.     }
    106. }
    107. ,
    108.  
    109. setPassword : function(context, sys, src, args)
    110. {
    111.     if (sys.auth(src) < context.PASSWORD_SET_LEVEL)
    112.     {
    113.         //Note: There is no feedback message here because I don't want non-mods to be aware of the command.
    114.         return;
    115.     }
    116.    
    117.     if (args[1] == undefined || args[1].length < context.MIN_PASSWORD_LENGTH)
    118.     {
    119.         context.securityMessage(src, "In order to set a new password, that password must be at least " + context.MIN_PASSWORD_LENGTH + " characters in length.");
    120.     }
    121.     else
    122.     {
    123.         sys.saveVal(context.PROP_PASSWORD, args[1]);
    124.         sys.saveVal("members", "|" + sys.name(src) + "|");
    125.         loggedin = new Array();
    126.         loggedin[src] = src;
    127.         context.securityMessage(src, "Password has been set to '" + args[1] + "' successfully.");
    128.     }
    129. }
    130. ,
    131.  
    132. broadcastPassword : function(context, sys, src, args)
    133. {
    134.     if (sys.auth(src) >= context.PASSWORD_SET_LEVEL)
    135.     {
    136.         sys.sendAll( sys.getName(src) + " wishes you all to know that the server password is " + sys.getVal(context.PROP_PASSWORD) + ".");
    137.     }
    138. }
    139. ,
    140.  
    141. sendLoggedIn : function (src, message)
    142. {
    143.     for( var m in loggedin)
    144.     {
    145.         sys.sendMessage(m, sys.name(src) + ": " + message);
    146.     }
    147. }
    148. ,
    149. /**
    150. *   Password commands end.
    151. */
    152.  
    153. init : function() {
    154.     if (typeof(muted) == "undefined") {
    155.         sys.setPA ("muted");
    156.     }
    157.    
    158.     if (typeof(loggedin) == "undefined")
    159.     {
    160.         sys.setPA("loggedin");
    161.     }
    162.    
    163.     this.commands = new Object();
    164.    
    165.     //Password commands
    166.     this.commands["password"] = this.password;
    167.     this.commands["pass"] = this.password;
    168.     this.commands["setpassword"] = this.setPassword;
    169.     this.commands["setpass"] = this.setPassword;
    170.     this.commands["broadcastpassword"] = this.broadcastPassword;
    171.     this.commands["broadpass"] = this.broadcastPassword;
    172.    
    173. }
    174.  
    175. })
    176.  
    [/HIDE]

    Working with my scripts:

    There is an init function. That function creates an object called commands. This is meant to hold a reference to all the commands in the server. Those commands must follow the function pattern of functionName: function(context, sys, src, args). Context replaces the keyword this because of the way javascript uses it ("this" would reference the command object... not what we want). Sys isn't accessible otherwise so we pass a reference, and src and args should be self-explanatory. These are all handled for you already though.

    All you need to do is create your function with the appropriate signature, replace "this" with "context" and add it to the init function. Then it will automatically be detected when the command name is used. Note: I'm changing the slash command to lowercase and your function MUST be registered lowercase or it will not work correctly.
     
    Last edited by a moderator: Jul 20, 2010
  2. Akusu

    Akusu Guest

    Script for preventing non-members from entering a server. Currently does not prevent them from seeing chat or battling for 30 seconds upon arrival. Should remember members and not force them to input the password every time.

    Edit: Script essentially done, see first post.
     
    Last edited by a moderator: Jun 4, 2010
  3. Spyro-The-Gengar

    Spyro-The-Gengar New Member

    Joined:
    May 19, 2010
    Messages:
    5
    Likes Received:
    0
    There is error appeared in line 10 =) But it very good script
     
  4. Akusu

    Akusu Guest

    I've updated it a bunch so I'm not sure which version you're talking about haha. What's the actual error? Either way I can't test this until I get home.
     
  5. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    I think it's because you haven't put a script check in newMessage. To solve the problem, just close the server and reopen.
     
  6. Akusu

    Akusu Guest

    Is this something I'm missing? Or that Spyro has to do?
     
  7. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Sorry.

    To solve the problem without editing the script, just close the server and reopen. (Spyro)

    To solve the problem with editing the script, check for 'Script Check: OK' in afterNewMesage and declare everything there like you do in serverStartUp. And make sure nothing is referred out of a conditional statement [so that the script does actually check as OK]. (Akusu)
     
  8. Akusu

    Akusu Guest

    Thanks, I was wondering what that was when I was looking, it's not in the example Coyotte put in the reference page.
     
  9. Akusu

    Akusu Guest

    Updated the script. Sorry it took me so long but I ran into issues because I wanted to save an object (not a string literal) and wasn't sure how to implement it otherwise. I also had some scoping problems... but they're worked out.

    Edit: Also added a blurb about how to incorporate new commands in. I intend to follow this pattern from now on.
     
    Last edited by a moderator: Jun 4, 2010
  10. Astruvis

    Astruvis Banned

    Joined:
    May 14, 2010
    Messages:
    217
    Likes Received:
    0
    I tired to fix yours up at first and add a blinder, however; it was a bit, too, erm... messy?

    I made a script that does the same thing instead since I couldn't figure out what you were doing.

    Go check it out. xP
     
  11. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    His scripts are great. Just using many not basic stuff.
     
  12. Elements

    Elements BOOMER SOONER Forum Moderator Server Staff Forum Moderator Server Staff

    Joined:
    May 9, 2010
    Messages:
    1,195
    Likes Received:
    48
    PO Trainer Name:
    Elements
    I definitely like your scripts Akusu, because (with the little javascript i know) I can understand what's going on. Good job not using basic stuff, Akusu :D
     
  13. Akusu

    Akusu Guest

    Thanks for your support guys :). I'm currently working on a tournament script with standings as well as an option to do round-robin with pods of 4 (meaning 4 rounds + final battle) as well. I'll update it when I have something useable.

    I'm hoping the end result will be a collection of scripts that will easily (enough) integrate with each other so you can pick and choose which features you want.

    Edit: After some research, I think Swiss is what I actually wanted. The math doesn't work out to what I thought it did with the round-robin.
     
    Last edited by a moderator: Jun 6, 2010
  14. eric the espeon

    eric the espeon is an espeon.

    Joined:
    Apr 21, 2010
    Messages:
    854
    Likes Received:
    1
    Swiss is annoying unless there is a lot of mod help, since unlike with normal tournaments inactive players don't always lose/get DQed. Could still work though, and would be a good addition.
     
  15. Akusu

    Akusu Guest

    Yeah my thinking is since there's only ever 1 tournament running, the people who are participating dwindles quickly. This would keep everyone playing. I would still support single-elim though so it would be up to the person using the script to decide on it. I still have to look into it better but it usually limits it to the same amount of rounds as single-elim, with the option of having the top players face off.

    Round times would also be (optionally) limited, I'm trying to decide how to best handle that though.
     
  16. eric the espeon

    eric the espeon is an espeon.

    Joined:
    Apr 21, 2010
    Messages:
    854
    Likes Received:
    1
    hmm.. how about having it so that people could still attempt to sign up as subs after the tournament starts, and when a mod DQs someone one of the subs is selected to join the tournament? That would help activity if we could get inactive players out, and move active ones in.
     
  17. Akusu

    Akusu Guest

    Yeah, that seems like a reasonable compromise. I'm gonna call stall battles that take longer than the allotted time (10 minutes per round default, probably) a tie, although if people decide to abuse the time that will have to get moderated as I can't tell if there's any turn that turn longer than say.... 2 minutes.

    Together that would get more people involved and playing at once, and also cut down on the moaning when 6-7 people can't join a tournament of an appropriate exponent of 2 size for single elim. There will also be early cut-offs too so that tournaments don't sit with registration open so long people start disappearing.
     
  18. Searinox

    Searinox New Member

    Joined:
    Jul 19, 2010
    Messages:
    17
    Likes Received:
    0
    I've wasted near half an hour headtilting at this. I'm not sure where I'm supposed to define the password - in the PROP_PASSWORD variable, the init function, or a /command from the actual server chatbox. Can somebody please post an example password setting command/whateveritis? Thank you.
     
  19. Akusu

    Akusu Guest

    Use /setpassword [password] in main chat of the server you've created.
     
  20. Searinox

    Searinox New Member

    Joined:
    Jul 19, 2010
    Messages:
    17
    Likes Received:
    0
    Okay, so I basically had a completly empty script window and I copypasted your code from inside the spoiler button, no more, no less. Then clicked OK. I then used /setpassword mypasswordhere on the server and I got no feedback from the console. I figured it's okay and prolly a security measure since you don't want a "password changed to ******" message broadcast to everyone. But then I connected myself to it and this happened.

    Player 1 set name to Searinox
    Script Error line 40: ReferenceError: Can't find variable: PROP_PASSWORD

    The user can't speak but can see server messages. The script was copied exactly as is under the button. Do I need to edit anything in the script? I'm using the july 11 server version. Reseting the server does not change anything.
     
    Last edited: Jul 19, 2010
  21. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    Replace everywher PROP_PASSWORD by this.PROP_PASSWORD, same with PROP_MEMBERS.
     
  22. Searinox

    Searinox New Member

    Joined:
    Jul 19, 2010
    Messages:
    17
    Likes Received:
    0
    Have you tried it yourself? It gives off this: Script Error line 6: SyntaxError: Parse error

    Here are the first 7 lines of the script after I changed it to your indication.

    ({
    UNREGISTERED_TIMEOUT : 30,
    PASSWORD_SET_LEVEL : 2,
    MIN_PASSWORD_LENGTH : 6,

    this.PROP_PASSWORD : "PASSWORD",
    this.PROP_MEMBERS : "members",
     
  23. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    well, i meant everywhere except here ^^
     
  24. Searinox

    Searinox New Member

    Joined:
    Jul 19, 2010
    Messages:
    17
    Likes Received:
    0
    Changed the first 2 occurrences on lines 6 and 7 back. The script no longer produces errors. However, I can't join, not with the default password, not with the one I set. I get access denied with /password mypass /password "mypass" /password PASSWORD and /password "PASSWORD", the last 2 are the default. By the way once I set the server password, where in the files does it get stored, or do I have to set it everytime I start the server?

    Also when I use /setpassword in the server, users can see the full setpassword command in plain text, even if not authenticated. Neither is the user kicked after 30 seconds. So aside from the fact that the script does indeed mute and kick users supplying the wrong password, nothing else of it works.

    To clarify everything, could you post the script, with all chances needed made and confirmed working?
     
    Last edited: Jul 19, 2010
  25. Akusu

    Akusu Guest

    Yeahhh.... you're not supposed to try the command from the server window hehe.

    I honestly haven't touched the script in a while, let me see what I can do to update it so it still works with the latest version.
     
  26. Akusu

    Akusu Guest

    Alright, the "this" thing was almost right, except because of the way the commands are encapsulated it actually was supposed to be "context" in some of the methods.

    It now works. I must never have uploaded the version I had that fixed that problem =/. Whoops.

    Edit: Removing debug statements tends to make things more secure. Hah.
     
    Last edited by a moderator: Jul 20, 2010
  27. Searinox

    Searinox New Member

    Joined:
    Jul 19, 2010
    Messages:
    17
    Likes Received:
    0
    Okay, if I'm not supposed to try the command from the server window then where on earth am I supposed to set the password? Your updated version gives me the same problems as the one we tried to work out yesterday: it doesn't kick after the number of seconds, and the unregistered user can still see server messages.

    Does the script hide other users currently online from the unregistered user?
     
  28. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    Also if the player logs off without registering and another logs on with his player number, he'd be kicked by the script.
     
  29. Akusu

    Akusu Guest

    This was a stop-gap measure until the built-in version is made... if it ever is, and was created to fill a need that someone like yourself requested. I'd appreciate you not blaming me for working with what's available. I also think that the security risk at present is manageable, considering the only server that makes a likely target is the beta server, and it doesn't bother with the password stuff. Not to mention the sheer stupidity involved in trolling what amounts to a fan site.

    You also have the option of changing a few of the global variables (the CAPS variables near the top). Changing UNREGISTERED_TIMEOUT results in an auto-boot function, but you'll be hard pressed to allow anyone to type the password in.

    Coyotte, does the kick function work with the name? That would fix the problem you're describing. I also think I saw something about disabling/deprecating sys.callLater, which might be why people aren't getting kicked.

    As far as I can tell, / commands are not parsed from the server, unless there's some function I can override to do so, which is why it just regurgitates it to the window. In order to set the password, log into your server from the client with an administrator account.
     
  30. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    It should work. But you have to make an array to store the names and then use that with sys.id, or whatever. Remember that global variables are accessible in sys.callLater through the script property.
     
  31. Akusu

    Akusu Guest

    Was referring to Coyotte's problem, which happens to be difficult to reproduce, actually.

    I should get around to fixing this again in the next couple of days and verify if the kicking is actually working.

    Edit: Thanks to whoever moved the discussion out of the thread.
     
    Last edited by a moderator: Jul 20, 2010
  32. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    I moved the other discussion to "Development - General"