From 0b789afdedb3b332521cb87e01708999df2ee0e3 Mon Sep 17 00:00:00 2001 From: Rob Pearce Date: Wed, 1 Jun 2016 16:14:52 +1000 Subject: [PATCH] Add "-s" and "-S" cli args to show hiscores. --- io.c | 38 +++++++++++++++++++++++++++----------- nexus.c | 18 ++++++++++++++++-- save.c | 2 +- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/io.c b/io.c index 1357cbd..bcf2a2f 100644 --- a/io.c +++ b/io.c @@ -24,6 +24,7 @@ #include "shops.h" #include "spell.h" +int gfxready = B_FALSE; // has graphics system been initialised? WINDOW *mainwin; WINDOW *gamewin; WINDOW *msgwin; @@ -33,8 +34,10 @@ extern enum WINGAMETYPE wintype; int statdirty = B_TRUE; + int inaskcoords = B_FALSE; // are we inside this function? + extern buildingusage_t buildingusage[]; extern int nbuildingusage; @@ -10001,9 +10004,10 @@ void initgfx(void) { snprintf(prevmsg, BUFLEN, "!nolastmessage!"); msgmulti = 1; - // handle sigstop (ctrl-y) signal(SIGTSTP, handle_ctrl_y); + + gfxready = B_TRUE; } int drop(object_t *o, int count) { @@ -12077,20 +12081,32 @@ int showhiscoreline(void *hilitescore, int ncols, char **argv, char **colname) { else if (streq(colname[i], "job")) job = strdup(argv[i]); else if (streq(colname[i], "killedby")) killer = strdup(argv[i]); } - if (streq(score, (char *)hilitescore)) setcol(mainwin, C_LIGHTGREEN); - wprintw(mainwin, HISCOREFORMAT, rank, score, name, job); - // last field should be wrapped with lines 2+ indented - getyx(mainwin, y, x); - origy = y; - wrapprint(mainwin, &y, &x, x, "%s", killer); + if (gfxready) { + if (streq(score, (char *)hilitescore)) setcol(mainwin, C_LIGHTGREEN); - if (streq(score, (char *)hilitescore)) unsetcol(mainwin, C_LIGHTGREEN); + wprintw(mainwin, HISCOREFORMAT, rank, score, name, job); - if (y == origy) { - wprintw(mainwin, "\n\n"); + // last field should be wrapped with lines 2+ indented + getyx(mainwin, y, x); + origy = y; + wrapprint(mainwin, &y, &x, x, "%s", killer); + + if (streq(score, (char *)hilitescore)) unsetcol(mainwin, C_LIGHTGREEN); + + if (y == origy) { + wprintw(mainwin, "\n\n"); + } else { + wprintw(mainwin, "\n"); + } } else { - wprintw(mainwin, "\n"); + // regular text with no hilighting + + printf(HISCOREFORMAT, rank, score, name, job); + + // last field should be wrapped with lines 2+ indented, but just + // print it without wrapping. + printf("%s\n",killer); } if (rank) free(rank); if (job) free(job); diff --git a/nexus.c b/nexus.c index a79008e..b1fd195 100644 --- a/nexus.c +++ b/nexus.c @@ -52,10 +52,12 @@ condset_t ccwalkable; condset_t ccwalkableroom; condset_t ccroom; + extern lifeform_t *godlf[]; extern int ngodlfs; +extern int gfxready; extern WINDOW *mainwin; int nextregionthingid = 0; @@ -151,7 +153,7 @@ int main(int argc, char **argv) { atexit(cleanup); - while ((ch = getopt(argc, argv, "f:")) != -1) { + while ((ch = getopt(argc, argv, "f:sS:")) != -1) { switch (ch) { case 'f': playerfile = fopen(optarg, "rt"); @@ -160,6 +162,16 @@ int main(int argc, char **argv) { exit(1); } break; + case 's': + showhiscores(NULL, 1, 10); + exit(0); + break; + case 'S': + i = atoi(optarg); + limit(&i, 1, 100); + showhiscores(NULL, 1, i); + exit(0); + break; case 'h': case '?': default: @@ -2452,7 +2464,9 @@ void timeeffectsworld(map_t *map, int updategametime) { } void usage(char *progname) { - printf("usage: %s [ -f playerfile ]\n",progname); + printf("usage: %s [options] \n",progname); printf("\t-f xx\tReads player details from file xx.\n"); + printf("\t-s\tShows top 10 hiscores.\n"); + printf("\t-S xx\tShows top xx hiscores (max 100).\n"); } diff --git a/save.c b/save.c index 6bd2e0a..d1a0779 100644 --- a/save.c +++ b/save.c @@ -1310,7 +1310,7 @@ int showhiscores(lifeform_t *lf, int min, int max) { // show top ten //snprintf(cmd, BUFLEN, "select * from hiscores order by score desc limit 10;"); - asprintf(&cmd, "select (select count(*) from hiscores b where b.score > a.score) + 1 as rank, score,name,job,killedby from hiscores a where (rank >= %d) and (rank <= %d) order by score desc limit 10;", min, max); + asprintf(&cmd, "select (select count(*) from hiscores b where b.score > a.score) + 1 as rank, score,name,job,killedby from hiscores a where (rank >= %d) and (rank <= %d) order by score desc limit 100;", min, max); snprintf(hilitescoretext, BUFLEN, "%ld",hilitescore); rc = sqlite3_exec(db, cmd, showhiscoreline, hilitescoretext, &errmsg);