| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#define _GNU_SOURCE |
| 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 |
|
| 30 |
|
| 31 |
typedef struct scr { |
| 32 |
char name[BIG_NAME]; |
| 33 |
char title[BIG_NAME]; |
| 34 |
char killer[BIG_NAME]; |
| 35 |
int64_t exp; |
| 36 |
char maplevel[BIG_NAME]; |
| 37 |
int maxhp, |
| 38 |
maxsp, |
| 39 |
maxgrace; |
| 40 |
int position; |
| 41 |
} score; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
typedef struct { |
| 47 |
char fname[MAX_BUF]; |
| 48 |
score entry[HIGHSCORE_LENGTH]; |
| 49 |
} score_table; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static score_table hiscore_table; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 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 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 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 |
|
| 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 |
|
| 208 |
|
| 209 |
if (new_score->exp < table->entry[i-1].exp) { |
| 210 |
|
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
i--; |
| 216 |
table->entry[i] = *new_score; |
| 217 |
table->entry[i].position = i+1; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 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 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 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 |
|
| 278 |
table->entry[i].position = i + 1; |
| 279 |
i++; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 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 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 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 |
|
| 344 |
|
| 345 |
|
| 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 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 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 |
} |