refactoring put_scores()

This commit is contained in:
David Meyer 2024-03-26 17:24:46 +09:00
parent 4520ba5a65
commit ccb43e061b
1 changed files with 48 additions and 77 deletions

125
score.c
View File

@ -227,8 +227,10 @@ struct score_entry {
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 FIRST_EXP_SCORE 5
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];
const char *name;
FILE *fp;
boolean dopause = score_only;
boolean dopause = score_only, score_update;
md_lock(1);
@ -386,110 +388,79 @@ put_scores(const object *monster, short other)
*/
/* 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)) {
/* found our score */
if (rogue.gold < scores[i].gold) {
/* we didn't do as well as last time */
score_only = 1;
} else {
/* we did better; mark entry for removal */
found_player = i;
if (rogue.gold >= scores[i].gold) {
/* current score greater or equal to previous rogueist high
* --> remove previous rogueist score
*/
while (i < (NUM_GOLD_SCORES - 1)) {
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;
}
}
/* 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)) {
/* found our score */
if (rogue.exp_points < scores[i].gold) {
/* we didn't do as well as last time */
score_only = 1;
} else {
/* we did better; mark entry for removal */
found_player_exp = i;
if (rogue.exp_points >= scores[i].gold) {
/* current score greater or equal to previous rogueist high
* --> remove previous rogueist score
*/
while (i < (NUM_SCORE_ENTRIES - 1)) {
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;
}
}
/* if (found_player == -1 && found_player_exp == -1) score_only = 1;*/
/* 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';
}
score_update = 0;
/* If we're going to insert ourselves, do it now */
if (!score_only) {
/* Gold&Glory: did we make the top gold list? */
/* if we aren't better than anyone, add at end. */
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--) {
/* Did rogueist make the top gold list? */
for (rank = 0; rank < NUM_GOLD_SCORES; ++ rank) {
if (rogue.gold >= scores[rank].gold) {
score_update = 1;
for (i = (NUM_GOLD_SCORES - 1); i > rank; -- i) {
scores[i] = scores[i-1];
}
/* Put our info in the slot */
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. */
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) {
if (rogue.exp_points >= scores[i].gold) {
rank_exp = i;
break;
}
}
if (rank_exp < NUM_SCORE_ENTRIES) {
/* Open up a slot */
for (i = NUM_SCORE_ENTRIES-1; i > rank; i--) {
/* Did rogueist make the top exp list? */
for (rank_exp = NUM_GOLD_SCORES; rank_exp < NUM_SCORE_ENTRIES; ++ rank_exp) {
if (rogue.exp_points >= scores[rank_exp].gold) {
score_update = 1;
for (i = (NUM_SCORE_ENRIES - 1); i > rank_exp; -- i) {
scores[i] = scores[i-1];
}
/* Put our info in the slot */
make_score(&scores[rank_exp], monster, other, TOP_EXP);
break;
}
}
if (score_update) {
/* Now rewrite the score file */
md_ignore_signals();
rewind(fp);
(void)xxx(1);
for (i = 0; i < numscores; i++) {
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(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 (i == rank) {
standout();