SHARE
TWEET

Unique Random Generator - SQLite

a guest Dec 19th, 2018 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  SA-MP Unique Random - SQLite
  2.  *
  3.  *  (c) Copyright 2018, Kaliber
  4.  *
  5.  */
  6. /******************************************************************************/
  7. /*
  8.     native CreateRandomPool(const pool, const min_value, const max_value);
  9.     native UniqueRandom(const pool);
  10. */
  11. /******************************************************************************/
  12. #if defined _u_random_included
  13.     #endinput
  14. #endif
  15. #define _u_random_included
  16. /******************************************************************************/
  17. #if !defined _samp_included
  18.     #include <a_samp>
  19. #endif
  20. /******************************************************************************/
  21. #define u_Hook:: _u@_
  22. /******************************************************************************/
  23. static stock DB:db;
  24. /******************************************************************************/
  25. #if defined FILTERSCRIPT
  26.     public OnFilterScriptInit()
  27.     {
  28.         init_db();
  29.         #if defined _u@_OnFilterScriptInit
  30.             return u_Hook::OnFilterScriptInit();
  31.         #else
  32.             return 1;
  33.         #endif
  34.     }
  35.     #if defined _u@_OnFilterScriptInit
  36.         forward u_Hook::OnFilterScriptInit();
  37.     #endif
  38.     #if defined _ALS_OnFilterScriptInit
  39.         #undef OnFilterScriptInit
  40.     #else
  41.         #define _ALS_OnFilterScriptInit
  42.     #endif
  43.     #define OnFilterScriptInit _u@_OnFilterScriptInit
  44. #else
  45.     public OnGameModeInit()
  46.     {
  47.         init_db();
  48.         #if defined _u@_OnGameModeInit
  49.             return u_Hook::OnGameModeInit();
  50.         #else
  51.             return 1;
  52.         #endif
  53.     }
  54.     #if defined _u@_OnGameModeInit
  55.         forward u_Hook::OnGameModeInit();
  56.     #endif
  57.     #if defined _ALS_OnGameModeInit
  58.         #undef OnGameModeInit
  59.     #else
  60.         #define _ALS_OnGameModeInit
  61.     #endif
  62.     #define OnGameModeInit _u@_OnGameModeInit
  63. #endif
  64. /******************************************************************************/
  65. static stock init_db()
  66. {
  67.     if((db = db_open("unique_random_pool.db")) == DB:0)
  68.     {
  69.         print(!"Fehler: [INCLUDE] u_random.inc - Kein Zugriff auf Datenbank. (Server hat keine Rechte für scriptfiles?)");
  70.     }
  71.     else
  72.     {
  73.         db_query(db, "PRAGMA synchronous = OFF");
  74.     }
  75. }
  76. /******************************************************************************/
  77. stock CreateRandomPool(const unique, const min_value, const max_value)
  78. {
  79.     if(max_value < min_value) return print(!"Fehler: [INCLUDE] u_random.inc - CreateRandomPool min_value größer als max_value!");
  80.     if(min_value < 0) return print(!"Fehler: [INCLUDE] u_random.inc - CreateRandmoPool min_value muss größer 0 sein.");
  81.     new query[80];
  82.     format(query,sizeof(query),"CREATE TABLE IF NOT EXISTS `t_%d` (`v` INTEGER PRIMARY KEY)",unique), db_query(db, query);
  83.    
  84.     new DBResult: Result;
  85.     format(query,sizeof(query), "SELECT `v` FROM `t_%d` LIMIT 1", unique),Result = db_query(db, query);
  86.     if(!db_num_rows(Result))
  87.     {
  88.         new cache[64];
  89.         format(cache,sizeof(cache),"INSERT INTO `t_%d` (`v`) VALUES (",unique);
  90.         for(new i=min_value,tmp[11]; i<max_value; i++) query = cache,valstr(tmp,i),strcat(query,tmp),strcat(query,")"),db_query(db, query);
  91.     }
  92.     db_free_result(Result);
  93.     return 1;
  94. }
  95. /******************************************************************************/
  96. stock UniqueRandom(const unique)
  97. {
  98.     new query[64], DBResult: Result, rows;
  99.     format(query,sizeof(query), "SELECT `v` FROM `t_%d` ORDER BY RANDOM() LIMIT 1", unique),Result = db_query(db, query);
  100.     if(!db_num_rows(Result)) return -1;
  101.     rows = db_get_field_int(Result), db_free_result(Result),format(query,sizeof(query),"DELETE FROM `t_%d` WHERE `v`='%d' LIMIT 1",unique,rows),db_query(db, query);
  102.     return rows;
  103. }
  104. /******************************************************************************/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top