#include <time.h>
#include <stdio.h>
#include <rmsdef.h>
#include <descrip.h>
#include "LPMUD_DIR:[secure]security.h"

void get_list (char *);
void sort_list (void);
void print_list (void);

struct player_struct
 {
  char name [40];
  char level_title [80];
  int level;
  int experience;
  int age;
 };


/*** Because this is a virtual memory machine, and this won't ***/
/*** normally be run when the mud is running, this array is o.k. ***/
/*** large, but o.k. ***/
struct player_struct players [5000];
static int player_number;

main ()
{
 get_list ("LPMUD_DIR:[mudlib.players]*.o");
 sort_list ();
 print_list ();
}


void get_list (char * path)
{
  /* Declare storage for the input file name, and the output file name */
  /* The VAX will want these in stupid descriptor form, hence the strange */
  /* declarations. */
  static $DESCRIPTOR (filespec, 
         "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
  static $DESCRIPTOR (new_files, 
         "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
  /* The context for the find_file call, so I can get multiple files using */
  /* wild card stuff. */
  unsigned long context = 0;
  /* The returned value from the call. */
  long status;
  /* number of files found */
  int file_count = 0;
  /* So we can re-initialize the length of the new_files descriptor */
  int old_length;
  /* Your basic dummy variables. */
  int i, j;

 strcpy (filespec.dsc$a_pointer, path);
 filespec.dsc$w_length = strlen (filespec.dsc$a_pointer);

 old_length = new_files . dsc$w_length;

 again:

  new_files . dsc$w_length = old_length;

  /* Go get me a file that matches that filespec */
  status = lib$find_file (&filespec, &new_files, &context, 0, 0, 0, 0);

  if (status & 1)
    {
     get_info (new_files . dsc$a_pointer);

     file_count ++;

     goto again;
   }
  else if (status == RMS$_FNF)
    printf ("File not found!\n");
  else if (status == RMS$_DNF)
    printf ("Directory not found!\n");
  else if (status == RMS$_NMF)
    ;  /* Do nothing.  We go to the end of the list. */
  else
     if (! (status & 1))
       {
        printf ("Status = %X\n", status);
        lib$signal (status);
       }

 /*** Free up file_find context memory. ***/
 lib$find_file_end (&context);
}


get_info (char * filename)
{
 FILE * f;
 char line [250];
 int level;
 char cap_name [40];
 char level_title [80];
 int age;
 int exp;

 f = fopen (filename, "r");

 while (! feof (f))
   {
    fgets (line, 249, f);
    if (sscanf (line, "level %d", &level) == 1)
      players [player_number] . level = level;

    if (sscanf (line, "age %d", &age) == 1)
      players [player_number] . age = age;

    if (sscanf (line, "experience %d", &exp) == 1)
      players [player_number] . experience = exp;

    if (sscanf (line, "cap_name %s", cap_name) == 1)
      strcpy (players [player_number] . name, cap_name);

    if (sscanf (line, "title %s", level_title) == 1)
      strcpy (players [player_number] . level_title, level_title);

   }

 if (level < EXPLORE)
   player_number ++;

 fclose (f);
}


void sort_list ()
{
 int i, j;
 struct player_struct temp;

 for (i = 0; i < player_number; i ++)
    for (j = i + 1; j < player_number; j ++)
       {
        if (players [i] . experience < players [j] . experience)
          {
           temp . experience = players [i] . experience;
           temp . level = players [i] . level;
           temp . age = players [i] . age;
           strcpy (temp . name, players [i] . name);
           strcpy (temp . level_title, players [i] . level_title);

           players [i] . experience = players [j] . experience;
           players [i] . level = players [j] . level;
           players [i] . age = players [j] . age;
           strcpy (players [i] . name, players [j] . name);
           strcpy (players [i] . level_title, players [j] . level_title);

           players [j] . experience = temp . experience;
           players [j] . level = temp . level;
           players [j] . age = temp . age;
           strcpy (players [j] . name, temp . name);
           strcpy (players [j] . level_title, temp . level_title);
          }
       }
}


void print_list ()
{
 FILE * f;
 int i;
 int t;


 f = fopen ("LPMUD_DIR:[mudlib]top.players", "w");

 t = time ("EST");
 fprintf (f, "Top Players list at %s\n", ctime (& t));
 fprintf (f, "Rank\t\t\tName\t\t\tLevel\n");
 if (player_number > 10)
   player_number = 10;
 for (i = 0; i < player_number; i++)
     fprintf (f, "<%2d>\t%20s\t\t\t%3d\n", i + 1,
                                players [i] . name,
                                players [i] . level);
 fclose (f);

}