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

18
io.c
View File

@ -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,7 +12081,10 @@ 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 (gfxready) {
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
@ -12092,6 +12099,15 @@ int showhiscoreline(void *hilitescore, int ncols, char **argv, char **colname) {
} else {
wprintw(mainwin, "\n");
}
} else {
// 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);
if (name) free(name);

18
nexus.c
View File

@ -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");
}

2
save.c
View File

@ -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);