home *** CD-ROM | disk | FTP | other *** search
/ Powerdrive 1997 February / POWERDRIVE0297.ISO / share / rollen / nethack / misc / reclist.c < prev    next >
C/C++ Source or Header  |  1994-12-13  |  7KB  |  242 lines

  1. /* List the Nethack record file and the average score
  2.    reclist -ffilename -l -uplayername -character -nnumber -d
  3. */
  4.  
  5. /* Original program for MS-DOS by J. Lahtinen, August 1993,
  6.    usenet: walker@mits.mdata.fi
  7.    Adapted for Unix, -l option added, sources translated from finnish,
  8.    by Boudewijn Wayers, March 1994, usenet: dedos4@win.tue.nl
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #define RECORD    "/home/svin04d/dedos4/bin/nethackdir/record"
  17. #define LOGFILE    "/home/svin04d/dedos4/bin/nethackdir/logfile"
  18. #define VERSION    "2.2"
  19.  
  20. char *ver[]="2.2";  /* version */
  21.  
  22. int main(argc, argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.  FILE *input;
  27.  
  28.  int  i,
  29.       num=0,
  30.       maxscores=0,
  31.       d=0;
  32.  long sum=0;
  33.  
  34.  char class=0,
  35.       line[150];
  36.  
  37.  char *name=NULL,
  38.       *filename=RECORD;
  39.  
  40.  long checkline();
  41.   int error();
  42.  
  43.  /* Command line parameters */
  44.  for (i=1; i<argc; i++) {
  45.     if (argv[i][0]=='/')
  46.        argv[i][0]='-';
  47.     if (argv[i][0]!='-')
  48.        error(argv[0],argv[i]);
  49.     if (!(strncmp(argv[i],"-f",2)))       /* filename        */
  50.        if (strlen(argv[i])==2) {
  51.           i++;
  52.           filename=argv[i]; }
  53.        else
  54.           filename=argv[i]+2;
  55.     else if (!(strncmp(argv[i],"-l",2)))  /* logfile request */
  56.        filename=LOGFILE;
  57.     else if (!(strncmp(argv[i],"-u",2)))  /* playername      */
  58.        if (strlen(argv[i])==2) {
  59.           i++;
  60.           name=argv[i]; }
  61.        else
  62.           name=argv[i]+2;
  63.     else if (!(strncmp(argv[i],"-n",2)))  /* number    */
  64.        if (strlen(argv[i])==2) {
  65.           i++;
  66.           maxscores=atoi(argv[i]); }
  67.        else
  68.           maxscores=atoi(argv[i]+2);
  69.     else if (argv[i][1]=='d')
  70.        d=1;
  71.     else
  72.        if (argv[i][0]=='-') {
  73.           switch (toupper(argv[i][1])) { /* character class */
  74.              case 'A': case 'B': case 'C': case 'E': case 'H':
  75.              case 'K': case 'P': case 'R': case 'S': case 'T':
  76.              case 'V': case 'W':
  77.                   class=toupper(argv[i][1]);
  78.                   break;
  79.              default:
  80.                   error(argv[0],argv[i]);
  81.  }     }  }
  82.  
  83.  /* Command line ok */
  84.  
  85.  if (!(input=fopen(filename,"r"))) {
  86.     printf ("File %s not found\n",filename);
  87.     exit (2);
  88.  }
  89.  printf("%-71s%s"," No     Points  Name","Hp [max]\n");
  90.  fgets(line,150,input);
  91.  while ((!(feof(input))) && ((!maxscores) || (num<maxscores))) {
  92.    sum += checkline(line,name,class,&num,d);
  93.    fgets(line,150,input);
  94.  }
  95.  if (num)
  96.     printf("\n%d scores. Average score %ld.\n",num,sum/num);
  97.  else
  98.     printf("\nNo games found.\n");
  99.  fclose (input);
  100.  return 0;
  101. }
  102.  
  103.  
  104. /* Interpret one line of the file */
  105.  
  106. long checkline(line,playername,playerclass,num,d)
  107. char *line;
  108. char *playername;
  109. char playerclass;
  110. int *num;
  111. int d;
  112. {
  113.  int  pos,
  114.       pos2,
  115.       dtype,
  116.       dlev,
  117.       maxlev,
  118.       hp,
  119.       maxhp;
  120.  static int nro=0;
  121.  long score,
  122.       pvm;
  123.  char class,
  124.       gender,
  125.       raato[70],
  126.       ascend=0;
  127.  char *syy;
  128.  static char *dungeon[]={"The Dungeons of Doom",/* 0 */
  129.                          "Gehennom",            /* 1 */
  130.                          "The Gnomish Mines",   /* 2 */
  131.                          "The Quest",           /* 3 */
  132.                          "Fort Ludios",         /* 4 */
  133.                          "Vlad's Tower",        /* 5 */
  134.                          "The Endgame"};        /* 6 */
  135.  
  136.  nro++;
  137.  
  138.  sscanf (line,"%ld %*d %d %d %d %d %d %ld %c%c",
  139.         &pvm,&dtype,&dlev,&maxlev,&hp,&maxhp,&score,&class,&gender);
  140.  
  141.  if (class<'A')
  142.     return (0);
  143.  if ((playerclass) && (playerclass!=class))
  144.     return (0);
  145.  
  146.  for (pos=0;!isalpha(line[pos]);pos++);
  147.  if (pos>strlen(line))  /* will not be in a valid file, just check.. */
  148.     return (0);
  149.  pos += 3;
  150.  for (pos2=0;(line[pos]!=',')&&(pos2<sizeof(raato));pos2++,pos++)
  151.     raato[pos2]=line[pos];
  152.  raato[pos2]=0;
  153.  
  154.  if ((playername) && (strcmp(playername,raato)))
  155.     return (0);
  156.  
  157.  printf("%3d%11ld  ",nro,score);
  158.  syy= &line[pos+1];
  159.  strcat(raato,"-");
  160.  pos=strlen(raato);
  161.  raato[pos]=class; raato[pos+1]=0;
  162.  if (!(strncmp(syy,"poisoned",8)))
  163.     strcat(raato," was poisoned");
  164.  else if (!(strncmp(syy,"petrified",9)))
  165.     strcat(raato," turned to stone");
  166.  else if (!(strncmp(syy,"starvation",10)))
  167.     strcat(raato," starved to death");
  168.  else if (!(strncmp(syy,"choked",6)))
  169.     if (gender=='M')
  170.        strcat(raato," choked on his food");
  171.     else
  172.        strcat(raato," choked on her food");
  173.  else if (!(strncmp(syy,"quit",4)))
  174.           strcat(raato," quit");
  175.  else if (!(strncmp(syy,"ascended",8))) {
  176.     strcat(raato," ascended to demigod-hood");
  177.     ascend=1; }
  178.  else
  179.     strcat(raato," died");
  180.  if (!ascend) {
  181.     pos=strlen(raato);
  182.     sprintf (raato+pos," in %s on level %d",dungeon[dtype],dlev);
  183.     if (maxlev!=dlev)
  184.        sprintf(raato+strlen(raato)," [max %d].",maxlev);
  185.     else strcat(raato,".");
  186.     if (strlen(raato)>55) {
  187.        raato[pos]=0;
  188.        if (d) {
  189.           printf ("%s\n %ld\t\t",raato,pvm);
  190.           d=0;
  191.        }
  192.        else
  193.           printf("%s\n\t\t",raato); /* kuolintapaan asti */
  194.        pos++;
  195.        pos2=0;
  196.        while (raato[pos]) {
  197.            raato[pos2]=raato[pos];
  198.            pos++;
  199.            pos2++; }
  200.        raato[pos2]=0;
  201.     }
  202.     if (strlen(raato)==55)
  203.        raato[54]=0;
  204.  }
  205.  printf("%-54s",raato);
  206.  if (hp>0)
  207.     printf("%3d",hp);
  208.  else
  209.     printf("  -");
  210.  sprintf(raato,"[%d]",maxhp);
  211.  printf("%6s\n",raato);
  212.  if ((strncmp(syy,"quit",4)) && (strncmp(syy,"starvation",10)) && (!ascend))
  213.  {   syy[0]=toupper(syy[0]);
  214.      syy[strlen(syy)-1]=0;  /* replace the LF  */
  215.     if (d)
  216.        printf(" %ld",pvm);
  217.     printf("\t\t%s.\n",syy);
  218.  }
  219.  (*num)++;
  220.  return (score);
  221. }
  222.  
  223. int error(argv_0,argv_i)
  224. char *argv_0;
  225. char *argv_i;
  226. { printf("This is %s, version %s.\n",argv_0,VERSION);
  227.   printf("List the Nethack record file and calculate the average score.\n\n");
  228.   printf("Usage: %s [-f file] [-l] [-u name] [-abcehkprstvw] [-n] [-d]\n\n",argv_0);
  229.   printf("The options are the following:\n\n");
  230.   printf("  -f file        to read the scores from file \"filename\"\n");
  231.   printf("  -l             to read the scores from the logfile\n");
  232.   printf("  -u name        to list only the scores of player \"name\"\n");
  233.   printf("  -abcehkprstvw  to list only the scores for one class\n");
  234.   printf("  -n number      to list only a given number of scores\n");
  235.   printf("  -d             to show the dates as well\n\n");
  236.   printf("You may specify any combination of these in any order.\n");
  237.   printf("The space after -f, -u or -n is optional.\n");
  238.   printf("The output may also be redirected with '>' or '|'.\n\n");
  239.   printf("Unknown parameter %s\n",argv_i);
  240.   exit(1);
  241. }
  242.