Add "-s" and "-S" cli args to show hiscores.

This commit is contained in:
Rob Pearce 2016-06-01 16:14:52 +10:00
parent 301f815ab8
commit 0b789afded
3 changed files with 44 additions and 14 deletions

38
io.c
View File

@ -24,6 +24,7 @@
#include "shops.h" #include "shops.h"
#include "spell.h" #include "spell.h"
int gfxready = B_FALSE; // has graphics system been initialised?
WINDOW *mainwin; WINDOW *mainwin;
WINDOW *gamewin; WINDOW *gamewin;
WINDOW *msgwin; WINDOW *msgwin;
@ -33,8 +34,10 @@ extern enum WINGAMETYPE wintype;
int statdirty = B_TRUE; int statdirty = B_TRUE;
int inaskcoords = B_FALSE; // are we inside this function? int inaskcoords = B_FALSE; // are we inside this function?
extern buildingusage_t buildingusage[]; extern buildingusage_t buildingusage[];
extern int nbuildingusage; extern int nbuildingusage;
@ -10001,9 +10004,10 @@ void initgfx(void) {
snprintf(prevmsg, BUFLEN, "!nolastmessage!"); snprintf(prevmsg, BUFLEN, "!nolastmessage!");
msgmulti = 1; msgmulti = 1;
// handle sigstop (ctrl-y) // handle sigstop (ctrl-y)
signal(SIGTSTP, handle_ctrl_y); signal(SIGTSTP, handle_ctrl_y);
gfxready = B_TRUE;
} }
int drop(object_t *o, int count) { 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], "job")) job = strdup(argv[i]);
else if (streq(colname[i], "killedby")) killer = 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 if (gfxready) {
getyx(mainwin, y, x); if (streq(score, (char *)hilitescore)) setcol(mainwin, C_LIGHTGREEN);
origy = y;
wrapprint(mainwin, &y, &x, x, "%s", killer);
if (streq(score, (char *)hilitescore)) unsetcol(mainwin, C_LIGHTGREEN); wprintw(mainwin, HISCOREFORMAT, rank, score, name, job);
if (y == origy) { // last field should be wrapped with lines 2+ indented
wprintw(mainwin, "\n\n"); 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 { } 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 (rank) free(rank);
if (job) free(job); if (job) free(job);

18
nexus.c
View File

@ -52,10 +52,12 @@ condset_t ccwalkable;
condset_t ccwalkableroom; condset_t ccwalkableroom;
condset_t ccroom; condset_t ccroom;
extern lifeform_t *godlf[]; extern lifeform_t *godlf[];
extern int ngodlfs; extern int ngodlfs;
extern int gfxready;
extern WINDOW *mainwin; extern WINDOW *mainwin;
int nextregionthingid = 0; int nextregionthingid = 0;
@ -151,7 +153,7 @@ int main(int argc, char **argv) {
atexit(cleanup); atexit(cleanup);
while ((ch = getopt(argc, argv, "f:")) != -1) { while ((ch = getopt(argc, argv, "f:sS:")) != -1) {
switch (ch) { switch (ch) {
case 'f': case 'f':
playerfile = fopen(optarg, "rt"); playerfile = fopen(optarg, "rt");
@ -160,6 +162,16 @@ int main(int argc, char **argv) {
exit(1); exit(1);
} }
break; 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 'h':
case '?': case '?':
default: default:
@ -2452,7 +2464,9 @@ void timeeffectsworld(map_t *map, int updategametime) {
} }
void usage(char *progname) { 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-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");
} }

2
save.c
View File

@ -1310,7 +1310,7 @@ int showhiscores(lifeform_t *lf, int min, int max) {
// show top ten // show top ten
//snprintf(cmd, BUFLEN, "select * from hiscores order by score desc limit 10;"); //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); snprintf(hilitescoretext, BUFLEN, "%ld",hilitescore);
rc = sqlite3_exec(db, cmd, showhiscoreline, hilitescoretext, &errmsg); rc = sqlite3_exec(db, cmd, showhiscoreline, hilitescoretext, &errmsg);