ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/hiscore.C
Revision: 1.8
Committed: Thu Sep 14 22:34:04 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
indent

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The authors can be reached via e-mail at <crossfire@schmorp.de>
22 */
23
24 #include <global.h>
25 #ifndef __CEXTRACT__
26 # include <sproto.h>
27 #endif
28
29 /*
30 * The score structure is used when treating new high-scores
31 */
32
33 typedef struct scr
34 {
35 char name[BIG_NAME]; /* name */
36 char title[BIG_NAME]; /* Title */
37 char killer[BIG_NAME]; /* name (+ title) or "quit" */
38 sint64 exp; /* Experience */
39 char maplevel[BIG_NAME]; /* Killed on what level */
40 int maxhp, maxsp, maxgrace; /* Max hp, sp, grace when killed */
41 int position; /* Position in the highscore list */
42 } score;
43
44 /*
45 * spool works mostly like strtok(char *, ":"), but it can also
46 * log a specified error message if something goes wrong.
47 */
48
49 char *
50 spool (char *bp, char *error)
51 {
52 static char *prev_pos = NULL;
53 char *next_pos;
54
55 if (bp == NULL)
56 {
57 if (prev_pos == NULL)
58 {
59 LOG (llevError, "Called spool (%s) with NULL without previous call.\n", error);
60 return NULL;
61 }
62 bp = prev_pos;
63 }
64 if (*bp == '\0')
65 {
66 LOG (llevError, "spool: End of line at %s\n", error);
67 return NULL;
68 }
69 if ((next_pos = strchr (bp, ':')) != NULL)
70 {
71 *next_pos = '\0';
72 prev_pos = next_pos + 1;
73 }
74 else
75 prev_pos = NULL;
76 return bp;
77 }
78
79 /*
80 * Does what it says, copies the contents of the first score structure
81 * to the second one.
82 */
83
84 static void
85 copy_score (score *sc1, score *sc2)
86 {
87 assign (sc2->name , sc1->name);
88 assign (sc2->title , sc1->title);
89 assign (sc2->killer , sc1->killer);
90 assign (sc2->maplevel, sc1->maplevel);
91 sc2->exp = sc1->exp;
92 sc2->maxhp = sc1->maxhp;
93 sc2->maxsp = sc1->maxsp;
94 sc2->maxgrace = sc1->maxgrace;
95 }
96
97 /*
98 * Writes the given score structure to a static buffer, and returns
99 * a pointer to it.
100 */
101
102 static char *
103 put_score (score *sc)
104 {
105 static char buf[MAX_BUF];
106
107 sprintf (buf, "%s:%s:%lld:%s:%s:%d:%d:%d", sc->name, sc->title, (long long) sc->exp, sc->killer, sc->maplevel,
108 sc->maxhp, sc->maxsp, sc->maxgrace);
109 return buf;
110 }
111
112 /*
113 * The oposite of put_score, get_score reads from the given buffer into
114 * a static score structure, and returns a pointer to it.
115 */
116
117 static score *
118 get_score (char *bp)
119 {
120 static score sc;
121 char *cp;
122
123 if ((cp = strchr (bp, '\n')) != NULL)
124 *cp = '\0';
125
126 if ((cp = spool (bp, "name")) == NULL)
127 return 0;
128
129 assign (sc.name, cp);
130
131 if ((cp = spool (0, "title")) == NULL)
132 return 0;
133
134 assign (sc.title, cp);
135
136 if ((cp = spool (0, "score")) == NULL)
137 return 0;
138
139 long long exp;
140 sscanf (cp, "%lld", &exp);
141 sc.exp = exp;
142
143 if ((cp = spool (0, "killer")) == NULL)
144 return 0;
145
146 assign (sc.killer, cp);
147
148 if ((cp = spool (0, "map")) == NULL)
149 return 0;
150
151 assign (sc.maplevel, cp);
152
153 if ((cp = spool (0, "maxhp")) == NULL)
154 return 0;
155
156 sscanf (cp, "%d", &sc.maxhp);
157
158 if ((cp = spool (0, "maxsp")) == NULL)
159 return 0;
160
161 sscanf (cp, "%d", &sc.maxsp);
162
163 if ((cp = spool (0, "maxgrace")) == NULL)
164 return 0;
165
166 sscanf (cp, "%d", &sc.maxgrace);
167 return &sc;
168 }
169
170 static char *
171 draw_one_high_score (score *sc)
172 {
173 static char retbuf[MAX_BUF];
174
175 if (!strncmp (sc->killer, "quit", MAX_NAME))
176 sprintf (retbuf, "%3d %10lld %s the %s quit the game on map %s [%d][%d][%d].",
177 sc->position, (long long) sc->exp, sc->name, sc->title, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
178 else if (!strncmp (sc->killer, "left", MAX_NAME))
179 sprintf (retbuf, "%3d %10lld %s the %s left the game on map %s [%d][%d][%d].",
180 sc->position, (long long) sc->exp, sc->name, sc->title, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
181 else
182 sprintf (retbuf, "%3d %10lld %s the %s was killed by %s on map %s [%d][%d][%d].",
183 sc->position, (long long) sc->exp, sc->name, sc->title, sc->killer, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
184 return retbuf;
185 }
186
187 /*
188 * add_score() adds the given score-structure to the high-score list, but
189 * only if it was good enough to deserve a place.
190 */
191
192 static score *
193 add_score (score *new_score)
194 {
195 FILE *fp;
196 static score old_score;
197 score *tmp_score, pscore[HIGHSCORE_LENGTH];
198 char buf[MAX_BUF], filename[MAX_BUF], *bp;
199 int nrofscores = 0, flag = 0, i, comp;
200
201 new_score->position = HIGHSCORE_LENGTH + 1;
202 old_score.position = -1;
203 sprintf (filename, "%s/%s", settings.localdir, HIGHSCORE);
204 if ((fp = open_and_uncompress (filename, 1, &comp)) != NULL)
205 {
206 while (fgets (buf, MAX_BUF, fp) != NULL && nrofscores < HIGHSCORE_LENGTH)
207 {
208 if ((tmp_score = get_score (buf)) == NULL)
209 break;
210 if (!flag && new_score->exp >= tmp_score->exp)
211 {
212 copy_score (new_score, &pscore[nrofscores]);
213 new_score->position = nrofscores;
214 flag = 1;
215 if (++nrofscores >= HIGHSCORE_LENGTH)
216 break;
217 }
218 if (!strcmp (new_score->name, tmp_score->name))
219 { /* Another entry */
220 copy_score (tmp_score, &old_score);
221 old_score.position = nrofscores;
222 if (flag)
223 continue;
224 }
225 copy_score (tmp_score, &pscore[nrofscores++]);
226 }
227 close_and_delete (fp, comp);
228 }
229 if (old_score.position != -1 && old_score.exp >= new_score->exp)
230 return &old_score; /* Did not beat old score */
231 if (!flag && nrofscores < HIGHSCORE_LENGTH)
232 copy_score (new_score, &pscore[nrofscores++]);
233 if ((fp = fopen (filename, "w")) == NULL)
234 {
235 LOG (llevError, "Cannot write to highscore file %s: %s\n", filename, strerror (errno));
236 return NULL;
237 }
238 for (i = 0; i < nrofscores; i++)
239 {
240 bp = put_score (&pscore[i]);
241 fprintf (fp, "%s\n", bp);
242 }
243 fclose (fp);
244 if (flag)
245 {
246
247 /* Eneq(@csd.uu.se): Patch to fix error in adding a new score to the
248 hiscore-list */
249 if (old_score.position == -1)
250 return new_score;
251 return &old_score;
252 }
253 new_score->position = -1;
254 if (old_score.position != -1)
255 return &old_score;
256 if (nrofscores)
257 {
258 copy_score (&pscore[nrofscores - 1], &old_score);
259 return &old_score;
260 }
261 LOG (llevError, "Highscore error.\n");
262 return NULL;
263 }
264
265 void
266 check_score (object *op)
267 {
268 score new_score;
269 score *old_score;
270
271 if (op->stats.exp == 0)
272 return;
273
274 if (!op->contr->name_changed)
275 {
276 if (op->stats.exp > 0)
277 {
278 new_draw_info (NDI_UNIQUE, 0, op, "As you haven't changed your name, you won't");
279 new_draw_info (NDI_UNIQUE, 0, op, "get into the high-score list.");
280 }
281 return;
282 }
283 if (QUERY_FLAG (op, FLAG_WAS_WIZ))
284 {
285 new_draw_info (NDI_UNIQUE, 0, op, "Since you have been in wizard mode,");
286 new_draw_info (NDI_UNIQUE, 0, op, "you can't enter the high-score list.");
287 return;
288 }
289 if (op->contr->explore)
290 {
291 new_draw_info (NDI_UNIQUE, 0, op, "Since you were in explore mode,");
292 new_draw_info (NDI_UNIQUE, 0, op, "you can't enter the high-score list.");
293 return;
294 }
295
296 assign (new_score.name, op->name);
297 assign (new_score.title, op->title.length () ? &op->title : op->contr->title);
298 assign (new_score.killer, op->contr->killer[0] ? op->contr->killer : "a dungeon collapse");
299 assign (new_score.maplevel, op->map ? op->map->name ? op->map->name : op->map->path : "");
300
301 new_score.exp = op->stats.exp;
302 new_score.maxhp = op->stats.maxhp;
303 new_score.maxsp = op->stats.maxsp;
304 new_score.maxgrace = op->stats.maxgrace;
305
306 if ((old_score = add_score (&new_score)) == NULL)
307 {
308 new_draw_info (NDI_UNIQUE, 0, op, "Error in the highscore list.");
309 return;
310 }
311
312 if (new_score.position == -1)
313 {
314 new_score.position = HIGHSCORE_LENGTH + 1; /* Not strictly correct... */
315 if (!strcmp (old_score->name, new_score.name))
316 new_draw_info (NDI_UNIQUE, 0, op, "You didn't beat your last highscore:");
317 else
318 new_draw_info (NDI_UNIQUE, 0, op, "You didn't enter the highscore list:");
319 new_draw_info (NDI_UNIQUE, 0, op, draw_one_high_score (old_score));
320 new_draw_info (NDI_UNIQUE, 0, op, draw_one_high_score (&new_score));
321 return;
322 }
323
324 if (old_score->exp >= new_score.exp)
325 new_draw_info (NDI_UNIQUE, 0, op, "You didn't beat your last score:");
326 else
327 new_draw_info (NDI_UNIQUE, 0, op, "You beat your last score:");
328
329 new_draw_info (NDI_UNIQUE, 0, op, draw_one_high_score (old_score));
330 new_draw_info (NDI_UNIQUE, 0, op, draw_one_high_score (&new_score));
331 }
332
333 /* displays the high score file. object is the calling object
334 * (null if being called via command line.) max is the maximum
335 * number of scores to display. match, if set, is the name or class
336 * to match to.
337 */
338
339 void
340 display_high_score (object *op, int max, const char *match)
341 {
342 const size_t maxchar = 80;
343 FILE *fp;
344 char buf[MAX_BUF], *scorebuf, *bp, *cp;
345 int i = 0, j = 0, comp;
346 score *sc;
347
348 sprintf (buf, "%s/%s", settings.localdir, HIGHSCORE);
349 if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
350 {
351 LOG (llevError, "Cannot open highscore file %s: %s\n", buf, strerror (errno));
352 if (op != NULL)
353 new_draw_info (NDI_UNIQUE, 0, op, "There is no highscore file.");
354 return;
355 }
356 if (op != NULL)
357 clear_win_info (op);
358 new_draw_info (NDI_UNIQUE, 0, op, "Nr Score Who [max hp][max sp][max grace]");
359
360 while (fgets (buf, MAX_BUF, fp) != NULL)
361 {
362 if (j >= HIGHSCORE_LENGTH || i >= (max - 1))
363 break;
364 if ((sc = get_score (buf)) == NULL)
365 break;
366 sc->position = ++j;
367 if (match == NULL)
368 {
369 scorebuf = draw_one_high_score (sc);
370 i++;
371 }
372 else
373 {
374 if (!strcasecmp (sc->name, match) || !strcasecmp (sc->title, match))
375 {
376 scorebuf = draw_one_high_score (sc);
377 i++;
378 }
379 else
380 continue;
381 }
382 /* Replaced what seemed to an overly complicated word wrap method
383 * still word wraps, but assumes at most 2 lines of data.
384 * mw - 2-12-97
385 */
386 assign (buf, scorebuf);
387
388 cp = buf;
389 while (strlen (cp) > maxchar)
390 {
391 bp = cp + maxchar - 1;
392 while (*bp != ' ' && bp > cp)
393 bp--;
394 *bp = '\0';
395
396 if (op == NULL)
397 LOG (llevDebug, "%s\n", cp);
398 else
399 new_draw_info (NDI_UNIQUE, 0, op, cp);
400
401 sprintf (buf, " %s", bp + 1);
402 cp = buf;
403 i++;
404 }
405
406 if (op == NULL)
407 LOG (llevDebug, "%s\n", buf);
408 else
409 new_draw_info (NDI_UNIQUE, 0, op, buf);
410 }
411
412 close_and_delete (fp, comp);
413 }