]> git.pond.sub.org Git - empserver/blob - src/lib/commands/play.c
33116983e59afdce907e8f9dfeb137e51d5ae76f
[empserver] / src / lib / commands / play.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  play.c: Who is logged on?
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1998
33  */
34
35 #include <config.h>
36
37 #include "misc.h"
38 #include "player.h"
39 #include "file.h"
40 #include "nat.h"
41 #include "optlist.h"
42 #include "commands.h"
43
44 static void play_header(void);
45 static int play_list(struct player *joe);
46
47 int
48 play(void)
49 {
50     struct player *joe;
51     int saw = 0;
52
53     play_header();
54     for (joe = player_prev(0); joe; joe = player_prev(joe)) {
55         saw += play_list(joe);
56     }
57     if (player->god || opt_BLITZ)
58         pr("%d player%s\n", saw, splur(saw));
59
60     return RET_OK;
61 }
62
63 static void
64 play_header(void)
65 {
66     prdate();
67     pr("%9s %3s %-32s %5s %5s %-20s\n",
68        "", "#", "", "time", "idle", "last command");
69 }
70
71 static int
72 play_list(struct player *joe)
73 {
74     time_t now;
75     char com[1 + 6*20 + 2];     /* UTF-8 */
76     struct natstr *natp;
77     struct natstr *us;
78     int n;
79
80     if (joe->cnum >= MAXNOC || !(natp = getnatp(joe->cnum)))
81         return 0;
82
83     us = getnatp(player->cnum);
84     if (player->god) {
85         /* We are a god, we see everything */
86     } else if (opt_BLITZ) {
87         /* It's a blitz, we see everything */
88     } else if (joe->god) {
89         /* This country is a god, so we see it */
90     } else if (us->nat_stat == STAT_VIS) {
91         /* We are a visitor country, we can't see squat, except deities */
92         return 0;
93     } else if (joe->cnum != player->cnum) {
94         /* This isn't us.  Can we see it? */
95         if (natp->nat_stat == STAT_VIS) {
96             /* Yes, we can see visitors are logged on */
97         } else if (getrel(natp, player->cnum) < ALLIED) {
98             /* This is a non-allied country, don't show it. */
99             return 0;
100         }
101     }
102
103     time(&now);
104     pr("%-9.9s %3d %32.32s %2d:%02d %4lds",
105        cname(joe->cnum),
106        joe->cnum,
107        player->god || joe->cnum == player->cnum ? praddr(joe) : "",
108        natp->nat_minused / 60,
109        natp->nat_minused % 60,
110        (long)(now - joe->curup));
111
112     if (player->god) {
113         if (!joe->combuf || !*joe->combuf)
114             pr(" NULL\n");
115         else {
116             n = ufindpfx(joe->combuf, 20);
117             if (CANT_HAPPEN(n + 3u > sizeof(com))) {
118                 pr(" BUGGY\n");
119                 return 1;
120             }
121             sprintf(com, " %.*s\n", n, joe->combuf);
122             uprnf(com);
123         }
124     } else
125         pr("\n");
126
127     return 1;
128 }