]> git.pond.sub.org Git - empserver/blob - src/lib/commands/play.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / commands / play.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  play.c: Who is logged on?
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1998
32  */
33
34 #include <config.h>
35
36 #include "optlist.h"
37 #include "commands.h"
38
39 static void play_header(void);
40 static int play_list(struct player *joe);
41
42 int
43 play(void)
44 {
45     struct player *joe;
46     int saw = 0;
47
48     play_header();
49     for (joe = player_prev(NULL); joe; joe = player_prev(joe)) {
50         saw += play_list(joe);
51     }
52     if (player->god || opt_BLITZ)
53         pr("%d player%s\n", saw, splur(saw));
54
55     return RET_OK;
56 }
57
58 static void
59 play_header(void)
60 {
61     prdate();
62     pr("%9s %3s %-32s %5s %5s %-20s\n",
63        "", "#", "", "time", "idle", "last command");
64 }
65
66 static int
67 play_list(struct player *joe)
68 {
69     time_t now;
70     char com[1 + 6*20 + 2];     /* UTF-8 */
71     struct natstr *natp;
72     struct natstr *us;
73     int n;
74
75     if (joe->cnum >= MAXNOC || !(natp = getnatp(joe->cnum)))
76         return 0;
77
78     us = getnatp(player->cnum);
79     if (player->god) {
80         /* We are a god, we see everything */
81     } else if (opt_BLITZ) {
82         /* It's a blitz, we see everything */
83     } else if (joe->god) {
84         /* This country is a god, so we see it */
85     } else if (us->nat_stat == STAT_VIS) {
86         /* We are a visitor country, we can't see squat, except deities */
87         return 0;
88     } else if (joe->cnum != player->cnum) {
89         /* This isn't us.  Can we see it? */
90         if (natp->nat_stat == STAT_VIS) {
91             /* Yes, we can see visitors are logged on */
92         } else if (relations_with(joe->cnum, player->cnum) < ALLIED) {
93             /* This is a non-allied country, don't show it. */
94             return 0;
95         }
96     }
97
98     time(&now);
99     pr("%-9.9s %3d %32.32s %2d:%02d %4lds",
100        cname(joe->cnum),
101        joe->cnum,
102        player->god || joe->cnum == player->cnum ? praddr(joe) : "",
103        natp->nat_timeused / 3600,
104        (natp->nat_timeused % 3600) / 60,
105        (long)(now - joe->curup));
106
107     if (player->god) {
108         if (!joe->combuf || !*joe->combuf)
109             pr(" NULL\n");
110         else {
111             n = ufindpfx(joe->combuf, 20);
112             if (CANT_HAPPEN(n + 3u > sizeof(com))) {
113                 pr(" BUGGY\n");
114                 return 1;
115             }
116             sprintf(com, " %.*s\n", n, joe->combuf);
117             uprnf(com);
118         }
119     } else
120         pr("\n");
121
122     return 1;
123 }