-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_scores.c
335 lines (299 loc) · 8.97 KB
/
c_scores.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
#include "c_includ.h"
/*
* cbzone_scores.c
* -- Todd W Mummert, December 1990, CMU
*
* RCS Info
* $Header: c_scores.c,v 1.1 91/01/12 02:03:36 mummert Locked $
*
* Derived from work I did on our local version of golddig. Thanks
* to Bennet Yee (CMU) for the flock() code.
*/
/*
* Okay, let's try to clean this up. The following things may
* happen:
* score < 0: then the user is just asking for scores. the score
* file should not be changed.
* score >= 0: score is either valid, for practice, or played by an
* old version. In all cases, the file should change.
*
* file status:
* can be not there
* available for reading
* available for both reading and writing
* not there but can be written
*
* we know the following:
* whether the game was for practice only.
* any scores in the score file WILL be in the correct order.
*
* the format of the score file is as follows:
* Version identification
* number of games played, followed by number of valid games
* scores, one per line w/uid and player's name
*
* how should we go about comparing users?
* use getuid and cuserid...check both of them since we may be
* saving scores over a distributed file system
*
* if the game was scoresonly, print to tty if opt->output allows;
* else print to X screen.
*/
extern int errno;
int x, y, ydelta;
void xprintf(s, output) /* print to the X screen */
char* s;
Bool output;
{
printstring (x, y, s, strlen(s));
y += ydelta;
}
void nprintf(s, output) /* print to the parent tty */
char* s;
Bool output;
{
if (output)
printf("%s\n", s);
}
void printandwait(s, c) /* print a string and then */
char* s; /* wait for a character c */
char c; /* to be pressed. If c==0 */
{ /* then any keypress will */
y += ydelta; /* do. */
xprintf(s, True);
waitforkey(c);
}
LONG scores(score)
LONG score;
{
struct score_struct {
LONG score;
int uid;
char name[50];
struct score_struct *next;
} player, *current, *top_score, *prev_score;
FILE *sfile;
char buf[100];
char version[100];
char *login, *getlogin();
int i;
Font fontid;
int numgame = 0;
int numscore = 0;
int numscoreable = 0;
int player_scores = 0;
int tries = MAX_RETRIES;
Bool score_printed = False;
Bool scoresonly = False;
Bool wrong_version = False;
Bool file_readable = False;
Bool file_writeable = False;
Bool practice = opt->practice;
struct passwd* pw;
void (*p)();
version[0] ='\0';
if (score < 0) {
practice = True;
scoresonly = True;
p = nprintf;
}
else {
p = xprintf;
gprsetclippingactive(False);
fontid = gprloadfontfile(GAMEOVERFONT);
gprsettextfont(fontid);
printstring (165, 300, "GAME OVER", 9);
fontid = gprloadfontfile(GENERALFONT);
gprsettextfont(fontid);
printstring (165, 320, "1986 JSR", 8);
flushwindow();
sleep(1);
clearentirescreen();
}
x = 350;
y = 100;
ydelta = 15;
sprintf(buf,"%s%s",TANKDIR,SCOREFILE);
sfile = fopen(buf,"r"); /* just check if it is there */
if (sfile == NULL) {
p("Score file not readable.", opt->output);
if (scoresonly)
return 1;
else {
p("Will try and create new scorefile.", opt->output);
}
}
else
file_readable = True;
if (!scoresonly) {
(void) signal(SIGINT, SIG_IGN); /* no leaving this routine */
(void) signal(SIGHUP, SIG_IGN); /* no how, no way */
file_writeable = True;
if (sfile != NULL)
fclose(sfile);
retry:
if (file_readable)
sfile = fopen(buf,"r+"); /* okay, now open for update */
else
sfile = fopen(buf,"w");
if (sfile != NULL) {
if (flock(fileno(sfile),LOCK_EX) < 0) {
if (errno == EWOULDBLOCK && AFS) {
fclose(sfile);
sleep(AFS_SLEEP);
if (tries--)
goto retry;
}
p("File not lockable, scores will not be saved.", opt->output);
file_writeable = False;
}
}
else {
p("File not writeable, scores will not be saved.", opt->output);
file_writeable = False;
}
/* okay, it's possible we could have closed the file and never
* reopened it. Also, we just may not have been able to lock
* it. In either case, lets close it again and then open only
* for reading.
*/
if (file_readable && !file_writeable) {
if (sfile != NULL)
fclose(sfile);
if ((sfile = fopen(buf, "r")) == NULL)
file_readable = False;
}
}
if (!file_readable && !file_writeable) {
p("Scorefile not readable or writeable...Goodbye!", opt->output);
(void) signal(SIGINT, SIG_DFL); /* this would probably happen */
(void) signal(SIGHUP, SIG_DFL); /* on exit anyway */
if (!scoresonly)
printandwait("Press any key to continue...", 0);
return 1;
}
if (file_readable) {
if(fgets(version,200,sfile) && file_writeable) {
version[strlen(version)-1] = '\0'; /* strip the newline */
if (strcmp(version,VERSION)) {
wrong_version = True;
p("Incorrect version played for this scorefile.", opt->output);
p("Score not valid for inclusion.", opt->output);
sprintf(buf,"Your version is \"%s\", while",VERSION);
p(buf, opt->output);
sprintf(buf, " the scorefile version is \"%s\"", version);
p(buf, opt->output);
}
}
if (*version == '\0')
strcpy(version,VERSION);
if(fgets(buf,200,sfile))
sscanf(buf,"%d %d",&numgame,&numscoreable);
top_score = (struct score_struct*) malloc(sizeof(struct score_struct));
current = top_score;
while(fgets(buf,200,sfile) && numscore < NUMHIGH)
if (sscanf(buf,"%d %d %[^\n]",¤t->score,
¤t->uid,current->name) != 3) {
p("Invalid line in score file...Skipping.", opt->output);
}
else {
current->next = (struct score_struct*)
malloc(sizeof(struct score_struct));
prev_score = current;
current = current->next;
numscore ++;
}
}
if (numscore)
prev_score->next = NULL;
else
top_score = NULL;
if (numgame < numscoreable)
numgame = numscoreable;
if (!scoresonly) {
/* try to get it from the passwd file first, just in case this
* person su'd from another acct.
*/
player.uid = getuid();
player.score = score;
pw = getpwuid(player.uid);
if (pw == NULL)
if ((login = getlogin()) != NULL)
strcpy(player.name, login);
else {
p("Can't find out who you are....bye.", opt->output);
fclose(sfile);
(void) signal(SIGINT, SIG_DFL);
(void) signal(SIGHUP, SIG_DFL);
if (!scoresonly)
printandwait("Press any key to continue...", 0);
return 1;
}
else
strcpy(player.name, pw->pw_name);
if (numscore < NUMHIGH || player.score > prev_score->score) {
score_printed = True;
numscore++;
for (current = top_score;
current != NULL && player.score <= current->score;
prev_score = current, current = current->next);
player.next = current;
if (current == top_score)
top_score = &player;
else
prev_score->next = &player;
}
numgame++;
if (!practice && !wrong_version)
numscoreable++;
}
sprintf(buf, "High scores after %d games, %d scoreable:",
numgame, numscoreable);
p(buf, opt->output);
current = top_score;
while (current != NULL) {
if (current == &player)
*buf = '>';
else
*buf = ' ';
sprintf(buf+1, "%-20s %8d", current->name, current->score);
p(buf, opt->output);
if (((wrong_version || practice) && current==&player) ||
(current->uid==player.uid && !strcmp(player.name, current->name) &&
++player_scores>INDIVIDUAL_SCORES)) {
numscore--;
if (current == top_score)
top_score = current->next;
else
prev_score->next = current->next;
}
else
prev_score = current;
current = current->next;
}
if (!scoresonly && !score_printed) {
p("...", opt->output);
sprintf(buf, ">%-20s %8d", player.name,player.score);
p(buf, opt->output);
}
if (file_writeable) {
rewind(sfile);
fprintf(sfile,"%s\n",version);
fprintf(sfile,"%d %d\n",numgame,numscoreable);
for(current = top_score, i = 0;
current != NULL && i < NUMHIGH;
current = current->next, i++)
fprintf(sfile,"%d %d %s\n",current->score,current->uid,current->name);
if (practice) {
y += ydelta;
p("Your game was for practice only...", opt->output);
p("For a valid score use: cbzone [-q] [-d 0-5]", opt->output);
}
}
fclose(sfile);
(void) signal(SIGINT, SIG_DFL);
(void) signal(SIGHUP, SIG_DFL);
if (!scoresonly)
printandwait("Press any key when ready...", 0);
return 0;
}