2017-03-14 11:06:50 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sqlite3.h>
|
2017-03-19 04:37:09 +05:30
|
|
|
#include "colors.h"
|
2017-03-14 11:06:50 +05:30
|
|
|
|
2017-03-20 06:38:02 +05:30
|
|
|
#define QOVERVIEW 'o'
|
|
|
|
#define QRPLAYER 'p'
|
|
|
|
#define QMLEADERBOARD 'm'
|
2017-03-20 06:17:52 +05:30
|
|
|
|
2017-03-20 06:38:02 +05:30
|
|
|
static inline char *get_filename(char * const c) {
|
2017-03-14 11:06:50 +05:30
|
|
|
char *qout = "queries/mranks.sql";
|
|
|
|
if (c != NULL) {
|
|
|
|
switch(*c) {
|
2017-03-22 03:53:54 +05:30
|
|
|
default:
|
|
|
|
*c = QOVERVIEW;
|
|
|
|
break;
|
2017-03-20 06:38:02 +05:30
|
|
|
case QMLEADERBOARD:
|
2017-03-14 11:06:50 +05:30
|
|
|
qout = "queries/mleaderboard-ojoin.sql";
|
|
|
|
break;
|
2017-03-20 06:38:02 +05:30
|
|
|
case QRPLAYER:
|
2017-03-14 11:06:50 +05:30
|
|
|
qout = "queries/rplayers.sql";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return qout;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void print_tblheader(const char *c) {
|
|
|
|
char *labels;
|
|
|
|
switch (*c) {
|
|
|
|
default:
|
|
|
|
labels = "<TABLE class='center'>\
|
|
|
|
<TH class='tablename' COLSPAN='4'> <H3><BR>Map List</H3> </TH>\
|
|
|
|
<TR>\
|
|
|
|
<TH class='columnname'>Name</TH>\
|
|
|
|
<TH class='columnname'>Records</TH>\
|
|
|
|
<TH class='columnname'>Best Time</TH>\
|
|
|
|
<TH class='columnname'>Held By</TH>\
|
|
|
|
</TR>";
|
|
|
|
break;
|
2017-03-20 06:38:02 +05:30
|
|
|
case QMLEADERBOARD:
|
2017-03-14 11:06:50 +05:30
|
|
|
labels = "<TABLE class='center'>\
|
|
|
|
<TH class='tablename' COLSPAN='3'><H3><BR>Leaderboard</H3></TH>\
|
|
|
|
<TR>\
|
|
|
|
<TH class='columnname'>Rank</TH>\
|
|
|
|
<TH class='columnname'>Name</TH>\
|
|
|
|
<TH class='columnname'>Time</TH>\
|
|
|
|
</TR>";
|
|
|
|
break;
|
2017-03-20 06:38:02 +05:30
|
|
|
case QRPLAYER:
|
2017-03-14 11:06:50 +05:30
|
|
|
labels = "<TABLE class='center'>\
|
|
|
|
<TH class='tablename' COLSPAN='3'> <H3><BR>Ranks</H3> </TH>\
|
|
|
|
<TR>\
|
|
|
|
<TH class='columnname'>Name</TH>\
|
|
|
|
<TH class='columnname'>Map</TH>\
|
|
|
|
<TH class='columnname'>Rank</TH>\
|
|
|
|
</TR>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%s", labels);
|
|
|
|
}
|
|
|
|
|
|
|
|
// input is an integer as a string.
|
|
|
|
// the time is in centiseconds.
|
|
|
|
// this means the longest XDF run this can support is
|
|
|
|
// 21,474,836.47 seconds
|
|
|
|
// someone would need to idle in-game for 248 days
|
|
|
|
static void print_time(const unsigned char *strcs) {
|
|
|
|
const unsigned int num = strtoul((const char*)strcs, NULL, 10);
|
|
|
|
unsigned int s = num/100;
|
|
|
|
const unsigned int cs = num % 100;
|
|
|
|
const unsigned int min = s/60;
|
|
|
|
s = s % 60;
|
|
|
|
if (min > 0) {
|
|
|
|
printf("<TD>%u:%.2u.%.2u</TD>", min, s, cs);
|
|
|
|
} else {
|
|
|
|
printf("<TD>%u.%.2u</TD>", s, cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// display and format the results of the query.
|
|
|
|
static void qresult(sqlite3_stmt * const sp, const char *c) {
|
2017-03-20 06:38:02 +05:30
|
|
|
#define ISPLAYERNAME(x, y) (y == 1 && *x == QMLEADERBOARD) || \
|
|
|
|
(y == 3 && *x == QOVERVIEW)|| \
|
|
|
|
(y == 0 && *x == QRPLAYER)
|
|
|
|
#define ISMAPNAME(x, y) (y == 0 && *x == QOVERVIEW) ||\
|
|
|
|
(y == 1 && *x == QRPLAYER)
|
2017-03-14 11:06:50 +05:30
|
|
|
int e;
|
|
|
|
unsigned int i;
|
2017-03-20 06:17:52 +05:30
|
|
|
const unsigned int cc = sqlite3_column_count(sp);
|
2017-03-14 11:06:50 +05:30
|
|
|
print_tblheader(c);
|
|
|
|
while ((e = sqlite3_step(sp)) == SQLITE_ROW) {
|
|
|
|
printf("<TR>");
|
2017-03-20 06:17:52 +05:30
|
|
|
for (i = 0; i < cc; ++i) {
|
|
|
|
unsigned const char * const field = sqlite3_column_text(sp, i);
|
|
|
|
if (ISPLAYERNAME(c, i)) {
|
|
|
|
print_plname(field);
|
|
|
|
} else if (ISMAPNAME(c, i)) {
|
|
|
|
printf("<TD><a href='/cgi/cts?map=%s'>%s</a></TD>", field, field);
|
2017-03-20 06:38:02 +05:30
|
|
|
} else if (i == 2 && (*c == QMLEADERBOARD || *c == QOVERVIEW)) {
|
2017-03-20 06:17:52 +05:30
|
|
|
print_time(field);
|
2017-03-14 11:06:50 +05:30
|
|
|
} else {
|
2017-03-20 06:17:52 +05:30
|
|
|
printf("<TD>%s</TD>", field);
|
2017-03-14 11:06:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("</TR>");
|
|
|
|
}
|
|
|
|
printf("</TABLE>");
|
|
|
|
}
|
|
|
|
|
|
|
|
// const char 'sql' is never NULL.
|
|
|
|
// const char 'str' may be NULL, however it is only when
|
|
|
|
// the contents of mranks.sql is in const char 'sql'.
|
|
|
|
static bool executequery(const char *sql, const char *str) {
|
|
|
|
const char *p = (str != NULL) ? strchr(str, '=') + 1 : NULL;
|
2017-03-20 06:38:02 +05:30
|
|
|
const char qc = (str != NULL) ? str[0] : QOVERVIEW;
|
2017-03-14 11:06:50 +05:30
|
|
|
sqlite3 *db;
|
|
|
|
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) {
|
2017-03-19 13:17:08 +05:30
|
|
|
fprintf(stderr, "Could not prepare SQL statement: %s\n\n%s", sqlite3_errmsg(db), sql);
|
2017-03-14 11:06:50 +05:30
|
|
|
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);
|
|
|
|
qresult(s, &qc);
|
|
|
|
sqlite3_finalize(s);
|
|
|
|
sqlite3_close(db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// both allocates and frees memory used up by the string..
|
|
|
|
// containing the data from query files.
|
2017-03-20 06:38:02 +05:30
|
|
|
void getquery(char * const qs) {
|
2017-03-14 11:06:50 +05:30
|
|
|
char *qf = NULL;
|
|
|
|
char *fname = get_filename(qs);
|
|
|
|
if (fname != NULL) {
|
|
|
|
FILE *f = fopen(fname, "r");
|
|
|
|
if (f != 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 = calloc(size, sizeof(char));
|
|
|
|
if (qf != NULL) {
|
|
|
|
fseek(f, 0, SEEK_SET); // go to the start of file
|
|
|
|
fread(qf, sizeof(char), size, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
executequery(qf, qs);
|
|
|
|
free(qf);
|
|
|
|
}
|
2017-03-20 06:17:52 +05:30
|
|
|
|