Fiddled and added notes to put_scores()
This commit is contained in:
parent
db01e17fd7
commit
4520ba5a65
352
score.c
352
score.c
@ -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++) {
|
|
||||||
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 */
|
/* Remove superseded entries, if any. */
|
||||||
if (!score_only) {
|
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';
|
||||||
|
}
|
||||||
|
|
||||||
/* Gold&Glory: did we make the top gold list? */
|
/* If we're going to insert ourselves, do it now */
|
||||||
/* if we aren't better than anyone, add at end. */
|
if (!score_only) {
|
||||||
rank = FIRST_EXP_SCORE;
|
|
||||||
|
|
||||||
/* Otherwise, find our slot. */
|
/* Gold&Glory: did we make the top gold list? */
|
||||||
for (i = 0; i < FIRST_EXP_SCORE; i++) {
|
/* if we aren't better than anyone, add at end. */
|
||||||
if (rogue.gold >= scores[i].gold) {
|
rank = FIRST_EXP_SCORE;
|
||||||
rank = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rank < FIRST_EXP_SCORE) {
|
/* Otherwise, find our slot. */
|
||||||
/* Open up a slot */
|
for (i = 0; i < FIRST_EXP_SCORE; i++) {
|
||||||
for (i = FIRST_EXP_SCORE-1; i > rank; i--) {
|
if (rogue.gold >= scores[i].gold) {
|
||||||
scores[i] = scores[i-1];
|
rank = i;
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Put our info in the slot */
|
if (rank < FIRST_EXP_SCORE) {
|
||||||
make_score(&scores[rank], monster, other, TOP_GOLD);
|
/* Open up a slot */
|
||||||
}
|
for (i = FIRST_EXP_SCORE-1; i > rank; i--) {
|
||||||
|
scores[i] = scores[i-1];
|
||||||
|
}
|
||||||
|
|
||||||
/* Gold&Glory: did we make the top experience list? */
|
/* Put our info in the slot */
|
||||||
/* if we aren't better than anyone, add at end. */
|
make_score(&scores[rank], monster, other, TOP_GOLD);
|
||||||
rank_exp = NUM_SCORE_ENTRIES;
|
}
|
||||||
|
|
||||||
/* Otherwise, find our slot. */
|
/* Gold&Glory: did we make the top experience list? */
|
||||||
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) {
|
/* if we aren't better than anyone, add at end. */
|
||||||
if (rogue.exp_points >= scores[i].gold) {
|
rank_exp = NUM_SCORE_ENTRIES;
|
||||||
rank_exp = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rank_exp < NUM_SCORE_ENTRIES) {
|
/* Otherwise, find our slot. */
|
||||||
/* Open up a slot */
|
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) {
|
||||||
for (i = NUM_SCORE_ENTRIES-1; i > rank; i--) {
|
if (rogue.exp_points >= scores[i].gold) {
|
||||||
scores[i] = scores[i-1];
|
rank_exp = i;
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Put our info in the slot */
|
if (rank_exp < NUM_SCORE_ENTRIES) {
|
||||||
make_score(&scores[rank_exp], monster, other, TOP_EXP);
|
/* Open up a slot */
|
||||||
}
|
for (i = NUM_SCORE_ENTRIES-1; i > rank; i--) {
|
||||||
|
scores[i] = scores[i-1];
|
||||||
|
}
|
||||||
|
|
||||||
/* Now rewrite the score file */
|
/* Put our info in the slot */
|
||||||
|
make_score(&scores[rank_exp], monster, other, TOP_EXP);
|
||||||
|
}
|
||||||
|
|
||||||
md_ignore_signals();
|
/* Now rewrite the score file */
|
||||||
rewind(fp);
|
|
||||||
(void)xxx(1);
|
|
||||||
|
|
||||||
for (i = 0; i < numscores; i++) {
|
md_ignore_signals();
|
||||||
write_score_entry(&scores[i], i, fp);
|
rewind(fp);
|
||||||
}
|
(void)xxx(1);
|
||||||
}
|
|
||||||
md_lock(0);
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
/* Display the scores */
|
for (i = 0; i < numscores; i++) {
|
||||||
|
write_score_entry(&scores[i], i, fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
md_lock(0);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
clear();
|
/* Display the scores */
|
||||||
mvaddstr(3, 27, "Top Gold & Glory Rogueists");
|
|
||||||
mvaddstr(7, 0, "Rank Gold Name");
|
|
||||||
|
|
||||||
for (i = 0; i < FIRST_EXP_SCORE; i++) {
|
clear();
|
||||||
if (scores[i].gold == -1L) break;
|
mvaddstr(3, 27, "Top Gold & Glory Rogueists");
|
||||||
if (i == rank) {
|
mvaddstr(7, 0, "Rank Gold Name");
|
||||||
standout();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scores[i].nickname[0]) {
|
for (i = 0; i < FIRST_EXP_SCORE; i++) {
|
||||||
name = scores[i].nickname;
|
if (scores[i].gold == -1L) break;
|
||||||
} else {
|
if (i == rank) {
|
||||||
name = scores[i].username;
|
standout();
|
||||||
}
|
}
|
||||||
|
|
||||||
mvprintw(i+8, 0, "%2d %6ld %s: %s",
|
if (scores[i].nickname[0]) {
|
||||||
i+1, scores[i].gold, name, scores[i].death);
|
name = scores[i].nickname;
|
||||||
|
} else {
|
||||||
|
name = scores[i].username;
|
||||||
|
}
|
||||||
|
|
||||||
if (i == rank) {
|
mvprintw(i+8, 0, "%2d %6ld %s: %s",
|
||||||
standend();
|
i+1, scores[i].gold, name, scores[i].death);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mvaddstr(14, 0, "Rank Lvl/Exp Name");
|
if (i == rank) {
|
||||||
|
standend();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) {
|
mvaddstr(14, 0, "Rank Lvl/Exp Name");
|
||||||
if (scores[i].gold == -1L) break;
|
|
||||||
if (i == rank_exp) {
|
|
||||||
standout();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scores[i].nickname[0]) {
|
for (i = FIRST_EXP_SCORE; i < NUM_SCORE_ENTRIES; i++) {
|
||||||
name = scores[i].nickname;
|
if (scores[i].gold == -1L) break;
|
||||||
} else {
|
if (i == rank_exp) {
|
||||||
name = scores[i].username;
|
standout();
|
||||||
}
|
}
|
||||||
|
|
||||||
mvprintw(i+10, 0, "%2d %2d/%-6ld %s: %s",
|
if (scores[i].nickname[0]) {
|
||||||
i+1 - expscore, get_exp_level(scores[i].gold), scores[i].gold,
|
name = scores[i].nickname;
|
||||||
name, scores[i].death);
|
} else {
|
||||||
|
name = scores[i].username;
|
||||||
|
}
|
||||||
|
|
||||||
if (i == rank_exp) {
|
mvprintw(i+10, 0, "%2d %2d/%-6ld %s: %s",
|
||||||
standend();
|
i+1 - expscore, get_exp_level(scores[i].gold), scores[i].gold,
|
||||||
}
|
name, scores[i].death);
|
||||||
}
|
|
||||||
refresh();
|
if (i == rank_exp) {
|
||||||
messagef(0, "%s", ""); /* gcc objects to just "" */
|
standend();
|
||||||
if (dopause) {
|
}
|
||||||
messagef(0, "%s", "");
|
}
|
||||||
}
|
refresh();
|
||||||
clean_up("");
|
messagef(0, "%s", ""); /* gcc objects to just "" */
|
||||||
|
if (dopause) {
|
||||||
|
messagef(0, "%s", "");
|
||||||
|
}
|
||||||
|
clean_up("");
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
|
Loading…
Reference in New Issue
Block a user