Advertisement
Guest User

Untitled

a guest
Mar 1st, 2011
1,286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.19 KB | None | 0 0
  1. IsNumeric(const string[])
  2. {
  3.     for (new i = 0, j = strlen(string); i < j; i++)
  4.     {
  5.         if (string[i] > '9' || string[i] < '0') return 0;
  6.     }
  7.     return 1;
  8. }
  9.  
  10. ReturnUser(text[], playerid = INVALID_PLAYER_ID)
  11. {
  12.     new pos = 0;
  13.     while (text[pos] < 0x21) // Strip out leading spaces
  14.     {
  15.         if (text[pos] == 0) return INVALID_PLAYER_ID; // No passed text
  16.         pos++;
  17.     }
  18.     new userid = INVALID_PLAYER_ID;
  19.     if (IsNumeric(text[pos])) // Check whole passed string
  20.     {
  21.         // If they have a numeric name you have a problem (although names are checked on id failure)
  22.         userid = strval(text[pos]);
  23.         if (userid >=0 && userid < MAX_PLAYERS)
  24.         {
  25.             if(!IsPlayerConnected(userid))
  26.             {
  27.                 /*if (playerid != INVALID_PLAYER_ID)
  28.                 {
  29.                     SendClientMessage(playerid, 0xFF0000AA, "User not connected");
  30.                 }*/
  31.                 userid = INVALID_PLAYER_ID;
  32.             }
  33.             else
  34.             {
  35.                 return userid; // A player was found
  36.             }
  37.         }
  38.         /*else
  39.         {
  40.             if (playerid != INVALID_PLAYER_ID)
  41.             {
  42.                 SendClientMessage(playerid, 0xFF0000AA, "Invalid user ID");
  43.             }
  44.             userid = INVALID_PLAYER_ID;
  45.         }
  46.         return userid;*/
  47.         // Removed for fallthrough code
  48.     }
  49.     // They entered [part of] a name or the id search failed (check names just incase)
  50.     new len = strlen(text[pos]);
  51.     new count = 0;
  52.     new name[MAX_PLAYER_NAME];
  53.     for (new i = 0; i < MAX_PLAYERS; i++)
  54.     {
  55.         if (IsPlayerConnected(i))
  56.         {
  57.             GetPlayerName(i, name, sizeof (name));
  58.             if (strcmp(name, text[pos], true, len) == 0) // Check segment of name
  59.             {
  60.                 if (len == strlen(name)) // Exact match
  61.                 {
  62.                     return i; // Return the exact player on an exact match
  63.                     // Otherwise if there are two players:
  64.                     // Me and MeYou any time you entered Me it would find both
  65.                     // And never be able to return just Me's id
  66.                 }
  67.                 else // Partial match
  68.                 {
  69.                     count++;
  70.                     userid = i;
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     if (count != 1)
  76.     {
  77.         if (playerid != INVALID_PLAYER_ID)
  78.         {
  79.             if (count)
  80.             {
  81.                 SendClientMessage(playerid, 0xFF0000AA, "Multiple users found, please narrow earch");
  82.             }
  83.             else
  84.             {
  85.                 SendClientMessage(playerid, 0xFF0000AA, "No matching user found");
  86.             }
  87.         }
  88.         userid = INVALID_PLAYER_ID;
  89.     }
  90.     return userid; // INVALID_USER_ID for bad return
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement