Raised This Month: $ Target: $400
 0% 

little help


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
daniel46
Senior Member
Join Date: Dec 2011
Old 09-18-2013 , 12:06   Re: little help
Reply With Quote #8

Quote:
Originally Posted by Black Rose View Post
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

Last edited by daniel46; 09-18-2013 at 12:06.
daniel46 is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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