ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/hiscore.C
Revision: 1.9
Committed: Fri Sep 29 11:53:09 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.8: +7 -7 lines
Log Message:
enlarge killer and other fields, do not use silly BIG_NAME with no meaning.

File Contents

# User Rev Content
1 elmex 1.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 root 1.8 The authors can be reached via e-mail at <crossfire@schmorp.de>
22 elmex 1.1 */
23    
24     #include <global.h>
25     #ifndef __CEXTRACT__
26 root 1.5 # include <sproto.h>
27 elmex 1.1 #endif
28    
29     /*
30     * The score structure is used when treating new high-scores
31     */
32    
33 root 1.5 typedef struct scr
34     {
35 root 1.9 char name[64]; // name */
36     char title[64]; // Title */
37     char killer[64]; // name (+ title) or "quit" */
38     sint64 exp; // Experience */
39     char maplevel[128]; // Killed on what level */
40     int maxhp, maxsp, maxgrace; // Max hp, sp, grace when killed */
41     int position; // Position in the highscore list */
42 elmex 1.1 } 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 root 1.5 char *
50     spool (char *bp, char *error)
51     {
52 elmex 1.1 static char *prev_pos = NULL;
53     char *next_pos;
54 root 1.5
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 elmex 1.1 return NULL;
68     }
69 root 1.5 if ((next_pos = strchr (bp, ':')) != NULL)
70     {
71     *next_pos = '\0';
72     prev_pos = next_pos + 1;
73     }
74     else
75 elmex 1.1 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 root 1.5 static void
85 root 1.6 copy_score (score *sc1, score *sc2)
86 root 1.5 {
87 root 1.7 assign (sc2->name , sc1->name);
88     assign (sc2->title , sc1->title);
89     assign (sc2->killer , sc1->killer);
90     assign (sc2->maplevel, sc1->maplevel);
91 root 1.5 sc2->exp = sc1->exp;
92     sc2->maxhp = sc1->maxhp;
93     sc2->maxsp = sc1->maxsp;
94     sc2->maxgrace = sc1->maxgrace;
95 elmex 1.1 }
96    
97     /*
98     * Writes the given score structure to a static buffer, and returns
99     * a pointer to it.
100     */
101    
102 root 1.5 static char *
103 root 1.6 put_score (score *sc)
104 root 1.5 {
105 elmex 1.1 static char buf[MAX_BUF];
106 root 1.5
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 elmex 1.1 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 root 1.5 static score *
118     get_score (char *bp)
119     {
120 elmex 1.1 static score sc;
121     char *cp;
122    
123 root 1.5 if ((cp = strchr (bp, '\n')) != NULL)
124     *cp = '\0';
125 elmex 1.1
126 root 1.5 if ((cp = spool (bp, "name")) == NULL)
127 root 1.7 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 elmex 1.1
139 root 1.4 long long exp;
140 root 1.5 sscanf (cp, "%lld", &exp);
141     sc.exp = exp;
142    
143 root 1.7 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 elmex 1.1
156 root 1.5 sscanf (cp, "%d", &sc.maxhp);
157 elmex 1.1
158 root 1.7 if ((cp = spool (0, "maxsp")) == NULL)
159     return 0;
160    
161 root 1.5 sscanf (cp, "%d", &sc.maxsp);
162 elmex 1.1
163 root 1.7 if ((cp = spool (0, "maxgrace")) == NULL)
164     return 0;
165    
166 root 1.5 sscanf (cp, "%d", &sc.maxgrace);
167 elmex 1.1 return &sc;
168     }
169    
170 root 1.5 static char *
171 root 1.6 draw_one_high_score (score *sc)
172 root 1.5 {
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 elmex 1.1
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 root 1.5 static score *
193 root 1.6 add_score (score *new_score)
194 root 1.5 {
195 elmex 1.1 FILE *fp;
196     static score old_score;
197 root 1.5 score *tmp_score, pscore[HIGHSCORE_LENGTH];
198 elmex 1.1 char buf[MAX_BUF], filename[MAX_BUF], *bp;
199 root 1.5 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 elmex 1.1 /* Eneq(@csd.uu.se): Patch to fix error in adding a new score to the
248     hiscore-list */
249 root 1.5 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 elmex 1.1 return &old_score;
256 root 1.5 if (nrofscores)
257     {
258     copy_score (&pscore[nrofscores - 1], &old_score);
259     return &old_score;
260     }
261     LOG (llevError, "Highscore error.\n");
262 elmex 1.1 return NULL;
263     }
264    
265 root 1.5 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 root 1.2 }
281 root 1.5 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 elmex 1.1 }
295 root 1.7
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 root 1.5 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 root 1.7
312 root 1.5 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 root 1.7
324 root 1.5 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 elmex 1.1
329 root 1.5 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 elmex 1.1 }
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 root 1.5 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 root 1.2 }
379 root 1.5 else
380     continue;
381 root 1.2 }
382 root 1.5 /* 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 root 1.7 assign (buf, scorebuf);
387    
388 root 1.5 cp = buf;
389     while (strlen (cp) > maxchar)
390     {
391     bp = cp + maxchar - 1;
392     while (*bp != ' ' && bp > cp)
393     bp--;
394     *bp = '\0';
395 root 1.7
396 root 1.5 if (op == NULL)
397 root 1.7 LOG (llevDebug, "%s\n", cp);
398 root 1.5 else
399 root 1.7 new_draw_info (NDI_UNIQUE, 0, op, cp);
400    
401 root 1.5 sprintf (buf, " %s", bp + 1);
402     cp = buf;
403     i++;
404 root 1.2 }
405 root 1.7
406 root 1.5 if (op == NULL)
407     LOG (llevDebug, "%s\n", buf);
408     else
409     new_draw_info (NDI_UNIQUE, 0, op, buf);
410 elmex 1.1 }
411 root 1.7
412 root 1.5 close_and_delete (fp, comp);
413 elmex 1.1 }