From bcf964d4fe3d671946d6cf647cba5870e92e80b1 Mon Sep 17 00:00:00 2001 From: dshaligram Date: Sun, 17 Dec 2006 16:46:59 +0000 Subject: Tweaked hiscores code to handle logfiles in addition to scorefiles. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/branches/stone_soup-0.1.6@665 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/acr.cc | 5 +-- crawl-ref/source/hiscores.cc | 96 +++++++++++++++++++++++++++----------------- crawl-ref/source/hiscores.h | 1 + crawl-ref/source/initfile.cc | 2 +- 4 files changed, 63 insertions(+), 41 deletions(-) diff --git a/crawl-ref/source/acr.cc b/crawl-ref/source/acr.cc index e0367a9e47..134ef99b35 100644 --- a/crawl-ref/source/acr.cc +++ b/crawl-ref/source/acr.cc @@ -228,10 +228,9 @@ int main( int argc, char *argv[] ) // now parse the args again, looking for everything else. parse_args( argc, argv, false ); - if (Options.sc_entries > 0) + if (Options.sc_entries != 0) { - printf( " Best Crawlers -" EOL ); - hiscores_print_list( Options.sc_entries, Options.sc_format ); + hiscores_print_all( Options.sc_entries, Options.sc_format ); exit(0); } else diff --git a/crawl-ref/source/hiscores.cc b/crawl-ref/source/hiscores.cc index da3061564c..b67cc72876 100644 --- a/crawl-ref/source/hiscores.cc +++ b/crawl-ref/source/hiscores.cc @@ -192,23 +192,72 @@ void logfile_new_entry( const scorefile_entry &ne ) hs_close(logfile, "a", log_file_name()); } +static void hiscores_print_entry(const scorefile_entry &se, + int index, + int format, + int (*pf)(const char *, ...)) +{ + char buf[INFO_SIZE]; + // print position (tracked implicitly by order score file) + snprintf( buf, sizeof buf, "%3d.", index + 1 ); + + pf("%s", buf); + + std::string entry; + // format the entry + if (format == SCORE_TERSE) + { + entry = hiscores_format_single( se ); + // truncate if we want short format + if (entry.length() > 75) + entry = entry.substr(0, 75); + } + else + { + entry = hiscores_format_single_long( se, (format == SCORE_VERBOSE) ); + } + + entry += EOL; + pf("%s", entry.c_str()); +} + +// Writes all entries in the scorefile to stdout in human-readable form. +void hiscores_print_all(int display_count, int format) +{ + FILE *scores = hs_open("r", score_file_name()); + if (scores == NULL) + { + // will only happen from command line + puts( "No scores." ); + return; + } + + for (int entry = 0; display_count <= 0 || entry < display_count; ++entry) + { + scorefile_entry se; + if (!hs_read(scores, se)) + break; + + hiscores_print_entry(se, entry, format, printf); + } + + hs_close( scores, "r", score_file_name() ); +} + +// Displays high scores using curses. For output to the console, use +// hiscores_print_all. void hiscores_print_list( int display_count, int format ) { FILE *scores; int i, total_entries; - bool use_printf = (Options.sc_entries > 0); if (display_count <= 0) - display_count = SCORE_FILE_ENTRIES; + return; // open highscore file (reading) scores = hs_open("r", score_file_name()); if (scores == NULL) - { - // will only happen from command line - puts( "No high scores." ); return; - } // read highscore file for (i = 0; i < SCORE_FILE_ENTRIES; i++) @@ -221,8 +270,7 @@ void hiscores_print_list( int display_count, int format ) // close off hs_close( scores, "r", score_file_name() ); - if (!use_printf) - textcolor(LIGHTGREY); + textcolor(LIGHTGREY); int start = (newest_entry > 10) ? newest_entry - 10: 0; @@ -237,38 +285,12 @@ void hiscores_print_list( int display_count, int format ) for (i = start; i < finish && i < total_entries; i++) { // check for recently added entry - if (i == newest_entry && !use_printf) + if (i == newest_entry) textcolor(YELLOW); - // print position (tracked implicitly by order score file) - snprintf( info, INFO_SIZE, "%3d.", i + 1 ); - if (use_printf) - printf("%s", info); - else - cprintf("%s", info); - - std::string entry; - // format the entry - if (format == SCORE_TERSE) - { - entry = hiscores_format_single( hs_list[i] ); - // truncate if we want short format - if (entry.length() > 75) - entry = entry.substr(0, 75); - } - else - { - entry = hiscores_format_single_long( hs_list[i], - (format == SCORE_VERBOSE) ); - } - - entry += EOL; - if(use_printf) - printf("%s", entry.c_str()); - else - cprintf("%s", entry.c_str()); + hiscores_print_entry(hs_list[i], i, format, cprintf); - if (i == newest_entry && !use_printf) + if (i == newest_entry) textcolor(LIGHTGREY); } } diff --git a/crawl-ref/source/hiscores.h b/crawl-ref/source/hiscores.h index 39a524ab96..5332b42baf 100644 --- a/crawl-ref/source/hiscores.h +++ b/crawl-ref/source/hiscores.h @@ -29,6 +29,7 @@ void logfile_new_entry( const scorefile_entry &se ); * called from: acr ouch * *********************************************************************** */ void hiscores_print_list( int display_count = -1, int format = SCORE_TERSE ); +void hiscores_print_all(int display_count = -1, int format = SCORE_TERSE); // last updated 16feb2001 {gdl} /* *********************************************************************** diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index bbff68a936..60fa8415f2 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -2069,7 +2069,7 @@ bool parse_args( int argc, char **argv, bool rc_only ) case CLO_TSCORES: case CLO_VSCORES: if (!next_is_param) - ecount = SCORE_FILE_ENTRIES; // default + ecount = -1; // default else // optional number given { ecount = atoi(next_arg); -- cgit v1.2.3-54-g00ecf