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