Advertisement
Guest User

Unique Random Generator - MySQL

a guest
Dec 19th, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. /*  SA-MP Unique Random - MySQL
  2.  *
  3.  *  (c) Copyright 2018, Kaliber
  4.  *
  5.  */
  6. /******************************************************************************/
  7. /*
  8.     native CreateRandomPool(MySQL:db, const pool, const min_value, const max_value);
  9.     native GenerateUniqueRandomNumber(playerid, MySQL:db, const pool, const token);
  10. */
  11. /******************************************************************************/
  12. #if defined _u_random_included
  13.     #endinput
  14. #endif
  15. #define _u_random_included
  16. /******************************************************************************/
  17. #if !defined _samp_included
  18.     #error Bitte <a_samp> vor <u_random> inkludieren!
  19. #endif
  20. /******************************************************************************/
  21. #if !defined mysql_included
  22.     #error Bitte <a_mysql> vor <u_random> inkludieren!
  23. #endif
  24. /******************************************************************************/
  25. stock CreateRandomPool(MySQL:db, const unique, const min_value, const max_value)
  26. {
  27.     if(max_value < min_value) return print(!"Fehler: [INCLUDE] u_random.inc - CreateRandomPool min_value größer als max_value!");
  28.     if(min_value < 0) return print(!"Fehler: [INCLUDE] u_random.inc - CreateRandmoPool min_value muss größer 0 sein.");
  29.     new query[80];
  30.     format(query,sizeof(query),"CREATE TABLE IF NOT EXISTS `t_%d` (`v` int(11) PRIMARY KEY)",unique);
  31.     mysql_tquery(db, query);
  32.  
  33.     format(query,sizeof(query), "SELECT `v` FROM `t_%d` LIMIT 1", unique);
  34.     mysql_tquery(db, query, "@CheckForRandomPool", "iiii", _:db, unique, min_value, max_value);
  35.     return 1;
  36. }
  37. @CheckForRandomPool(db, const unique, const min_value, const max_value);
  38. @CheckForRandomPool(db, const unique, const min_value, const max_value)
  39. {
  40.     if(!cache_num_rows())
  41.     {
  42.         new query[64],cache[64];
  43.         format(cache,sizeof(cache),"INSERT INTO `t_%d` (`v`) VALUES (",unique);
  44.         for(new i=min_value,tmp[11]; i<max_value; i++) query = cache,valstr(tmp,i),strcat(query,tmp),strcat(query,")"),mysql_tquery(MySQL:db, query);
  45.     }
  46.     return 1;
  47. }
  48. /******************************************************************************/
  49. stock StartGenerateUniqueRandomNumber(playerid, MySQL:db, const unique, const token)
  50. {
  51.     new query[64];
  52.     format(query,sizeof(query), "SELECT `v` FROM `t_%d` ORDER BY RAND() LIMIT 1", unique, unique);
  53.     mysql_tquery(db, query, "@PreCheckRandomNumber", "iiii", playerid, _:db, unique, token);
  54.     return 0;
  55. }
  56. @PreCheckRandomNumber(playerid, db, const unique, const token);
  57. @PreCheckRandomNumber(playerid, db, const unique, const token)
  58. {
  59.     if(!cache_num_rows()) return OnRandomNumberGenerated(playerid, unique, token, -1),1;
  60.     new value, query[64];
  61.     cache_get_value_index_int(0, 0, value), OnRandomNumberGenerated(playerid, unique, token, value);
  62.     return format(query,sizeof(query),"DELETE FROM `t_%d` WHERE `v`='%d' LIMIT 1",unique,value),mysql_tquery(MySQL:db, query),1;
  63. }
  64. /******************************************************************************/
  65. forward OnRandomNumberGenerated(playerid, const pool, const token, const status);
  66. /******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement