Raised This Month: $ Target: $400
 0% 

Keep List of players with details!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
johnally
Member
Join Date: May 2011
Location: Mauritius
Old 05-10-2011 , 14:41   Keep List of players with details!
Reply With Quote #1

I am trying to keep the list of players in my server with details on them as follows:
PHP Code:
playerName newplayerName IP address 
I initialized a 2d array as follows:
PHP Code:
new players[32][3]; 
The Functions are pretty self explanatory:
PHP Code:
// ----------------------------------------------------------------------------
// ------------------------Add details to playerlist---------------------------
public add_players(id) { 
    new 
ip[16];
    new 
playerName[32]
    
    
// Get the name and ip
    
get_user_name(idplayerName31);
    
get_user_ip(idip151/*no port*/);
    
    
// Add these to our array using id as key
    
format(players[id 1][0], strlen(playerName), "%s"playerName);
    
format(players[id 1][1], strlen(playerName), "%s"playerName);
    
format(players[id 1][2], 15"%s"ip);
}
// ----------------------------------------------------------------------------
// -----------------------Record new name for Player---------------------------
public client_infochanged(id) {      
    new 
new_name[32];
    new 
old_name[32];
    
    
get_user_info(id"name"new_name31);
    
get_user_name(idold_name31);
    
    if(!
equal(new_nameold_name)){
        
// Put his new name in details :)
        
format(players[id 1][1], sizeof(new_name), "%s"new_name);
    }
}
// ----------------------------------------------------------------------------
// -----------------------Delete old player details---------------------------
public del_players(id) {      
    
format(players[id 1][0], 32"[none]");
    
format(players[id 1][1], 32"[none]");
    
format(players[id 1][2], 16"PLAYER_LEFT");
}
// ----------------------------------------------------------------------------
// --------------------Detect when a client disconnects-------------------------
public client_disconnect(id) {
    
del_players(id);
}
// ----------------------------------------------------------------------------
// --------------------List all players on server :)-------------------------
public list_players(id) {
    
client_print(idprint_chat"Check console for Players' Details")
    for (new 
i=0i<sizeof(players); i++)
    {
        
console_print(id"%s : %s : %s"players[i][0], players[i][1], players[i][2])
    }

My issue is when I call list_players as clcmd (client command e.g "say /players"), I get the following output in the players console:
PHP Code:
tt127.0.0.1 t127.0.0.1 127.0.0.1
27.0.0.1 
7.0.0.1 .0.0.1
0.0.1 
.0.1 0.1
.1 

 :  : 
 :  : 
Why does the output appear so strangely? Am I doing something wrong?

The ideal output I'm needing should be:
PHP Code:
test123 test123 127.0.0.1 
Thanks guys..
__________________
No allowed!


Last edited by johnally; 05-10-2011 at 14:46.
johnally is offline
SonicSonedit
Veteran Member
Join Date: Nov 2008
Location: Silent Hill
Old 05-10-2011 , 15:45   Re: Keep List of players with details!
Reply With Quote #2

Should be something like this:

new
players[33]
[3][32];

players[id][1]=playerName
players[id][0]=playerName
players[id][2]=ip
__________________

SonicSonedit is offline
johnally
Member
Join Date: May 2011
Location: Mauritius
Old 05-10-2011 , 16:14   Re: Keep List of players with details!
Reply With Quote #3

Not sure If I'm allowed, but geez I want to jump through my screen and hug you

^^ you are of a real help here Really happy to have you on alliedmodders (This site holds its name)


Thanks A LOT!
__________________
No allowed!

johnally is offline
SonicSonedit
Veteran Member
Join Date: Nov 2008
Location: Silent Hill
Old 05-10-2011 , 16:23   Re: Keep List of players with details!
Reply With Quote #4

johnally
It's nice to see people who actually want to learn how to script here, too. Most people just want something to be done/fixed/changed for them. Also there is some interesting thread http://forums.alliedmods.net/showthread.php?t=154538 about how most of people ask questions here
Well, let's not start offtopic.
__________________

SonicSonedit is offline
johnally
Member
Join Date: May 2011
Location: Mauritius
Old 05-10-2011 , 16:35   Re: Keep List of players with details!
Reply With Quote #5

I don't want pre-made stuffs :S.. I'm no script kiddie in some sense..

Oh, yeah.. no offtopic.. some mods will slap us

Anyway, I got the listing down.. however, my new del_players function is not really attractive.. Replacing existing values in an array, when you can delete them, is not recommended.. do you know of a way to delete a whole index array entry.. lets say "players[5]" ?? In the amx/pawn wiki it does NOT show how

New del_players:
PHP Code:
// ----------------------------------------------------------------------------
// -----------------------Delete old player details---------------------------
public del_players(id) {      
    
players[id 1][0] = "[none]"
    
players[id 1][1] = "[none]"
    
players[id 1][2] = "[Player Left]"

Will it work if I use "^0" ?
__________________
No allowed!


Last edited by johnally; 05-10-2011 at 16:44.
johnally is offline
SonicSonedit
Veteran Member
Join Date: Nov 2008
Location: Silent Hill
Old 05-10-2011 , 16:47   Re: Keep List of players with details!
Reply With Quote #6

johnally
Quote:
Will it work if I use "^0" ?
In your current example you can simply use players[id - 1][0]=0 (putting string termination symbol as first symbol in the string) and then check if (players[id - 1][0][0]) (if true then string is not free)
Arrays are the whole other story - Check cellarray.inc. Anyway, you don't need them: there is no point in creating a dynamic array when you surely know that your max array size is 32.
__________________


Last edited by SonicSonedit; 05-10-2011 at 16:51.
SonicSonedit is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 05-10-2011 , 16:48   Re: Keep List of players with details!
Reply With Quote #7

That's not list. Make real list of ids of connected players and you won't have to worry about removing strings.
__________________
Impossible is Nothing
Sylwester is offline
johnally
Member
Join Date: May 2011
Location: Mauritius
Old 05-10-2011 , 16:53   Re: Keep List of players with details!
Reply With Quote #8

Thanks sonysonedit, I got the idea

@Sylwester.. Yeah, I know.. But instead of getting someone else work, I thought I could get down a custom player 'list'

PHP Code:
// ----------------------------------------------------------------------------
// ------------------------Add details to playerlist---------------------------
public add_players(id) { 
    new 
ip[16];
    new 
playerName[32]
    
    
// Get the name and ip
    
get_user_name(idplayerName31);
    
get_user_ip(idip151/*no port*/);
    
    new 
tempip[5][5]; // Hold the client ip temp for test
    
    // Check if user is local.. e.g local ip gets changed to external
    
ExplodeString(tempip54ip'.') ; // Split it up
    
if ((equal(tempip[0], "127")) || (equal(tempip[0], "192")) || (equal(tempip[0], "10")) || (equal(tempip[0], "172"))) {
        
format(ip,sizeof(ip),"%s",hostip);
    }
    
    
// Add these to our array using id as key
    
if (players[id 1][0][0]){ 
        
players[id 1][0] = playerName
        players
[id 1][1] = playerName
        players
[id 1][2] = ip
    
}

I'm pretty sure I won't need the 'if (players[id - 1][0][0])' check as two player cannot get same id?

Thanks,
__________________
No allowed!


Last edited by johnally; 05-10-2011 at 16:58.
johnally is offline
SonicSonedit
Veteran Member
Join Date: Nov 2008
Location: Silent Hill
Old 05-10-2011 , 16:53   Re: Keep List of players with details!
Reply With Quote #9

Sylwester
Slower & harder.
__________________

SonicSonedit is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 05-10-2011 , 17:13   Re: Keep List of players with details!
Reply With Quote #10

Quote:
Originally Posted by SonicSonedit View Post
Slower
Wrong.
Quote:
Originally Posted by SonicSonedit View Post
harder
For some users maybe. Not for me though.

EDIT:
I would do it this way:
PHP Code:
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Macros
#define _Set(%1,%2) %1|=1<<%2
#define _UnSet(%1,%2) %1&=~(1<<%2)
#define _Is(%1,%2) (%1&1<<%2)
new _pg_on_list

new _pg_list_pos[33]
new 
_pg_list[32]
new 
_pg_cnt
#define _ListAdd(%1) if(!_Is(_pg_on_list,%1)){_Set(_pg_on_list,%1);_pg_list[_pg_list_pos[%1]=_pg_cnt++]=%1;}
#define _ListRemove(%1) if(_Is(_pg_on_list,%1)){_UnSet(_pg_on_list,%1);_pg_list_pos[_pg_list[_pg_list_pos[%1]] = _pg_list[--_pg_cnt]] = _pg_list_pos[%1];}
#define _GetListMax() _pg_cnt
#define _GetListPlayer(%1) _pg_list[%1]
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


new g_ips[33][16]
new 
g_names[33][32]

public 
add_player(id) { 
    
_ListAdd(id)
    
get_user_name(idg_names[id], 31)
    
get_user_ip(idg_ips[id], 151)
}

public 
remove_player(id){
    
_ListRemove(id)
}

public 
client_infochanged(id){      
    new 
new_name[32]
    
get_user_info(id"name"new_name31)
    if(!
equal(new_nameg_names[id])){
        
copy(g_names[id], 31new_name)
    }
}

public 
list_players(id){
    new 
player_id
    client_print
(idprint_chat"Check console for Players' Details")
    for (new 
i=0i<_GetListMax(); i++){
        
player_id _GetListPlayer(i)
        
console_print(id"%s : %s "g_names[player_id], g_ips[player_id])
    }

__________________
Impossible is Nothing

Last edited by Sylwester; 05-10-2011 at 17:32.
Sylwester is offline
Reply



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 04:23.


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