AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   little help (https://forums.alliedmods.net/showthread.php?t=226448)

daniel46 09-18-2013 07:12

little help
 
some why this code dont work
can anyone fix it

Code:

#include <amxmodx>

new const SteamIds[ ][ ] =
{
        "STEAM_0:0:42242392",
        "STEAM_0:0:42242393"
}

public client_putinserver( client )
{
        new szID[35]; get_user_authid(client, szID, charsmax(szID) )
        for( new i = 0 ; i < sizeof SteamIds - 1 ; i ++ )
        {
                if(!(equali( SteamIds[ i ] , szID ) ) )
                {
                        server_cmd("kick #%d Ola", szID)
                }
        }
}


3xit. 09-18-2013 08:03

Re: little help
 
Don't need a plugin :

Quote:

Originally Posted by YamiKaitou (Post 2036633)
Change the amx_mode cvar to 2 and then put all the SteamIDs into the users.ini file


hornet 09-18-2013 08:04

Re: little help
 
Because szID is the string array holding the player's Steam ID. The value in your server_cmd() line should instead be get_user_userid( client ).
Also your loop should be checking size of array not size of array - 1 Otherwise it will not check the last string.

Forgot to mention that every player who joins your server is going to be kicked unless their Steam ID matches the first one in the array.

Quote:

Originally Posted by 3xit. (Post 2036818)
Don't need a plugin :

He asked in Scripting Help which means he wants to learn ( to whatever extent ).

3xit. 09-18-2013 08:07

Re: little help
 
Quote:

Originally Posted by hornet (Post 2036819)
He asked in Scripting Help which means he wants to learn ( to whatever extent ).

Ahhh your right my bad :/

daniel46 09-18-2013 08:42

Re: little help
 
Code:

#include <amxmodx>

new const SteamIds[ ][ ] =
{
        "STEAM_0:0:42242392",
        "STEAM_0:0:42242393"
}

public client_putinserver( client )
{
        new szID[35]; get_user_authid(client, szID, charsmax(szID) )
        for( new i = 0 ; i < sizeof (SteamIds) ; i ++ )
        {
                if(!(equali( szID , SteamIds[ i ] ) ) )
                {
                        server_cmd("kick #%s Ola", szID)
                }
        }
}

now its working but its kicks the steam id not the other players and i dont get why its

if(!(equali( szID , SteamIds[ i ] ) ) )

its kicks all

xxxnenadxxx 09-18-2013 10:06

Re: little help
 
Put ! in front of equali
Like this
PHP Code:

if((!equaliszID SteamIds]))) 


Black Rose 09-18-2013 11:52

Re: little help
 
The problem with the code is the loop is badly written.

The only reason it would let anyone in the server is if the SteamIds array contains one or more of the same SteamId.

Let me explain further.
Let's say you have these steamids stored in that array:
STEAM_0:0:1234567
STEAM_0:0:2345678
STEAM_0:0:4567890

Now, the player with the id STEAM_0:0:2345678 joins.

At the first run of the loop it would compare the steamid. Since STEAM_0:0:2345678 is not equal to STEAM_0:0:1234567 ( first in loop ) it kicks immediately.

Now let's say the person who's first in the array joins.
The first run will be fine because STEAM_0:0:1234567 does equal to STEAM_0:0:1234567.
The problem is the loop doesn't know this is a point to break, so it would continue to the second steamid.
And because STEAM_0:0:12345678 does not equal to STEAM_0:0:2345678 the person will then be kicked even though the first run of the loop was a success.

Basically you have to do something like this:
In the first two examples you can continue adding code after the loop for both statements (if or if not the player was found)

The first example is using a variable called match_found. This will add readability and make it easier for people to understand. You, of course, are using one more variable, but that's not really any problem.
Code:
new szID[35], match_found; get_user_authid(client, szID, charsmax(szID)) for ( new i = 0 ; i < sizeof SteamIds ; i++ ) {     if ( equali(szID, SteamIds[i]) ) {         match_found = true;         break;     } } if ( ! match_found )     server_cmd("kick #%d", get_user_userid(client))

The second example can be a bit more confusing for beginners.
This is how it works:
The loop increments after every finished loop.
It will run as long as the statement (i < sizeof SteamIds) is true.
If there's a match, the variable i will always be lower than sizeof SteamIds because we break the function before it increments the variable i.
If there's no match it will run the loop, incrementing the variable i until it no longer matches condition. I.e. the variable i is equal to or larger than sizeof SteamIds. Because it's incrementing in steps of 1, it will always equal to sizeof SteamIds.
Code:
new szID[35], i; get_user_authid(client, szID, charsmax(szID)) for ( i = 0 ; i < sizeof SteamIds ; i++ ) {     if ( equali(szID, SteamIds[i]) )         break; } if ( i == sizeof SteamIds )     server_cmd("kick #%d", get_user_userid(client))

The third differs that it will return upon match, making all the code under the loop unreachable for players found in the array of SteamIds.
Code:
new szID[35]; get_user_authid(client, szID, charsmax(szID)) for ( new i = 0 ; i < sizeof SteamIds ; i++ ) {     if ( equali(szID, SteamIds[i]) )         return } server_cmd("kick #%d", get_user_userid(client))

So which one should you use? The two first is all about coding style and personal preference. The third option is the most simple one, not requiring any variables or double-checking. But it has the least amount of flexibility. It depends on if you want to do anything to the actual matching players.



If you want to learn more, here's how a for loop works (more or less):
Code:
#include <amxmodx> public plugin_init() {     register_plugin("Test Plugin 6", "", "");         new i = 0;         for ( server_print("i is set to %d^n", (i = 0)) ; server_print("Is i(%d) lower than 5? %s", i, i < 5 ? "yes, continue loop." : "no, break loop") && i < 5 ; i++, server_print("i is incremented to %d^n", i) ) {         server_print("Actual contents of the loop, i: %d", i);     }     server_print("Loop is finished, i: %d", i); }

Code:

i is set to 0

Is i(0) lower than 5? yes, continue loop.
Actual contents of the loop, i: 0
i is incremented to 1

Is i(1) lower than 5? yes, continue loop.
Actual contents of the loop, i: 1
i is incremented to 2

Is i(2) lower than 5? yes, continue loop.
Actual contents of the loop, i: 2
i is incremented to 3

Is i(3) lower than 5? yes, continue loop.
Actual contents of the loop, i: 3
i is incremented to 4

Is i(4) lower than 5? yes, continue loop.
Actual contents of the loop, i: 4
i is incremented to 5

Is i(5) lower than 5? no, break loop
Loop is finished, i: 5

Code:
#include <amxmodx> public plugin_init() {     register_plugin("Test Plugin 6", "", "");         new i = 0;         for ( server_print("i is set to %d^n", (i = 0)) ; server_print("Is i(%d) lower than 5? %s", i, i < 5 ? "yes, continue loop." : "no, break loop") && i < 5 ; i++, server_print("i is incremented to %d^n", i) ) {         server_print("Actual contents of the loop, i: %d", i);         if ( i == 4 )             break;     }     server_print("Loop is finished, i: %d", i); }

Code:

i is set to 0

Is i(0) lower than 5? yes, continue loop.
Actual contents of the loop, i: 0
i is incremented to 1

Is i(1) lower than 5? yes, continue loop.
Actual contents of the loop, i: 1
i is incremented to 2

Is i(2) lower than 5? yes, continue loop.
Actual contents of the loop, i: 2
i is incremented to 3

Is i(3) lower than 5? yes, continue loop.
Actual contents of the loop, i: 3
i is incremented to 4

Is i(4) lower than 5? yes, continue loop.
Actual contents of the loop, i: 4

Loop is finished, i: 4

Notice that in both examples the last loop content contains i as 4, but in the second example we break the loop before it increments.

So basically, a for loop constructed like this:
Code:
for ( 0 ; 1 ; 3 ) {     2 }
will be called in that order. 0 will only be called once.
0, 1, 2, 3
1, 2, 3
1, 2, 3
and so further

It's the same as writing a while loop like this:
Code:
0 while ( 1 ) {     2         3 }

daniel46 09-18-2013 12:06

Re: little help
 
Quote:

Originally Posted by Black Rose (Post 2036913)
The problem with the code is the loop is badly written.

The only reason it would let anyone in the server is if the SteamIds array contains one or more of the same SteamId.

Let me explain further.
Let's say you have these steamids stored in that array:
STEAM_0:0:1234567
STEAM_0:0:2345678
STEAM_0:034567890

Now, the player with the id STEAM_0:0:2345678 joins.

At the first run of the loop it would compare the steamid. Since STEAM_0:0:2345678 is not equal to STEAM_0:0:1234567 ( first in loop ) it kicks immediately.

Now let's say the person who's first in the array joins.
The first run will be fine because STEAM_0:0:1234567 does equal to STEAM_0:0:1234567.
The problem is the loop doesn't know this is a point to break, so it would continue to the second steamid.
And because STEAM_0:0:12345678 does not equal to STEAM_0:0:2345678 the person will then be kicked even though the first run of the loop was a success.

Basically you have to do something like this:
In the first two examples you can continue adding code after the loop for both statements (if or if not the player was found)

The first example is using a variable called match_found. This will add readability and make it easier for people to understand. You of course are using one more variable, but that's not really any problem.
Code:
new szID[35], match_found; get_user_authid(client, szID, charsmax(szID)) for ( new i = 0 ; i < sizeof SteamIds ; i++ ) {     if ( equali(szID, SteamIds[i]) ) {         match_found = true;         break;     } } if ( ! match_found )     server_cmd("kick #%d", get_user_userid(client))

The second example can be a bit more confusing for beginners.
This is how it works:
The loop increments after every finished loop.
It will run as long as the statement (i < sizeof SteamIds) is true.
If there's a match, the variable i will always be lower than sizeof SteamIds because we break the function before it increments the variable i.
If there's no match it will run the loop, incrementing the variable i until it no longer matches condition. I.e. the variable i is equal to or larger than sizeof SteamIds. Because it's incrementing in steps of 1, it will always equal to sizeof SteamIds.
Code:
new szID[35], i; get_user_authid(client, szID, charsmax(szID)) for ( i = 0 ; i < sizeof SteamIds ; i++ ) {     if ( equali(szID, SteamIds[i]) )         break; } if ( i == sizeof SteamIds )     server_cmd("kick #%d", get_user_userid(client))

The third differs that it will return upon match, making all the code under the loop unreachable for players found in the array of SteamIds.
Code:
new szID[35]; get_user_authid(client, szID, charsmax(szID)) for ( new i = 0 ; i < sizeof SteamIds ; i++ ) {     if ( equali(szID, SteamIds[i]) )         return } server_cmd("kick #%d", get_user_userid(client))

So which one should you use? The two first is all about coding style and personal preference. The third option is the most simple one, not requiring any variables or double-checking. But it has the least amount of flexibility. It depends on if you want to do anything to the actual matching players.

yes thank you i was thinking about this but at start i didnt realise it


All times are GMT -4. The time now is 19:00.

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