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

  1. /* node.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 <stdio.h>
  22. #include <string.h>
  23. #include "dfcss.h"
  24. #include "node.h"
  25.  
  26. Node nodes[MAX_NODES];
  27. int nodecount = 0, nodeidx = -1;
  28.  
  29. char *nodetypes[] = {
  30.     "player",
  31.     "guest",
  32.     "status",
  33.     "god",
  34.     "unknown",
  35. };
  36.  
  37. int InitNode(n)
  38. int n;
  39. {
  40.     nodes[n].type = PLAYER;
  41.     nodes[n].status = UNUSED;
  42.     nodes[n].sd = -1;
  43.     strcpy(nodes[n].name, "Anonymous");
  44.     strcpy(nodes[n].email, "email@host");
  45.     strcpy(nodes[n].alias, "alias");
  46.     strcpy(nodes[n].ip, "0.0.0.0");
  47. }
  48.  
  49. int InitNodes()
  50. {
  51.     int i;
  52.     for (i=0; i<MAX_NODES; i++) InitNode(i);
  53. }
  54.  
  55. int NewNodeIndex()
  56. {
  57.     int i;
  58.     for (i=0; i<MAX_NODES; i++)
  59.         if (IsUnused(nodes[i])) return(i);
  60.  
  61.     return(-1);
  62. }
  63.  
  64. int NewNode(type, sd, name, email, alias, ip)
  65. int type;
  66. int sd;
  67. char *name, *email, *alias;
  68. char *ip;
  69. {
  70.     int n;
  71.  
  72.     n = NewNodeIndex();
  73.     if (n!=-1){
  74.         nodes[n].status = ACTIVE;
  75.         nodes[n].type = type;
  76.         nodes[n].sd = sd;
  77.         if (name) strcpy(nodes[n].name, name);
  78.         if (email) strcpy(nodes[n].email, email);
  79.         if (alias) strcpy(nodes[n].alias, alias);
  80.         if (ip) strcpy(nodes[n].ip, ip);
  81.     }
  82.     return(n);
  83. }
  84.  
  85. int NumPlayers()
  86. {
  87.     int i, n=0;
  88.     for (i=0; i<MAX_NODES; i++)
  89.         if (IsPlayer(nodes[i])) n++;
  90.     return(n);
  91. }
  92.  
  93. int NumGuests()
  94. {
  95.     int i, n=0;
  96.     for (i=0; i<MAX_NODES; i++)
  97.         if (IsGuest(nodes[i])) n++;
  98.     return(n);
  99. }
  100.  
  101. int NumGods()
  102. {
  103.     int i, n=0;
  104.     for (i=0; i<MAX_NODES; i++)
  105.         if (IsGod(nodes[i])) n++;
  106.     return(n);
  107. }
  108.  
  109.  
  110. int GetNodeType(s)
  111. char *s;
  112. {
  113.     int i;
  114.     for (i=0; i< UNKNOWN; i++)
  115.         if (!strcmp(s, nodetypes[i])) return(i);
  116.  
  117.     return(UNKNOWN);
  118. }
  119.  
  120. char *StrNodeType(type)
  121. int type;
  122. {
  123.     if (type < UNKNOWN) return(nodetypes[type]);
  124.     else return(nodetypes[UNKNOWN]);
  125. }
  126.  
  127. int WriteNodes(msg)
  128. char *msg;
  129. {
  130.     int n=0;
  131.  
  132.     for (nodeidx=0; nodeidx<MAX_NODES; nodeidx++){
  133.         if (IsActive(nodes[nodeidx])){
  134.             ASockWrite(nodes[nodeidx].sd, msg);
  135.             n++;
  136.         }
  137.     }
  138.     return(n);
  139. }
  140.  
  141. char *NodeStat(n)
  142. int n;
  143. {
  144.     static char msg[MAXMSG+1];
  145.  
  146.     sprintf(msg, nodes[n].alias);
  147.     strncat(msg, "<", MAXMSG);
  148.     strncat(msg, nodes[n].email, MAXMSG);
  149.     strncat(msg, "> ", MAXMSG);
  150.     strncat(msg, nodes[n].name, MAXMSG);
  151.     strncat(msg, " [", MAXMSG);
  152.     strncat(msg, nodes[n].ip, MAXMSG);
  153.     strncat(msg, "]", MAXMSG);
  154.     return(msg);
  155. }
  156.  
  157.  
  158. char *GuestsStat()
  159. {
  160.     static char msg[MAXMSG+1];
  161.     char buff[128];
  162.     int i;
  163.  
  164.     msg[0] = '\0';
  165.     if (NumGuests()){
  166.         sprintf(msg, "* Guests");
  167.         sprintf(buff, "(%d):", NumGuests()); strcat(msg, buff);
  168.         for (i=0; i<MAX_NODES; i++){
  169.             if (IsGuest(nodes[i])){
  170.                 sprintf(buff, "\n* %2d ", i); strncat(msg, buff, MAXMSG);
  171.                 strncat(msg, NodeStat(i), MAXMSG);
  172.             }
  173.         }
  174.     }
  175.     return(msg);
  176. }
  177.  
  178.  
  179. char *PlayersStat()
  180. {
  181.     static char msg[MAXMSG+1];
  182.     char buff[128];
  183.     int i;
  184.  
  185.     msg[0] = '\0';
  186.     if (NumPlayers()){
  187.         sprintf(msg, "* Players");
  188.         sprintf(buff, "(%d):", NumPlayers()); strcat(msg, buff);
  189.         for (i=0; i<MAX_NODES; i++){
  190.             if (IsPlayer(nodes[i])){
  191.                 sprintf(buff, "\n* %2d ", i); strncat(msg, buff, MAXMSG);
  192.                 strncat(msg, NodeStat(i), MAXMSG);
  193.             }
  194.         }
  195.     }
  196.     return(msg);
  197. }
  198.