Debian Codegraph @

server/hiscore.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Hiscore handling functions.
17 */
18 #define _GNU_SOURCE // strcasestr() is a GNU extension in string.h
19
20 #include "global.h"
21
22 #include <errno.h>
23 #include <string.h>
24
25 #include "sproto.h"
26 #include "output_file.h"
27
28 /**
29 * The score structure is used when treating new high-scores.
30 */
31 typedef struct scr {
32 char name[BIG_NAME]; /**< Name. */
33 char title[BIG_NAME]; /**< Title. */
34 char killer[BIG_NAME]; /**< Name (+ title) or "left". */
35 int64_t exp; /**< Experience. */
36 char maplevel[BIG_NAME]; /**< Killed on what level. */
37 int maxhp, /**< Max hp when killed. */
38 maxsp, /**< Max sp when killed. */
39 maxgrace; /**< Max grace when killed. */
40 int position; /**< Position in the highscore list. */
41 } score;
42
43 /**
44 * A highscore table.
45 */
46 typedef struct {
47 char fname[MAX_BUF]; /**< Filename of the backing file. */
48 score entry[HIGHSCORE_LENGTH]; /**< The entries in decreasing exp order. */
49 } score_table;
50
51 /**
52 * The highscore table. Unused entries are set to zero (except for position).
53 */
54 static score_table hiscore_table;
55
56 /**
57 * Writes the given score structure to specified buffer.
58 *
59 * @param sc
60 * score to write.
61 * @param buf
62 * buffer to write to.
63 * @param size
64 * buf's size.
65 */
66 static void put_score(const score *sc, char *buf, size_t size) {
67 snprintf(buf, size, "%s:%s:%"FMT64":%s:%s:%d:%d:%d", sc->name, sc->title, sc->exp, sc->killer, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
68 }
69
70 /**
71 * Saves the highscore_table into the highscore file.
72 *
73 * @param table
74 * the highscore table to save.
75 */
76 static void hiscore_save(const score_table *table) {
77 FILE *fp;
78 OutputFile of;
79 size_t i;
80 char buf[MAX_BUF];
81
82 LOG(llevDebug, "Writing highscore files %s\n", table->fname);
83
84 fp = of_open(&of, table->fname);
85 if (fp == NULL)
86 return;
87
88 for (i = 0; i < HIGHSCORE_LENGTH; i++) {
89 if (table->entry[i].name[0] == '\0')
90 break;
91
92 put_score(&table->entry[i], buf, sizeof(buf));
93 fprintf(fp, "%s\n", buf);
94 }
95 of_close(&of);
96 }
97
98 /**
99 * The opposite of put_score(), get_score reads from the given buffer into
100 * a static score structure, and returns a pointer to it.
101 *
102 * @param bp
103 * string to parse.
104 * @param sc
105 * returns the parsed score.
106 * @return
107 * whether parsing was successful
108 */
109
110 static int get_score(char *bp, score *sc) {
111 char *cp;
112 char *tmp[8];
113
114 cp = strchr(bp, '\n');
115 if (cp != NULL)
116 *cp = '\0';
117
118 if (split_string(bp, tmp, 8, ':') != 8)
119 return 0;
120
121 strncpy(sc->name, tmp[0], BIG_NAME);
122 sc->name[BIG_NAME-1] = '\0';
123
124 strncpy(sc->title, tmp[1], BIG_NAME);
125 sc->title[BIG_NAME-1] = '\0';
126
127 sscanf(tmp[2], "%"FMT64, &sc->exp);
128
129 strncpy(sc->killer, tmp[3], BIG_NAME);
130 sc->killer[BIG_NAME-1] = '\0';
131
132 strncpy(sc->maplevel, tmp[4], BIG_NAME);
133 sc->maplevel[BIG_NAME-1] = '\0';
134
135 sscanf(tmp[5], "%d", &sc->maxhp);
136
137 sscanf(tmp[6], "%d", &sc->maxsp);
138
139 sscanf(tmp[7], "%d", &sc->maxgrace);
140 return 1;
141 }
142
143 /**
144 * Formats one score to display to a player.
145 *
146 * @param sc
147 * score to format.
148 * @param buf
149 * buffer to write to. Will contain suitably formatted score.
150 * @param size
151 * length of buf.
152 * @return
153 * buf.
154 */
155 static char *draw_one_high_score(const score *sc, char *buf, size_t size) {
156 const char *s1;
157 const char *s2;
158
159 if (strcmp(sc->killer, "quit") == 0 || strcmp(sc->killer, "left") == 0) {
160 s1 = sc->killer;
161 s2 = "the game";
162 } else {
163 s1 = "was killed by";
164 s2 = sc->killer;
165 }
166 snprintf(buf, size, "[fixed]%3d %10"FMT64"[print] %s%s%s %s %s on map %s <%d><%d><%d>.",
167 sc->position, sc->exp, sc->name, sc->title[0]==',' ? "" : " ", sc->title, s1, s2, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace);
168 return buf;
169 }
170
171 /**
172 * Adds the given score-structure to the high-score list, but
173 * only if it was good enough to deserve a place.
174 *
175 * @param table
176 * the highscore table to add to.
177 * @param new_score
178 * score to add.
179 * @param old_score
180 * returns the old player score.
181 */
182 static void add_score(score_table *table, score *new_score, score *old_score) {
183 size_t i;
184
185 new_score->position = HIGHSCORE_LENGTH+1;
186 memset(old_score, 0, sizeof(*old_score));
187 old_score->position = -1;
188
189 /* find existing entry by name */
190 for (i = 0; i < HIGHSCORE_LENGTH; i++) {
191 if (table->entry[i].name[0] == '\0') {
192 table->entry[i] = *new_score;
193 table->entry[i].position = i+1;
194 break;
195 }
196 if (strcmp(new_score->name, table->entry[i].name) == 0) {
197 *old_score = table->entry[i];
198 if (table->entry[i].exp <= new_score->exp) {
199 table->entry[i] = *new_score;
200 table->entry[i].position = i+1;
201 }
202 break;
203 }
204 }
205
206 if (i >= HIGHSCORE_LENGTH) {
207 /* entry for unknown name */
208
209 if (new_score->exp < table->entry[i-1].exp) {
210 /* new exp is less than lowest hiscore entry => drop */
211 return;
212 }
213
214 /* new exp is not less than lowest hiscore entry => add */
215 i--;
216 table->entry[i] = *new_score;
217 table->entry[i].position = i+1;
218 }
219
220 /* move entry to correct position */
221 while (i > 0 && new_score->exp >= table->entry[i-1].exp) {
222 score tmp;
223
224 tmp = table->entry[i-1];
225 table->entry[i-1] = table->entry[i];
226 table->entry[i] = tmp;
227
228 table->entry[i-1].position = i;
229 table->entry[i].position = i+1;
230
231 i--;
232 }
233
234 new_score->position = table->entry[i].position;
235 hiscore_save(table);
236 }
237
238 /**
239 * Loads the hiscore_table from the highscore file.
240 *
241 * @param table
242 * the highscore table to load.
243 */
244 static void hiscore_load(score_table *table) {
245 FILE *fp;
246 size_t i;
247
248 i = 0;
249
250 fp = fopen(table->fname, "r");
251 if (fp == NULL) {
252 if (errno == ENOENT) {
253 LOG(llevInfo, "Highscore file %s does not exist\n", table->fname);
254 } else {
255 LOG(llevError, "Cannot open highscore file %s: %s\n", table->fname, strerror(errno));
256 }
257 } else {
258 LOG(llevDebug, "Reading highscore file %s\n", table->fname);
259 while (i < HIGHSCORE_LENGTH) {
260 char buf[MAX_BUF];
261
262 if (fgets(buf, MAX_BUF, fp) == NULL) {
263 break;
264 }
265
266 if (!get_score(buf, &table->entry[i]))
267 break;
268 table->entry[i].position = i+1;
269 i++;
270 }
271
272 fclose(fp);
273 }
274
275 while (i < HIGHSCORE_LENGTH) {
276 memset(&table->entry[i], 0, sizeof(table->entry[i]));
277 // This cannot be ++i due the right-to-left association of assignment.
278 table->entry[i].position = i + 1;
279 i++;
280 }
281 }
282
283 /**
284 * Initializes the module.
285 */
286 void hiscore_init(void) {
287 snprintf(hiscore_table.fname, sizeof(hiscore_table.fname), "%s/%s", settings.localdir, HIGHSCORE);
288 hiscore_load(&hiscore_table);
289 }
290
291 /**
292 * Checks if player should enter the hiscore, and if so writes her into the list.
293 *
294 * @param op
295 * player to check.
296 * @param quiet
297 * If set, don't print anything out - used for periodic updates during game
298 * play or when player unexpected quits - don't need to print anything
299 * in those cases
300 */
301 void hiscore_check(object *op, int quiet) {
302 score new_score;
303 score old_score;
304 char bufscore[MAX_BUF];
305 const char *message;
306
307 if (op->stats.exp == 0 || !op->contr->name_changed)
308 return;
309
310 if (QUERY_FLAG(op, FLAG_WAS_WIZ)) {
311 if (!quiet)
312 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
313 "Since you have been in wizard mode, "
314 "you can't enter the high-score list.");
315 return;
316 }
317 if (!op->stats.exp) {
318 if (!quiet)
319 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
320 "You don't deserve to save your character yet.");
321 return;
322 }
323
324 strncpy(new_score.name, op->name, BIG_NAME);
325 new_score.name[BIG_NAME-1] = '\0';
326 player_get_title(op->contr, new_score.title, sizeof(new_score.title));
327 strncpy(new_score.killer, op->contr->killer, BIG_NAME);
328 if (new_score.killer[0] == '\0')
329 strcpy(new_score.killer, "a dungeon collapse");
330 new_score.killer[BIG_NAME-1] = '\0';
331 new_score.exp = op->stats.exp;
332 if (op->map == NULL)
333 *new_score.maplevel = '\0';
334 else {
335 strncpy(new_score.maplevel, op->map->name ? op->map->name : op->map->path, BIG_NAME-1);
336 new_score.maplevel[BIG_NAME-1] = '\0';
337 }
338 new_score.maxhp = (int)op->stats.maxhp;
339 new_score.maxsp = (int)op->stats.maxsp;
340 new_score.maxgrace = (int)op->stats.maxgrace;
341 add_score(&hiscore_table, &new_score, &old_score);
342
343 /* Everything below here is just related to print messages
344 * to the player. If quiet is set, we can just return
345 * now.
346 */
347 if (quiet)
348 return;
349
350 if (old_score.position == -1) {
351 if (new_score.position > HIGHSCORE_LENGTH)
352 message = "You didn't enter the highscore list:";
353 else
354 message = "You entered the highscore list:";
355 } else {
356 if (new_score.position > HIGHSCORE_LENGTH)
357 message = "You left the highscore list:";
358 else if (new_score.exp > old_score.exp)
359 message = "You beat your last score:";
360 else
361 message = "You didn't beat your last score:";
362 }
363
364 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_HISCORE, message);
365 if (old_score.position != -1)
366 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_HISCORE,
367 draw_one_high_score(&old_score, bufscore, sizeof(bufscore)));
368 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_HISCORE,
369 draw_one_high_score(&new_score, bufscore, sizeof(bufscore)));
370 }
371
372 /**
373 * Displays the high score file.
374 *
375 * @param op
376 * player asking for the score file.
377 * @param max
378 * maximum number of scores to display.
379 * @param match
380 * if non-empty, will only print players with name or title containing the string (non case-sensitive).
381 */
382 void hiscore_display(object *op, int max, const char *match) {
383 int printed_entries;
384 size_t j;
385
386 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_HISCORE,
387 "[fixed]Nr Score [print] Who <max hp><max sp><max grace>");
388
389 printed_entries = 0;
390 for (j = 0; j < HIGHSCORE_LENGTH && hiscore_table.entry[j].name[0] != '\0' && printed_entries < max; j++) {
391 char scorebuf[MAX_BUF];
392
393 if (*match != '\0'
394 && !strcasestr_local(hiscore_table.entry[j].name, match)
395 && !strcasestr_local(hiscore_table.entry[j].title, match))
396 continue;
397
398 draw_one_high_score(&hiscore_table.entry[j], scorebuf, sizeof(scorebuf));
399 printed_entries++;
400
401 if (op == NULL)
402 LOG(llevDebug, "%s\n", scorebuf);
403 else
404 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_HISCORE, scorebuf);
405 }
406 }