Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Initialise the alias table
- * @param db The database
- */
- stock Alias_Init(DB:db)
- {
- if(db) {
- db_free_result(db_query(db, "CREATE TABLE IF NOT EXISTS `Aliases` ( `name` TEXT NOT NULL COLLATE NOCASE , `ip` TEXT NOT NULL , `datetime` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP , `onlinetime` INTEGER NOT NULL DEFAULT 0 , PRIMARY KEY ( `name` , `ip` ) )"));
- }
- }
- /**
- * Add or update an alias
- * @param db The database
- * @param playername The player name
- * @param playerip The player IP
- */
- stock bool:Alias_Add(DB:db, const playername[], const playerip[])
- {
- if(db && !isnull(playername) && !isnull(playerip)) {
- new string[100 + MAX_PLAYER_NAME + MAX_IP];
- format(string, sizeof(string), "SELECT ROWID FROM `Aliases` WHERE ( `name` = '%s' AND `ip` = '%s' )", playername, playerip);
- new DBResult:result = db_query(db, string);
- if(db_num_rows(result)) {
- db_get_field(result, 0, string, sizeof(string));
- db_free_result(result);
- format(string, sizeof(string), "UPDATE `Aliases` SET `datetime` = CURRENT_TIMESTAMP WHERE ( ROWID = %s )", string);
- db_free_result(db_query(db, string));
- } else {
- format(string, sizeof(string), "INSERT INTO `Aliases` ( `name` , `ip` ) VALUES ( '%s' , '%s' )", playername, playerip);
- db_free_result(db_query(db, string));
- }
- return true;
- }
- return false;
- }
- /**
- * Update an alias with the time online
- * @param db The database
- * @param playername The player name
- * @param playerip The player IP
- * @param seconds The time online in seconds
- */
- stock bool:Alias_Update(DB:db, const playername[], const playerip[], seconds)
- {
- if(db && !isnull(playername) && !isnull(playerip) && seconds > 0) {
- new string[160 + MAX_PLAYER_NAME + MAX_IP];
- format(string, sizeof(string), "UPDATE `Aliases` SET `datetime` = CURRENT_TIMESTAMP , `onlinetime` = `onlinetime` + %i WHERE ( `name` = '%s' AND `ip` = '%s' )", seconds, playername, playerip);
- db_free_result(db_query(db, string));
- return true;
- }
- return false;
- }
- /**
- * Get the last IP used by a name
- * @param db The database
- * @param playername The player name
- * @return The last IP
- */
- stock Alias_GetLastIP(DB:db, const playername[])
- {
- new pIP[MAX_IP];
- if(db && !isnull(playername)) {
- new string[90 + MAX_PLAYER_NAME];
- format(string, sizeof(string), "SELECT `ip` FROM `Aliases` WHERE ( `name` = '%s' ) ORDER BY `datetime` DESC LIMIT 1", playername);
- new DBResult:result = db_query(db, string);
- if(db_num_rows(result)) {
- db_get_field(result, 0, pIP, sizeof(pIP));
- }
- db_free_result(result);
- }
- return pIP;
- }
- /**
- * Get a player's aliases from a name (find's the last IP used by the name)
- * @param db The database
- * @param playername The player name
- * @param names The array to concatenate the aliases onto
- * @param maxlength The size of the names array
- * @return The number of aliases
- */
- stock Alias_GetAliasFromName(DB:db, const playername[], names[], maxlength=sizeof(names))
- {
- if(db && !isnull(playername)) {
- new string[170 + MAX_PLAYER_NAME];
- format(string, sizeof(string), "SELECT DISTINCT `name` FROM `Aliases` WHERE ( `ip` = ( SELECT `ip` FROM `Aliases` WHERE ( `name` = '%s' ) ORDER BY `datetime` DESC LIMIT 1 ) ) ORDER BY `datetime` DESC", playername);
- new DBResult:result = db_query(db, string),
- num_rows = db_num_rows(result);
- if(num_rows) {
- db_get_field(result, 0, string, sizeof(string));
- strcat(names, string, maxlength);
- for(new i = 1; i < num_rows; i++) {
- db_next_row(result);
- db_get_field(result, 0, string, sizeof(string));
- if(maxlength - strlen(names) < strlen(string) + 2) break;
- format(names, maxlength, "%s %s", names, string);
- }
- db_free_result(result);
- return num_rows;
- }
- db_free_result(result);
- }
- return 0;
- }
- /**
- * Get a player's aliases from an IP
- * @param db The database
- * @param playerip The player IP
- * @param names The array to concatenate the aliases onto
- * @param maxlength The size of the names array
- * @return The number of aliases
- */
- stock Alias_GetAliasFromIP(DB:db, const playerip[], names[], maxlength=sizeof(names))
- {
- if(db && !isnull(playerip)) {
- new string[110 + MAX_IP];
- format(string, sizeof(string), "SELECT DISTINCT `name` FROM ( SELECT * FROM `Aliases` WHERE ( `ip` GLOB '%s' ) ORDER BY `datetime` DESC )", playerip);
- new DBResult:result = db_query(db, string),
- num_rows = db_num_rows(result);
- if(num_rows) {
- db_get_field(result, 0, string, sizeof(string));
- strcat(names, string, maxlength);
- for(new i = 1; i < num_rows; i++) {
- db_next_row(result);
- db_get_field(result, 0, string, sizeof(string));
- if(maxlength - strlen(names) < strlen(string) + 2) break;
- format(names, maxlength, "%s %s", names, string);
- }
- db_free_result(result);
- return num_rows;
- }
- db_free_result(result);
- }
- return 0;
- }
- /**
- * Get the aliases used by a player's subnet from their name (find's the last IP used by the name)
- * @param db The database
- * @param playername The player name
- * @param names The array to concatenate the aliases onto
- * @param maxlength The size of the names array
- * @return The number of aliases
- */
- stock Alias_GetAlias2FromName(DB:db, const playername[], names[], maxlength=sizeof(names))
- {
- if(db && !isnull(playername)) {
- new string[90 + MAX_IP + MAX_PLAYER_NAME];
- format(string, sizeof(string), "SELECT `ip` FROM `Aliases` WHERE ( `name` = '%s' ) ORDER BY `datetime` DESC LIMIT 1", playername);
- new DBResult:result = db_query(db, string);
- if(db_num_rows(result)) {
- db_get_field(result, 0, string, sizeof(string));
- db_free_result(result);
- format(string, sizeof(string), "SELECT DISTINCT `name` FROM `Aliases` WHERE ( `ip` GLOB '%s' ) ORDER BY `datetime` DESC", IPsubnet(string));
- result = db_query(db, string);
- new num_rows = db_num_rows(result);
- if(num_rows) {
- db_get_field(result, 0, string, sizeof(string));
- strcat(names, string, maxlength);
- for(new i = 1; i < num_rows; i++) {
- db_next_row(result);
- db_get_field(result, 0, string, sizeof(string));
- if(maxlength - strlen(names) < strlen(string) + 2) break;
- format(names, maxlength, "%s %s", names, string);
- }
- db_free_result(result);
- return num_rows;
- }
- }
- db_free_result(result);
- return 0;
- }
- return 0;
- }
- /**
- * Get the aliases used by a player's subnet
- * @param db The database
- * @param playerip The player IP
- * @param names The array to concatenate the aliases onto
- * @param maxlength The size of the names array
- * @return The number of aliases
- */
- stock Alias_GetAlias2FromIP(DB:db, const playerip[], names[], maxlength=sizeof(names))
- {
- if(db && !isnull(playerip)) {
- new string[90 + MAX_IP];
- format(string, sizeof(string), "SELECT DISTINCT `name` FROM `Aliases` WHERE ( `ip` GLOB '%s' ) ORDER BY `datetime` DESC", IPsubnet(playerip));
- new DBResult:result = db_query(db, string),
- num_rows = db_num_rows(result);
- if(num_rows) {
- db_get_field(result, 0, string, sizeof(string));
- strcat(names, string, maxlength);
- for(new i = 1; i < num_rows; i++) {
- db_next_row(result);
- db_get_field(result, 0, string, sizeof(string));
- if(maxlength - strlen(names) < strlen(string) + 2) break;
- format(names, maxlength, "%s %s", names, string);
- }
- db_free_result(result);
- return num_rows;
- }
- db_free_result(result);
- return 0;
- }
- return 0;
- }
- /**
- * Get the aliases of a player's previous IPs
- * @param db The database
- * @param playername The player name
- * @param names The array to concatenate the aliases onto
- * @param maxlength The size of the names array
- * @return The number of aliases
- */
- stock Alias_GetAlias3FromName(DB:db, const playername[], names[], maxlength=sizeof(names))
- {
- if(db && !isnull(playername)) {
- new string[140 + MAX_PLAYER_NAME];
- format(string, sizeof(string), "SELECT DISTINCT `name` FROM `Aliases` WHERE ( `ip` IN ( SELECT `ip` FROM `Aliases` WHERE ( `name` = '%s' ) ) ) ORDER BY `datetime` DESC", playername);
- new DBResult:result = db_query(db, string),
- num_rows = db_num_rows(result);
- if(num_rows) {
- db_get_field(result, 0, string, sizeof(string));
- strcat(names, string, maxlength);
- for(new i = 1; i < num_rows; i++) {
- db_next_row(result);
- db_get_field(result, 0, string, sizeof(string));
- if(maxlength - strlen(names) < strlen(string) + 2) break;
- format(names, maxlength, "%s %s", names, string);
- }
- db_free_result(result);
- return num_rows;
- }
- db_free_result(result);
- }
- return 0;
- }
- /**
- * Get a player's previous IPs
- * @param db The database
- * @param playername The player name
- * @param IPs The array to concatenate the IPs onto
- * @param maxlength The size of the IPs array
- * @return The number of IPs
- */
- stock Alias_GetIPsFromName(DB:db, const playername[], IPs[], maxlength=sizeof(IPs))
- {
- if(db && !isnull(playername)) {
- new string[90 + MAX_PLAYER_NAME];
- format(string, sizeof(string), "SELECT DISTINCT `ip` FROM `Aliases` WHERE ( `name` = '%s' ) ORDER BY `datetime` DESC", playername);
- new DBResult:result = db_query(db, string),
- num_rows = db_num_rows(result);
- if(num_rows) {
- db_get_field(result, 0, string, sizeof(string));
- strcat(IPs, string, maxlength);
- for(new i = 1; i < num_rows; i++) {
- db_next_row(result);
- db_get_field(result, 0, string, sizeof(string));
- if(maxlength - strlen(IPs) < strlen(string) + 2) break;
- format(IPs, maxlength, "%s %s", IPs, string);
- }
- db_free_result(result);
- return num_rows;
- }
- db_free_result(result);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment