@@ -29,9 +29,49 @@ static int port=DEFAULT_PORT;
29
29
static struct sockaddr_in server ;
30
30
static char * name = NULL ;
31
31
static int ttime = 1 ;
32
+ static int verbose = 0 ; /* whether to print a verbose message */
33
+ static int toplist = 0 ; /* whether to print the list of top players */
32
34
int fd ;
33
35
34
36
37
+ /* print info based on the recieved packet */
38
+ static void print_info (char * packet )
39
+ {
40
+ int n_pl ; /* number of players */
41
+
42
+ n_pl = get_int (packet + 1 );
43
+ if (verbose ) {
44
+ if (n_pl )
45
+ printf ((n_pl > 1 )? "There are %d players on the server.\n"
46
+ : "There is %d player on the server.\n" , n_pl );
47
+ else
48
+ printf ("This server is not being used.\n" );
49
+ } else
50
+ printf ("%d\n" ,n_pl );
51
+ if (toplist && n_pl ) {
52
+ unsigned int n_tp = * (packet + 5 ); /* number of players on top list */
53
+
54
+ if (!n_tp )
55
+ printf ("The top list is empty.\n" );
56
+ else {
57
+ unsigned int i ; /* iterator */
58
+ /* pointer to the next set of player-specific info */
59
+ char * p = packet + 6 ;
60
+
61
+ packet [256 - 1 ]= '\0' ;
62
+ printf ("Players on top list (%d):\n" , n_tp );
63
+ printf ("rank\tfrags\tdeaths\tcolor\tname\n" );
64
+ for (i = 0 ; i < n_tp ; i ++ ) {
65
+ printf ("#%d\t%d\t%d\t%d\t%s\n" ,
66
+ i + 1 , get_int (p ), get_int (p + 4 ),
67
+ * (p + 8 ), p + 9 );
68
+ p += 10 + strlen (p + 9 );
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+
35
75
/* find server address from name and put it into server structure */
36
76
static void find_server (void )
37
77
{
@@ -58,7 +98,6 @@ static void init_socket(void)
58
98
static int contact_server (void )
59
99
{
60
100
static char packet [256 ];
61
- int a ;
62
101
63
102
fd_set fds ;
64
103
struct timeval tv ;
@@ -80,8 +119,7 @@ static int contact_server(void)
80
119
}
81
120
82
121
if ((* packet )!= P_INFO ){fprintf (stderr ,"Server error.\n" );return 1 ;}
83
- a = get_int (packet + 1 );
84
- printf ("%d\n" ,a );
122
+ print_info (packet );
85
123
return 0 ;
86
124
}
87
125
@@ -92,7 +130,9 @@ static void print_help(void)
92
130
{
93
131
printf ( "0verkill server testing program.\n"
94
132
"(c)2000 Brainsoft\n"
95
- "Usage: test_server [-h] [-p <port number>] [-t <timeout>] -a <address>\n" );
133
+ "Usage: test_server [-hvT] [-t <timeout>] [-p <port number>] -a <address>\n"
134
+ " -v produce verbose output\n"
135
+ " -T print the list of top players (implies -v)\n" );
96
136
}
97
137
98
138
@@ -117,10 +157,16 @@ static void parse_command_line(int argc,char **argv)
117
157
{
118
158
int a ;
119
159
120
- while ((a = getopt (argc ,argv ,"hp :a:t:" )) != -1 )
160
+ while ((a = getopt (argc ,argv ,"hvTp :a:t:" )) != -1 )
121
161
{
122
162
switch (a )
123
163
{
164
+ case 'T' :
165
+ toplist = 1 ;
166
+ case 'v' :
167
+ verbose = 1 ;
168
+ break ;
169
+
124
170
case 'a' :
125
171
name = optarg ;
126
172
break ;
0 commit comments