1
0

utils.inc 2.2 KB

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