-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.c
337 lines (293 loc) · 7.07 KB
/
score.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*-
* Copyright (c) 1994, 1995, 1999
* Simon Burge. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <curses.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include "zombies.h"
typedef struct {
int s_uid;
int s_score;
int s_level;
int s_starttime;
int s_endtime;
char s_name[MAXNAME];
char s_host[MAXNAME];
} SCORE;
int StartTime, EndTime;
static SCORE scores[MAX_SCORES];
static void set_name(SCORE *s);
/*
* score:
* Post the player's score, if reasonable, and then print out the
* top list.
*/
void
score(void)
{
int done_show, i, lfd;
int newscore, top, bottom, topline;
char lockfile[BUFSIZ];
SCORE *s;
strcpy(lockfile, SCORE_FILE);
strcat(lockfile, ".lock");
/* Lock the score file */
while (((lfd = open(lockfile, O_CREAT | O_EXCL, 0644)) < 0) &&
errno == EEXIST)
sleep(1);
if (lfd < 0)
return;
/*
* Read in the score file. If the score is not good enough, then
* return, otherwise write out the revised score file.
*/
read_scores();
newscore = add_score();
if (!newscore) {
unlink(lockfile);
return;
}
write_scores();
unlink(lockfile);
/* Show the user how good he is! */
move(1, 10);
printw(" Pos\tScore\tLevel\tHost\t\tName\t");
move(2, 10);
printw(" ---\t-----\t-----\t----------\t----\t");
done_show = FALSE;
if ((top = newscore - (DISPLAY_SCORES / 2)) < 1)
top = 1;
if ((bottom = top + DISPLAY_SCORES - 1) > MAX_SCORES)
bottom = MAX_SCORES;
topline = 3 - top;
for (i = top, s = &scores[top - 1]; i <= bottom; i++, s++) {
if (s->s_score <= 0)
break;
move(i + topline, 10);
if (!done_show && s->s_uid == MyUid && s->s_score == Score)
standout();
printw(" %3d\t%5d\t%3d\t%-15s\t%-8.8s ", i, s->s_score,
s->s_level, s->s_host, s->s_name);
if (!done_show && s->s_uid == MyUid && s->s_score == Score) {
standend();
done_show = TRUE;
}
}
}
int
add_score(void)
{
int i, newscore, uidcount;
SCORE *s, ns, os, temp;
if (scores[MAX_SCORES - 1].s_score > Score) {
/* Score not good enough... */
return(0);
}
/* Initialise the new score. */
ns.s_score = Score;
ns.s_level = Level - 1;
ns.s_uid = MyUid;
ns.s_starttime = StartTime;
ns.s_endtime = time(0);
set_name(&ns);
os.s_score = -1;
uidcount = newscore = 0;
for (i = 0, s = scores; i < MAX_SCORES; i++) {
temp = *s;
if (s->s_score <= Score && !newscore &&
uidcount < USER_SCORES) {
/*
* If our score is better than the current
* score, and we haven't added our score yet,
* put it in the table.
*/
*s = ns;
newscore = i + 1;
uidcount++;
s++;
/* Save current score. */
os = temp;
}
else if (os.s_score > 0 &&
(os.s_uid != MyUid || uidcount < USER_SCORES)) {
/*
* If we have an old score saved, put it
* in the table, and keep the current one
* for the next loop. And bump up the uid
* count in necessary...
*/
if (os.s_uid == MyUid)
uidcount++;
*s++ = os;
os = temp;
}
else if (os.s_score == -1) {
/*
* If we haven't inserted our score yet, just
* move along after checking the uid count.
*/
if (s->s_uid == MyUid)
uidcount++;
s++;
}
}
return(newscore);
}
static void
set_name(SCORE *s)
{
char *p, buf[MAXHOSTNAMELEN];
struct passwd *pp;
if ((pp = getpwuid(s->s_uid)) == NULL)
pp->pw_name = "???";
strncpy(s->s_name, pp->pw_name, MAXNAME);
gethostname(buf, MAXHOSTNAMELEN);
if ((p = strchr(buf, '.')) != NULL)
*p = '\0';
strncpy(s->s_host, buf, MAXNAME);
}
/*
* show_score:
* Show the score list for the '-s' option.
*
* also used to return top score for init_field();
*/
void
show_score(void)
{
int i, inf;
SCORE *s;
read_scores();
inf = 1;
printf("\n");
printf("\t%s\t%s\t%s\t%s\t\t%s\n",
"Pos", "Score", "Level", "Host", "Name");
printf("\t%s\t%s\t%s\t%s\t%s\n",
"---", "-----", "-----", "---------", "--------");
for (i = 0, s = scores; i < MAX_SCORES; i++, s++)
if (s->s_score > 0) {
printf("\t%3d\t%5d\t%4d\t%-15s\t%.*s\n",
inf++, s->s_score, s->s_level, s->s_host,
(int)sizeof(s->s_name), s->s_name);
}
printf("\n");
}
/*
* read_scores:
* Read the current score file into memory.
*
*/
void
read_scores(void)
{
int i;
char buf[BUFSIZ];
FILE *sf;
SCORE *s;
/* open score file */
if ((sf = fopen(SCORE_FILE, "r")) == NULL)
/* no score file yet, outta here... */
return;
/* zero score list */
for (i = 0, s = scores; i < MAX_SCORES; i++, s++)
s->s_score = 0;
/* read score file */
for (i = 0, s = scores; i < MAX_SCORES; i++, s++) {
s->s_score = 0;
if (!feof(sf) && !ferror(sf) && fgets(buf, BUFSIZ, sf)) {
sscanf(buf, "%d %d %d %d %d %s %s\n",
&s->s_uid, &s->s_score, &s->s_level,
&s->s_starttime, &s->s_endtime,
s->s_name, s->s_host);
}
}
fclose(sf);
}
/*
* write_scores:
* write the score file back to disk.
*
*/
void
write_scores(void)
{
int i;
FILE *sf;
SCORE *s;
if ((sf = fopen(SCORE_FILE, "w")) == NULL)
/* can't write score file, oh well... */
return;
for (i = 0, s = scores; i < MAX_SCORES && s->s_score > 0; i++, s++) {
fprintf(sf, "%d %d %d %d %d %s %s\n",
s->s_uid, s->s_score, s->s_level,
s->s_starttime, s->s_endtime,
s->s_name, s->s_host);
}
fclose(sf);
}
/*
* top_score:
* Return the current highest score.
*
*/
int
top_score(void)
{
int s;
char buf[BUFSIZ];
FILE *sf;
s = scores->s_score;
if ((sf = fopen(SCORE_FILE, "r")) != NULL) {
if (fgets(buf, BUFSIZ, sf) != NULL)
sscanf(buf, "%*d %d", &s);
fclose(sf);
}
return(s);
}
/*
* give_bonus:
* Give a bonus depending on the number of walls left on the screen.
*
*/
void
give_bonus(void)
{
int bonus;
bonus = Level * WallsLeft;
mvprintw(Y_BONUS, X_BONUS, "Bonus: %d", bonus);
refresh();
add_to_score(bonus);
if (Pause)
sleep(2);
else
getchar();
move(Y_BONUS, X_BONUS);
clrtoeol();
}