]> git.pond.sub.org Git - empserver/blob - src/lib/commands/play.c
a6335c484ca63cc7121b8fe605a840a586a421c1
[empserver] / src / lib / commands / play.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, 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  *     Markus Armbruster, 2005-2011
33  */
34
35 #include <config.h>
36
37 #include "optlist.h"
38 #include "commands.h"
39
40 static void play_header(void);
41 static int play_list(struct player *joe);
42
43 int
44 play(void)
45 {
46     struct player *joe;
47     int saw = 0;
48
49     play_header();
50     for (joe = player_prev(NULL); joe; joe = player_prev(joe)) {
51         saw += play_list(joe);
52     }
53     if (player->god || opt_BLITZ)
54         pr("%d player%s\n", saw, splur(saw));
55
56     return RET_OK;
57 }
58
59 static void
60 play_header(void)
61 {
62     prdate();
63     pr("%9s %3s %-32s %5s %5s %-20s\n",
64        "", "#", "", "time", "idle", "last command");
65 }
66
67 static int
68 play_list(struct player *joe)
69 {
70     time_t now;
71     char com[1 + 6*20 + 2];     /* UTF-8 */
72     struct natstr *natp;
73     struct natstr *us;
74     int n;
75
76     if (joe->cnum >= MAXNOC || !(natp = getnatp(joe->cnum)))
77         return 0;
78
79     us = getnatp(player->cnum);
80     if (player->god) {
81         /* We are a god, we see everything */
82     } else if (opt_BLITZ) {
83         /* It's a blitz, we see everything */
84     } else if (joe->god) {
85         /* This country is a god, so we see it */
86     } else if (us->nat_stat == STAT_VIS) {
87         /* We are a visitor country, we can't see squat, except deities */
88         return 0;
89     } else if (joe->cnum != player->cnum) {
90         /* This isn't us.  Can we see it? */
91         if (natp->nat_stat == STAT_VIS) {
92             /* Yes, we can see visitors are logged on */
93         } else if (relations_with(joe->cnum, player->cnum) < ALLIED) {
94             /* This is a non-allied country, don't show it. */
95             return 0;
96         }
97     }
98
99     time(&now);
100     pr("%-9.9s %3d %32.32s %2d:%02d %4lds",
101        cname(joe->cnum),
102        joe->cnum,
103        player->god || joe->cnum == player->cnum ? praddr(joe) : "",
104        natp->nat_timeused / 3600,
105        (natp->nat_timeused % 3600) / 60,
106        (long)(now - joe->curup));
107
108     if (player->god) {
109         n = ufindpfx(joe->combuf, 20);
110         if (CANT_HAPPEN(n + 3u > sizeof(com))) {
111             pr(" BUGGY\n");
112             return 1;
113         }
114         sprintf(com, " %.*s\n", n, joe->combuf);
115         uprnf(com);
116     } else
117         pr("\n");
118
119     return 1;
120 }