24.04.2013, 09:10
(
Последний раз редактировалось Yashas; 25.04.2013 в 13:14.
)
Extracting Playerid from IP
What is this??
A small script that returns the playerid from the given IP.
Very simple but there are few who don't get it.
Why this tutorial??
One n00b PMed me asking how to get playerid on OnRconLoginAttempt(ip[],password[],success)
What you need??
The PAWNO!!
How to get the playerid form IP??
I don't know weather there is much more optimized way for this.But I do it by this.
In this small tutorial, all my code are put under OnRconLoginAttempt Callback.
As there is no native function for finding the playerid from his IP.We'll have to write our own.
This works on loops.You keep trying to match the every players IP with the given IP(As the parameter).
Step 1:Create a loop
Create a loop such that the code in the for blocks are executed for all players.i.e From 0 to MAX_PLAYERS
Step 2:Check Player's Connection
We'll check if the Player is connected or not then check for his IP instead of wasting CPU.
The function "IsPlayerConnected(playerid)" will tell us if the player is connected.So in an applicable code, it would look similar to this.
After adding ^^(this) the code would look like
Step 3:Check players IP
Now, we have to check the players IP with the given IP for match.If it matches ,that was the player who attempted the RCON Login or else we have to continue the loop checking other players.
For checking the players IP, we need to get the players IP first.For that we must use "GetPlayerIp(playerid,dest,length);"
The code would look like this:
To check the player IP and the given we must use "strcmp" as they are strings.
This will check the strings and tell us if they match or not
Adding it to the main code:
Step 4:The final makeup
From the above code you'll get to know the player who attempted the RCON Login.But there is nothing to do.
This final code will deal with the player weather to he his attempt was success or not.
The "success" parameter tells if the password entered by the player was correct or not.
If success is == 1 then it was correct else it was wrong.
Putting this last peice to the main code the final code would be:
Final Code
Bugs && Notes:
Optimized:The variable declaration was which was inside thefor loop is moved outside the loop since it created unnecessary extra variables.
What is this??
A small script that returns the playerid from the given IP.
Very simple but there are few who don't get it.
Why this tutorial??
One n00b PMed me asking how to get playerid on OnRconLoginAttempt(ip[],password[],success)
What you need??
The PAWNO!!
How to get the playerid form IP??
I don't know weather there is much more optimized way for this.But I do it by this.
In this small tutorial, all my code are put under OnRconLoginAttempt Callback.
As there is no native function for finding the playerid from his IP.We'll have to write our own.
This works on loops.You keep trying to match the every players IP with the given IP(As the parameter).
Step 1:Create a loop
Create a loop such that the code in the for blocks are executed for all players.i.e From 0 to MAX_PLAYERS
Код:
public OnRconLoginAttempt(ip[], password[], success) { for(new i = 0; i < MAX_PLAYERS; i++) { } return 1; }
We'll check if the Player is connected or not then check for his IP instead of wasting CPU.
The function "IsPlayerConnected(playerid)" will tell us if the player is connected.So in an applicable code, it would look similar to this.
Код:
if(IsPlayerConnected(playerid) { //PLAYER IS CONNECTED - GO CHECK HIS IP } else { //NO PLAYER SO CONTINUE THE LOOP }
Код:
public OnRconLoginAttempt(ip[], password[], success) { for(new i = 0; i < MAX_PLAYERS; i++) { if(IsPlayerConnected(i)) { } } return 1; }
Now, we have to check the players IP with the given IP for match.If it matches ,that was the player who attempted the RCON Login or else we have to continue the loop checking other players.
For checking the players IP, we need to get the players IP first.For that we must use "GetPlayerIp(playerid,dest,length);"
The code would look like this:
Код:
new playerIP[16]; GetPlayerIp(i,playerIP,sizeof(playerIP));
This will check the strings and tell us if they match or not
Код:
if(!strcmp(pIP,ip,true)) { //THEY MATCH - SO THIS IS THE PLAYER } else { //THEY DONT - NOT THIS PLAYER }
Код:
public OnRconLoginAttempt(ip[], password[], success) { new playerIP[16]; for(new i = 0; i < MAX_PLAYERS; i++) { if(IsPlayerConnected(i)) { GetPlayerIp(i, playerIP,sizeof(playerIP)); if(!strcmp(playerIP,ip,true)) { //THE CODE HERE WILL BE EXECUTED IF THE PLAYER IS FOUND } } } return 1; }
From the above code you'll get to know the player who attempted the RCON Login.But there is nothing to do.
This final code will deal with the player weather to he his attempt was success or not.
The "success" parameter tells if the password entered by the player was correct or not.
If success is == 1 then it was correct else it was wrong.
Код:
if(success) { SendClientMessage(i,COLOR_GREEN,"You have been logged in as RCON Administrator"); } else { Kick(i); }
Код:
public OnRconLoginAttempt(ip[], password[], success) { new pIP[16]; for(new i = 0; i < MAX_PLAYERS; i++) //Loop to check the IP of very player who is connected { if(IsPlayerConnected(i)) //Checks if the player is connected { GetPlayerIp(i, pIP, 16); //Gets the players IP if(!strcmp(pIP,ip,true)) //Checks if the players IP matches { if(success) { SendClientMessage(i,COLOR_GREEN,"You have logged in as RCON Administrator"); //Player entered the correct RCON password } else { //Player entered the wrong password hence he gets kicked Kick(i); } } } } return 1; }
Код:
public OnRconLoginAttempt(ip[], password[], success) { new pIP[16]; for(new i = 0; i < MAX_PLAYERS; i++) { if(IsPlayerConnected(i)) { GetPlayerIp(i, pIP, 16); if(!strcmp(pIP,ip,true)) { if(success) { SendClientMessage(i,COLOR_GREEN,"You have logged in as RCON Administrator"); } else { Kick(i); } } } } return 1; }
Optimized:The variable declaration was which was inside thefor loop is moved outside the loop since it created unnecessary extra variables.