Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[New G2O][Shared] Command Handler
#1
*Introduction:

Info: This script is stable and propably doesn't have any bugs.

Hi everyone, i've recently thinking about writing command-handler, but there was one little problem..
Command-handler would be useful in both sites (client and server), so i write it a piece of code,
which you can load on both sides. You don't have to load it in both sides, you can load this script freely.
Script works perfectly, but i'm thinking on better way, to identify script-side.

License

Download

*List of functions:



void addCommand(string or array cmd, void callback)
void removeCommand(string or array cmd, [[optional: void callback ]])



Including script in xml:

Info: I'm recommending you, to place script in default lib folder.



<script src="lib/cmd-handler.nut" type="client" />
<script src="lib/cmd-handler.nut" type="server" />



*Examples:



*Adding command(s):

Squirrel Script
  1. // -----[Client-Side]-----
  2.  
  3. addCommand("quit",function(arg) // our function must have it's own argument. argument "arg" is command parameters
  4. {
  5. exitGame()
  6. })
  7.  
  8. addCommand(["q","quit","exit"],function(arg) // we have registered one function in three diffrent commands
  9. {
  10. exitGame()
  11. })
  12.  
  13. // -----[Server-Side]-----
  14.  
  15. addCommand("ban",function(pid,arg) // our function must have two arguments. First is the player id that typed the command, second is command parameters.
  16. {
  17. if (arg != "")
  18. {
  19. arg = sscanf("dds",arg) || sscanf("dd",arg) || sscanf("d",arg) // we can add some "optional" command parameters, this is the example of it.
  20.  
  21. if (type(arg) == "array")
  22. {
  23. if (2 in arg) // checking if arg[2] value exists
  24. {
  25. sendMessageToAll(255,0, 0,"Id"+pid+" banned id "+arg[0]+" for "+arg[1]+"minutes. Reason: "+arg[2])
  26. ban(pid,arg[0],arg[1])
  27. }
  28. else if (1 in arg) // checking if arg[1] value exists
  29. {
  30. sendMessageToAll(255,0, 0,"Id "+pid+" banned id "+arg[0]+" for "+arg[1]+"minutes.")
  31. ban(pid,arg[0],arg[1],"none")
  32. }
  33. else
  34. {
  35. sendMessageToAll(255,0, 0,"Id "+pid+" banned id "+arg[0]+".")
  36. ban(pid,-1,"none")
  37. }
  38. }
  39. else
  40. {
  41. sendMessageToPlayer(pid,255,0, 0,"Wrong syntax, <pid> or <minutes> argument must be integer!")
  42. }
  43. }
  44. else
  45. {
  46. sendMessageToPlayer(pid,255,0, 0,"Wrong syntax, type: /ban (pid) <minutes> <reason>") // command parameters in () are required, but parameters <> are optional.
  47. }
  48. })



*Removing command(s):

Squirrel Script
  1. // -----[Client-Side]-----
  2.  
  3. function tp1() // creating some function
  4. {
  5. setPlayerPosition(heroId,0,0,0)
  6. }
  7.  
  8. addCommand("test",tp1) // we are hooking tp1 function into "test" command
  9.  
  10. addCommand("test",function(arg) // we can add more than one function into our command
  11. {
  12. setPlayerAngle(heroId,0)
  13. })
  14.  
  15. removeCommand("test",tp1) // this line removes only tp1 function from "test" command (function tp1 still exists)
  16.  
  17. // -----[Server-Side]-----
  18.  
  19. addCommand(["print","type"],function(pid,arg)
  20. {
  21. print(pid+" typed: "+arg)
  22. }
  23.  
  24. addCommand(["print","type"],function(pid,arg)
  25. {
  26. print(pid+" name is "+getPlayerName(pid))
  27. }
  28.  
  29. removeCommand(["print","type"]) // this line removes all hooks from commands "print" and "type", we can remove more than one command


Reply
#2
Script has been updated. I've removed some repeated things in code.
Reply
#3
Thread (not script) has been updated.
Reply
#4
Script has been updated!

I've made some simple optimalizations in it, basically i've removed if statement from foreach loop in onPlayerCommand, and remove unnecessary loop. Also i'm now binding specific function based on script side, that's why i don't need if statement.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [New G2O][Client] Free Camera Patrix 2 1,325 06.05.2020, 17:53
Last Post: Patrix
  [New G2O][Shared] OOP Area Manager Patrix 4 3,971 21.02.2019, 08:35
Last Post: Patrix
  [New G2O][Client-Side] bindKey Patrix 6 3,979 14.11.2018, 01:32
Last Post: Patrix
  [New G2O][Client-Side] OOP Camera Movement Patrix 1 1,796 20.10.2018, 13:22
Last Post: Patrix
  [New G2O][Shared] packetListener Patrix 3 2,951 20.06.2018, 17:43
Last Post: Tommy
  [G2O v.0.1.2][Server-Side] NPC Manager Quarchodron 16 7,779 21.01.2018, 18:59
Last Post: Quarchodron
  [New G2O][Client-Side] Key Combination Patrix 1 1,607 23.12.2017, 12:16
Last Post: Patrix
  [G2O v.0.1.0][Client-Side] Notice Board Quarchodron 0 1,337 27.06.2017, 13:28
Last Post: Quarchodron

Forum Jump:


Users browsing this thread: 1 Guest(s)