Advertisement
Guest User

VC:MP TeamSystem [UNTESTED CODE]

a guest
Oct 28th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 19.97 KB | None | 0 0
  1. // VC:MP TeamSystem [UNTESTED CODE]
  2. // ------------------------------------------------------------------------------------------------
  3. // Global table used to scope script commands
  4. _CMD <- {}
  5.  
  6. // ------------------------------------------------------------------------------------------------
  7. // Global enumeration to hold the command syntax
  8. enum CmdSyn
  9. {
  10.     TeamUp = "!teamup <partener-name>",
  11.     SplitUp = "!splitup",
  12.     DenyReq = "!denyreq <partener-name>",
  13.     DenyAll = "!denyall",
  14.     AcceptReq = "!acceptreq <partener-name>",
  15.     FlushReq = "!flushreq",
  16.     PlaySolo = "!playsolo",
  17.     Partner = "!partner"
  18. }
  19.  
  20. // ------------------------------------------------------------------------------------------------
  21. // Global enumeration to hold the message style and colours
  22. enum MsgTag {
  23.     Dbg = "[#11455D][DEBUG][#F4F3EE]",
  24.     Msg = "[#64A5BB][MESSAGE][#F4F3EE]",
  25.     Scs = "[#698433][SUCCESS][#F4F3EE]",
  26.     Inf = "[#FFD35C][INFO][#F4F3EE]",
  27.     Wrn = "[#FF8202][WARNING][#F4F3EE]",
  28.     Err = "[#EA3E70][ERROR][#F4F3EE]",
  29.     Ftl = "[#C82C3A][FATAL][#F4F3EE]"
  30. }
  31.  
  32. // ------------------------------------------------------------------------------------------------
  33. // Scoped functions used to send formatted messages to players
  34. _Msg <- {
  35.     // --------------------------------------------------------------------------------------------
  36.     function Client(...)
  37.     {
  38.         local i_player = vargv[0], r = vargv[1], g = vargv[2], b = vargv[3];
  39.         vargv = vargv.slice(3);
  40.         vargv[0] = this;
  41.         local str = ::format.acall(vargv);
  42.         ClientMessage(str, i_player, r, g, b);
  43.     }
  44.     // --------------------------------------------------------------------------------------------
  45.     function ClientAlpha(...)
  46.     {
  47.         local i_player = vargv[0], r = vargv[1], g = vargv[2], b = vargv[3], a = vargv[4];
  48.         vargv = vargv.slice(4);
  49.         vargv[0] = this;
  50.         local str = ::format.acall(vargv);
  51.         ClientMessage(str, i_player, r, g, b, a);
  52.     }
  53.     // --------------------------------------------------------------------------------------------
  54.     function ClientAll(...)
  55.     {
  56.         local r = vargv[0], g = vargv[1], b  = vargv[2];
  57.         vargv = vargv.slice(2);
  58.         vargv[0] = this;
  59.         local str = ::format.acall(vargv);
  60.         ClientMessageToAll(str, r, g, b);
  61.     }
  62.     // --------------------------------------------------------------------------------------------
  63.     function ClientAllAlpha(...)
  64.     {
  65.         local r = vargv[0], g = vargv[1], b = vargv[2], a = vargv[3];
  66.         vargv = vargv.slice(3);
  67.         vargv[0] = this;
  68.         local str = ::format.acall(vargv);
  69.         ClientMessageToAll(str, r, g, b, a);
  70.     }
  71.     // --------------------------------------------------------------------------------------------
  72.     function Game(...)
  73.     {
  74.         local i_player = vargv[0], r = vargv[1], g = vargv[2], b = vargv[3];
  75.         vargv = vargv.slice(3);
  76.         vargv[0] = this;
  77.         local str = ::format.acall(vargv);
  78.         Announce(str, i_player, r, g, b);
  79.     }
  80.     // --------------------------------------------------------------------------------------------
  81.     function GameAlpha(...)
  82.     {
  83.         local i_player = vargv[0], r = vargv[1], g = vargv[2], b = vargv[3], a = vargv[4];
  84.         vargv = vargv.slice(4);
  85.         vargv[0] = this;
  86.         local str = ::format.acall(vargv);
  87.         Announce(str, i_player, r, g, b, a);
  88.     }
  89.     // --------------------------------------------------------------------------------------------
  90.     function GameAll(...)
  91.     {
  92.         local r = vargv[0], g = vargv[1], b = vargv[2];
  93.         vargv = vargv.slice(2);
  94.         vargv[0] = this;
  95.         local str = ::format.acall(vargv);
  96.         AnnounceAll(str, r, g, b);
  97.     }
  98.     // --------------------------------------------------------------------------------------------
  99.     function GameAllAlpha(...)
  100.     {
  101.         local r = vargv[0], g = vargv[1], b = vargv[2], a = vargv[3];
  102.         vargv = vargv.slice(3);
  103.         vargv[0] = this;
  104.         local str = ::format.acall(vargv);
  105.         AnnounceAll(str, r, g, b, a);
  106.     }
  107.     // --------------------------------------------------------------------------------------------
  108.     function Player(...)
  109.     {
  110.         local i_player = vargv[0];
  111.         vargv[0] = this;
  112.         local str = ::format.acall(vargv);
  113.         MessagePlayer(str, i_player);
  114.     }
  115. }
  116.  
  117. // ------------------------------------------------------------------------------------------------
  118. _ClientPool <- []
  119.  
  120. // ------------------------------------------------------------------------------------------------
  121. _RequestPool <- []
  122.  
  123. // ------------------------------------------------------------------------------------------------
  124. // Class responsible for management and identification of player instances
  125. class _Client
  126. {
  127.     // --------------------------------------------------------------------------------------------
  128.     function constructor(i_player)
  129.     {
  130.         // Verify the player instance
  131.         if (typeof i_player != "instance") return null;
  132.         // Store the player instance
  133.         else Instance = i_player
  134.     }
  135.     // --------------------------------------------------------------------------------------------
  136.     function Connect()
  137.     {
  138.         // Ignored....
  139.     }
  140.     // --------------------------------------------------------------------------------------------
  141.     function Disconnect()
  142.     {
  143.         // See if the player had any partner
  144.         if (Partner != null) {
  145.             // Notify the partner about the split-up
  146.             _Msg.Player(Partner, @"%s %s has disconnected and you no longer have a partner.", MsgTag.Inf, Instance.Name);
  147.             // Free the partner from used
  148.             _ClientPool[Partner.ID].Partner = null;
  149.             // Clear any stored instances
  150.             Partner = null;
  151.         }
  152.         // See if there's any requests made by this instance and clear them
  153.         if (_RequestPool[Instance.ID] != null) {
  154.             // Notify the partner about the result
  155.             _Msg.Player(_RequestPool[Instance.ID].Target, @"%s %s has disconnected and his team-up request was flushed.", MsgTag.Inf, Instance.Name);
  156.             // Remove the request from the pool/list
  157.             _RequestPool[Instance.ID] = null;
  158.         }
  159.         // See if there's any requests made to this instance and clear them
  160.         foreach (idx, req in _RequestPool) {
  161.             // See if this is a valid request
  162.             if (req != null && req.Target.ID == Instance.ID) {
  163.                 // Notify the partner about the rejected request
  164.                 _Msg.Player(req.Invoker, @"%s %s has disconnected and cannot accept team-up request.", MsgTag.Inf, i_player.Name);
  165.                 // Clear the request from the pool/list
  166.                 _RequestPool[idx] = null;
  167.             }
  168.         }
  169.         // Clear any stored instances
  170.         Instance = null;
  171.     }
  172.     // --------------------------------------------------------------------------------------------
  173.     Instance = null
  174.     // --------------------------------------------------------------------------------------------
  175.     Partner = null
  176.     // --------------------------------------------------------------------------------------------
  177.     PlaySolo = false
  178. }
  179.  
  180. // ------------------------------------------------------------------------------------------------
  181. _CMD.TeamUp <- function(i_player, args)
  182. {
  183.     // See if there isn't already a request in the pool and send a notice to the invoker to flush his previous request
  184.     if (_RequestPool[i_player.ID] != null) return _Msg.Player(i_player, @"%s Please flush your previous request to:  %s", MsgTag.Scs, _RequestPool[i_player.ID].Target.Name);
  185.  
  186.     // See if we are dealing with a valid command
  187.     if (typeof args != "string" || args.len() <= 0) return _Msg.Player(i_player, @"%s Syntax: %s", MsgTag.Inf, CmdSyn.TeamUp);
  188.     // Strip the command from unnecessary space characters
  189.     args = strip(args);
  190.    
  191.     // Check one more time if we still have a valid command
  192.     if (typeof args != "string" || args.len() <= 0) return _Msg.Player(i_player, @"%s Syntax: %s", MsgTag.Inf, CmdSyn.TeamUp);
  193.     // Try to find the target instance by name first
  194.     local i_partner = FindPlayer(args);
  195.    
  196.     // See if we found a valid player instance for our partner
  197.     if (typeof i_partner != "instance") return _Msg.Player(i_player, @"%s There's no player on the server identified by: %s", MsgTag.Err, args);
  198.     // See if the invoker tries to team up with him self
  199.     else if (i_player.ID == i_partner.ID) return _Msg.Player(i_player, @"%s You cannot team up with your self.", MsgTag.Wrn);
  200.     // See if the invoker has the option to play solo enabled
  201.     else if (_ClientPool[i_player.ID].PlaySolo) return _Msg.Player(i_player, @"%s You already chose to play solo.", MsgTag.Wrn);
  202.     // See if the partner has the option to play solo enabled
  203.     else if (_ClientPool[i_partner.ID].PlaySolo) return _Msg.Player(i_player, @"%s %s chose to play solo.", MsgTag.Wrn, i_partner.Name);
  204.     // See if the invoker is already in a team
  205.     else if (_ClientPool[i_player.ID].Partner != null) return _Msg.Player(i_player, @"%s You are already teamed-up with: %s", MsgTag.Wrn, _ClientPool[i_player.ID].Partner.Name);
  206.     // See if the requested partner is already in a team
  207.     else if (_ClientPool[i_partner.ID].Partner != null) return _Msg.Player(i_player, @"%s %s is already teamed-up with: %s", MsgTag.Wrn, i_partner.Name, _ClientPool[i_partner.ID].Partner.Name);
  208.     // Submit the request to the pool
  209.     _RequestPool[i_player.ID] = {Invoker = i_player, Target = i_partner, Timeout = (time()+30)};
  210.     // Notify the requested partner
  211.     _Msg.Player(i_partner, @"%s %s would like to team-up with you.", MsgTag.Inf, i_player.Name);
  212.     // Notify the invoker about his request
  213.     _Msg.Player(i_player, @"%s %s successfully received your team-up request.", MsgTag.Scs, i_partner.Name);
  214. }
  215.  
  216. // ------------------------------------------------------------------------------------------------
  217. _CMD.SplitUp <- function(i_player, args)
  218. {
  219.     // See if the invoker is in a team
  220.     if (_ClientPool[i_player.ID].Partner != null) {
  221.         // Inform the invoker about his action
  222.         _Msg.Player(i_player, @"%s You split-up from: %s", MsgTag.Scs, _ClientPool[i_player.ID].Partner.Name);
  223.         // Send a notice to the partner
  224.         _Msg.Player(_ClientPool[i_player.ID].Partner, @"%s %s split-up from you.", MsgTag.Inf, i_player.Name);
  225.         // Remove the link from the partner
  226.         _ClientPool[_ClientPool[i_player.ID].Partner.ID].Partner = null;
  227.         // Remove the link from the invoker
  228.         _ClientPool[i_player.ID].Partner = null;
  229.     // Notify the invoker that he doesn't have a team to split-up
  230.     } else _Msg.Player(i_player, @"%s You don't have a team to split-up.", MsgTag.Wrn);
  231. }
  232.  
  233. // ------------------------------------------------------------------------------------------------
  234. _CMD.DenyReq <- function(i_player, args)
  235. {
  236.     // See if we are dealing with a valid command
  237.     if (typeof args != "string" || args.len() <= 0) return _Msg.Player(i_player, @"%s Syntax: %s", MsgTag.Inf, CmdSyn.DenyReq);
  238.     // Strip the command from unnecessary space characters
  239.     args = strip(args);
  240.    
  241.     // Check one more time if we still have a valid command
  242.     if (typeof args != "string" || args.len() <= 0) return _Msg.Player(i_player, @"%s Syntax: %s", MsgTag.Inf, CmdSyn.DenyReq);
  243.     // Try to find the target instance by name first
  244.     local i_partner = FindPlayer(args);
  245.    
  246.     // See if we found a valid player instance for our partner
  247.     if (typeof i_partner != "instance") return _Msg.Player(i_player, @"%s There's no player on the server identified by: %s", MsgTag.Err, args);
  248.    
  249.     // Notify the partner about the rejected request
  250.     _Msg.Player(i_partner, @"%s %s rejected your team-up request.", MsgTag.Inf, i_player.Name);
  251.     // Notify the invoker about the rejected request
  252.     _Msg.Player(i_player, @"%s You successfully rejected the team-up request from %s", MsgTag.Scs, i_partner.Name);
  253.     // Flush the request from the pool/list
  254.     _RequestPool[i_partner.ID] = null;
  255. }
  256.  
  257. // ------------------------------------------------------------------------------------------------
  258. _CMD.DenyAll <- function(i_player, args)
  259. {
  260.     // Go through each element in the request pool and clear it if it's addressed to the invoker
  261.     foreach (idx, req in _RequestPool) {
  262.         // See if this is a valid request
  263.         if (req != null && req.Target.ID == i_player.ID) {
  264.             // Notify the partner about the rejected request
  265.             _Msg.Player(req.Invoker, @"%s %s rejected your team-up request.", MsgTag.Inf, i_player.Name);
  266.             // Notify the invoker about the rejected request
  267.             _Msg.Player(i_player, @"%s You successfully rejected the team-up request from %s", MsgTag.Scs, req.Invoker.Name);
  268.             // Clear the request from the pool/list
  269.             _RequestPool[idx] = null;
  270.         }
  271.     }
  272. }
  273.  
  274. // ------------------------------------------------------------------------------------------------
  275. _CMD.AcceptReq <- function(i_player, args)
  276. {
  277.     // See if we are dealing with a valid command
  278.     if (typeof args != "string" || args.len() <= 0) return _Msg.Player(i_player, @"%s Syntax: %s", MsgTag.Inf, CmdSyn.AcceptReq);
  279.     // Strip the command from unnecessary space characters
  280.     args = strip(args);
  281.    
  282.     // Check one more time if we still have a valid command
  283.     if (typeof args != "string" || args.len() <= 0) return _Msg.Player(i_player, @"%s Syntax: %s", MsgTag.Inf, CmdSyn.AcceptReq);
  284.     // Try to find the target instance by name first
  285.     local i_partner = FindPlayer(args);
  286.    
  287.     // See if we found a valid player instance for our partner
  288.     if (typeof i_partner != "instance") return _Msg.Player(i_player, @"%s There's no player on the server identified by: %s", MsgTag.Err, args);
  289.    
  290.     // Notify the invoker about the result
  291.     _Msg.Player(i_partner, @"%s %s accepted your team up request.", MsgTag.Inf, i_player.Name);
  292.     // Notify the partner about the result
  293.     _Msg.Player(i_player, @"%s You accepted the team up request from %s", MsgTag.Scs, i_partner.Name);
  294.     // Remove this request from the pool/list
  295.     _RequestPool[i_partner.ID] = null;
  296.     // Link the players together
  297.     _ClientPool[i_player.ID].Partner = i_partner;
  298.     _ClientPool[i_partner.ID].Partner = i_player;
  299.    
  300.     // A variable to count the rest of the requests
  301.     local req_count = 0;
  302.     // Go through each element in the request pool and clear it if it's addressed to the invoker
  303.     foreach (idx, req in _RequestPool) {
  304.         // See if this is a valid request
  305.         if (req != null && req.Target.ID == i_player.ID) {
  306.             // Notify the partner about the rejected request
  307.             _Msg.Player(req.Invoker, @"%s %s rejected your team-up request.", MsgTag.Inf, i_player.Name);
  308.             // Increase the request counter
  309.             req_count++;
  310.             // Clear the request from the pool/list
  311.             _RequestPool[idx] = null;
  312.         }
  313.     }
  314.     // Notify the invoker about the rest of the requests
  315.     _Msg.Player(i_player, @"%s You successfully rejected the rest of the requests (%d).", MsgTag.Inf, req_count);
  316. }
  317.  
  318. // ------------------------------------------------------------------------------------------------
  319. _CMD.FlushReq <- function(i_player, args)
  320. {
  321.     // See if there's a request in the pool/list
  322.     if (_RequestPool[i_player.ID] != null) {
  323.         // Inform the invoker about his action
  324.         _Msg.Player(i_player, @"%s You revoked your team-up request to:  %s", MsgTag.Scs, _RequestPool[i_player.ID].Target.Name);
  325.         // Send a notice to the target
  326.         _Msg.Player(_RequestPool[i_player.ID].Target, @"%s %s revoked his team-up request.", MsgTag.Inf, i_player.Name);
  327.         // Flush the request
  328.         _RequestPool[i_player.ID] = null;
  329.     // Notify the invoker that there is no previous request
  330.     } else _Msg.Player(i_player, @"%s You don't have any team-up request.", MsgTag.Wrn);
  331. }
  332.  
  333. // ------------------------------------------------------------------------------------------------
  334. _CMD.PlaySolo <- function(i_player, args)
  335. {
  336.     // See if the invoker isn't in a team already
  337.     if (_ClientPool[i_player.ID].Partner != null) return _Msg.Player(i_player, @"%s You need to split-up from %x", MsgTag.Wrn, _ClientPool[i_player.ID].Partner.Name);
  338.     // See if the invoker has this option enabled or not
  339.     if (_ClientPool[i_player.ID].PlaySolo) {
  340.         // Deactivate the play solo option
  341.         _ClientPool[i_player.ID].PlaySolo = false;
  342.         // Notify the invoker about his action
  343.         _Msg.Player(i_player, @"%s You successfully deactivated the play solo option.", MsgTag.Scs);
  344.     } else {
  345.         // Activate the play solo option
  346.         _ClientPool[i_player.ID].PlaySolo = true;
  347.         // Notify the invoker about his action
  348.         _Msg.Player(i_player, @"%s You successfully activated the play solo option.", MsgTag.Scs);
  349.     }
  350. }
  351.  
  352. // ------------------------------------------------------------------------------------------------
  353. _CMD.Partner <- function(i_player, args)
  354. {
  355.     // See if the invoker has a partner or not
  356.     if (_ClientPool[i_player.ID].Partner != null) {
  357.         // Inform the invoker about his partner
  358.         _Msg.Player(i_player, @"%s Your partner is %s", MsgTag.Inf, _ClientPool[i_player.ID].Partner.Name);
  359.     // Notify the invoker that he doesn't have a partner
  360.     } else _Msg.Player(i_player, @"%s You don't have a partner.", MsgTag.Wrn);
  361. }
  362.  
  363. // ------------------------------------------------------------------------------------------------
  364. // Processes all the active team-up requests
  365. function _RequestProcess()
  366. {
  367.     // Get the current time
  368.     local cur_time = time();
  369.     // Loop through all the queued requests
  370.     foreach (idx, req in _RequestPool) {
  371.         // See if this is a valid request
  372.         if (req != null && req.Timeout <= cur_time) {
  373.             // Notify the invoker about the expired request
  374.             _Msg.Player(req.Invoker, @"%s Your team-up request to %s has expired.", MsgTag.Inf, req.Target.Name);
  375.             // Clear the request from the pool/list
  376.             _RequestPool[idx] = null;
  377.         }
  378.     }
  379. }
  380.  
  381. // ------------------------------------------------------------------------------------------------
  382. function onServerStart()
  383. {
  384.     // Reserve enough space to hold the maximum number of Clients that the server can hold
  385.     _ClientPool = array(GetMaxPlayers());
  386.     // Reserve enough space to hold the maximum number of Clients that the server can hold
  387.     _RequestPool = array(GetMaxPlayers());
  388.     // Attempt to find any previously connected Clients in case of a script reload
  389.     foreach (idx, inst in _ClientPool) {
  390.         // Attempt to find the player by it's unique identifier
  391.         local res = FindPlayer(idx);
  392.         // See if a valid player instance was found at this ID
  393.         if (res != null) {
  394.             // Create a Client instance for this player instance
  395.             inst = _Client(res);
  396.             // Allow the Client instance to initialize
  397.             inst.Connect();
  398.         }
  399.     }
  400.     // Start the request cleaning timer
  401.     NewTimer("_RequestProcess", 5000, 0);
  402. }
  403.  
  404. // ------------------------------------------------------------------------------------------------
  405. function onServerStop()
  406. {
  407.     // Attempt to de-initialize all connected players from the server
  408.     foreach (inst in _ClientPool) {
  409.         // See if there was any Client instance in this slot
  410.         if (inst != null) {
  411.             // Allow the Client instance to de-initialize
  412.             inst.Disconnect();
  413.             // Clear the slot
  414.             _ClientPool[i_player.ID] = null;
  415.         }
  416.     }
  417.     // Clear all the team-up requests
  418.     foreach (idx, req in _RequestPool) _RequestPool[idx] = null;
  419. }
  420.  
  421. // ------------------------------------------------------------------------------------------------
  422. function onPlayerJoin(i_player)
  423. {
  424.     // Create a Client instance for this player instance
  425.     _ClientPool[i_player.ID] = _Client(i_player);
  426.     // Allow the newly created Client instance to initialize
  427.     _ClientPool[i_player.ID].Connect();
  428. }
  429.  
  430. // ------------------------------------------------------------------------------------------------
  431. function onPlayerPart(i_player)
  432. {
  433.     // Allow the Client instance to de-initialize
  434.     _ClientPool[i_player.ID].Disconnect();
  435.     // Clear the slot for other Clients
  436.     _ClientPool[i_player.ID] = null;
  437. }
  438.  
  439. // ------------------------------------------------------------------------------------------------
  440. function onPlayerCommand(i_player, cmd, args)
  441. {
  442.     switch (cmd)
  443.     {
  444.         // ----------------------------------------------------------------------------------------
  445.         case "teamup":      _CMD.TeamUp(i_player, args); break;
  446.         case "splitup":     _CMD.SplitUp(i_player, args); break;
  447.         // ----------------------------------------------------------------------------------------
  448.         case "denyreq":     _CMD.DenyReq(i_player, args); break;
  449.         case "denyall":     _CMD.DenyAll(i_player, args); break;
  450.         case "acceptreq":   _CMD.AcceptReq(i_player, args); break;
  451.         // ----------------------------------------------------------------------------------------
  452.         case "flushreq":    _CMD.FlushReq(i_player, args); break;
  453.         case "playsolo":    _CMD.PlaySolo(i_player, args); break;
  454.         case "partner":     _CMD.Partner(i_player, args); break;
  455.         // ----------------------------------------------------------------------------------------
  456.         default: _Msg.Player(i_player, "%s Unknown command: %s %s", MsgTag.Err, cmd, args);
  457.     }
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement