Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* SA-MP Unique Random - SQLite
- *
- * (c) Copyright 2018, Kaliber
- *
- */
- /******************************************************************************/
- /*
- native CreateRandomPool(const pool, const min_value, const max_value);
- native UniqueRandom(const pool);
- */
- /******************************************************************************/
- #if defined _u_random_included
- #endinput
- #endif
- #define _u_random_included
- /******************************************************************************/
- #if !defined _samp_included
- #include <a_samp>
- #endif
- /******************************************************************************/
- #define u_Hook:: _u@_
- /******************************************************************************/
- static stock DB:db;
- /******************************************************************************/
- #if defined FILTERSCRIPT
- public OnFilterScriptInit()
- {
- init_db();
- #if defined _u@_OnFilterScriptInit
- return u_Hook::OnFilterScriptInit();
- #else
- return 1;
- #endif
- }
- #if defined _u@_OnFilterScriptInit
- forward u_Hook::OnFilterScriptInit();
- #endif
- #if defined _ALS_OnFilterScriptInit
- #undef OnFilterScriptInit
- #else
- #define _ALS_OnFilterScriptInit
- #endif
- #define OnFilterScriptInit _u@_OnFilterScriptInit
- #else
- public OnGameModeInit()
- {
- init_db();
- #if defined _u@_OnGameModeInit
- return u_Hook::OnGameModeInit();
- #else
- return 1;
- #endif
- }
- #if defined _u@_OnGameModeInit
- forward u_Hook::OnGameModeInit();
- #endif
- #if defined _ALS_OnGameModeInit
- #undef OnGameModeInit
- #else
- #define _ALS_OnGameModeInit
- #endif
- #define OnGameModeInit _u@_OnGameModeInit
- #endif
- /******************************************************************************/
- static stock init_db()
- {
- if((db = db_open("unique_random_pool.db")) == DB:0)
- {
- print(!"Fehler: [INCLUDE] u_random.inc - Kein Zugriff auf Datenbank. (Server hat keine Rechte für scriptfiles?)");
- }
- else
- {
- db_query(db, "PRAGMA synchronous = OFF");
- }
- }
- /******************************************************************************/
- stock CreateRandomPool(const unique, const min_value, const max_value)
- {
- if(max_value < min_value) return print(!"Fehler: [INCLUDE] u_random.inc - CreateRandomPool min_value größer als max_value!");
- if(min_value < 0) return print(!"Fehler: [INCLUDE] u_random.inc - CreateRandmoPool min_value muss größer 0 sein.");
- new query[80];
- format(query,sizeof(query),"CREATE TABLE IF NOT EXISTS `t_%d` (`v` INTEGER PRIMARY KEY)",unique), db_query(db, query);
- new DBResult: Result;
- format(query,sizeof(query), "SELECT `v` FROM `t_%d` LIMIT 1", unique),Result = db_query(db, query);
- if(!db_num_rows(Result))
- {
- new cache[64];
- format(cache,sizeof(cache),"INSERT INTO `t_%d` (`v`) VALUES (",unique);
- 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);
- }
- db_free_result(Result);
- return 1;
- }
- /******************************************************************************/
- stock UniqueRandom(const unique)
- {
- new query[64], DBResult: Result, rows;
- format(query,sizeof(query), "SELECT `v` FROM `t_%d` ORDER BY RANDOM() LIMIT 1", unique),Result = db_query(db, query);
- if(!db_num_rows(Result)) return -1;
- 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);
- return rows;
- }
- /******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment