merged branch 'speed-records', squashed

This commit is contained in:
2024-10-18 21:21:02 -07:00
parent ded4e29594
commit 4d1816e0ce
11 changed files with 275 additions and 79 deletions

View File

@@ -80,42 +80,36 @@ void rgb2hsl(struct Hls *dest, const struct Rgb const *src) {
}
}
static void decspan(const int d) {
static const char* decspan(const int d) {
switch(d) {
case 0:
printf("<span style='color:rgb(128,128,128)'>");
break;
return "<span style='color:rgb(128,128,128)'>";
case 1:
printf("<span style='color:rgb(255,0,0)'>");
break;
return "<span style='color:rgb(255,0,0)'>";
case 2:
printf("<span style='color:rgb(51,255,0)'>");
break;
return "<span style='color:rgb(51,255,0)'>";
case 3:
printf("<span style='color:rgb(255,255,0)'>");
break;
return "<span style='color:rgb(255,255,0)'>";
case 4:
printf("<span style='color:rgb(51,102,255)'>");
break;
return "<span style='color:rgb(51,102,255)'>";
case 5:
printf("<span style='color:rgb(51,255,255)'>");
break;
return "<span style='color:rgb(51,255,255)'>";
case 6:
printf("<span style='color:rgb(255,51,102)'>");
break;
return "<span style='color:rgb(255,51,102)'>";
case 7:
printf("<span style='color:rgb(255,255,255)'>");
break;
return "<span style='color:rgb(255,255,255)'>";
case 8:
printf("<span style='color:rgb(153,153,153)'>");
break;
return "<span style='color:rgb(153,153,153)'>";
case 9:
printf("<span style='color:rgb(128,128,128)'>");
break;
return "<span style='color:rgb(128,128,128)'>";
}
}
static void hexspan(const char *str) {
static void hexspan(char *buf, int bufsize, const char *str) {
// length of ...
// "<span style=\"color:rgb(%d,%d,%d)\">"
// where each %d ranges from 0 to 255
// char buf[40];
const char h1[2] = {str[0], '\0'};
const char h2[2] = {str[1], '\0'};
const char h3[2] = {str[2], '\0'};
@@ -131,17 +125,21 @@ static void hexspan(const char *str) {
nhls.l = MIN_CONTRAST;
hsl2rgb(&nrgb, &nhls);
}
printf("<span style=\"color:rgb(%d,%d,%d)\">", nrgb.r, nrgb.g, nrgb.b);
int wrote = snprintf(
buf, bufsize,
"<span style=\"color:rgb(%d,%d,%d)\">",
nrgb.r, nrgb.g, nrgb.b);
// output = buf;
}
static void b(char * const str) {
#define TAG_LEN 40
static void colorize_noalloc(char * const str) {
char *token = strtok(str, "^");
char c;
printf("<TD>");
while (token) {
c = token[0];
if (isdigit(c)) {
decspan(c - '0');
printf( decspan(c - '0') );
if (strlen(token) > 1) {
printf("%s", token + 1);
}
@@ -150,7 +148,9 @@ static void b(char * const str) {
(isxdigit(token[1]) &&
isxdigit(token[2]) &&
isxdigit(token[3]))) {
hexspan(token + 1); //exclude x
char tag[TAG_LEN];
hexspan(tag, TAG_LEN, token + 1);
printf( tag ); //exclude x
if (strlen(token) > 4){
printf("%s", token + 4);
}
@@ -160,7 +160,16 @@ static void b(char * const str) {
}
token = strtok(NULL, "^");
}
printf("</TD>");
}
static void sanitize(char *user_name) {
if (user_name == NULL) {
return;
}
char *pos = user_name;
while (pos = strstr(pos, "^^")) {
strcpy(pos, (pos + 1));
}
}
void print_plname(const char* str) {
@@ -170,11 +179,85 @@ void print_plname(const char* str) {
char *copy;
copy = calloc(strlen(str) + 1, sizeof(char));
strcpy(copy, str);
char *pos = copy;
while (pos = strstr(pos, "^^")) {
strcpy(pos, (pos + 1));
}
b(copy);
sanitize(copy);
colorize_noalloc(copy);
fflush(stdout);
free(copy);
}
static char* append_to_str(char *dest, const char *src) {
if (dest == NULL || src == NULL) {
fprintf(stderr, "append_to_str(): warning - received null ptr" );
return NULL;
}
size_t new_len = strlen(dest) + strlen(src) + 1;
char *new_str = realloc(dest, new_len);
if (new_str != NULL) {
strcat(new_str, src);
}
return new_str;
}
// the most colorful names are the longest
// names with 8 colors can go to 400 chars
char* colorize_name(char *buf, /*int bufsize,*/ char * const str) {
char *token = strtok(str, "^");
char c;
// unsigned int i = 0;
while (token) {
c = token[0];
if (isdigit(c)) {
// printf("%i : %s\n", i, buf);;
buf = append_to_str(buf, decspan(c - '0') );
if (strlen(token) > 1) {
buf = append_to_str(buf, token + 1);
}
buf = append_to_str(buf, "</span>");
} else if ((c == 'x' && strlen(token) > 3) &&
(isxdigit(token[1]) &&
isxdigit(token[2]) &&
isxdigit(token[3]))) {
char tag[TAG_LEN];
hexspan(tag, TAG_LEN, token + 1); //exclude x
buf = append_to_str(buf, tag );
if (strlen(token) > 4){
buf = append_to_str(buf, token + 4);
}
buf = append_to_str(buf, "</span>");
} else {
buf = append_to_str(buf, token);
}
token = strtok(NULL, "^");
// i++;
}
return buf;
}
/* test:
./colors ^9[^1S^9]^x469Kom^0ier^7
./colors ^9[^1S^9]^^x469Kom^0ier^7
*/
#ifdef COLORS4PYTHON
int main(int argc, const char **argv) {
if (argc < 1) {
return -1;
}
char *colored = (char*)calloc(strlen(argv[1]) + 1, sizeof(char));
char *player_name = (char*)calloc(strlen(argv[1]) + 1, sizeof(char));
strcpy(player_name, argv[1]);
sanitize(player_name);
colored = colorize_name(colored, /*sizeof(colored),*/ player_name);
fprintf(stdout, "%s\n", colored);
// clean up
fflush(stdout);
free(colored);
free(player_name);
return 0;
}
#endif

View File

@@ -5,9 +5,10 @@
#include <sqlite3.h>
#include "colors.h"
#define QOVERVIEW 'o'
#define QRPLAYER 'p'
#define QMLEADERBOARD 'm'
#define QOVERVIEW 'o' // default case - see get_filename()
#define QRPLAYER 'p' // ?player=
#define QMLEADERBOARD 'm' // ?map=
#define QFASTEST 'f'
static inline char *get_filename(char * const c) {
char *qout = "queries/mranks.sql";
@@ -22,6 +23,9 @@ static inline char *get_filename(char * const c) {
case QRPLAYER:
qout = "queries/rplayers.sql";
break;
case QFASTEST:
qout = "queries/fastest-players.sql";
break;
}
}
return qout;
@@ -58,6 +62,15 @@ static inline void print_tblheader(const char *c) {
<TH class='columnname'>Rank</TH>\
</TR>";
break;
case QFASTEST:
labels = "<table class='leaderboard'>\
<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'>Highest Velocty (qu/s)</th>\
<th class='columnname'>Held By</th>\
</tr>";
}
printf("%s", labels);
}
@@ -84,9 +97,11 @@ static void print_time(const unsigned char *strcs) {
static void qresult(sqlite3_stmt * const sp, const char *c) {
#define ISPLAYERNAME(x, y) (y == 1 && *x == QMLEADERBOARD) || \
(y == 3 && *x == QOVERVIEW)|| \
(y == 0 && *x == QRPLAYER)
(y == 0 && *x == QRPLAYER) || \
(y == 3 && *x == QFASTEST)
#define ISMAPNAME(x, y) (y == 0 && *x == QOVERVIEW) ||\
(y == 1 && *x == QRPLAYER)
(y == 1 && *x == QRPLAYER) || \
(y == 0 && *x == QFASTEST)
int e;
unsigned int i;
const unsigned int cc = sqlite3_column_count(sp);
@@ -96,7 +111,9 @@ static void qresult(sqlite3_stmt * const sp, const char *c) {
for (i = 0; i < cc; ++i) {
unsigned const char * const field = sqlite3_column_text(sp, i);
if (ISPLAYERNAME(c, i)) {
printf("<TD>");
print_plname(field);
printf("</TD>");
} else if (ISMAPNAME(c, i)) {
#ifdef STATICGEN
printf("<TD><a href='./maps/%s.html'>%s</a></TD>", field, field);
@@ -105,11 +122,13 @@ static void qresult(sqlite3_stmt * const sp, const char *c) {
#endif
} else if (i == 2 && (*c == QMLEADERBOARD || *c == QOVERVIEW)) {
print_time(field);
} else if (i == 2 && *c == QFASTEST) { // velocity
printf("<TD>%.2f</TD>", atof(field) );
} else {
printf("<TD>%s</TD>", field);
}
}
printf("</TR>");
printf("</TR>\n");
}
printf("</TABLE>");
}