Fiddled and added notes to put_scores()

This commit is contained in:
David Meyer 2024-03-26 00:02:00 +09:00
parent db01e17fd7
commit 4520ba5a65
2 changed files with 187 additions and 173 deletions

BIN
roguegg

Binary file not shown.

360
score.c
View File

@ -337,206 +337,220 @@ write_score_entry(const struct score_entry *se, int rank, FILE *fp)
void void
put_scores(const object *monster, short other) put_scores(const object *monster, short other)
{ {
short i, rank=-1, found_player = -1, numscores = NUM_SCORE_ENTRIES; short i, rank=-1, found_player = -1, numscores = NUM_SCORE_ENTRIES;
short rank_exp=-1, expscore = FIRST_EXP_SCORE, found_player_exp = -1; short rank_exp=-1, expscore = FIRST_EXP_SCORE, found_player_exp = -1;
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;
md_lock(1); md_lock(1);
setegid(egid); setegid(egid);
if ((fp = fopen(_PATH_SCOREFILE, "r+")) == NULL && if ((fp = fopen(_PATH_SCOREFILE, "r+")) == NULL &&
(fp = fopen(_PATH_SCOREFILE, "w+")) == NULL) { (fp = fopen(_PATH_SCOREFILE, "w+")) == NULL) {
setegid(gid); setegid(gid);
messagef(0, "cannot read/write/create score file"); messagef(0, "cannot read/write/create score file");
sf_error(); sf_error();
} }
setegid(gid); setegid(gid);
rewind(fp); rewind(fp);
(void)xxx(1); (void)xxx(1);
/* Initialize scores array */ /* Initialize scores array */
for (i = 0; i < NUM_SCORE_ENTRIES; i++) { for (i = 0; i < NUM_SCORE_ENTRIES; i++) {
scores[i].gold = -1L; scores[i].gold = -1L;
scores[i].username[0] = '\0'; scores[i].username[0] = '\0';
scores[i].death[0] = '\0'; scores[i].death[0] = '\0';
scores[i].nickname[0] = '\0'; scores[i].nickname[0] = '\0';
} }
for (i = 0; i < NUM_SCORE_ENTRIES; i++) { for (i = 0; i < NUM_SCORE_ENTRIES; i++) {
if (read_score_entry(&scores[i], fp) == 0) { if (read_score_entry(&scores[i], fp) == 0) {
break; break;
} }
} }
/* Search the gold score list. */ /* Gold&Glory
for (i = 0; i < FIRST_EXP_SCORE; i++) { * At the end of a game, the rogueist's gold and experience scores can be in
if (!strcmp(scores[i].username, login_name)) { * one of the following conditions with respect to the top scores:
/* found our score */ * 1. The rogueist is not in the top scorer list
if (rogue.gold < scores[i].gold) { * --> Add rogueist to top score list if score is high enough
/* we didn't do as well as last time */ * 2. The rogueist is in the top score list, but his current score is not greater
/* score_only = 1;*/ * than his previous top score
} else { * --> Do nothing
/* we did better; mark entry for removal */ * 3. The rogueist is in the top score list, and his current score is greater
found_player = i; * than his previous top score
} * --> Remove rogueist from top score list; readd rogueist at new rank
break; * (condition of gold and experience scores are independent)
} */
}
/* Search the exp score list. */ /* Search the gold score list. */
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) { for (i = 0; i < FIRST_EXP_SCORE; 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.gold < scores[i].gold) {
/* we didn't do as well as last time */ /* we didn't do as well as last time */
/* score_only = 1;*/ score_only = 1;
} else { } else {
/* we did better; mark entry for removal */ /* we did better; mark entry for removal */
found_player_exp = i; found_player = i;
} }
break; break;
} }
} }
if (found_player == -1 && found_player_exp == -1) score_only = 1; /* Search the exp score list. */
for (i = FIRST_EXP_SCORE; 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;
}
break;
}
}
/* Remove superseded entries, if any. */ /* if (found_player == -1 && found_player_exp == -1) score_only = 1;*/
if (found_player != -1) {
for (i = found_player; i < FIRST_EXP_SCORE-1; i++) { /* Remove superseded entries, if any. */
scores[i] = scores[i+1]; if (found_player != -1) {
} for (i = found_player; i < FIRST_EXP_SCORE-1; i++) {
scores[i].gold = -1L; scores[i] = scores[i+1];
scores[i].username[0] = '\0'; }
scores[i].death[0] = '\0'; scores[i].gold = -1L;
scores[i].nickname[0] = '\0'; scores[i].username[0] = '\0';
} scores[i].death[0] = '\0';
if (found_player_exp != -1) { scores[i].nickname[0] = '\0';
for (i = found_player_exp; i < NUM_SCORE_ENTRIES-1; i++) { }
scores[i] = scores[i+1]; if (found_player_exp != -1) {
} for (i = found_player_exp; i < NUM_SCORE_ENTRIES-1; i++) {
scores[i].gold = -1L; scores[i] = scores[i+1];
scores[i].username[0] = '\0'; }
scores[i].death[0] = '\0'; scores[i].gold = -1L;
scores[i].nickname[0] = '\0'; 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 */ /* If we're going to insert ourselves, do it now */
if (!score_only) { 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;
}
}
/* Gold&Glory: did we make the top gold list? */ if (rank < FIRST_EXP_SCORE) {
/* if we aren't better than anyone, add at end. */ /* Open up a slot */
rank = FIRST_EXP_SCORE; for (i = FIRST_EXP_SCORE-1; i > rank; i--) {
scores[i] = scores[i-1];
/* 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) { /* Put our info in the slot */
/* Open up a slot */ make_score(&scores[rank], monster, other, TOP_GOLD);
for (i = FIRST_EXP_SCORE-1; i > rank; i--) { }
scores[i] = scores[i-1];
} /* Gold&Glory: did we make the top experience list? */
/* if we aren't better than anyone, add at end. */
rank_exp = NUM_SCORE_ENTRIES;
/* Put our info in the slot */ /* Otherwise, find our slot. */
make_score(&scores[rank], monster, other, TOP_GOLD); for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) {
} if (rogue.exp_points >= scores[i].gold) {
rank_exp = i;
break;
}
}
/* Gold&Glory: did we make the top experience list? */ if (rank_exp < NUM_SCORE_ENTRIES) {
/* if we aren't better than anyone, add at end. */ /* Open up a slot */
rank_exp = NUM_SCORE_ENTRIES; for (i = NUM_SCORE_ENTRIES-1; i > rank; i--) {
scores[i] = scores[i-1];
}
/* Otherwise, find our slot. */ /* Put our info in the slot */
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) { make_score(&scores[rank_exp], monster, other, TOP_EXP);
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--) {
scores[i] = scores[i-1];
}
/* Put our info in the slot */
make_score(&scores[rank_exp], monster, other, TOP_EXP);
}
/* Now rewrite the score file */ /* Now rewrite the score file */
md_ignore_signals();
rewind(fp);
(void)xxx(1);
md_ignore_signals(); for (i = 0; i < numscores; i++) {
rewind(fp); write_score_entry(&scores[i], i, fp);
(void)xxx(1); }
}
md_lock(0);
fclose(fp);
for (i = 0; i < numscores; i++) { /* Display the scores */
write_score_entry(&scores[i], i, fp);
}
}
md_lock(0);
fclose(fp);
/* Display the scores */ clear();
mvaddstr(3, 27, "Top Gold & Glory Rogueists");
mvaddstr(7, 0, "Rank Gold Name");
clear(); for (i = 0; i < FIRST_EXP_SCORE; i++) {
mvaddstr(3, 27, "Top Gold & Glory Rogueists"); if (scores[i].gold == -1L) break;
mvaddstr(7, 0, "Rank Gold Name"); if (i == rank) {
standout();
}
for (i = 0; i < FIRST_EXP_SCORE; i++) { if (scores[i].nickname[0]) {
if (scores[i].gold == -1L) break; name = scores[i].nickname;
if (i == rank) { } else {
standout(); name = scores[i].username;
} }
if (scores[i].nickname[0]) { mvprintw(i+8, 0, "%2d %6ld %s: %s",
name = scores[i].nickname; i+1, scores[i].gold, name, scores[i].death);
} else {
name = scores[i].username;
}
mvprintw(i+8, 0, "%2d %6ld %s: %s", if (i == rank) {
i+1, scores[i].gold, name, scores[i].death); standend();
}
}
if (i == rank) { mvaddstr(14, 0, "Rank Lvl/Exp Name");
standend();
}
}
mvaddstr(14, 0, "Rank Lvl/Exp Name"); for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) {
if (scores[i].gold == -1L) break;
if (i == rank_exp) {
standout();
}
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) { if (scores[i].nickname[0]) {
if (scores[i].gold == -1L) break; name = scores[i].nickname;
if (i == rank_exp) { } else {
standout(); name = scores[i].username;
} }
if (scores[i].nickname[0]) { mvprintw(i+10, 0, "%2d %2d/%-6ld %s: %s",
name = scores[i].nickname; i+1 - expscore, get_exp_level(scores[i].gold), scores[i].gold,
} else { name, scores[i].death);
name = scores[i].username;
}
mvprintw(i+10, 0, "%2d %2d/%-6ld %s: %s", if (i == rank_exp) {
i+1 - expscore, get_exp_level(scores[i].gold), scores[i].gold, standend();
name, scores[i].death); }
}
if (i == rank_exp) { refresh();
standend(); messagef(0, "%s", ""); /* gcc objects to just "" */
} if (dopause) {
} messagef(0, "%s", "");
refresh(); }
messagef(0, "%s", ""); /* gcc objects to just "" */ clean_up("");
if (dopause) {
messagef(0, "%s", "");
}
clean_up("");
} }
static static