refactoring put_scores()

This commit is contained in:
David Meyer 2024-03-26 17:24:46 +09:00
parent 4520ba5a65
commit ccb43e061b

125
score.c
View File

@ -227,8 +227,10 @@ struct score_entry {
char nickname[30]; char nickname[30];
}; };
/* Gold&Glory: NUM_SCORE_ENTRIES must be sum of NUM_GOLD_SCORES and NUM_EXP_SCORES */
#define NUM_GOLD_SCORES 5
#define NUM_EXP_SCORES 5
#define NUM_SCORE_ENTRIES 10 #define NUM_SCORE_ENTRIES 10
#define FIRST_EXP_SCORE 5
static void make_score(struct score_entry *, const object *, int, int); static void make_score(struct score_entry *, const object *, int, int);
@ -342,7 +344,7 @@ put_scores(const object *monster, short other)
struct score_entry scores[NUM_SCORE_ENTRIES]; struct score_entry scores[NUM_SCORE_ENTRIES];
const char *name; const char *name;
FILE *fp; FILE *fp;
boolean dopause = score_only; boolean dopause = score_only, score_update;
md_lock(1); md_lock(1);
@ -386,110 +388,79 @@ put_scores(const object *monster, short other)
*/ */
/* Search the gold score list. */ /* Search the gold score list. */
for (i = 0; i < FIRST_EXP_SCORE; i++) { for (i = 0; i < NUM_GOLD_SCORES; i++) {
if (!strcmp(scores[i].username, login_name)) { if (!strcmp(scores[i].username, login_name)) {
/* found our score */ /* found our score */
if (rogue.gold < scores[i].gold) { if (rogue.gold >= scores[i].gold) {
/* we didn't do as well as last time */ /* current score greater or equal to previous rogueist high
score_only = 1; * --> remove previous rogueist score
} else { */
/* we did better; mark entry for removal */ while (i < (NUM_GOLD_SCORES - 1)) {
found_player = i; scores[i] = scores[i+1];
++ i;
}
scores[i].gold = -1L;
scores[i].username[0] = '\0';
scores[i].death[0] = '\0';
scores[i].nickname[0] = '\0';
} }
break; break;
} }
} }
/* Search the exp score list. */ /* Search the exp score list. */
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) { for (i = NUM_GOLD_SCORES; i < NUM_SCORE_ENTRIES; i++) {
if (!strcmp(scores[i].username, login_name)) { if (!strcmp(scores[i].username, login_name)) {
/* found our score */ /* found our score */
if (rogue.exp_points < scores[i].gold) { if (rogue.exp_points >= scores[i].gold) {
/* we didn't do as well as last time */ /* current score greater or equal to previous rogueist high
score_only = 1; * --> remove previous rogueist score
} else { */
/* we did better; mark entry for removal */ while (i < (NUM_SCORE_ENTRIES - 1)) {
found_player_exp = i; scores[i] = scores[i+1];
++ i;
}
scores[i].gold = -1L;
scores[i].username[0] = '\0';
scores[i].death[0] = '\0';
scores[i].nickname[0] = '\0';
} }
break; break;
} }
} }
/* if (found_player == -1 && found_player_exp == -1) score_only = 1;*/ score_update = 0;
/* Remove superseded entries, if any. */
if (found_player != -1) {
for (i = found_player; i < FIRST_EXP_SCORE-1; i++) {
scores[i] = scores[i+1];
}
scores[i].gold = -1L;
scores[i].username[0] = '\0';
scores[i].death[0] = '\0';
scores[i].nickname[0] = '\0';
}
if (found_player_exp != -1) {
for (i = found_player_exp; i < NUM_SCORE_ENTRIES-1; i++) {
scores[i] = scores[i+1];
}
scores[i].gold = -1L;
scores[i].username[0] = '\0';
scores[i].death[0] = '\0';
scores[i].nickname[0] = '\0';
}
/* If we're going to insert ourselves, do it now */ /* Did rogueist make the top gold list? */
if (!score_only) { for (rank = 0; rank < NUM_GOLD_SCORES; ++ rank) {
if (rogue.gold >= scores[rank].gold) {
/* Gold&Glory: did we make the top gold list? */ score_update = 1;
/* if we aren't better than anyone, add at end. */ for (i = (NUM_GOLD_SCORES - 1); i > rank; -- i) {
rank = FIRST_EXP_SCORE;
/* Otherwise, find our slot. */
for (i = 0; i < FIRST_EXP_SCORE; i++) {
if (rogue.gold >= scores[i].gold) {
rank = i;
break;
}
}
if (rank < FIRST_EXP_SCORE) {
/* Open up a slot */
for (i = FIRST_EXP_SCORE-1; i > rank; i--) {
scores[i] = scores[i-1]; scores[i] = scores[i-1];
} }
/* Put our info in the slot */
make_score(&scores[rank], monster, other, TOP_GOLD); make_score(&scores[rank], monster, other, TOP_GOLD);
break;
} }
}
/* Gold&Glory: did we make the top experience list? */
/* if we aren't better than anyone, add at end. */
rank_exp = NUM_SCORE_ENTRIES;
/* Otherwise, find our slot. */ /* Did rogueist make the top exp list? */
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) { for (rank_exp = NUM_GOLD_SCORES; rank_exp < NUM_SCORE_ENTRIES; ++ rank_exp) {
if (rogue.exp_points >= scores[i].gold) { if (rogue.exp_points >= scores[rank_exp].gold) {
rank_exp = i; score_update = 1;
break; for (i = (NUM_SCORE_ENRIES - 1); i > rank_exp; -- i) {
}
}
if (rank_exp < NUM_SCORE_ENTRIES) {
/* Open up a slot */
for (i = NUM_SCORE_ENTRIES-1; i > rank; i--) {
scores[i] = scores[i-1]; scores[i] = scores[i-1];
} }
/* Put our info in the slot */
make_score(&scores[rank_exp], monster, other, TOP_EXP); make_score(&scores[rank_exp], monster, other, TOP_EXP);
break;
} }
}
if (score_update) {
/* Now rewrite the score file */ /* Now rewrite the score file */
md_ignore_signals(); md_ignore_signals();
rewind(fp); rewind(fp);
(void)xxx(1); (void)xxx(1);
for (i = 0; i < numscores; i++) { for (i = 0; i < numscores; i++) {
write_score_entry(&scores[i], i, fp); write_score_entry(&scores[i], i, fp);
} }
@ -503,7 +474,7 @@ put_scores(const object *monster, short other)
mvaddstr(3, 27, "Top Gold & Glory Rogueists"); mvaddstr(3, 27, "Top Gold & Glory Rogueists");
mvaddstr(7, 0, "Rank Gold Name"); mvaddstr(7, 0, "Rank Gold Name");
for (i = 0; i < FIRST_EXP_SCORE; i++) { for (i = 0; i < NUM_GOLD_SCORES; i++) {
if (scores[i].gold == -1L) break; if (scores[i].gold == -1L) break;
if (i == rank) { if (i == rank) {
standout(); standout();