Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* SA-MP Unique Random - MySQL
- *
- * (c) Copyright 2018, Kaliber
- *
- */
- /******************************************************************************/
- /*
- native CreateRandomPool(MySQL:db, const pool, const min_value, const max_value);
- native GenerateUniqueRandomNumber(playerid, MySQL:db, const pool, const token);
- */
- /******************************************************************************/
- #if defined _u_random_included
- #endinput
- #endif
- #define _u_random_included
- /******************************************************************************/
- #if !defined _samp_included
- #error Bitte <a_samp> vor <u_random> inkludieren!
- #endif
- /******************************************************************************/
- #if !defined mysql_included
- #error Bitte <a_mysql> vor <u_random> inkludieren!
- #endif
- /******************************************************************************/
- stock CreateRandomPool(MySQL:db, 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` int(11) PRIMARY KEY)",unique);
- mysql_tquery(db, query);
- format(query,sizeof(query), "SELECT `v` FROM `t_%d` LIMIT 1", unique);
- mysql_tquery(db, query, "@CheckForRandomPool", "iiii", _:db, unique, min_value, max_value);
- return 1;
- }
- @CheckForRandomPool(db, const unique, const min_value, const max_value);
- @CheckForRandomPool(db, const unique, const min_value, const max_value)
- {
- if(!cache_num_rows())
- {
- new query[64],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,")"),mysql_tquery(MySQL:db, query);
- }
- return 1;
- }
- /******************************************************************************/
- stock StartGenerateUniqueRandomNumber(playerid, MySQL:db, const unique, const token)
- {
- new query[64];
- format(query,sizeof(query), "SELECT `v` FROM `t_%d` ORDER BY RAND() LIMIT 1", unique, unique);
- mysql_tquery(db, query, "@PreCheckRandomNumber", "iiii", playerid, _:db, unique, token);
- return 0;
- }
- @PreCheckRandomNumber(playerid, db, const unique, const token);
- @PreCheckRandomNumber(playerid, db, const unique, const token)
- {
- if(!cache_num_rows()) return OnRandomNumberGenerated(playerid, unique, token, -1),1;
- new value, query[64];
- cache_get_value_index_int(0, 0, value), OnRandomNumberGenerated(playerid, unique, token, value);
- return format(query,sizeof(query),"DELETE FROM `t_%d` WHERE `v`='%d' LIMIT 1",unique,value),mysql_tquery(MySQL:db, query),1;
- }
- /******************************************************************************/
- forward OnRandomNumberGenerated(playerid, const pool, const token, const status);
- /******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement