ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/hiscore.C
Revision: 1.19
Committed: Sun Mar 18 03:05:40 2007 UTC (17 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.18: +1 -4 lines
Log Message:
- reduce default output-sync to less than a second
- output-sync command now uses seconds as unit, not
  something users cannot even know.
- lots of useless const adjustments.

File Contents

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