Author Topic: Adding certain clients to a list.  (Read 1025 times)

I want it so if a client types a certain command, a / command, they will be added to a list. Other clients can then view that list with another command. Mainly I just need to know how to add a client to a list, and if they type the same / command again, they will be removed from that list.

Code: [Select]
function serverCmdAddMe(%client) {
   // Add them to the "list"
   $ClientList[ $ClientListCount++ ] = %client;

   // Done
}

function serverCmdRemoveMe(%client) {
   // Remove them from the "list"

   // Wait... are they even on it? Let's check:
   %index = -1;
   for(%i = 1; %i <= $ClientListCount; %i++) {
      if( $ClientList[ %i ] == %client ) {
         %index = %i;
         // Yep, they're on the list
         break;
      }
   }

   if( %index < 0 ) {
      // They're not on the list!!
      messageClient(%client, '', '\c3You\'re not on the list...');
      return;
   }

    // Remove them from the list
   $ClientList[ %index ] = "";

   // Bump back all the clients so we don't have a blank space in our $ClientList array

   for(%i = %index; %i <= $ClientListCount; %i++) {
      $ClientList[ %i ] = $ClientList[ %i + 1 ];
   }

   $ClientListCount--;
}

function serverCmdReadList(%client) {
   // Show them the list... loop thru it and append each client's name
   for(%i = 1; %i <= $ClientListCount; %i++) {
      %current_client_on_list = $ClientList[ %i ];
      %current_name = %current_client_on_list.name;
      %msg = %msg @ %current_name @ (%i < $ClientListCount ? "; " : "");
   }

   // Message them our %msg
   messageClient(%client, '', '\c3Clients on the list: \c2%1', %msg);
}
« Last Edit: November 26, 2007, 07:33:32 PM by exidyne »

Didn't really need the comments... but I guess they help a little. Thanks! :cookieMonster:

The bumpback will result in the person in the 0 value being pushed to the -1 value...

No, it only 'bumps back' the indexes above the blank space.

So if you have
1. a
2.
3. c
4. d

c & d get pushed back, so it's

1. a
2. c
3. d

Clients don't get removed from the list.

$ClientListCount = -1; //First ++ means the first value is $ClientList[0]

function serverCmdAddMe(%client) {
   // Add them to the "list"
   $ClientList[ $ClientListCount++ ] = %client;

   // Done
}

function serverCmdRemoveMe(%client) {
   // Remove them from the "list"

   // Wait... are they even on it? Let's check:
   %index = -1;
   for(%i = 0; %i <= $ClientListCount; %i++) { //Standard programming has arrays 0-(N-1) not 1-N
      if( $ClientList[ %i ] == %client ) {
         %index = %i;
         // Yep, they're on the list
         break;
      }
   }

   if( %index < 0 ) {
      // They're not on the list!!
      messageClient(%client, '', '\c3You\'re not on the list...');
      return;
   }

    // Remove them from the list
   $ClientList[ %index ] = "";

   // Bump back all the clients so we don't have a blank space in our $ClientList array

   for(%i = %index; %i <= $ClientListCount; %i++) {
      $ClientList[ %i ] = $ClientList[ %i + 1 ];
   }

   $ClientListCount--;
}

function serverCmdReadList(%client) {
   // Show them the list... loop thru it and append each client's name
   for(%i = 0; %i <= $ClientListCount; %i++) {
      %current_client_on_list = $ClientList[ %i ];
      %current_name = %current_client_on_list.name;
      %msg = %msg @ %current_name @ (%i < $ClientListCount ? "; " : "");
   }

   // Message them our %msg
   messageClient(%client, '', '\c3Clients on the list: \c2%1', %msg);
}