Turf MUD

Turf Data Retrieval System

  1. What is the Turf Data Retrieval System
  2. Clanlib - Simple Clan-Orientated Dynamic Content
  3. Using the Turf Data Retrieval System through PHP
  4. Turf Data System Commands
  5. Turf Data System Internals

What is the Turf Data Retrieval System

The Turf Data Retrieval System allows generic applications, such as dynamic web pages, to fetch up-to-the-instant public information from Turf in an easy-to-use format. The Turf Data Retrieval System is used by many pages, already, including the Turf homepage, the clans listing page, the leagues listing page, various clan hompages, and this page (introspectively).

Clanlib - Simple Clan-Orientated Dynamic Content


This section refers to a language known as PHP - you may pick up the essentials of this language from the section below, however, complete documentation is available in a variety of formats from the PHP documentation page.


Clanlib provides a simple interface for clans to extract dynamic information. To make use of this library create a file ending with .php - if this is to be your clan's main page name it index.php. Add the following to your .php file at the top:

        <?php
          require( "http://www.turf.org/datasys/clanlib.phps" );
        ?>
      
All example fragmets of code below assume that this sequence has appeared at the top of the file.

The basic unit of data in Clanlib is an array representing your clan. Bits of information can then be easily displayed. For example, this would get the clan information of clan "Newbies" and show their player killing status:

        <?php
          $newbies = clan_info( "newbies" );

          if ( $newbies["pk"] ) {
            echo( $newbies["name"] . " is a player killing clan - die!" );
          } else {
            echo( $newbies["name"] . " is a peaceful clan - group with us." );
          }
        ?>
      
This example also handles name changes - "Newbies" was the original name, so we can use it in Clanlib and Clanlib will tell us the current name in case it was changed.

The clan info array provides the following pieces of information:

"active"
"balance"
"chieftain"
"clanheroes"
"description"
"election"
"election_end"
"financing"
"internal_name"
"joinable"
"joinclose"
"max_clanheroes"
"max_subchiefs"
"mdeaths"
"members"
"mkills"
"motto"
"name"
"overlord"
"pdeaths"
"pk"
"pkills"
"plosses"
"ps"
"pthefts"
"salestax"
"score"
"subchiefs"
"url"
"withdraw_chieftain"
"withdraw_clanhero"
"withdraw_clansman"
"withdraw_subchief"

Clanlib provides the following functions:

clan_info( "clan name" )

This function returns an array containing the clan information shown above for the clan matching "clan name". Note that clan names can change - if you use the original clan name your page will always work.

Example Code

              <?php
                $omks = clan_info( "omks" );

                if ( $omks["overlord"] ) {
                  echo( "The current OMKS Overlord is " . $omks["overlord"] );
                } else {
                  echo( "We're currently electing our overlord - the voting "
                        . "ends around " . $omks["election"] );
                }
              ?>
            
Example Result
The current OMKS Overlord is Mango
or
We're currently electing our overlord - the voting ends around 4.42:13 PM - Fri, 13 April 2001

clan_sortby( "clan attribute" )

Set the sorting order to be dependent on "clan attribute" - this may by any attribute in a clan array (as listed above). This function affects clan_target( "clan name" ), clan_anklebiter( "clan name" ), clan_rival( "clan name" ), and clan_rank( "clan name" ).

Example Code

              <?php
                clan_sortby( "balance" );

                $rank = clan_rank( "omks" );

                if ( $rank == 1 )
                  $rd = "st";
                elseif ( $rank == 2 )
                  $rd = "nd";
                elseif ( $rank == 3 )
                  $rd = "rd";
                else
                  $rd = "th";

                echo( "OMKS currently ranks " . $rank . $rd . " in clan bank "
                      . "account balance." );
              ?>
            
Example Result
OMKS currently ranks 1st in clan bank account balance.

clan_rank( "clan name" )

This function returns the ranking of clan "clan name" compared to all other clans regarding whatever the current sorting attribute is (set by clan_sortby( "clan attribute" ) and defaulting to "score" if clan_sortby( "clan attribute" ) has not been called yet.)

clan_target( "clan name" )

This function returns the ranking above clan "clan name" by one place regarding whatever the current sorting attribute is (set by clan_sortby( "clan attribute" ) and defaulting to "score" if clan_sortby( "clan attribute" ) has not been called yet.) This function may return nothing if no clan is above "clan name" with regards to the sorting attribute (i.e. if your clan is in first place then you don't have a target).

clan_anklebiter( "clan name" )

This function returns the ranking below clan "clan name" by one place regarding whatever the current sorting attribute is (set by clan_sortby( "clan attribute" ) and defaulting to "score" if clan_sortby( "clan attribute" ) has not been called yet.) This function may return nothing if no clan is below "clan name" with regards to the sorting attribute (i.e. if your clan is in last place then you don't have an anklebiter).

clan_rival( "clan name" )

This function returns the clan with the score closest to clan "clan name" whether ahead or behind by one rank regarding whatever the current sorting attribute is (set by clan_sortby( "clan attribute" ) and defaulting to "score" if clan_sortby( "clan attribute" ) has not been called yet.) This function may return nothing if no clan is above or below "clan name" with regards to the sorting attribute (i.e. if your clan is the only one then you clearly don't have a rival).

clan_members( "clan name" )

This function returns an array of clan members, sorted in rank order. Each element of the array is in turn an array containing:

Example Code

              <TABLE border="0">
                <TR>
                  <TD>Name</TD>
                  <TD>Rank</TD>
                  <TD>Contribution</TD>
                </TR>
                <?php
                  $members = clan_members( "omks" );

                  for ( $index = 0; $index < count( $members ); ++$index ) {
                    echo( "<TR>\n" );
                    echo( "<TD>" . $members[$index]["name"] . "</TD>\n" );
                    echo( "<TD>" . $members[$index]["rank"] . "</TD>\n" );
                    echo( "<TD align=\"right\">" . $members[$index]["contribution"] . "</TD>\n" );
                    echo( "</TR>\n" );
                  }
                ?>
              </TABLE>
            
Example Result
Name Rank Contribution
Mango overlord 51
Katie chieftain 703

Example Clanlib Use
More example code using Clanlib can seen here.

Using the Turf Data Retrieval System through PHP


This section assumes a good working knowledge of PHP - you may pick up the essentials of this language from the section below, however, complete documentation is available in a variety of formats from the PHP documentation page.

Turf's Data Retrieval System can be used through PHP with the following PHP library file:

If the page using this file is located on www.turf.org it may be a good idea to use the link directly in PHP rather than copy the file, to ensure that the latest version is always used.

To use the library call

where command_list is an array of commands you wish to have executed. This function will return an array which contains one entry for each command sent. Each of these entries is in turn an array with at least three fields docstring is a documentation string for the command, and may include some helpful information. error will be an empty string ("") unless there was in fact an error, in which case there will be a descriptive message. records is an array and will contain one entry, starting at 0, for each record that a Turf command produces. Note that putting as many Turf data system commands in a single call to turf_fetch_data is much more efficient than using multiple calls, and will result in much faster loading times for your pages.

You can view an example of using the library and the resulting PHP data structures and array members for an example commmand using this form:

Example Code Using turfDRS.phps
This (complete) section of code will list the heroes in the Heroes's League.

        <?php
          require( "datasys/turfDRS.phps" );

          $pos = array( "Zeroth", "First", "Second",
                        "Third", "Fourth", "Fifth" );

          $data = turf_fetch_data( array( "/league/heroes" ) );
        ?>
        <TABLE border="1">
          <?php
            $hl = $data["/league/heroes"]["records"];

            for ( $index = 0; $index < count( $hl ) && $index < 50; ++$index ) {
              if ( $index % 10 == 0 ) {
                echo( "<TR>\n" );
                echo( "  <TH colspan=\"4\">" . $pos[$index / 10 + 1] . " Division</TH>\n" );
                echo( "</TR>\n" );
              }

              echo( "<TR>\n" );
              echo( "  <TD>" . (($index % 10) + 1) . "</TD>\n" );
              echo( "  <TD>" . $hl[$index]["name"] . "</TD>\n" );
              echo( "  <TD class=\"digit\">" . number_format( $hl[$index]["score"] ) . "</TD>\n" );
              echo( "  <TD>" . ereg_replace( " ", " ", date( "h.i:s A - D, d F Y T", $hl[$index]["time"] ) ) . "</TD>\n" );
              echo( "</TR>\n" );
            }
          ?>
        </TABLE>
      
More Examples

Turf Data System Commands

The following commands are currently available from the Turf Data System:

/help
This is the help index for the Turf data retrieval system. Listed below are the available commands - each will be preceeded by a "docstring" entry explaining any command-specific details.

This command returns a single record with the element "commands" which is a list of available commands.
/clans/info
Clan information

"internal_name" is a constant name which can be used to track a clans progression - it is not the public name and may NOT be displayed - use "name" for display purposes.

All strings are either the value stored or "" - there are no replacement strings like "Promote someone to Chieftain!".

Clans are listed in ranked order.

Ordering of the financing list is important.
/clans/members
Clan Members Listing

Each item in "members" is an associate array with the following fields:

{ name, rank, contribution }

This is subject to change - no assumptions should be made about the length of this list, however all additions will be appended to the list, so existing fields will not change position (maybe).

Note that not all members will be listed here, only active ones.
/clans/scoring
Clan Scoring Info

This contains information relevant to clan scoring. Currentlyonly the minimum divisor used to calculate contribution.
/finger
Finger

This function requires a player name argument.

Note that clan is a bit funny, as a player may be listed here as clan, but not in the corresponding members list. This indicates an idle member.
/league/heroes
Heroes' League

This should be self-explanatory.
/league/millionaires
Millionaires' League

This should be self-explanatory.
/league/waypoints
Waypoint League

This should be self-explanatory.
/records
Turf Records

Fields may or may not be present - test for existence, don't depend on it.
/time
Time/Date information

Provides the Turfish time, date, day and month.
/weather
Turf Weather

Provides information about the weather from a globalperspective.

Change : -6 to 6
Temp : -5 to 5
Virtual Temp : -5 to 5
Clouds : 0 to 5
Wind : 0 to 4
Sun : 0 to 3
Virtual temp is the temperature including wind/rain/clouds. Use it unless you know what you're doing as this is the 'apparent' temperature.
Sun is in dark, rise, light, or set, respectively. light is the pre-dawn glow, set is the post-sunset glow.
To see what data Turf will give out you can either use the above links to manually view the resulting data, or view the data as given by the PHP library provided using the form below.

Turf Data System Internals

The Turf data system is built on top of HTTP. The port on which it runs is the port Turf runs on plus 2. Only the GET line of a request will be honoured, and all connections are connection-close style only.

The Turf data system is organised into records - each command will return one or more records, each on a line of its own. Each record is composed of name-value pairs. The exact sequence of return is:

A special line may replace any or all records in the reply to a command. This does not necessarily indicate abortion of the command execution or the end of a reply.

The value half of a name-value pair may be any of four data types - please note that regardless of type all values are followed by a comma:

A digit is a whole-number represented in base 10. A string is represented by a URL-encoded string surrounded in double-quotes ("). URL-encoding prevents mistaking the data for the output formatting. A list is represented by opening and closing brackets - '(' and ')' - a list may contain sub-values of any given value type. Ordering of list elements may be incidental or intentional - consult the relevant docstring for the command. An associative array is represented by a set of curly brackets - '{' and '}' - and contains a series of name-value pairs.


Back to Turf's Homepage