Team System v1.0.2

Started by Fjose, Oct 21, 2014, 03:16 AM

Previous topic - Next topic

Fjose

#30
PTS v1.0.1
- FindPlr() was removed, all functions/cmds uses now FindPlayer()
- onPlayerPart modified.
- all commands were updated.
- other issues were fixed.

everybody should update their script with this version.

.

#31
NOTE: This is untested code! If you use this please make sure to test it first.

// Moved to PasteBin to preserve formatting: http://pastebin.com/F9j0EUtj
.

Drake

Why the fuck doesn't it works for me!!!!!!!

Now Developers can only Fix the Problem......

soulshaker

Just tried the code with seby's blank server and got the some errors:-

Error on Console:-



line 385:-



line 133:-




.

#34
Try using this version of the GetTok() and NumTok() functions. They do various checks on the passed arguments before using them so they might be safer.

function GetTok(str, sep, pos, len = null)
{
    // First round is parameter validation
    if (typeof str != "string" || str.len() < 1) return ""; // Not a valid string
    else if (typeof sep != "string" || sep.len() < 1) return ""; // Not a valid separator
    // Split the string into tokens
    local tok = split(str, sep);
    // Second round is result validation
    if (typeof tok != "array") return str; // Maybe an internal error
    else if (tok.len() == 1 && tok[0] == str) return tok[0]; // No tokens we're found
    // Third round is position parameter adjustment
    if (typeof pos != "integer")
    {
        if (typeof pos == "float" || typeof pos == "string") pos = pos.tointeger();
        else return ""; // Invalid position
    }
    // Make sure the position is in bounds
    if (pos >= tok.len()) return ""; // The position is out of bounds
    else if (pos < 0) pos = 0;
    // Fourth round is length parameter adjustment
    if (len == null)
        len = (tok.len() - pos);
    else if (typeof len != "integer")
    {
        if (typeof len == "float" || typeof len == "string") len = len.tointeger();
        else return ""; // Invalid length
    }
    // Make sure the length is in bounds
    if ((pos+len) > tok.len()) len = (tok.len() - pos);
    else if (len <= 0) return ""; // The length is out of bounds or invalid
    // Retrieve the required tokens
    tok = tok.slice(pos, pos+len);
    // Make sure we don't go into a loop for nothing
    if (tok.len() > 1)
    {
        // Create a temporary variable to hold the generated string
        local res = tok[0];
        // Remove the element that we just used
        tok.remove(0);
        // Implode those tokens using the specified separator
        foreach (tk in tok) res += sep + tk;
        // Return the result
        return res;
    }
    // Just return the requested token
    else if (tok.len() == 1) return tok[0];
    // The array slicing failed somehow
    else return "";
}

function NumTok(str, sep)
{
    // First round is parameter validation
    if (typeof str != "string" || str.len() < 1) return 0; // Not a valid string
    else if (typeof sep != "string" || sep.len() < 1) return 0; // Token must be a string
    // Split the string into tokens
    local tok = split(str, sep);
    // Second round is result validation
    if (typeof tok != "array") return 0; // Maybe an internal error
    else if (tok.len() == 1 && tok[0] == str) return 0; // No tokens we're found
    else return tok.len(); // Return the number of found tokens
}

Be aware that they work a little different and the rest of the code might have to be adjusted to work with them:
function GetTok(str, sep, pos, len = null);
pos: Is the position where you want your string slicing to start. If you set it to 0 it will start from where the first separator is encountered. If it's set to 1 it will start from where the second separator is encountered. If it's set to 2 it will start from where the third separator is encountered. And so on...

len: Is the position where you want your string slicing to end. If you set it to 0 it return an empty string because that means to return no token. If it's set to 1 it'll end where the first separator is encountered after the start. If it's set to 2 it'll end where the second separator is encountered after the start. And so on...
Setting the len to null or simply omitting it will return everything from the start position until the last separator is encountered.

Here are some tests and the expected results:
local text = "param1 arg2 param3 val4 blah blah blah";

GetTok(text, " ", 0);
// Returns: "param1 arg2 param3 val4 blah blah blah"

GetTok(text, " ", 1);
// Returns: "arg2 param3 val4 blah blah blah"

GetTok(text, " ", 2);
// Returns: "param3 val4 blah blah blah"

GetTok(text, " ", 0, 2);
// Returns: "param1 arg2"

GetTok(text, " ", 2, 4);
// Returns: "param3 val4 blah"

GetTok(text, " ", 67);
// Returns: "" (Out of bounds)

GetTok(text, " ", -242, 4);
// Returns: "param1 arg2 param3 val4" (Got converted to 0)

GetTok(text, " ", 2, -23);
// Returns: "" (Out of bounds)

GetTok(text, "#", 2, -23);
// Returns: "param1 arg2 param3 val4 blah blah blah" (Separator not found)

GetTok(text, " ", "3", 3.2454);
// Returns: "val4 blah blah" (Automatically converted to integer)

GetTok(text, 261, "3", 3.2454);
// Returns: "" (Not a valid separator)

GetTok(null, " ", "3", 3.2454);
// Returns: "" (Not a valid string)
.

soulshaker

Thanks SLC that worked :D

@Fjose:-
Quote from: Fjose on Oct 21, 2014, 03:16 AMfunction onPlayerPart( player, reason )
{
   if ( pinfo[ player.ID ].Team == true )
   {
      local plr = FindPlayer( pinfo[ player.ID ].Partner );
      if ( plr ) {
         PrivMessage( plr, "[!] " + player.Name + " left the team." );
         pinfo[ plr.ID ].Partner = null;
         pinfo[ plr.ID ].Team = true;
      }
   }
   pinfo[ player.ID ] = null;
}
it must be false or the player will be in team always. Anyways you can update the script with this one:-

Functions:-
class PlayerInfo
{
Team = false;
Request = false;
TimeRequest = 0;
Partner = null;
}

function onScriptLoad()
{
pinfo <- array( GetMaxPlayers(), null );
}

function onPlayerJoin( player )
{
pinfo[ player.ID ] = PlayerInfo();
}

function onPlayerPart( player, reason )
{
if ( pinfo[ player.ID ].Team == true )
{
local plr = FindPlayer( pinfo[ player.ID ].Partner );
if ( plr )
{
PrivMessage( plr, "[!] " + player.Name + " left the team." );
pinfo[ plr.ID ].Partner = null;
pinfo[ plr.ID ].Team = false;
}
}
 pinfo[ player.ID ] = null;
}

function onPlayerSpawn( player )
{
if ( pinfo[ player.ID ].Team == true )
{
local plr = FindPlayer( pinfo[ player.ID ].Partner );
if ( plr ) player.Pos = plr.Pos;
}
}

function GetTok(str, sep, pos, len = null)
{
 if (typeof str != "string" || str.len() < 1) return "";
 else if (typeof sep != "string" || sep.len() < 1) return "";
 local tok = split(str, sep);
 if (typeof tok != "array") return str;
 else if (tok.len() == 1 && tok[0] == str) return tok[0];
 if (typeof pos != "integer")
 {
  if (typeof pos == "float" || typeof pos == "string") pos = pos.tointeger();
  else return "";
 }
 if (pos >= tok.len()) return "";
 else if (pos < 0) pos = 0;
 if (len == null)
  len = (tok.len() - pos);
 else if (typeof len != "integer")
 {
  if (typeof len == "float" || typeof len == "string") len = len.tointeger();
  else return "";
 }
 if ((pos+len) > tok.len()) len = (tok.len() - pos);
 else if (len <= 0) return "";
 tok = tok.slice(pos, pos+len);
 if (tok.len() > 1)
 {
  local res = tok[0];
  tok.remove(0);
  foreach (tk in tok) res += sep + tk;
  return res;
 }
 else if (tok.len() == 1) return tok[0];
 else return "";
}

function NumTok(str, sep)
{
 if (typeof str != "string" || str.len() < 1) return 0;
 else if (typeof sep != "string" || sep.len() < 1) return 0;
 local tok = split(str, sep);
 if (typeof tok != "array") return 0;
 else if (tok.len() == 1 && tok[0] == str) return 0;
 else return tok.len();
}

Commands:-
function onPlayerCommand( player, cmd, text )
{
local plr, i = 0;
if ( text )
{
plr = FindPlayer( text );
i = NumTok(text, " ").tointeger();
}
if ( cmd == "team" )
    {
  local plr = FindPlayer( GetTok( text, " ", 1 ) );
  if ( pinfo[ player.ID ].Team == true ) PrivMessage( player, "[Error] You already have a team." );
  else if ( !text ) PrivMessage( player, "Syntax: /team <player name or id>" );
  else if ( !plr ) PrivMessage( player, "[Error] Unknown player." );
  else if ( pinfo[ plr.ID ].Team == true ) PrivMessage( player, "[Error] " + plr.Name + " is already in a team." );
  else if ( pinfo[ player.ID ].Request == true ) PrivMessage( player, "[Error] You already have a request." );
  else if ( pinfo[ plr.ID ].Request == true ) PrivMessage( player, "[Error] " + plr.Name + " already have a request." );
  else
  {
   if ( pinfo[ player.ID ].TimeRequest < time() ) {
   pinfo[ player.ID ].Request = false; pinfo[ player.ID ].Partner = null;  pinfo[ player.ID ].TimeRequest = 0;
   pinfo[ plr.ID ].Request = false; pinfo[ plr.ID ].Partner = null; pinfo[ plr.ID ].TimeRequest = 0;
   }
   
   PrivMessage( plr, "[!] " + player.Name + " would like to team up with you." );
   PrivMessage( player, "Request submitted." );
   pinfo[ player.ID ].Partner = plr.ID;
   pinfo[ plr.ID ].Partner = player.ID;
   pinfo[ player.ID ].TimeRequest = time() + 30;
   pinfo[ plr.ID ].TimeRequest = time() + 30;
   pinfo[ plr.ID ].Request = true;
   pinfo[ player.ID ].Request = true;
  }
 }
 
 else if ( cmd == "accept" )
 {
  local plr = FindPlayer( pinfo[ player.ID ].Partner );
  if ( pinfo[ player.ID ].Request == false ) PrivMessage( player, "[Error] You don't have a request." );
  else if ( pinfo[ player.ID ].Team == true ) PrivMessage( player, "[Error] You already have a team-mate." );
  else
  {
   if ( pinfo[ player.ID ].TimeRequest < time() ) {
   PrivMessage( player, "[Error] Time ended." );
   pinfo[ player.ID ].Request = false;
   pinfo[ player.ID ].Partner = null;
   pinfo[ plr.ID ].Request = false;
   pinfo[ plr.ID ].Partner = null;
   pinfo[ player.ID ].TimeRequest = 0;
   pinfo[ plr.ID ].TimeRequest = 0;
      } else {
   
   PrivMessage( plr, "[!] " + player.Name + " is doing team with you." );
   PrivMessage( player, "Request Accepted! You are now teaming with " + plr.Name );
   pinfo[ player.ID ].Team = true;
   pinfo[ plr.ID ].Team = true;
   pinfo[ player.ID ].Request = false;
   pinfo[ plr.ID ].Request = false;
   pinfo[ player.ID ].TimeRequest = 0;
   pinfo[ plr.ID ].TimeRequest = 0;
   player.Pos = plr.Pos;
   }
  }
 }
   
 else if ( cmd == "deny" )
 {
  local plr = FindPlayer( pinfo[ player.ID ].Partner );
  if ( pinfo[ player.ID ].Request == false ) PrivMessage( player, "[Error] You don't have a request." );
  else
  {
     if ( pinfo[ player.ID ].TimeRequest < time() ) {
   PrivMessage( player, "[Error] Time ended." );
   pinfo[ player.ID ].Request = false;
   pinfo[ plr.ID ].Request = false;
   pinfo[ player.ID ].Partner = null;
   pinfo[ plr.ID ].Partner = null;
   pinfo[ player.ID ].TimeRequest = 0;
   pinfo[ plr.ID ].TimeRequest = 0;
     } else {
     
     PrivMessage( plr, "[!] " + plr.Name + " denied your request." );
     PrivMessage( player, "Request Denied." );
     pinfo[ player.ID ].Request = false;
     pinfo[ plr.ID ].Request = false;
     pinfo[ player.ID ].TimeRequest = 0;
     pinfo[ plr.ID ].TimeRequest = 0;
     }
  }
 }

 else if ( cmd == "leave" )
 {
  local plr = FindPlayer( pinfo[ player.ID ].Partner );
  if ( pinfo[ player.ID ].Team == false ) PrivMessage( player, "[Error] You don't have team." );
  else
  {
   PrivMessage( plr, "[!] " + player.Name + " left the team." );
   PrivMessage( player, "You are not more team of " + plr.Name );
   pinfo[ player.ID ].Team = false;
   pinfo[ plr.ID ].Team = false;
   pinfo[ player.ID ].Partner = null;
   pinfo[ plr.ID ].Partner = null;
  }
 }
return 1;
}

Fjose

#36
@soulshaker I'll fix the onplayerpart. I'll add the GetTok and NumTok. still I thinking why the gettok mine had problems, incredibly I never had that error.

Did you a change with the cmds?


Script Updated v1.0.2
- GetTok & NumTok were replaced (thanks S.L.C.)
- Fixed a bad declaration when player leaves the server. (thanks souldshaker)

Drake

#37
GetTok and NumTok are fine enough to work.....No need to change that

My problem also Fixed..... I had constants problem nothing else and now its working fine...Thanx Fjose....I will be waiting for another Snippet made by you..:D


soulshaker

#38
Quote from: Fjose on Oct 29, 2014, 02:55 PMDid you a change with the cmds?
nope i didn't, just tried your previous code in a blank script and got those errors, and the functions by SLC worked like a charm :D

Quote from: Fjose on Oct 21, 2014, 03:16 AMClass PlayerInfo {
Team = false;
Request = false;
TimeRequest = 0;
Partner = null;
}

Class gives me a error of "expected (" but class works :)

Drake

Souly ...you are doing something wrong.......As I don't have any problem now

soulshaker

what wrong lol? i already said the script is working( and already said the errors i faced and fixed them)

ThunderStorm

Because there is no text to slice..
if ( cmd == "team" )
    {
  local plr = FindPlayer( GetTok( text, " ", 1 ) ); // Error here
  if ( pinfo[ player.ID ].Team == true ) PrivMessage( player, "[Error] You already have a team." );
  else if ( !text ) PrivMessage( player, "Syntax: /team <player name or id>" );
  else if ( !plr ) PrivMessage( player, "[Error] Unknown player." );
  else if ( pinfo[ plr.ID ].Team == true ) PrivMessage( player, "[Error] " + plr.Name + " is already in a team." );
  else if ( pinfo[ player.ID ].Request == true ) PrivMessage( player, "[Error] You already have a request." );
  else if ( pinfo[ plr.ID ].Request == true ) PrivMessage( player, "[Error] " + plr.Name + " already have a request." );
  else
  {
   if ( pinfo[ player.ID ].TimeRequest < time() ) {
   pinfo[ player.ID ].Request = false; pinfo[ player.ID ].Partner = null;  pinfo[ player.ID ].TimeRequest = 0;
   pinfo[ plr.ID ].Request = false; pinfo[ plr.ID ].Partner = null; pinfo[ plr.ID ].TimeRequest = 0;
   }
   
   PrivMessage( plr, "[!] " + player.Name + " would like to team up with you." );
   PrivMessage( player, "Request submitted." );
   pinfo[ player.ID ].Partner = plr.ID;
   pinfo[ plr.ID ].Partner = player.ID;
   pinfo[ player.ID ].TimeRequest = time() + 30;
   pinfo[ plr.ID ].TimeRequest = time() + 30;
   pinfo[ plr.ID ].Request = true;
   pinfo[ player.ID ].Request = true;
  }
 }

Modify it like

if ( cmd == "team" )
    {
 
  if ( pinfo[ player.ID ].Team == true ) PrivMessage( player, "[Error] You already have a team." );
  else if ( !text ) PrivMessage( player, "Syntax: /team <player name or id>" ); // Checks if there is text.
  else
  {
   local plr = FindPlayer( GetTok( text, " ", 1 ) );
  else if ( !plr ) PrivMessage( player, "[Error] Unknown player." );
  else if ( pinfo[ plr.ID ].Team == true ) PrivMessage( player, "[Error] " + plr.Name + " is already in a team." );
  else if ( pinfo[ player.ID ].Request == true ) PrivMessage( player, "[Error] You already have a request." );
  else if ( pinfo[ plr.ID ].Request == true ) PrivMessage( player, "[Error] " + plr.Name + " already have a request." );
  else
  {
   if ( pinfo[ player.ID ].TimeRequest < time() ) {
   pinfo[ player.ID ].Request = false; pinfo[ player.ID ].Partner = null;  pinfo[ player.ID ].TimeRequest = 0;
   pinfo[ plr.ID ].Request = false; pinfo[ plr.ID ].Partner = null; pinfo[ plr.ID ].TimeRequest = 0;
   }
   
   PrivMessage( plr, "[!] " + player.Name + " would like to team up with you." );
   PrivMessage( player, "Request submitted." );
   pinfo[ player.ID ].Partner = plr.ID;
   pinfo[ plr.ID ].Partner = player.ID;
   pinfo[ player.ID ].TimeRequest = time() + 30;
   pinfo[ plr.ID ].TimeRequest = time() + 30;
   pinfo[ plr.ID ].Request = true;
   pinfo[ player.ID ].Request = true;
   }
   }
 }



Drake

Correct Thunder....
I already did that.....

ToMz


PsyChO_KiLLeR

this is bug script
dont need to use it bug my full script