AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Compare Array Question (https://forums.alliedmods.net/showthread.php?t=25443)

Anthraxnz 03-14-2006 01:18

Compare Array Question
 
hi

i have 2 arrays one called admins[32] and one called adminSQL[100]

i need to compare these arrays and cant get my head around how :(

admins[32] stores the steam id of any admins on the server
adminSQL[100] stores all the admin steamids from "auth" field

how would i compare these 2 arrays since SQL could have anywhere from 1 admin to 100 admins and the server could have 32 admins on it.

heres what i got so far
Code:
    new adminSteamIDs[32]     new authID[32]     new players[32], inum     new i = 0         get_players(players,inum)         //Find All Admins On Server and Store SteamID in array     while( i < inum ){         if( get_user_flags(players[i])){             get_user_authid(players[i],authID,31)             copy(adminSteamIDs[i],31,authID)         }         i++     }     //Retrieve All Admins From SQL Database and Store In Array     new steamSQL[100]     new adminName[100]     new j = 0         result = dbi_query(dbc,"SELECT auth,name FROM admins")         while( j < dbi_num_rows(result)){         dbi_nextrow(result)         dbi_result(result,"auth",steamSQL[j])     }         //Compare Admins Array With SQL Array And Print If They Match

someone help please :)

Anthraxnz 03-14-2006 04:55

heres what iv got so far. dont know if it works but im guessing it wont.
any suggestions on how to make it work?

Code:
    new j = 0     new authSQL[32]     new nameSQL[32]         result = dbi_query(dbc,"SELECT name,auth FROM admins")         while( j < inum ){                 new k = 0         while( k < dbi_num_rows(result)){             dbi_nextrow(result)             dbi_result(result,"auth",authSQL,31)                         if( equali(authSQL[k],adminSteamIDs[j])){                 dbi_result(result,"name",nameSQL,31)                 console_print(id,"%s = %s",adminSteamIDs[j],nameSQL)             }             k++         }         j++     }

Anthraxnz 03-14-2006 05:37

works if theres only one person on the server.

if theres more then it just prints a whole bunch of ='s with some random letters

capndurk 03-14-2006 08:53

Show the output and the database files, so maybe we can get a better look at the problem you're having.

Also, to make things easier to read, I'd use a for loop within a for loop, instead of two whiles:

Code:
for(new i = 0; i < whatever; i++) {      for(new j = 0; j < whatever; j++)      {           if(they equal) // blah blah blah      } }

Brad 03-14-2006 10:00

You only want the matching records from SQL, correct?

I haven't tested the code below but what it's intended to do is create a where clause containing each of the STEAM_ID's you're looking to match. It then only pulls the records from the table that do match. Therefore, the number of matches will be equal to the new of records returned from the query.

Code:
    new authID[32]     new players[32], inum, playerID         get_players(players,inum)         //Find All Admins On Server and Store SteamID in where string     for (new i = 0; i < inum; i++){         playerID = players[i]         if( get_user_flags(playerID)){             get_user_authid(playerID,authID,31)             format(whereClause, 1023, "%s OR auth = '%s'", whereClause, authID)         }     }     copy(whereClause, 1023, whereClause[4])     //Retrieve All Admins From SQL Database and Store In Array     new adminName[100]     new j = 0         result = dbi_query(dbc,"SELECT auth,name FROM admins WHERE %s", whereClause)         while( j < dbi_num_rows(result)){         dbi_nextrow(result)         dbi_result(result,"auth",steamSQL[j])     }

Anthraxnz 03-14-2006 16:07

cool, thanks will give it a try when i get back :)

Anthraxnz 03-15-2006 04:30

been workin on this and it still doesnt work.

if im admin in slot #1 it works but if im in #2 then i just does nothing.
cant figure out why heres what i got so far that seems to work when im in #1

Code:
//Admin Checking Function--------------------------------------------------------------- public checkAdmin(id){         if (!(get_user_flags(id)&ADMIN_KICK)){         console_print(id,"You have no access to that command")         return PLUGIN_HANDLED     }     if(! get_playersnum()){         console_print(id,"No Players Found")         return PLUGIN_HANDLED     }         new players[32], inum     get_players(players,inum)     //Retrieve All Admins From SQL Database and Store In Array         new authSQL[32]     new nameSQL[32]         new nameHLDS[32]     new authHLDS[32]         result = dbi_query(dbc,"SELECT name,auth FROM admins")         for( new j = 0; j < inum; j++ ){         get_user_authid(players[j],authHLDS,31)         get_user_name(players[j],nameHLDS,31)                 for( new k = 0; k <= dbi_num_rows(result); k++ ){             dbi_nextrow(result)             dbi_result(result,"auth",authSQL,31)                         if( equali(authSQL,authHLDS)){                 dbi_result(result,"name",nameSQL,31)                 console_print(id,"%s = %s",nameHLDS,nameSQL)             }         }     }         dbi_free_result(result)     return PLUGIN_HANDLED   }

Brad 03-15-2006 08:41

Are you trying to be inefficient with your code? Also, you're more likely to get help if you post the entire plugin so that others can make/test changes.

Anthraxnz 03-15-2006 16:23

Code:
/* Plugin generated by AMXX-Studio */ #include <amxmodx> #include <amxmisc> #include <dbi> new Sql:dbc new Result:result public plugin_init() {     register_plugin("SQL Admin Checker","1.0","Anthrax")     register_concmd("amx_admins","checkAdmin",ADMIN_KICK," - Checks For Admins Real Name")     set_task(Float:10.0,"sql_init") } //Start SQL Connection Function--------------------------------------------------------- public sql_init(id){     new host[64], username[32], password[32], dbname[32], error[32]     get_cvar_string("amx_sql_host",host,63)     get_cvar_string("amx_sql_user",username,31)     get_cvar_string("amx_sql_pass",password,31)     get_cvar_string("amx_sql_db",dbname,31)     dbc = dbi_connect(host,username,password,dbname,error,31)     if (dbc == SQL_FAILED)         log_amx("[AMXX] SQL Connection Failed") } //Admin Checking Function--------------------------------------------------------------- public checkAdmin(id){         if (!(get_user_flags(id)&ADMIN_KICK)){         console_print(id,"You have no access to that command")         return PLUGIN_HANDLED     }     if(! get_playersnum()){         console_print(id,"No Players Found")         return PLUGIN_HANDLED     }         new players[32], inum     get_players(players,inum)     //Retrieve All Admins From SQL Database and Store In Array         new authSQL[32]     new nameSQL[32]         new nameHLDS[32]     new authHLDS[32]         result = dbi_query(dbc,"SELECT name,auth FROM admins")         for( new j = 0; j < inum; j++ ){         get_user_authid(players[j],authHLDS,31)         get_user_name(players[j],nameHLDS,31)                 for( new k = 0; k <= dbi_num_rows(result); k++ ){             dbi_nextrow(result)             dbi_result(result,"auth",authSQL,31)                         if( equali(authSQL,authHLDS)){                 dbi_result(result,"name",nameSQL,31)                 console_print(id,"%s = %s",nameHLDS,nameSQL)             }         }     }         dbi_free_result(result)     return PLUGIN_HANDLED   }

as for effiency i cant think of any other way of doing it.

Brad 03-15-2006 19:17

Quote:

Originally Posted by Anthraxnz
as for effiency i cant think of any other way of doing it.

Quote:

Originally Posted by Brad
I haven't tested the code below but what it's intended to do is create a where clause containing each of the STEAM_ID's you're looking to match. It then only pulls the records from the table that do match. Therefore, the number of matches will be equal to the new of records returned from the query.

Using the code I provided, or at least a reasonable facisimile is more efficient than the double loop. As a general rule, you always want to limit the amount of data you're bringing over the wire. In other words, don't bring over more than you need.


All times are GMT -4. The time now is 20:15.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.