Coding by Example: +where
Step #1: Set up the object.
Step #2: The Command
The command has two parts:
I've decided I only want two elements listed for each player connected to the game -- the name and the name of the room they are in. Thus, I set up my header information with two justifications and a line to separate the header from the data. I assign the name region to have 20 spaces and the room name to have 40 spaces. To do this, the ljust() function is utilized. Ljust() is merely the function call for left justification.
To separate it, I place a line feed (%r) after the second ljust call and then put in a repeat to write a line of dashes. This section is @pemit %#=[ljust(Name,18)][ljust(Room,40)]%r[repeat(-,78)];
Evaluation of this appears as:
Now we need to start working on the list of players remembering that we have allocated 20 spaces to the player name and 40 spaces to the room name. Since we want one line per player as output we will make use of the list() function rather than using iter(). This allows easier control of formatted outputs.
The first step is to produce the first argument for the list() function. The historical method would be just lwho(). However, since +where is commonly used on privileged objects as a global command, we want to make sure that lwho() is processed as the enactor. That eliminates the case of dark wizards being returned in the lwho() list. To do this, we make use of the objeval() function which evaluates a set of code like a specific user rather than based on the privileges of the object it is on.
The list portion of the list() function will be:
Next we want to write the formatted text for each player as the second argument for the list() function. Remember, as list() processes each element in its first argument, that value is passed as ## to the second argument. The first half of the outputed text will be the player name left justified in a 20 space section:
The processing for this starts with ## which is a dbref from the lwho() listing. It then calls the name() function which returns the name associated with that dbref and finally left justifiies the returned name over 20 spaces.
The second half of the outputed text is the room name where the player player is located. However, if the player has the Unfindable flag set, we do not want to return that players location. To do this we will be using switch() combined with the hasflag() function. There are several alternate ways to do this but hasflag() is sufficient for our purpose.
The code for the room string looks like this:
The logic flow is as follows:
Does the player have the unfind flag?
Here is how the ouput looks once the full system is working:
The @decompile of the object is: