Advertisement
Guest User

CaHbKo

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