From 34efe6ac4a0c1d9a19d01ed0776390e951af51c9 Mon Sep 17 00:00:00 2001 From: <> Date: Fri, 10 Mar 2017 16:59:58 -0800 Subject: [PATCH] Initial - added better mleaderboard query --- main.c | 111 +++++++++++++++++++++++++++++++++ queries/mleaderboard-ojoin.sql | 5 ++ queries/mleaderboard.sql | 8 +++ queries/mplayers.sql | 7 +++ queries/mranks.sql | 8 +++ queries/rplayers.sql | 4 ++ 6 files changed, 143 insertions(+) create mode 100644 main.c create mode 100644 queries/mleaderboard-ojoin.sql create mode 100644 queries/mleaderboard.sql create mode 100644 queries/mplayers.sql create mode 100644 queries/mranks.sql create mode 100644 queries/rplayers.sql diff --git a/main.c b/main.c new file mode 100644 index 0000000..99ac890 --- /dev/null +++ b/main.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include + +typedef enum { + MAP, + PLAYER, + OVERVIEW, + NONE +} Querytype; + +inline char *get_filename(const char c, Querytype *q) { + char *qout; + switch(c) { + default: + qout = "queries/mranks.sql"; + *q = OVERVIEW; + break; + case 'm': + qout = "queries/mleaderboard-ojoin.sql"; + *q = MAP; + break; + case 'p': + qout = "queries/rplayers.sql"; + *q = PLAYER; + break; + case 't': + qout = "queries/test.txt"; + *q = NONE; + break; + } + return qout; +} + +bool executequery(const char *sql, const char *p, int tbl) { + sqlite3 *db; + char *error; + int con = sqlite3_open("db/cts.db", &db); + if (con != SQLITE_OK) { + fprintf(stderr, "Can not open database: %s\n", sqlite3_errmsg(db)); + return false; + } + sqlite3_stmt *s; + // prepare sql to s, reading until null character. + con = sqlite3_prepare_v2(db, sql, -1, &s, NULL); + if (con != SQLITE_OK) { + fprintf(stderr, "Could not prepare SQL statement: %s\n", sqlite3_errmsg(db)); + return false; + } + // bind parameter with a string length that is the # of bytes to null char. + con = sqlite3_bind_text(s, 1, p, -1, NULL); + int e; + unsigned int cc, i; + while ((e = sqlite3_step(s)) == SQLITE_ROW) { + cc = sqlite3_column_count(s); + for (i = 0; i < cc; i++) { + printf("%s, ", sqlite3_column_text(s, i)); + } + printf("\n"); + } + sqlite3_finalize(s); + sqlite3_close(db); + return true; +} + +// both allocates and frees memory used up by the string.. +// containing the data from query files. +void getquery(const char *qs) { + char *qf, *qp; + Querytype qt; + char *fname = get_filename(qs[0], &qt); + if (fname != NULL) { + FILE *f = fopen(fname, "r"); + if (f != NULL && qf != NULL) { + // change from fseek/SEEK_END when sql queries are >2 GB in size. + fseek(f, 0, SEEK_END); // go to the end of file + unsigned int size = ftell(f); + qf = malloc(size); + if (qf != NULL) { + fseek(f, 0, SEEK_SET); // go to the start of file + fread(qf, sizeof(char), size, f); + } + } + } + qp = strchr(qs, '=') + 1; // increment to exclude '=' + printf("%s\n", qf); + executequery(qf, qp, qt); + free(qf); +} + +void html(void) { + const char *html_top = "Content-Type: text/html\n\n\ + \ + \ + /v/ - Xonotic\ + "; + const char *html_bot = "

Pages under construction.
\ + Service may sporadically become unavailable.
\ + In-game database is not directly synced with this web server.\ +

\ + "; + getquery(getenv("QUERY_STRING")); + printf("%s %s", html_top, html_bot); + return; +} + +int main(void) { + getquery("map=pornstar-kaine"); +} diff --git a/queries/mleaderboard-ojoin.sql b/queries/mleaderboard-ojoin.sql new file mode 100644 index 0000000..6246bd9 --- /dev/null +++ b/queries/mleaderboard-ojoin.sql @@ -0,0 +1,5 @@ +select trank, tvalue, ifnull(alias, "Unregistered Player") +from Cts_times, Cts_ranks +left join Id2alias + on idvalue = cryptokey +where trank = idrank and Cts_times.mapid = Cts_ranks.mapid and Cts_times.mapid = ? diff --git a/queries/mleaderboard.sql b/queries/mleaderboard.sql new file mode 100644 index 0000000..753d5a3 --- /dev/null +++ b/queries/mleaderboard.sql @@ -0,0 +1,8 @@ +select trank, idvalue, tvalue +from Cts_times, Cts_ranks +where trank = idrank and Cts_times.mapid = Cts_ranks.mapid and Cts_times.mapid = ? +order by trank + +-- get full leaderboard of the map +-- aliases are not included because +-- not ever player has a registered alias. \ No newline at end of file diff --git a/queries/mplayers.sql b/queries/mplayers.sql new file mode 100644 index 0000000..85d8200 --- /dev/null +++ b/queries/mplayers.sql @@ -0,0 +1,7 @@ +select idrank, idvalue, alias +from Cts_ranks, Id2alias +where idvalue = cryptokey and mapid = ? +order by idrank + +-- get registered/aliased players who have played on +-- the specified map. \ No newline at end of file diff --git a/queries/mranks.sql b/queries/mranks.sql new file mode 100644 index 0000000..1331a22 --- /dev/null +++ b/queries/mranks.sql @@ -0,0 +1,8 @@ +select Cts_ranks.mapid, max(trank), min(tvalue), alias +from Cts_ranks, Cts_times, Id2alias +where Cts_ranks.mapid = Cts_times.mapid + and tvalue != 0 + and cryptokey = idvalue + and idrank = 1 +group by Cts_ranks.mapid +order by max(trank); \ No newline at end of file diff --git a/queries/rplayers.sql b/queries/rplayers.sql new file mode 100644 index 0000000..109140e --- /dev/null +++ b/queries/rplayers.sql @@ -0,0 +1,4 @@ +select alias, mapid, idrank +from Cts_ranks, Id2alias +where idvalue = cryptokey and cryptokey = ? +order by idrank \ No newline at end of file