1
0

utils.inc 2.3 KB

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