home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / doom_i / program / tcpsrv12.exe / TCPSRV12.TAR / command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-30  |  3.6 KB  |  177 lines

  1. /* command.c */
  2. /*
  3.  
  4.  * Copyright 1994 A.Oliver De Guzman
  5.  * All rights reserved
  6.  
  7.  *   Permission is granted to any individual to copy, use, and/or
  8.  * distribute this software provided that the distribution retains this
  9.  * entire copyright notice. No part of this software may be used and/or
  10.  * sold for profit or used with any commercial product.
  11.  
  12.  * DISCLAIMER:
  13.  *   This software comes with NO WARRANTIES of any kind. In no event
  14.  * will the author be liable for any financial, physical, moral, and/or
  15.  * mental damages incurred directly or indirectly by the use or intent
  16.  * to use of this software.
  17.  
  18.  */
  19.  
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "dfcss.h"
  23. #include "server.h"
  24. #include "node.h"
  25. #include "sockio.h"
  26. #include "fileio.h"
  27.  
  28. void PrintStat();
  29. void PrintSet();
  30. void Banner();
  31. void ListServers();
  32. void Who();
  33. void QuitCmd();
  34. void Help();
  35. extern void HelpSet();
  36. void ToPlayer();
  37. void ToGuest();
  38.  
  39. typedef struct{
  40.     char *name;
  41.     void (*func)();
  42. } Command;
  43.  
  44. Command commands[] = {
  45.     { "status", PrintStat },
  46.     { "stat", PrintStat },
  47.     { "who", Who },
  48.     { "quit", QuitCmd },
  49.     { "banner", Banner },
  50.     { "servers", ListServers },
  51.     { "player", ToPlayer },
  52.     { "guest", ToGuest },
  53.     { "help", Help },
  54.     { "help set", HelpSet },
  55.     { "?", Help },
  56. };
  57. #define NCOMMANDS        (sizeof(commands)/sizeof(Command))
  58.  
  59. char *helpstr[] = {
  60.     "/status                - print server status",
  61.     "/set <param> <value>   - set the value of parameter <param>",
  62.     "/who                   - list all nodes logged in",
  63.     "/banner                - print server's banner message",
  64.     "/servers               - list all known servers",
  65.     "/player                - become a player",
  66.     "/guest                 - become a guest",
  67.     "/help                  - this help message",
  68.     "/help set              - list parameter names",
  69.     "/quit                  - quit",
  70. };
  71. #define NHELP        (sizeof(helpstr)/sizeof(char *))
  72.     
  73.  
  74. int ServerStat(sd)
  75. int sd;
  76. {
  77.     extern int NUMPLAYERS;
  78.     char msg[MAXMSG+1], buff[64];
  79.     int i;
  80.  
  81.     sprintf(msg, "* Server version: %s", SERVERID);
  82.     sprintf(buff, "\n* Current Game: %d Player Game, %d Players, %d Guest(s)",
  83.         NUMPLAYERS, NumPlayers(), NumGuests());
  84.     strcat(msg, buff);
  85.     if (NumPlayers()){
  86.         strcat(msg, "\n");
  87.         strcat(msg, PlayersStat());
  88.     }
  89.     if (NumGuests()){
  90.         strcat(msg, "\n");
  91.         strcat(msg, GuestsStat());
  92.     }
  93.     ASockWrite(sd, msg);
  94. }
  95.  
  96. void PrintStat()
  97. {
  98.     int i;
  99.     ServerStat(nodes[nodeidx].sd);
  100. }
  101.  
  102. void Banner()
  103. {
  104.     extern char banner[];
  105.     ASockWrite(nodes[nodeidx].sd, banner);
  106. }
  107.  
  108. static char servers[MAXMSG+1];
  109. void ListServers()
  110. {
  111.     extern char SERVERLIST[];
  112.  
  113.     servers[0] = '\0';
  114.     if (readfile(SERVERLIST, servers, MAXMSG) < 0)
  115.         sprintf(servers, "List of Servers not found[%s].", SERVERLIST);
  116.     ASockWrite(nodes[nodeidx].sd, servers);
  117. }
  118.  
  119. void Who()
  120. {
  121.     if (NumPlayers()) ASockWrite(nodes[nodeidx].sd, PlayersStat());
  122.     if (NumGuests()) ASockWrite(nodes[nodeidx].sd, GuestsStat());
  123. }
  124.  
  125. void ToPlayer()
  126. {
  127.     extern int NUMPLAYERS;
  128.  
  129.     if (NumPlayers() < NUMPLAYERS && !IsUnused(nodes[nodeidx]))
  130.         nodes[nodeidx].type = PLAYER;
  131.     ServerStat(nodes[nodeidx].sd);
  132. }
  133.  
  134. void ToGuest()
  135. {
  136.     if (NumGuests() < MAX_GUESTS && !IsUnused(nodes[nodeidx]))
  137.         nodes[nodeidx].type = GUEST;
  138.     ServerStat(nodes[nodeidx].sd);
  139. }
  140.  
  141. void QuitCmd()
  142. {
  143.     ASockWrite(nodes[nodeidx].sd, ABORT);
  144.     ResetNode();
  145. }
  146.  
  147. void Help()
  148. {
  149.     char msg[MAXMSG+1];
  150.     int i;
  151.  
  152.     msg[0] = '\0';
  153.     for (i=0; i< NHELP; i++){
  154.         strncat(msg, "\n", MAXMSG);
  155.         strncat(msg, helpstr[i], MAXMSG);
  156.     }
  157.  
  158.     ASockWrite(nodes[nodeidx].sd, msg);
  159. }
  160.  
  161. int DoCommand(n, command)
  162. int n;
  163. char *command;
  164. {
  165.     int i;
  166.  
  167.     nodeidx = n;
  168.  
  169.     for (i=0; i<NCOMMANDS; i++){
  170.         if (!strcmp(command+1, commands[i].name)){
  171.             (commands[i].func)();
  172.             return(i);
  173.         }
  174.     }
  175.     return(-1);
  176. }
  177.