ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/hiscore.C
Revision: 1.25
Committed: Mon May 5 22:38:48 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.24: +2 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.23 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.18 *
4 root 1.23 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.20 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.18 *
8 root 1.23 * Deliantra is free software: you can redistribute it and/or modify
9 root 1.22 * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation, either version 3 of the License, or
11     * (at your option) any later version.
12 pippijn 1.18 *
13 root 1.22 * 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 pippijn 1.18 *
18 root 1.22 * You should have received a copy of the GNU General Public License
19     * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 root 1.20 *
21 root 1.23 * The authors can be reached via e-mail to <support@deliantra.net>
22 pippijn 1.18 */
23 elmex 1.1
24     #include <global.h>
25 root 1.14 #include <sproto.h>
26 elmex 1.1
27     /*
28     * The score structure is used when treating new high-scores
29     */
30 root 1.5 typedef struct scr
31     {
32 root 1.9 char name[64]; // name */
33     char title[64]; // Title */
34     char killer[64]; // name (+ title) or "quit" */
35     sint64 exp; // Experience */
36     char maplevel[128]; // Killed on what level */
37     int maxhp, maxsp, maxgrace; // Max hp, sp, grace when killed */
38     int position; // Position in the highscore list */
39 elmex 1.1 } score;
40    
41     /*
42     * spool works mostly like strtok(char *, ":"), but it can also
43     * log a specified error message if something goes wrong.
44     */
45 root 1.5 char *
46 root 1.19 spool (char *bp, const char *error)
47 root 1.5 {
48 elmex 1.1 static char *prev_pos = NULL;
49     char *next_pos;
50 root 1.5
51     if (bp == NULL)
52     {
53     if (prev_pos == NULL)
54     {
55     LOG (llevError, "Called spool (%s) with NULL without previous call.\n", error);
56     return NULL;
57     }
58     bp = prev_pos;
59     }
60     if (*bp == '\0')
61     {
62     LOG (llevError, "spool: End of line at %s\n", error);
63 elmex 1.1 return NULL;
64     }
65 root 1.5 if ((next_pos = strchr (bp, ':')) != NULL)
66     {
67     *next_pos = '\0';
68     prev_pos = next_pos + 1;
69     }
70     else
71 elmex 1.1 prev_pos = NULL;
72     return bp;
73     }
74    
75     /*
76     * Does what it says, copies the contents of the first score structure
77     * to the second one.
78     */
79 root 1.5 static void
80 root 1.6 copy_score (score *sc1, score *sc2)
81 root 1.5 {
82 root 1.7 assign (sc2->name , sc1->name);
83     assign (sc2->title , sc1->title);
84     assign (sc2->killer , sc1->killer);
85     assign (sc2->maplevel, sc1->maplevel);
86 root 1.5 sc2->exp = sc1->exp;
87     sc2->maxhp = sc1->maxhp;
88     sc2->maxsp = sc1->maxsp;
89     sc2->maxgrace = sc1->maxgrace;
90 elmex 1.1 }
91    
92     /*
93     * Writes the given score structure to a static buffer, and returns
94     * a pointer to it.
95     */
96 root 1.5 static char *
97 root 1.6 put_score (score *sc)
98 root 1.5 {
99 elmex 1.1 static char buf[MAX_BUF];
100 root 1.5
101 root 1.12 sprintf (buf, "%s:%s:%" PRId64 ":%s:%s:%d:%d:%d",
102     sc->name, sc->title, (sint64)sc->exp, sc->killer,
103     sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
104 elmex 1.1 return buf;
105     }
106    
107     /*
108     * The oposite of put_score, get_score reads from the given buffer into
109     * a static score structure, and returns a pointer to it.
110     */
111    
112 root 1.5 static score *
113     get_score (char *bp)
114     {
115 elmex 1.1 static score sc;
116     char *cp;
117    
118 root 1.5 if ((cp = strchr (bp, '\n')) != NULL)
119     *cp = '\0';
120 elmex 1.1
121 root 1.5 if ((cp = spool (bp, "name")) == NULL)
122 root 1.7 return 0;
123    
124     assign (sc.name, cp);
125    
126     if ((cp = spool (0, "title")) == NULL)
127     return 0;
128    
129     assign (sc.title, cp);
130    
131     if ((cp = spool (0, "score")) == NULL)
132     return 0;
133 elmex 1.1
134 root 1.12 sscanf (cp, "%" SCNd64, &sc.exp);
135 root 1.5
136 root 1.7 if ((cp = spool (0, "killer")) == NULL)
137     return 0;
138    
139     assign (sc.killer, cp);
140    
141     if ((cp = spool (0, "map")) == NULL)
142     return 0;
143    
144     assign (sc.maplevel, cp);
145    
146     if ((cp = spool (0, "maxhp")) == NULL)
147     return 0;
148 elmex 1.1
149 root 1.5 sscanf (cp, "%d", &sc.maxhp);
150 elmex 1.1
151 root 1.7 if ((cp = spool (0, "maxsp")) == NULL)
152     return 0;
153    
154 root 1.5 sscanf (cp, "%d", &sc.maxsp);
155 elmex 1.1
156 root 1.7 if ((cp = spool (0, "maxgrace")) == NULL)
157     return 0;
158    
159 root 1.5 sscanf (cp, "%d", &sc.maxgrace);
160 elmex 1.1 return &sc;
161     }
162    
163 root 1.5 static char *
164 root 1.6 draw_one_high_score (score *sc)
165 root 1.5 {
166     static char retbuf[MAX_BUF];
167    
168     if (!strncmp (sc->killer, "quit", MAX_NAME))
169     sprintf (retbuf, "%3d %10lld %s the %s quit the game on map %s [%d][%d][%d].",
170     sc->position, (long long) sc->exp, sc->name, sc->title, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
171 root 1.25 else if (!strncmp (sc->killer, "leaving", MAX_NAME))
172 root 1.5 sprintf (retbuf, "%3d %10lld %s the %s left the game on map %s [%d][%d][%d].",
173     sc->position, (long long) sc->exp, sc->name, sc->title, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
174     else
175     sprintf (retbuf, "%3d %10lld %s the %s was killed by %s on map %s [%d][%d][%d].",
176     sc->position, (long long) sc->exp, sc->name, sc->title, sc->killer, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
177 root 1.25
178 root 1.5 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 root 1.24 assign (new_score.killer, op->contr->killer_name ());
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 root 1.21
329 root 1.5 new_draw_info (NDI_UNIQUE, 0, op, "Nr Score Who [max hp][max sp][max grace]");
330    
331     while (fgets (buf, MAX_BUF, fp) != NULL)
332     {
333     if (j >= HIGHSCORE_LENGTH || i >= (max - 1))
334     break;
335 root 1.21
336 root 1.5 if ((sc = get_score (buf)) == NULL)
337     break;
338 root 1.21
339 root 1.5 sc->position = ++j;
340     if (match == NULL)
341     {
342     scorebuf = draw_one_high_score (sc);
343     i++;
344     }
345     else
346     {
347     if (!strcasecmp (sc->name, match) || !strcasecmp (sc->title, match))
348     {
349     scorebuf = draw_one_high_score (sc);
350     i++;
351 root 1.2 }
352 root 1.5 else
353     continue;
354 root 1.2 }
355 root 1.21
356 root 1.5 /* Replaced what seemed to an overly complicated word wrap method
357     * still word wraps, but assumes at most 2 lines of data.
358     * mw - 2-12-97
359     */
360 root 1.7 assign (buf, scorebuf);
361    
362 root 1.5 cp = buf;
363     while (strlen (cp) > maxchar)
364     {
365     bp = cp + maxchar - 1;
366     while (*bp != ' ' && bp > cp)
367     bp--;
368     *bp = '\0';
369 root 1.7
370 root 1.5 if (op == NULL)
371 root 1.7 LOG (llevDebug, "%s\n", cp);
372 root 1.5 else
373 root 1.7 new_draw_info (NDI_UNIQUE, 0, op, cp);
374    
375 root 1.5 sprintf (buf, " %s", bp + 1);
376     cp = buf;
377     i++;
378 root 1.2 }
379 root 1.7
380 root 1.5 if (op == NULL)
381     LOG (llevDebug, "%s\n", buf);
382     else
383     new_draw_info (NDI_UNIQUE, 0, op, buf);
384 elmex 1.1 }
385 root 1.7
386 root 1.5 close_and_delete (fp, comp);
387 elmex 1.1 }
388 root 1.21