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